-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
188 lines (162 loc) · 4.01 KB
/
utils.c
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
//
// Weather Station Poller
//
// Copyright (C) 2010 Joakim Söderberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include "wsp.h"
#include "utils.h"
int svn_revision()
{
char *s = BUILD_NUM;
while (*s)
{
if (*s == ':')
{
s++;
return atoi(s);
}
s++;
}
return 0;
}
void debug_printf(unsigned int debug_level, const char* format, ... )
{
if (debug >= debug_level)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
}
//
// Parses a date in BCD format (http://en.wikipedia.org/wiki/Binary-coded_decimal).
// Each nibble of each byte corresponds to one number. The first byte contains the year.
//
bcd_date_t parse_bcd_date(unsigned char date[5])
{
bcd_date_t d;
d.year = 2000 + (((date[0] >> 4) & 0xf) * 10) + (date[0] & 0xf);
d.month = (((date[1] >> 4) & 0xf) * 10) + (date[1] & 0xf);
d.day = (((date[2] >> 4) & 0xf) * 10) + (date[2] & 0xf);
d.hour = (((date[3] >> 4) & 0xf) * 10) + (date[3] & 0xf);
d.minute = (((date[4] >> 4) & 0xf) * 10) + (date[4] & 0xf);
return d;
}
//
// Prints a BCD date.
//
void print_bcd_date(unsigned char date[5])
{
bcd_date_t d = parse_bcd_date(date);
printf("%u-%02u-%02u %02u:%02u:00", d.year, d.month, d.day, d.hour, d.minute);
}
const char *get_bcd_date_string(unsigned char date[5])
{
static char str[1024];
bcd_date_t d = parse_bcd_date(date);
sprintf(str, "%u-%02u-%02u %02u:%02u:00", d.year, d.month, d.day, d.hour, d.minute);
return str;
}
//
// Gets a string for the wind direction from the settings byte.
//
const char *get_wind_direction(const char data)
{
switch (data)
{
default:
case 0: return "N";
case 1: return "NNE";
case 2: return "NE";
case 3: return "NEE";
case 4: return "E";
case 5: return "SEE";
case 6: return "SE";
case 7: return "SSE";
case 8: return "S";
case 9: return "SSW";
case 10: return "SW";
case 11: return "WSW";
case 12: return "W";
case 13: return "WNW";
case 14: return "NW";
case 15: return "NNW";
}
}
//
// Prints bytes for debug purposes.
//
void print_bytes(unsigned int debug_level, char *bytes, unsigned int len)
{
if ((debug >= debug_level) && (len > 0))
{
unsigned int i;
for (i = 0; i < len; i++)
{
printf("%02x ", (int)((unsigned char)bytes[i]));
}
debug_printf(debug_level, "\n");
}
}
int file_exists(const char *filename)
{
FILE *f;
if ((f = fopen(program_settings.dumpfile, "r")) == NULL)
{
return 0;
}
fclose(f);
return 1;
}
char prompt_user()
{
char c;
fflush(stdin);
scanf("%c", &c);
return c;
}
char *get_timestamp(time_t t)
{
static char tbuf[128];
strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:00", localtime(&t));
return tbuf;
}
char *get_local_timestamp()
{
static char tbuf[128];
time_t now = time(NULL);
strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:00", localtime(&now));
return tbuf;
}
time_t bcd_to_unix_date(bcd_date_t date)
{
time_t rawtime;
struct tm timeinfo;
memset(&timeinfo, 0, sizeof(timeinfo));
timeinfo.tm_year = date.year - 1900;
timeinfo.tm_mon = date.month - 1;
timeinfo.tm_mday = date.day;
timeinfo.tm_hour = date.hour;
timeinfo.tm_min = date.minute;
timeinfo.tm_isdst = 1;
rawtime = mktime(&timeinfo);
return rawtime;
}