From 8e09177f2400b97504a1fd40d142e79a654c8ba1 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 4 Nov 2024 15:20:26 +0400 Subject: [PATCH] Version was set to 1.2.0. Now m, K, M, G, T units can be specified for wattage. Dropped support for long arguments (--). Functions were reorganised. --- PKGBUILD | 2 +- kwh-cost.c | 132 ++++++++++++++++++++++++++++------------------------- 2 files changed, 70 insertions(+), 64 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 738d146..c1260fc 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: Alexander "Arav" Andreev pkgname=kwh-cost -pkgver=1.1.3 +pkgver=1.2.0 pkgrel=1 pkgdesc="Calculates cost of electricity." arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64') diff --git a/kwh-cost.c b/kwh-cost.c index 127d5b0..a17fd82 100644 --- a/kwh-cost.c +++ b/kwh-cost.c @@ -2,66 +2,12 @@ #include #include -#define VERSION "1.1.3" +#define VERSION "1.2.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); - -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 = 0; - wattage_t wattage = 0; - interval_t interval = 0; - - switch (parse_arguments(argc, argv, &price, &wattage, &interval)) { - case 1: print_usage(); return 0; - case 2: print_version(); return 0; - case 99: fputs("Unknown flag provided.\n", stderr); print_usage(); return 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 == 1) - 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); @@ -80,28 +26,88 @@ interval_t interval_to_seconds(const char *const interval) { } } +wattage_t wattage_to_watts(const char *const wattage) { + wattage_t wattg = (wattage_t)atof(wattage); + + switch (wattage[strlen(wattage)-1]) { + case 'm': + return wattg / 1000.; + case 'k': + case 'K': + return wattg * 1000.; + case 'M': + return wattg * 1000000.; + case 'G': + return wattg * 1000000000.; + case 'T': + return wattg * 1000000000000.; + default: + return wattg; + } +} + price_t price_kwh_to_wattsecond(const price_t price) { return price / 1000.f / 3600.f; } +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 (strlen(argv[i]) < 2) return 99; + switch (argv[i][1]) { + case 'h': return 1; + case 'v': return 2; + default: 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_to_watts(argv[2]); + ++pos; + } else + *interval += interval_to_seconds(argv[i]); + + return 0; +} + void print_version(void) { fputs("kwh-cost Ver. " VERSION "\n" "Copyright (C) 2021-2024 Alexander \"Arav\" Andreev . All rights reserved.\n" "This program is distributed under the terms of MIT+NIGGER license.\n" - "You can see a license for this program in /usr/share/licenses/kwh-cost/LICENSE.\n", stderr); + "A license for this program is placed 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" + " -h display this usage information.\n" + " -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" + " wattage power usage in Watts (m, K, M, G, T).\n" + " interval time interval in seconds (m'inutes, h'ours, d'ays,\n" + " like 5d 3h) during which given power is constantly used.\n" " All given intervals will be summed up.\n", stderr); } + +int main(const int argc, const char *const *const argv) { + price_t price = 0; + wattage_t wattage = 0; + interval_t interval = 0; + + switch (parse_arguments(argc, argv, &price, &wattage, &interval)) { + case 1: print_usage(); return 0; + case 2: print_version(); return 0; + case 99: fputs("Unknown flag provided.\n", stderr); print_usage(); return 1; + } + + printf("%.2Lf\n", (price * wattage) * interval); + + return 0; +}