-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheas_decode.cpp
231 lines (216 loc) · 6.54 KB
/
eas_decode.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <time.h>
#include <pcre.h>
#include "eas_decode.h"
using namespace std;
static time_t mkgmtime(struct tm *tm)
{
static const int mdays[13] = {
0,31,59,90,120,151,181,212,243,273,304,334
};
return ((((((tm->tm_year - 70) * 365) + mdays[tm->tm_mon] + tm->tm_mday-1 + (tm->tm_year-68-1+(tm->tm_mon>=2))/4) * 24) + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
}
struct Description {
const char *abbr;
const char *desc;
};
static const Description Originators[] = {
{"CIV", "Civil authorities"},
{"EAS", "Broadcast station or cable station"},
{"PEP", "Primary Entry Point System"},
{"WXR", "National Weather Service"},
};
static const Description Events[] = {
{"ADR", "Administrative Message"},
{"AVA", "Avalanche Watch"},
{"AVW", "Avalanche Warning"},
{"BZW", "Blizzard Warning"},
{"CAE", "Child Abduction Emergency"},
{"CDW", "Civil Danger Warning"},
{"CEM", "Civil Emergency Message"},
{"CFA", "Coastal Flood Watch"},
{"CFW", "Coastal Flood Warning"},
{"DMO", "Practice/Demo Warning"},
{"DSW", "Dust Storm Warning"},
{"EAN", "Emergency Action Notification"},
{"EAT", "Emergency Action Termination"},
{"EQW", "Earthquake Warning"},
{"EVI", "Evacuation Immediate"},
{"FFA", "Flash Flood Watch"},
{"FFS", "Flash Flood Statement"},
{"FFW", "Flash Flood Warning"},
{"FLA", "Flood Watch"},
{"FLS", "Flood Statement"},
{"FLW", "Flood Warning"},
{"FRW", "Fire Warning"},
{"HLS", "Hurricane Statement"},
{"HMW", "Hazardous Materials Warning"},
{"HUA", "Hurricane Watch"},
{"HUW", "Hurricane Warning"},
{"HWA", "High Wind Watch"},
{"HWW", "High Wind Warning"},
{"LAE", "Local Area Emergency"},
{"LEW", "Law Enforcement Warning"},
{"NIC", "National Information Center"},
{"NMN", "Network Message Notification"},
{"NPT", "National Periodic Test"},
{"NUW", "Nuclear Power Plant Warning"},
{"RHW", "Radiological Hazard Warning"},
{"RMT", "Required Monthly Test"},
{"RWT", "Required Weekly Test"},
{"SMW", "Special Marine Warning"},
{"SPS", "Special Weather Statement"},
{"SPW", "Shelter in Place Warning"},
{"SVA", "Severe Thunderstorm Watch"},
{"SVR", "Severe Thunderstorm Warning"},
{"SVS", "Severe Weather Statement"},
{"TOA", "Tornado Watch"},
{"TOE", "911 Telephone Outage Emergency"},
{"TOR", "Tornado Warning"},
{"TRA", "Tropical Storm Watch"},
{"TRW", "Tropical Storm Warning"},
{"TSA", "Tsunami Watch"},
{"TSW", "Tsunami Warning"},
{"VOW", "Volcano Warning"},
{"WSA", "Winter Storm Watch"},
{"WSW", "Winter Storm Warning"},
};
static int strcompare(const void *p1, const void *p2)
{
return strcmp((const char *)p1, ((const Description *)p2)->abbr);
}
static string getOriginatorDesc(const string &originator)
{
Description *d = (Description *)bsearch(originator.c_str(), Originators, sizeof(Originators)/sizeof(Originators[0]), sizeof(Originators[0]), strcompare);
if (d == NULL) {
return string();
}
return d->desc;
}
static string getEventDesc(const string &event)
{
Description *d = (Description *)bsearch(event.c_str(), Events, sizeof(Events)/sizeof(Events[0]), sizeof(Events[0]), strcompare);
if (d == NULL) {
return string();
}
return d->desc;
}
struct County {
int fips;
const char *name;
};
static const County Counties[] = {
#include "fips.inc"
};
static const char *Parts[] = {
"",
"Northwest ",
"North ",
"Northeast ",
"West ",
"Central ",
"East ",
"Southwest ",
"South ",
"Southeast ",
};
static int fipscompare(const void *p1, const void *p2)
{
return *(int *)p1 - ((County *)p2)->fips;
}
static string getAreaDesc(const eas::Message::Area &area)
{
int fips = 1000 * area.state + area.county;
County *c = (County *)bsearch(&fips, Counties, sizeof(Counties)/sizeof(Counties[0]), sizeof(Counties[0]), fipscompare);
if (c == NULL) {
return string();
}
return string(Parts[area.part]) + c->name;
}
static string getSenderDesc(const string &sender)
{
return string();
}
bool eas::Decode(const char *s, Message &message)
{
const char *errptr;
int erroffset;
pcre *re = pcre_compile(
"^ZCZC-(\\w+)-(\\w+)((?:-[^+-]+){1,31})\\+(\\d{2})(\\d{2})-(\\d{3})(\\d{2})(\\d{2})-([^-]+)-",
// 1 2 3 4 5 6 7 8 9
0,
&errptr,
&erroffset,
NULL);
if (re == NULL) {
return false;
}
int ovector[3*10];
int r = pcre_exec(
re,
NULL,
s,
strlen(s),
0,
0,
ovector,
sizeof(ovector)/sizeof(ovector[0]));
pcre_free(re);
if (r < 0) {
return false;
}
const char **matches;
pcre_get_substring_list(s, ovector, r, &matches);
int yday = atoi(matches[6]);
time_t now = time(0);
struct tm *tt;
for (;;) {
tt = gmtime(&now);
if (1+tt->tm_yday == yday) {
break;
} else if (1+tt->tm_yday < yday) {
now += 86400;
} else if (1+tt->tm_yday > yday) {
now -= 86400;
}
}
tt->tm_hour = atoi(matches[7]);
tt->tm_min = atoi(matches[8]);
tt->tm_sec = 0;
message.raw = s;
message.originator = matches[1];
message.originator_desc = getOriginatorDesc(message.originator);
message.event = matches[2];
message.event_desc = getEventDesc(message.event);
message.areas.clear();
string a;
for (const char *p = matches[3]; ; p++) {
if (*p == '-' || *p == 0) {
if (*p == '-') {
p++;
}
if (!a.empty()) {
Message::Area area;
area.code = a;
if (a.length() == 6 && strspn(a.c_str(), "0123456789") == 6) {
area.part = a[0] - '0';
area.state = 10*(a[1] - '0') + (a[2] - '0');
area.county = 10*(10*(a[3] - '0') + (a[4] - '0')) + (a[5] - '0');
}
area.desc = getAreaDesc(area);
message.areas.push_back(area);
}
a.erase();
if (*p == 0) {
break;
}
}
a += *p;
}
message.issued = mkgmtime(tt);
message.received = time(0);
message.purge = message.issued + 60*(60*atoi(matches[4]) + atoi(matches[5]));
message.sender = matches[9];
message.sender_desc = getSenderDesc(message.sender);
pcre_free_substring_list(matches);
return true;
}