Compare commits

...

19 Commits

Author SHA1 Message Date
Alexander Andreev a9a649d280
To make it compile with clang I added void to print_*() funcs' arguments. Also I had to remove inline specifiers because clang was throwing `undefined reference`. 2024-04-26 04:45:27 +04:00
Alexander Andreev 4b4e64500a
Updated a README.md. 2024-04-26 04:35:58 +04:00
Alexander Andreev f24c7aafc1
Let's put some more safety to parse_arguments(). 2024-04-26 04:29:26 +04:00
Alexander Andreev 328cbc3b0c
In PKGBUILD DESTDIR was removed in build(), and in package() was set to "$pkgdir". 2024-04-26 04:17:42 +04:00
Alexander Andreev 4e016a3b4f
Do not set DESTDIR in Makefile. 2024-04-26 04:09:34 +04:00
Alexander Andreev 43d86103c4
A version was set to 1.1.2. And a year 2023 was set to 2024. 2024-04-26 04:06:31 +04:00
Alexander Andreev 9112fc018c
src directory as well as a separate header file is absolutely not necessary. Also fixed a typo price_kwh_to_wattspersecond -> price_kwh_to_wattsecond, because because a price per kilowatt-hour is being converted to watt-second. Also added a notice about a licence that is used. 2024-04-26 04:03:48 +04:00
Alexander Andreev 5fa6142b21
From PKGBUILD removed provides and conflicts options. 2023-09-23 05:20:03 +04:00
Alexander Andreev ba6f28db2b
In main.h set VERSION to 1.1.1. 2023-09-23 05:16:47 +04:00
Alexander Andreev a999cf3b98
Removed makedepends from PKGBUILD, because gcc is a part of base-devel package. 2023-09-17 23:53:50 +04:00
Alexander Andreev 64bcbfbc45
if else was replaced by switch. Added handling for an unknown flag. 2023-09-17 23:49:06 +04:00
Alexander Andreev 18a593e800
Return 99 if an unknown flag occured. Also updated price_to_wattspersecond to price_kwh_to_wattspersecond. 2023-09-17 23:48:13 +04:00
Alexander Andreev cd84aee61f
Removed a space symbol after an * symbol. 2023-09-17 22:51:28 +04:00
Alexander Andreev caa57721f7
A func price_to_wattspersecond was renamed to price_kwh_to_wattspersecond. 2023-09-17 22:49:13 +04:00
Alexander Andreev ca7859f8f8
puts was replaced by fputs to set stderr output. In version changed years and an e-mail address. 2023-09-17 22:48:18 +04:00
Alexander Andreev 69ff91861b
Removed a space symbol after * symbol. Renamed price_to_wattspersecond to price_kwh_to_wattspersecond. 2023-09-17 22:47:17 +04:00
Alexander Andreev f4ce191ae7
Added PREFIX in Makefile.Fixed .PHONY. Standard c2x was changed to c99. 2023-09-17 22:46:12 +04:00
Alexander Andreev b0f1b367d2
Updated domain, added PREFIX to make, made use of $pkgver. Updated version to 1.1.1. 2023-09-17 22:45:14 +04:00
Alexander Andreev 044b898db3
Updated years and an e-mail in LICENSE. 2023-09-17 22:43:54 +04:00
7 changed files with 127 additions and 136 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2021 Alexander "Arav" Andreev <me@arav.top>
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

@ -1,29 +1,26 @@
TARGET=kwh-cost
CFLAGS=--std=c2x -O3 -Wall -Werror -Wextra -pedantic
DESTDIR:=
PREFIX:=/usr/local
SRC_DIR=src
OBJ_DIR=obj
CFLAGS=--std=c99 -O3 -Wall -Werror -Wextra -pedantic
DESTDIR=/
.PHONY: all ${TARGET} clean install uninstall
.PHONY: clean install uninstall test
all: ${TARGET}
${TARGET}: ${SRC_DIR}/main.c ${SRC_DIR}/main.h
${TARGET}: main.c
${CC} ${CFLAGS} ${LDFLAGS} $< -o $@
clean:
rm ${TARGET}
install:
install -Dm755 ${TARGET} ${DESTDIR}usr/bin/${TARGET}
install -Dm644 LICENSE ${DESTDIR}usr/share/licenses/${TARGET}/LICENSE
install -Dm755 ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET}
install -Dm644 LICENSE ${DESTDIR}${PREFIX}/share/licenses/${TARGET}/LICENSE
uninstall:
rm -f ${DESTDIR}usr/bin/${TARGET}
rm -rf ${DESTDIR}usr/share/licenses/${TARGET}
rm -f ${DESTDIR}${PREFIX}/bin/${TARGET}
rm -rf ${DESTDIR}${PREFIX}/share/licenses/${TARGET}
test:
@echo -n "Does input \"3.68 120 30d\" gives 317.95? "

View File

