-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation.c
99 lines (83 loc) · 2.79 KB
/
station.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "station.h"
char* station_data_trim(const char* str) {
// Skip leading spaces
while (*str == ' ') str++;
// If the string is empty or contains only spaces
if (*str == '\0') return NULL;
// Skip trailing spaces
const char* end = str + strlen(str) - 1;
while (end > str && *end == ' ') end--;
// Calculate length and copy trimmed string
size_t len = end - str + 1;
char* result = (char*)malloc(len + 1);
if (result) {
strncpy(result, str, len);
result[len] = '\0';
}
return result;
}
station_t station_create(const char* data) {
station_t station = {0};
char temp[1024];
char *ptr;
strncpy(temp, data, sizeof(temp));
temp[sizeof(temp) - 1] = '\0';
// Extract frequency (up to 13 characters)
char freq[14] = {0}; // 13 characters + null terminator
strncpy(freq, temp, 13);
station.frequency = station_data_trim(freq);
ptr = temp + 14;
// Extract time (up to 9 characters)
char time[10] = {0}; // 9 characters + null terminator
strncpy(time, ptr, 9);
station.time = station_data_trim(time);
ptr += 10;
// Extract days (up to 5 characters)
char days[6] = {0}; // 5 characters + null terminator
strncpy(days, ptr, 5);
station.days = station_data_trim(days);
ptr += 6;
// Extract ITU (up to 3 characters)
char itu[4] = {0}; // 3 characters + null terminator
strncpy(itu, ptr, 3);
station.itu = station_data_trim(itu);
ptr += 4;
// Extract station (up to 24 characters)
char station_name[25] = {0}; // 24 characters + null terminator
strncpy(station_name, ptr, 24);
station.station = station_data_trim(station_name);
ptr += 25;
// Extract language (up to 3 characters)
char language[4] = {0}; // 3 characters + null terminator
strncpy(language, ptr, 3);
station.language = station_data_trim(language);
ptr += 4;
// Extract target area (up to 11 characters)
char target_area[12] = {0}; // 11 characters + null terminator
strncpy(target_area, ptr, 11);
station.target_area = station_data_trim(target_area);
ptr += 12;
// Extract remarks (remaining part of the string)
station.remarks = station_data_trim(ptr);
// If remarks is just spaces, it should be NULL
if (station.remarks != NULL && strlen(station.remarks) == 0) {
free(station.remarks);
station.remarks = NULL;
}
return station;
}
void station_free(station_t* station) {
if (station != NULL) {
free(station->time);
free(station->days);
free(station->itu);
free(station->station);
free(station->language);
free(station->target_area);
free(station->remarks);
free(station);
}
}