aboutsummaryrefslogtreecommitdiff
path: root/wcli.c
blob: 30e69571e19bb58826dfe1fc7f32269fd327a977 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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;
}