-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcunique.c
292 lines (247 loc) · 9.04 KB
/
cunique.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#define _XOPEN_SOURCE
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define STRLEN 32
#define LINELEN 256
#define USAGE "Usage: %s -f filename\n"
#define FMT "%Y-%m-%d %H:%M:%S"
#define FMT1 "%Y-%m-%d %H:%M"
#define FMT2 "%H:%M UTC"
#define CONTINENTS {"AF", "AS", "EU", "NA", "OC", "SA" }
#define MAXCONT 6
// #define BANDS { "6m", "10m", "12m", "15m", "17m", "20m", "30m", "40m", "60m", "80m", "160m" }
#define BANDS { "160m", "80m", "60m", "40m", "30m", "20m", "17m", "15m", "12m", "10m", "6m" }
#define MAXBANDS 11
#define MAXCALLS 100000
#define MAXSKIMMERS 500
int main(int argc, char *argv[])
{
FILE *fp;
char filename[LINELEN] = "", line[LINELEN] = "";
int c, totalspots = 0;
time_t firstspot, lastspot;
struct tm stime;
struct Counter {
char call[MAXCALLS][STRLEN];
char skimmer[MAXSKIMMERS][STRLEN];
int skimcount;
int callcount;
};
static char bandname[MAXBANDS][STRLEN] = BANDS;
static char contname[MAXCONT][STRLEN] = CONTINENTS;
// List of all skimmers found
static char skimarray[MAXSKIMMERS][STRLEN];
int totalskimmers = 0;
// List of all calls found
static char callarray[2 * MAXCALLS][STRLEN];
int totalcalls = 0;
static struct Counter contbandarray[MAXCONT][MAXBANDS];
static struct Counter contarray[MAXCONT];
static struct Counter bandarray[MAXBANDS];
int strindex(char *string, char array[][STRLEN], int size)
{
for (int i = 0; i < size; i++)
{
if (strcmp(string, array[i]) == 0)
{
return i;
}
}
return -1;
}
for (int bi = 0; bi < MAXBANDS; bi++)
{
bandarray[bi].skimcount = 0;
bandarray[bi].callcount = 0;
for (int ci = 0; ci < MAXCONT; ci++)
{
contbandarray[ci][bi].skimcount = 0;
contbandarray[ci][bi].callcount = 0;
contarray[ci].skimcount = 0;
contarray[ci].callcount = 0;
}
}
while ((c = getopt(argc, argv, "f:")) != -1)
{
switch (c)
{
case 'f': // Filename
strcpy(filename, optarg);
break;
case '?':
default:
fprintf(stderr, USAGE, argv[0]);
return 1;
}
}
if (strlen(filename) == 0)
{
fprintf(stderr, USAGE, argv[0]);
return 1;
}
fp = fopen(filename, "r");
if (fp == NULL)
{
fprintf(stderr, "Can not open file \"%s\". Abort.\n", filename);
return 1;
}
while (fgets(line, LINELEN, fp) != NULL)
{
char band[STRLEN], decont[STRLEN], dxcall[STRLEN], dxcont[STRLEN], mode[STRLEN],
timestring[LINELEN], decall[STRLEN];
int snr;
double freq;
// time_t spottime = 0;
// int year, month;
int got = sscanf(line, "%[^,],%*[^,],%[^,],%lf,%[^,],%[^,],%*[^,],%[^,],%*[^,],%d,%[^,],%*[^,],%s",
decall, decont, &freq, band, dxcall, dxcont, &snr, timestring, mode);
if (got == 9) // If parsing is successful
{
int bandindex = strindex(band, bandname, MAXBANDS);
int decontindex = strindex(decont, contname, MAXCONT);
int dxcontindex = strindex(dxcont, contname, MAXCONT);
(void)strptime(timestring, FMT, &stime);
stime.tm_isdst = 0;
time_t spottime = mktime(&stime);
// fprintf(stderr, "timestring=%s spottime=%ld year=%d mon=%d day=%d hour=%d min=%d sec=%d\n",
// timestring, spottime, stime.tm_year + 1900, stime.tm_mon + 1, stime.tm_mday, stime.tm_hour,
// stime.tm_min, stime.tm_sec);
if (totalspots == 0)
{
firstspot = spottime;
lastspot = spottime;
}
else
{
if (spottime > lastspot)
lastspot = spottime;
if (spottime < firstspot)
firstspot = spottime;
}
totalspots++;
if (dxcontindex != -1 && decontindex != -1 && bandindex != -1)
{
// Check if new dx call for continent
if (strindex(dxcall, contarray[dxcontindex].call, contarray[dxcontindex].callcount) == -1)
{
strcpy(contarray[dxcontindex].call[contarray[dxcontindex].callcount++], dxcall);
}
// Check if new dx call for band
if (strindex(dxcall, bandarray[bandindex].call, bandarray[bandindex].callcount) == -1)
{
strcpy(bandarray[bandindex].call[bandarray[bandindex].callcount++], dxcall);
}
// Check if new dx call for band and continent
if (strindex(dxcall, contbandarray[dxcontindex][bandindex].call, contbandarray[dxcontindex][bandindex].callcount) == -1)
{
strcpy(contbandarray[dxcontindex][bandindex].call[contbandarray[dxcontindex][bandindex].callcount++], dxcall);
}
// Check if new dx call
if (strindex(dxcall, callarray, totalcalls) == -1)
{
strcpy(callarray[totalcalls++], dxcall);
}
// Check if new skimmer for continent
if (strindex(decall, contarray[decontindex].skimmer, contarray[decontindex].skimcount) == -1)
{
strcpy(contarray[decontindex].skimmer[contarray[decontindex].skimcount++], decall);
}
// Check if new skimmer for band
if (strindex(decall, bandarray[bandindex].skimmer, bandarray[bandindex].skimcount) == -1)
{
strcpy(bandarray[bandindex].skimmer[bandarray[bandindex].skimcount++], decall);
}
// Check if new skimmer for band and continent
if (strindex(decall, contbandarray[decontindex][bandindex].skimmer, contbandarray[decontindex][bandindex].skimcount) == -1)
{
strcpy(contbandarray[decontindex][bandindex].skimmer[contbandarray[decontindex][bandindex].skimcount++], decall);
}
// Check if new skimmer for any band
if (strindex(decall, skimarray, totalskimmers) == -1)
{
strcpy(skimarray[totalskimmers++], decall);
}
}
else
{
// printf("Ignored: %s", line);
}
}
}
(void)fclose(fp);
char firsttimestring[LINELEN], lasttimestring[LINELEN];
stime = *localtime(&firstspot);
(void)strftime(firsttimestring, LINELEN, FMT1, &stime);
stime = *localtime(&lastspot);
(void)strftime(lasttimestring, LINELEN, FMT2, &stime);
printf("RBN activity between %s and %s.\n", firsttimestring, lasttimestring);
printf("%d spots with %d unique callsigns from %d active skimmers.\n",
totalspots, totalcalls, totalskimmers);
printf("\n Unique callsigns spotted per continent and band\n");
printf("---------------------------------------------------------------\n");
#define LEFTCOL "%-3s"
#define COLS "%5s"
#define COLN "%5d"
printf(LEFTCOL, "");
for (int bi = 0; bi < MAXBANDS; bi++)
{
if (bandarray[bi].callcount > 9999) printf(" ");
printf(COLS, bandname[bi]);
}
printf("\n");
int longcont = 0;
for (int ci = 0; ci < MAXCONT; ci++)
{
longcont |= contarray[ci].callcount > 9999;
}
for (int ci = 0; ci < MAXCONT; ci++)
{
printf(LEFTCOL, contname[ci]);
for (int bi = 0; bi < MAXBANDS; bi++)
{
if (bandarray[bi].callcount > 9999) printf(" ");
printf(COLN, contbandarray[ci][bi].callcount);
}
if (longcont) printf(" ");
printf(COLN, contarray[ci].callcount);
printf("\n");
}
printf(LEFTCOL, "");
for (int bi = 0; bi < MAXBANDS; bi++)
{
if (bandarray[bi].callcount > 9999) printf(" ");
printf(COLN, bandarray[bi].callcount);
}
printf("\n");
printf("\n Active skimmers per continent and band\n");
printf("---------------------------------------------------------------\n");
printf(LEFTCOL, "");
for (int bi = 0; bi < MAXBANDS; bi++)
{
if (bandarray[bi].callcount > 9999) printf(" ");
printf(COLS, bandname[bi]);
}
printf("\n");
for (int ci = 0; ci < MAXCONT; ci++)
{
printf(LEFTCOL, contname[ci]);
for (int bi = 0; bi < MAXBANDS; bi++)
{
if (bandarray[bi].callcount > 9999) printf(" ");
printf(COLN, contbandarray[ci][bi].skimcount);
}
if (longcont) printf(" ");
printf(COLN, contarray[ci].skimcount);
printf("\n");
}
printf(LEFTCOL, "");
for (int bi = 0; bi < MAXBANDS; bi++)
{
if (bandarray[bi].callcount > 9999) printf(" ");
printf(COLN, bandarray[bi].skimcount);
}
printf("\n");
return 0;
}