@ -1,24 +1,21 @@
# Maintainer: Alexander "Arav" Andreev <me@arav.top>
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
pkgname=kwh-cost
pkgver=1.1.0
pkgver=1.1.2
pkgrel=1
pkgdesc="Calculates cost of electricity."
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')
url="https://git.arav.top/Arav/kwh-cost"
url="https://git.arav.su/Arav/kwh-cost"
license=('MIT')
makedepends=('git' 'gcc')
provides=('kwh-cost')
conflicts=('kwh-cost')
options=('strip')
source=('https://git.arav.top/Arav/kwh-cost/archive/1.1.0.tar.gz')
source=("${pkgver}.tar.gz::https://git.arav.su/Arav/kwh-cost/archive/${pkgver}.tar.gz")
md5sums=('SKIP')
build() {
cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/"
make
}
package() {
cd "$srcdir/$pkgname"
make DESTDIR="$pkgdir/" 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.

107
main.c Normal file
View File

@ -0,0 +1,107 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
interval_t interval = 0;
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);
}
printf("%.2Lf\n", (price * wattage) * interval);
return 0;
}
int parse_arguments(const int argc, const char *const *const argv,
price_t *const price, wattage_t *const wattage, interval_t *const interval) {
if (argc < 2) return 1;
for (int i = 1, pos = 0; i < argc; ++i)
if (argv[i][0] == '-') {
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][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_wattsecond((price_t)atof(argv[i]));
++pos;
} else if (pos == 1) {
*wattage = (wattage_t)atof(argv[2]);
++pos;
} else
*interval += interval_to_seconds(argv[i]);
return 0;
}
interval_t interval_to_seconds(const char *const interval) {
interval_t intrvl = (interval_t)atol(interval);
switch (interval[strlen(interval) - 1]) {
case 'm':
case 'M':
return intrvl * 60;
case 'h':
case 'H':
return intrvl * 60 * 60;
case 'd':
case 'D':
return intrvl * 60 * 60 * 24;
default:
return intrvl;
}
}
price_t price_kwh_to_wattsecond(const price_t price) {
return price / 1000.f / 3600.f;
}
void print_version(void) {
fputs("kwh-cost Ver. " VERSION "\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);
}
void print_usage(void) {
fputs("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
"Options:\n"
" --help, -h display this usage information.\n"
" --version, -v display program version.\n"
"Arguments:\n"
" price price per kWh.\n"
" wattage power usage in Watts.\n"
" interval time interval in seconds during which\n"
" this much power to be constantly used.\n"
" You may specify other time units such as:\n"
" m'inute, h'our, d'ay. Example: 5d 3h.\n"
" All given intervals will be summed up.\n", stderr);
}

View File

@ -1,91 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
int main(const int argc, const char * const * const argv) {
price_t price;
wattage_t wattage;
interval_t interval = 0;
int res = parse_arguments(argc, argv, &price, &wattage, &interval);
if (res != 0) {
if (res == 1)
print_usage();
else if (res == 2)
print_version();
exit(0);
}
printf("%.2Lf\n", (price * wattage) * interval);
return 0;
}
int parse_arguments(const int argc, const char * const * const argv,
price_t * const price, wattage_t * const wattage, interval_t * const interval) {
if (argc < 2) return 1;
for (int i = 1, pos = 0; i < argc; ++i)
if (argv[i][0] == '-') {
if (argv[i][1] == 'h' || argv[i][2] == 'h')
return 1;
else if (argv[i][1] == 'v' || argv[i][2] == 'v')
return 2;
} else if (argc < 4) {
return 1;
} else if (pos == 0) {
*price = price_to_wattspersecond((price_t)atof(argv[i]));
++pos;
} else if (pos == 1) {
*wattage = (wattage_t)atof(argv[2]);
++pos;
} else
*interval += interval_to_seconds(argv[i]);
return 0;
}
inline interval_t interval_to_seconds(const char * const interval) {
interval_t intrvl = (interval_t)atol(interval);
switch (interval[strlen(interval) - 1]) {
case 'm':
case 'M':
return intrvl * 60;
case 'h':
case 'H':
return intrvl * 60 * 60;
case 'd':
case 'D':
return intrvl * 60 * 60 * 24;
default:
return intrvl;
}
}
inline price_t price_to_wattspersecond(const price_t price) {
return price / 1000.f / 3600.f;
}
inline void print_version() {
puts("kwh-cost Ver. " VERSION "\n"
"Copyright (C) 2021,2022 Alexander \"Arav\" Andreev <me@arav.top>. All rights reserved.\n"
"You can see a license for this program in /usr/share/licenses/kwh-cost/LICENSE.");
}
inline void print_usage() {
puts("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
"Options:\n"
" --help, -h display this usage information.\n"
" --version, -v display program version.\n"
"Arguments:\n"
" price price per kWh.\n"
" wattage power usage in Watts.\n"
" interval time interval in seconds during which\n"
" this much power to be constantly used.\n"
" You may specify other time units such as:\n"
" m'inute, h'our, d'ay. Example: 5d 3h.\n"
" All given intervals will be summed up.");
}

View File

@ -1,19 +0,0 @@
#ifndef _MAIN_H_
#define _MAIN_H_
#define VERSION "1.1.0"
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_to_wattspersecond(const price_t price);
inline void print_version();
inline void print_usage();
#endif