[main.c] Structure arguments_t removed. Use ptrs to vars instead. parse_arguments now returns status codes instead of printing stuff and closing program.

`to_wattpersecond` is now `to_wattspersecond`.
Fixed typo in usage, capitalised `options` word. Rephrased `print_version` text.
Removed unnecessary check from `interval_to_seconds` (former `to_seconds`) function.
This commit is contained in:
Alexander Andreev 2021-12-25 23:27:53 +04:00
parent 1799c62c48
commit b457c60f75
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -5,91 +5,85 @@
#include "main.h"
int main(int argc, char **argv) {
arguments_t * args = parse_arguments(argc, argv);
price_t price;
wattage_t wattage;
interval_t interval = 0;
printf("%.2Lf\n", (args->price * args->wattage) * args->interval);
free(args);
return 0;
}
arguments_t * parse_arguments(int argc, char **argv) {
if (argc == 1) {
print_usage();
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);
}
arguments_t *args = (arguments_t *)calloc(1, sizeof(arguments_t));
int pos_args_pos = 0;
printf("%.2Lf\n", (price * wattage) * interval);
for (int i = 1; i < argc; ++i) {
return 0;
}
int parse_arguments(int argc, char **argv, price_t *price, wattage_t *wattage, interval_t *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') {
print_usage();
exit(0);
} else if (argv[i][1] == 'v' || argv[i][2] == 'v') {
print_version();
exit(0);
}
} else if (pos_args_pos == 0) {
args->price = to_wattpersecond((price_t)atof(argv[i]));
pos_args_pos++;
} else if (pos_args_pos == 1) {
args->wattage = atof(argv[i]);
pos_args_pos++;
} else if (pos_args_pos > 1) {
args->interval += to_seconds(argv[i]);
pos_args_pos++;
}
}
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 = to_wattspersecond((price_t)atof(argv[i]));
++pos;
} else if (pos == 1) {
*wattage = (wattage_t)atof(argv[2]);
++pos;
} else
*interval += (interval_t)interval_to_seconds(argv[i]);
if (pos_args_pos < 3) {
print_usage();
exit(1);
}
return args;
return 0;
}
interval_t to_seconds(char *interval) {
inline interval_t interval_to_seconds(char *interval) {
interval_t intrvl = (interval_t)atol(interval);
char unit = interval[strlen(interval) - 1];
if (!(unit >= '0' && unit <= '9'))
switch (unit) {
case 'm':
case 'M':
return intrvl * 60;
case 'h':
case 'H':
return intrvl * 60 * 60;
case 'd':
case 'D':
return intrvl * 60 * 60 * 24;
}
return intrvl;
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 to_wattpersecond(price_t price) {
inline price_t to_wattspersecond(price_t price) {
return price / 1000.f / 3600.f;
}
void print_version() {
inline void print_version() {
puts("kwh-cost Ver. " VERSION "\n"
"Copyright (C) 2021 Alexander \"Arav\" Andreev <me@arav.top>. All rights reserved.\n"
"License for this program is stored in /usr/share/licenses/kwh-cost/LICENSE.");
"Copyright (C) 2021 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.");
}
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 Specify power usage in Watts.\n"
" interval Time interval in seconds total cost to be calculated for.\n"
" You may specify other time units such as:\n"
" m'imute, h'our, d'ay. Example: 5d 3h.\n"
" All given intervals will be summed up.");
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 Specify power usage in Watts.\n"
" interval Time interval in seconds total cost to be calculated for.\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.");
}