Compare commits

...

8 Commits

6 changed files with 38 additions and 44 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2021-2023 Alexander "Arav" Andreev <me@arav.su>
Copyright (c) 2021-2024 Alexander "Arav" Andreev <me@arav.su>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -5,13 +5,10 @@ PREFIX:=/usr/local
CFLAGS=--std=c99 -O3 -Wall -Werror -Wextra -pedantic
SRC_DIR=src
DESTDIR=/
.PHONY: clean install uninstall test
${TARGET}: ${SRC_DIR}/main.c ${SRC_DIR}/main.h
${TARGET}: main.c
${CC} ${CFLAGS} ${LDFLAGS} $< -o $@
clean:

View File

@ -1,6 +1,6 @@
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
pkgname=kwh-cost
pkgver=1.1.1
pkgver=1.1.2
pkgrel=1
pkgdesc="Calculates cost of electricity."
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')
@ -12,10 +12,10 @@ md5sums=('SKIP')
build() {
cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/"
make
}
package() {
cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/" PREFIX="/usr" install
make DESTDIR="$pkgdir" PREFIX="/usr" install
}

View File

@ -13,7 +13,7 @@ Options are `-v, --version` to show program's version; and `-h, --help` to show
`price` is a price per kWh. `wattage` is how many Watts being consumed. `interval` is for how long.
`interval` is in seconds, but you can specify a unit. Units are the following:
`m` for minutes; `h` for hours; and `d` for days. There could be as many `interval`s passed as you need.
`m|M` for minutes; `h|H` for hours; and `d|D` for days. There could be as many `interval`s passed as you need.
Here is an example:
@ -27,10 +27,10 @@ Will give 348.29.
### Manually
First run `make` to build a program, and then `make install` to copy executable in `/usr/bin/`. Later, use `make uninstall` to delete it, or just delete `/usr/bin/kwh-cost`.
First run `make` to build a program, and then `make install` to copy executable in `/usr/local/bin/`. Later, use `make uninstall` to delete it, or just delete `/usr/local/bin/kwh-cost`.
A `LICENSE` file is also being copied to `/usr/local/share/licenses/kwh-cost/LICENSE`.
### For ArchLinux
You can take a [PKGBUILD](/Arav/kwh-cost/raw/branch/master/PKGBUILD) file and in a directory with it run `makepkg -i`.
If you want to use a pre-built package for Arch from Releases, then install it using `pacman -U` command.

View File

@ -2,15 +2,27 @@
#include <stdlib.h>
#include <string.h>
#include "main.h"
#define VERSION "1.1.2"
typedef long double price_t;
typedef long double wattage_t;
typedef unsigned long interval_t;
int parse_arguments(const int argc, const char *const *const argv,
price_t *const price, wattage_t *const wattage, interval_t *const interval);
interval_t interval_to_seconds(const char *const interval);
price_t price_kwh_to_wattsecond(const price_t price);
void print_version(void);
void print_usage(void);
int main(const int argc, const char *const *const argv) {
price_t price;
wattage_t wattage;
price_t price = 0;
wattage_t wattage = 0;
interval_t interval = 0;
int res = parse_arguments(argc, argv, &price, &wattage, &interval);
switch (res) {
switch (parse_arguments(argc, argv, &price, &wattage, &interval)) {
case 1: print_usage(); exit(0);
case 2: print_version(); exit(0);
case 99: fputs("Unknown flag provided.\n", stderr); print_usage(); exit(1);
@ -27,16 +39,19 @@ int parse_arguments(const int argc, const char *const *const argv,
for (int i = 1, pos = 0; i < argc; ++i)
if (argv[i][0] == '-') {
if (argv[i][1] == 'h' || argv[i][2] == 'h')
size_t l = strlen(argv[i]);
if (l < 2)
return 99;
if (argv[i][1] == 'h' || (argv[i][1] == '-' && l > 2 && argv[i][2] == 'h'))
return 1;
else if (argv[i][1] == 'v' || argv[i][2] == 'v')
else if (argv[i][1] == 'v' || (argv[i][1] == '-' && l > 2 && argv[i][2] == 'v'))
return 2;
else
return 99;
} else if (argc < 4) {
return 1;
} else if (pos == 0) {
*price = price_kwh_to_wattspersecond((price_t)atof(argv[i]));
*price = price_kwh_to_wattsecond((price_t)atof(argv[i]));
++pos;
} else if (pos == 1) {
*wattage = (wattage_t)atof(argv[2]);
@ -47,7 +62,7 @@ int parse_arguments(const int argc, const char *const *const argv,
return 0;
}
inline interval_t interval_to_seconds(const char *const interval) {
interval_t interval_to_seconds(const char *const interval) {
interval_t intrvl = (interval_t)atol(interval);
switch (interval[strlen(interval) - 1]) {
@ -65,17 +80,18 @@ inline interval_t interval_to_seconds(const char *const interval) {
}
}
inline price_t price_kwh_to_wattspersecond(const price_t price) {
price_t price_kwh_to_wattsecond(const price_t price) {
return price / 1000.f / 3600.f;
}
inline void print_version() {
void print_version(void) {
fputs("kwh-cost Ver. " VERSION "\n"
"Copyright (C) 2021-2023 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
"Copyright (C) 2021-2024 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
"This program is distributed under the terms of MIT+NIGGER licence.\n"
"You can see a license for this program in /usr/share/licenses/kwh-cost/LICENSE.\n", stderr);
}
inline void print_usage() {
void print_usage(void) {
fputs("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
"Options:\n"
" --help, -h display this usage information.\n"

View File

@ -1,19 +0,0 @@
#ifndef _MAIN_H_
#define _MAIN_H_
#define VERSION "1.1.1"
typedef long double price_t;
typedef long double wattage_t;
typedef unsigned long interval_t;
int parse_arguments(const int argc, const char *const *const argv,
price_t *const price, wattage_t *const wattage, interval_t *const interval);
inline interval_t interval_to_seconds(const char *const interval);
inline price_t price_kwh_to_wattspersecond(const price_t price);
inline void print_version();
inline void print_usage();
#endif