2021-12-23 23:59:00 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-11-04 15:20:26 +04:00
|
|
|
#define VERSION "1.2.0"
|
2024-04-26 04:03:48 +04:00
|
|
|
|
|
|
|
typedef long double price_t;
|
|
|
|
typedef long double wattage_t;
|
|
|
|
typedef unsigned long interval_t;
|
|
|
|
|
2024-11-04 15:20:26 +04:00
|
|
|
interval_t interval_to_seconds(const char *const interval) {
|
|
|
|
interval_t intrvl = (interval_t)atol(interval);
|
2024-04-26 04:03:48 +04:00
|
|
|
|
2024-11-04 15:20:26 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-04-26 04:03:48 +04:00
|
|
|
|
2024-11-04 15:20:26 +04:00
|
|
|
wattage_t wattage_to_watts(const char *const wattage) {
|
|
|
|
wattage_t wattg = (wattage_t)atof(wattage);
|
2021-12-23 23:59:00 +04:00
|
|
|
|
2024-11-04 15:20:26 +04:00
|
|
|
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;
|
2024-05-19 05:21:35 +04:00
|
|
|
}
|
2024-11-04 15:20:26 +04:00
|
|
|
}
|
2021-12-25 23:27:53 +04:00
|
|
|
|
2024-11-04 15:20:26 +04:00
|
|
|
price_t price_kwh_to_wattsecond(const price_t price) {
|
|
|
|
return price / 1000.f / 3600.f;
|
2021-12-23 23:59:00 +04:00
|
|
|
}
|
|
|
|
|
2023-09-17 22:51:28 +04:00
|
|
|
int parse_arguments(const int argc, const char *const *const argv,
|
|
|
|
price_t *const price, wattage_t *const wattage, interval_t *const interval) {
|
2021-12-25 23:27:53 +04:00
|
|
|
if (argc < 2) return 1;
|
2021-12-23 23:59:00 +04:00
|
|
|
|
2021-12-25 23:27:53 +04:00
|
|
|
for (int i = 1, pos = 0; i < argc; ++i)
|
2021-12-23 23:59:00 +04:00
|
|
|
if (argv[i][0] == '-') {
|
2024-11-04 15:20:26 +04:00
|
|
|
if (strlen(argv[i]) < 2) return 99;
|
|
|
|
switch (argv[i][1]) {
|
|
|
|
case 'h': return 1;
|
|
|
|
case 'v': return 2;
|
|
|
|
default: return 99;
|
|
|
|
}
|
2021-12-25 23:27:53 +04:00
|
|
|
} else if (argc < 4) {
|
|
|
|
return 1;
|
|
|
|
} else if (pos == 0) {
|
2024-04-26 04:03:48 +04:00
|
|
|
*price = price_kwh_to_wattsecond((price_t)atof(argv[i]));
|
2021-12-25 23:27:53 +04:00
|
|
|
++pos;
|
|
|
|
} else if (pos == 1) {
|
2024-11-04 15:20:26 +04:00
|
|
|
*wattage = wattage_to_watts(argv[2]);
|
2021-12-25 23:27:53 +04:00
|
|
|
++pos;
|
|
|
|
} else
|
2022-10-07 03:50:57 +04:00
|
|
|
*interval += interval_to_seconds(argv[i]);
|
2021-12-23 23:59:00 +04:00
|
|
|
|
2021-12-25 23:27:53 +04:00
|
|
|
return 0;
|
2021-12-23 23:59:00 +04:00
|
|
|
}
|
|
|
|
|
2024-04-26 04:45:27 +04:00
|
|
|
void print_version(void) {
|
2023-09-17 22:48:18 +04:00
|
|
|
fputs("kwh-cost Ver. " VERSION "\n"
|
2024-04-26 04:06:31 +04:00
|
|
|
"Copyright (C) 2021-2024 Alexander \"Arav\" Andreev <me@arav.su>. All rights reserved.\n"
|
2024-05-19 21:57:18 +04:00
|
|
|
"This program is distributed under the terms of MIT+NIGGER license.\n"
|
2024-11-04 15:20:26 +04:00
|
|
|
"A license for this program is placed in /usr/share/licenses/kwh-cost/LICENSE.\n", stderr);
|
2021-12-23 23:59:00 +04:00
|
|
|
}
|
|
|
|
|
2024-04-26 04:45:27 +04:00
|
|
|
void print_usage(void) {
|
2023-09-17 22:48:18 +04:00
|
|
|
fputs("Usage: kwh-cost [OPTIONS] price wattage interval [interval ...]\n"
|
2021-12-25 23:27:53 +04:00
|
|
|
"Options:\n"
|
2024-11-04 15:20:26 +04:00
|
|
|
" -h display this usage information.\n"
|
|
|
|
" -v display program version.\n"
|
2021-12-25 23:27:53 +04:00
|
|
|
"Arguments:\n"
|
2022-10-07 03:51:52 +04:00
|
|
|
" price price per kWh.\n"
|
2024-11-04 15:20:26 +04:00
|
|
|
" 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"
|
2023-09-17 22:48:18 +04:00
|
|
|
" All given intervals will be summed up.\n", stderr);
|
2021-12-24 01:01:43 +04:00
|
|
|
}
|
2024-11-04 15:20:26 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|