-
Notifications
You must be signed in to change notification settings - Fork 2
/
favlist.c
155 lines (137 loc) · 3.33 KB
/
favlist.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define member_size(type, member) sizeof(((type *)0)->member)
struct strings_list {
unsigned short size;
struct item *items;
unsigned short end;
};
struct item {
unsigned char zero;
unsigned char flag1;
unsigned char space;
unsigned char six_bytes[6];
unsigned char flag2;
unsigned char title_len;
unsigned char eighty;
char title[99];
char url[250];
unsigned char zeros[5];
struct item *next_item;
};
#define STRUCT_SIZE_NO_PTR 366 /* because of struct alignment next_item pointer adds not 8, but 10*/
int add_to_list(struct strings_list *list, const char *title, const char *url) {
struct item *item;
struct item *prv_item;
if (title == NULL || url == NULL) {
printf("Title or URL are null\n");
return -2;
}
if ((strlen(title)+2) > member_size(struct item, title)) {
printf("Title length is too long (%d)\n", (int)strlen(title));
return -2;
}
if (strlen(url) > member_size(struct item, url)) {
printf("URL length is too long (%d)\n", (int)strlen(url));
return -2;
}
if (list->size == 0) {
list->items = (struct item *)calloc(1,sizeof(struct item));
if (list->items == NULL) {
printf("Error allocating memory\n");
return -2;
}
item = list->items;
} else {
item = list->items->next_item;
prv_item = list->items;
while (item != NULL) {
prv_item = item;
item = item->next_item;
}
item = (struct item *)calloc(1,sizeof(struct item));
if (item == NULL) {
printf("Error allocating memory\n");
return -2;
}
prv_item->next_item = item;
}
list->size++;
item->space = 0x20;
item->title_len = strlen(title)+2;
item->eighty = 0x80;
strcpy(item->title,title);
strcpy(item->url,url);
item->next_item = NULL;
return 1;
}
const char* getfield(char* line, int num) {
const char* tok;
for (tok = strtok(line, ","); tok && *tok; tok = strtok(NULL, ",\n")) {
if (!--num)
return tok;
}
return NULL;
}
int save_to_file(struct strings_list *list, char *filename) {
FILE *f;
unsigned short i;
struct item *item;
f = fopen(filename, "wb");
if (f != NULL) {
fwrite(&(list->size),sizeof(list->size), 1, f);
for( i = 0; i < list->size; i++) {
if (i == 0) {
item = list->items;
} else {
item = item->next_item;
}
if (item == NULL) {
printf("End of list at %d, but size is %d. Stop the loop.\n",i,list->size);
break;
}
fwrite(item, STRUCT_SIZE_NO_PTR, 1, f);
}
fwrite(&(list->end),sizeof(list->end), 1, f);
fclose(f);
return 0;
} else {
printf("Failed to open file '%s' for write\n",filename);
return -1;
}
}
int main() {
struct strings_list *radio_list=calloc(1,sizeof(struct strings_list));
char *import = "playlist.csv";
char *export = "myradio.cfg";
int res;
char line[1024];
char *tmp, *tmp1;
FILE* input = fopen(import, "r");
if (input != NULL) {
while (fgets(line, 1024, input)) {
tmp = strdup(line);
tmp1 = strdup(line);
if (add_to_list(radio_list, getfield(tmp, 1), getfield(tmp1, 2)) != 1) {
printf("Failed to create binary list\n");
free(tmp);
free(tmp1);
fclose(input);
return 1;
}
free(tmp);
free(tmp1);
}
fclose(input);
} else {
printf("Failed to open file '%s' for read\n",import);
return 1;
}
res = save_to_file(radio_list,export);
if (res < 0) {
return 1;
}
printf("Successfully wrote '%s' binary playlist\n",export);
return 0;
}