From 7d03901e2492e0261ceca462d5210d54508f7367 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Thu, 23 Dec 2021 23:59:00 +0400 Subject: [PATCH] Source files. --- src/main.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.h | 27 ++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/main.c create mode 100644 src/main.h diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..fbed0db --- /dev/null +++ b/src/main.c @@ -0,0 +1,101 @@ +#include +#include +#include + +#include "main.h" + +int main(int argc, char **argv) { + arguments_t * args = parse_arguments(argc, argv); + + 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(); + exit(0); + } + + 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(); + 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++; + } + } + + printf("%d\n", pos_args_pos); + + if (pos_args_pos < 3) { + print_usage(); + exit(1); + } + + return args; +} + +interval_t 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; +} + +inline price_t to_wattpersecond(price_t price) { + return price / 1000.f / 3600.f; +} + +void print_version() { + printf("kwh-calc Ver. " VERSION "\n" + "Copyright (C) 2021 Alexander \"Arav\" Andreev . All rights reserved.\n" + "The software is provided \"AS IS\", without WARRANTY of ANY KIND, EXPRESS or\n" + "IMPLIED, including but not limited to the WARRANTIES of MERCHANTABILITY, fitness\n" + "for a PARTICULAR PURPOSE and NONINFRINGEMENT.\n" + "See LICENSE file, containing a text of MIT+NIGGER license, that comes with\n" + "the source code for a full text.\n"); +} + +void print_usage() { + printf("Usage: kwh-calc [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.\n"); +} \ No newline at end of file diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..89b758b --- /dev/null +++ b/src/main.h @@ -0,0 +1,27 @@ +#ifndef _MAIN_H_ +#define _MAIN_H_ + +#define VERSION "1.0.0" + +typedef long double price_t; +typedef long double wattage_t; +typedef unsigned long interval_t; + +typedef struct { + price_t price; + wattage_t wattage; + interval_t interval; +} arguments_t; + +arguments_t *parse_arguments(int argc, char **argv); + + +interval_t to_seconds(char *interval); +inline price_t to_wattpersecond(price_t price); + + +void print_version(); +void print_usage(); + + +#endif \ No newline at end of file