1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <json-c/json.h>
#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]);
}
|