#include #include #include #include #include #define VERBOSE #include "config.h" char *wdir; void error(const char* restrict format, ...); int printv(const char* restrict format, ...); static int _printv(const char* restrict format, va_list args); void error(const char* restrict format, ...) { va_list args; va_start(args,format); _printv(format,args); va_end(args); exit(1); return; } int printv(const char* restrict format, ...) { va_list args; va_start(args,format); int ret = _printv(format,args); va_end(args); return ret; } int _printv(const char* restrict format, va_list args) { #ifndef VERBOSE return 0; #endif int ret = vprintf(format,args); return ret; } 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("Error while concatenating \"%s\" and \"%s\".\n", s1, s2); str_conc_const(s1, s2, res); return res; } void parse(char *fn) { json_object *weather, *root, *day, *hly, *hour; char *file, *pref, suf[3]; FILE *fl; int i, j, s, h; root = json_object_from_file(fn); if (!root) error("Error\n"); /* Assuming no errors */ weather = json_object_object_get(root, "weather"); s = json_object_array_length(weather); suf[0] = '-'; suf[2] = '\0'; for (i = 0; i < s; i++) { day = json_object_array_get_idx(weather,i); hly = json_object_object_get(day, "hourly"); if (!i) pref = str_conc(wdir,json_object_get_string(json_object_object_get(day, "date"))); else str_conc_const(wdir,json_object_get_string(json_object_object_get(day, "date")),pref); h = json_object_array_length(hly); for (j = 0; j < h; j++) { suf[1] = j+'0'; if (!i && !j) file = str_conc(pref,suf); else str_conc_const(pref,suf,file); hour = json_object_array_get_idx(hly,j); fl = fopen(file,"w"); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "tempC"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "tempF"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "FeelsLikeC"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "FeelsLikeF"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "windspeedKmph"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "WindGustKmph"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "winddir16Point"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "winddirDegree"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "visibility"))); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "weatherCode"))); hour = json_object_array_get_idx(json_object_object_get(hour, "weatherDesc"),0); fprintf(fl, "%s\n", json_object_get_string(json_object_object_get(hour, "value"))); fclose(fl); } } free(file); free(pref); json_object_put(root); return; } int main(int argc, char** argv) { if (argc < 2) error("Not enough arguments.\n"); if (working_dir[0] == '~') { wdir = str_conc(getenv("HOME"), &working_dir[1]); } else { wdir = working_dir; } parse(argv[1]); }