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