-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
263 lines (222 loc) · 8.84 KB
/
main.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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "curl/curl.h"
#include "hashtable.h"
#include "station.h"
void download_eibi_schedule() {
CURL* curl = curl_easy_init();
if (curl) {
FILE* file = fopen("eibi_schedule.txt", "wb");
if (file) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.eibispace.de/dx/eibi.txt");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_perform(curl);
fclose(file);
}
curl_easy_cleanup(curl);
}
}
char** read_file_into_string(const char* filename, int* num_lines) {
char** lines = NULL;
*num_lines = 0;
size_t line_capacity = 0;
char* buffer = NULL;
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Failed to open file");
return NULL;
}
while (getline(&buffer, &line_capacity, file) != -1) {
buffer[strcspn(buffer, "\n")] = '\0';
lines = realloc(lines, (*num_lines + 1) * sizeof(char*));
if (lines == NULL) {
perror("Failed to reallocate memory");
free(buffer);
fclose(file);
return NULL;
}
lines[*num_lines] = malloc(strlen(buffer) + 1);
strcpy(lines[*num_lines], buffer);
(*num_lines)++;
}
free(buffer);
fclose(file);
return lines;
}
int all_patterns_found(const char* line, const char* patterns[], int num_patterns) {
for (int i = 0; i < num_patterns; i++)
if (strstr(line, patterns[i]) == NULL) return 0;
return 1;
}
char** find_lines_with_patterns(char* lines[], int num_lines, const char* patterns[], int num_patterns, int* num_matches) {
char** matched_lines = NULL;
*num_matches = 0;
for (int i = 0; i < num_lines; i++) {
if (all_patterns_found(lines[i], patterns, num_patterns)) {
matched_lines = realloc(matched_lines, (*num_matches + 1) * sizeof(char*));
if (matched_lines == NULL) {
fprintf(stderr, "Failed to reallocate memory for matched lines\n");
return NULL;
}
matched_lines[*num_matches] = lines[i];
(*num_matches)++;
}
}
return matched_lines;
}
// char** find_pattern_in_string(char* lines[], int num_lines, const char* pattern, int* num_matches) {
// char** matched_lines = NULL;
// *num_matches = 0;
//
// for (int i = 0; i < num_lines; i++) {
// if (strstr(lines[i], pattern) != NULL) {
// matched_lines = realloc(matched_lines, (*num_matches + 1) * sizeof(char*));
// if (matched_lines == NULL) {
// perror("Failed to reallocate memory for matched lines");
// return NULL;
// }
// matched_lines[*num_matches] = lines[i];
// (*num_matches)++;
// }
// }
//
// return matched_lines;
// }
time_t get_expiration_date(const char* line) {
struct tm tm = {0};
const char* month_names[] = {
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};
const char* end_date_str = strstr(line, " - ");
if (end_date_str == NULL) {
fprintf(stderr, "Cannot find the end date in line: %s\n", line);
return (time_t)-1;
}
end_date_str += 3;
char month[20];
int day, year;
if (sscanf(end_date_str, "%s %d, %d", month, &day, &year) != 3) {
fprintf(stderr, "Error parsing date from line: %s\n", end_date_str);
return (time_t)-1;
}
int month_index = -1;
for (int i = 0; i < 12; i++) {
if (strcmp(month, month_names[i]) == 0) {
month_index = i;
break;
}
}
if (month_index == -1) {
fprintf(stderr, "Cannot find month in line: %s\n", month);
return (time_t)-1;
}
tm.tm_year = year - 1900;
tm.tm_mon = month_index;
tm.tm_mday = day;
time_t expiration_time = mktime(&tm);
if (expiration_time == (time_t)-1) {
fprintf(stderr, "Error converting time to time_t\n");
return (time_t)-1;
}
return expiration_time;
}
int file_exists(const char* filename) {
FILE* file = fopen(filename, "r");
if (file) {
fclose(file);
return 1;
}
return 0;
}
char* convert_time_range(const char* time_range) {
char* result = (char*)malloc(50 * sizeof(char));
if (result == NULL) {
perror("Failed to allocate memory");
return NULL;
}
char start_time[5] = {0};
char end_time[5] = {0};
sscanf(time_range, "%4s-%4s", start_time, end_time);
char start_hour[3] = {0};
char start_minute[3] = {0};
sscanf(start_time, "%2s%2s", start_hour, start_minute);
char start_formatted[10];
snprintf(start_formatted, sizeof(start_formatted), "%s:%s UTC", start_hour, start_minute);
char end_hour[3] = {0};
char end_minute[3] = {0};
sscanf(end_time, "%2s%2s", end_hour, end_minute);
char end_formatted[10];
snprintf(end_formatted, sizeof(end_formatted), "%s:%s UTC", end_hour, end_minute);
snprintf(result, 50, "%s to %s", start_formatted, end_formatted);
return result;
}
typedef struct args_t {
char* name;
char* frequency;
char* time;
char* country;
char* language;
char* target_area;
} args_t;
int main(int argc, char* argv[]) {
args_t args = {0};
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--name") == 0) args.name = argv[++i];
else if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--freq") == 0) args.frequency = argv[++i];
else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--time") == 0) args.time = argv[++i];
else if (strcmp(argv[i], "--country") == 0) args.country = argv[++i];
else if (strcmp(argv[i], "-lang") == 0 || strcmp(argv[i], "--language") == 0) args.language = argv[++i];
else if (strcmp(argv[i], "--target") == 0) args.target_area = argv[++i];
}
hash_table_t* ht_lang_codes = hash_table_create();
hash_table_t* ht_country_codes = hash_table_create();
hash_table_t* ht_target_area_codes = hash_table_create();
hash_table_load_from_file(ht_lang_codes, "eibi_language_codes.txt");
hash_table_load_from_file(ht_country_codes, "eibi_country_codes.txt");
hash_table_load_from_file(ht_target_area_codes, "eibi_target_area_codes.txt");
const char* schedule_file = "eibi_schedule.txt";
int num_lines;
char** lines = read_file_into_string(schedule_file, &num_lines);
if (lines == NULL) {
fprintf(stderr, "Failed to read file into string\n");
return 1;
}
int num_matches;
time_t expiration_date = get_expiration_date(find_lines_with_patterns(lines, num_lines, (const char*[]){"Valid"}, 1, &num_matches)[0]);
if (expiration_date == (time_t)-1 || time(NULL) > expiration_date) {
download_eibi_schedule();
lines = read_file_into_string(schedule_file, &num_lines);
}
num_matches = 0;
//////////
const char* patterns[] = {
args.name == NULL ? "" : args.name,
args.frequency == NULL ? "" : args.frequency,
args.time == NULL ? "" : args.time,
args.country == NULL ? "" : args.country,
args.language == NULL ? "" : hash_table_lookup_key(ht_lang_codes, args.language) == NULL ? "" : hash_table_lookup_key(ht_lang_codes, args.language),
args.target_area == NULL ? "" : (hash_table_lookup_key(ht_target_area_codes, args.target_area) == NULL ? "" : hash_table_lookup_key(ht_target_area_codes, args.target_area))
};
int num_patterns = sizeof(patterns) / sizeof(patterns[0]);
char** matched_lines = find_lines_with_patterns(lines + 17, num_lines - 17, patterns, num_patterns, &num_matches);
station_t stations[num_matches];
for (int i = 0; i < num_matches; i++) {
stations[i] = station_create(matched_lines[i]);
}
printf("Number of matches: %d\n\n", num_matches);
for (int i = 0; i < num_matches; i++) {
printf("Frequency: %s kHz\n", stations[i].frequency);
printf("Time: %s\n", convert_time_range(stations[i].time));
printf("Days: %s\n", stations[i].days == NULL ? "N/A" : stations[i].days);
printf("ITU: %s\n", stations[i].itu == NULL ? "N/A" : hash_table_lookup(ht_country_codes, stations[i].itu));
printf("Station: %s\n", stations[i].station);
printf("Language: %s\n", stations[i].language == NULL ? "N/A" : hash_table_lookup(ht_lang_codes, stations[i].language));
printf("Target Area: %s\n", stations[i].target_area == NULL ? "N/A" : hash_table_lookup(ht_target_area_codes, stations[i].target_area));
printf("Remarks: %s\n", stations[i].remarks == NULL ? "None" : stations[i].remarks);
printf("\n");
}
//////////
return 0;
}