aboutsummaryrefslogtreecommitdiff
path: root/wcli.c
diff options
context:
space:
mode:
authorKrow Savcik <krow@savcik.xyz>2024-01-07 15:18:29 +0200
committerKrow Savcik <krow@savcik.xyz>2024-01-07 15:18:29 +0200
commit5138d33e0c0adb0c4d13977af047e375eee934d8 (patch)
tree543825c957813dca637acacc8e5b7574c0979fb3 /wcli.c
initial commit
Diffstat (limited to 'wcli.c')
-rw-r--r--wcli.c192
1 files changed, 192 insertions, 0 deletions
diff --git a/wcli.c b/wcli.c
new file mode 100644
index 0000000..30e6957
--- /dev/null
+++ b/wcli.c
@@ -0,0 +1,192 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <time.h>
+
+#include "config.h"
+
+#define DLEN 25
+
+time_t rawtime;
+struct tm *timeinfo;
+
+struct data {
+ int temp_c;
+ int temp_f;
+ int feel_temp_c;
+ int feel_temp_f;
+ int wind_speed;
+ int wind_gust;
+ char wind_dir_16[4];
+ int wind_dir_deg;
+ int vis;
+ int weather_code;
+ char weather_desc[DLEN];
+} w;
+
+void
+error()
+{
+ printf("doom");
+ exit(1);
+ return;
+}
+
+void
+str_conc_const(const char *s1, const char *s2, char *res)
+{
+ const size_t l1 = strlen(s1);
+ const size_t l2 = strlen(s2);
+
+ memcpy(res,s1,l1);
+ memcpy(res+l1,s2,l2+1);
+ return;
+}
+
+char*
+str_conc(const char *s1, const char *s2)
+{
+ const size_t l1 = strlen(s1);
+ const size_t l2 = strlen(s2);
+
+ char* res = malloc(l1 + l2 + 1);
+ if (res == NULL)
+ error();
+
+ str_conc_const(s1, s2, res);
+
+ return res;
+}
+
+char
+get_dig(short int u, int x)
+{
+ while (--x)
+ u /= 10;
+ return (u % 10) + '0';
+}
+
+void
+get_file(char *file)
+{
+ int i, of;
+ of = 0;
+
+ for (i = 0; i < 4; ++i)
+ file[of++] = get_dig(timeinfo->tm_year+1900, 4-i);
+ file[of++] = '-';
+
+ for (i = 0; i < 2; ++i)
+ file[of++] = get_dig(timeinfo->tm_mon+1, 2-i);
+ file[of++] = '-';
+
+ for (i = 0; i < 2; ++i)
+ file[of++] = get_dig(timeinfo->tm_mday, 2-i);
+ file[of++] = '-';
+
+ file[of++] = (timeinfo->tm_hour/3) + '0';
+ file[of] = '\0';
+}
+
+void
+read_data(char *file)
+{
+ FILE *f = fopen(file, "r");
+ if (!f)
+ error();
+
+ fscanf(f, "%d\n", &w.temp_c);
+ fscanf(f, "%d\n", &w.temp_f);
+ fscanf(f, "%d\n", &w.feel_temp_c);
+ fscanf(f, "%d\n", &w.feel_temp_f);
+ fscanf(f, "%d\n", &w.wind_speed);
+ fscanf(f, "%d\n", &w.wind_gust);
+ fscanf(f, "%s\n", w.wind_dir_16);
+ fscanf(f, "%d\n", &w.wind_dir_deg);
+ fscanf(f, "%d\n", &w.vis);
+ fscanf(f, "%d\n", &w.weather_code);
+ fgets(w.weather_desc, DLEN, f);
+ if (strlen(w.weather_desc) &&
+ w.weather_desc[strlen(w.weather_desc)-1] == '\n')
+ w.weather_desc[strlen(w.weather_desc)-1] = '\0';
+
+ fclose(f);
+ return;
+}
+
+void
+place_holder(char c)
+{
+ switch(c) {
+ case 'c': printf("%d", w.temp_c); break;
+ case 'f': printf("%d", w.temp_f); break;
+ case 'C': printf("%d", w.feel_temp_c); break;
+ case 'F': printf("%d", w.feel_temp_f); break;
+ case 's': printf("%d", w.wind_speed); break;
+ case 'g': printf("%d", w.wind_gust); break;
+ case 'd': printf("%s", w.wind_dir_16); break;
+ case 'D': printf("%d", w.wind_dir_deg); break;
+ case 'v': printf("%d", w.vis); break;
+ case 'w': printf("%d", w.weather_code); break;
+ case 'W': printf("%s", w.weather_desc); break;
+ case '%': printf("%c", '%'); break;
+ }
+ return;
+}
+
+void
+print(char *f)
+{
+ short int sp = 0;
+ int p = 0;
+
+ while (f[p] != '\0') {
+ if (sp) {
+ place_holder(f[p]);
+ sp = 0;
+ } else {
+ if (f[p] == '%')
+ sp = 1;
+ else
+ printf("%c", f[p]);
+ }
+
+ ++p;
+ }
+
+ return;
+}
+
+
+int
+main(int argc, char** argv)
+{
+ char file[28];
+ char *filename, *tempn;
+
+ if (argc < 2)
+ error();
+
+ time(&rawtime);
+ rawtime += (time_t)5400; /* adds 1.5h because time between data is 3 hours */
+ timeinfo = localtime(&rawtime);
+
+ get_file(file);
+
+ if (working_dir[0] == '~') {
+ tempn = str_conc(getenv("HOME"), &working_dir[1]);
+ filename = str_conc(tempn,file);
+ free(tempn);
+ } else {
+ filename = str_conc(working_dir,file);
+ }
+
+
+ read_data(filename);
+ free(filename);
+
+ print(argv[1]);
+
+ return 0;
+}