-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
199 lines (195 loc) · 4.83 KB
/
util.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
#include "util.h"
struct Track *track_add (struct Track **entries, const char *uri, int idx, int st)
{
if (!uri)
return NULL;
// Allocate memory for new node;
struct Track *list = *entries;
struct Track *link = (struct Track*) malloc (sizeof (struct Track));
if (!link)
{
//NPrintf ("!fm_job_add malloc failed for %d>%s\n", dir, fn);
return NULL;
}
//
//Nprintf("adding %s\n", uri);
link->uri = strdup (uri);
link->index = idx;
link->streamType = st;
//
link->prev = NULL;
link->next = NULL;
// If head is empty, create new list
if (list == NULL)
{
*entries = link;
}
else
{
struct Track *current = list;
while (current->next && strcasecmp (current->uri, uri) < 0)
current = current->next;
//
if (strcasecmp (current->uri, uri) < 0)
{
//Nprintf ("adding after %s\n", current->uri);
struct Track *next = current->next;
if (next)
next->prev = link;
current->next = link;
link->prev = current;
link->next = next;
}
else
{
//Nprintf ("adding before %s\n", current->uri);
struct Track *prev = current->prev;
if (prev)
prev->next = link;
else
//new head
*entries = link;
current->prev = link;
link->prev = prev;
link->next = current;
}
}
return link;
}
struct Subs *subs_get (struct Subs *head, long cts)
{
struct Subs *node;
if (!head)
return NULL;
for (node = head; node && cts > node->smsts; node = node->next)
;
if (node)
return node->prev;
return NULL;
}
struct Subs *subs_clear (struct Subs *subs)
{
struct Subs *ptr;
if (!subs)
return CELL_OK;
for (ptr = subs; ptr != NULL; ptr = subs)
{
subs = ptr->next;
if (ptr->text)
free (ptr->text);
free (ptr);
}
return NULL;
}
/* SRT sub format:
1097\n
01:20:45,138 --> 01:20:48,164\n
You'd say anything now\n
to get what you want.\n
\n
-- elements:
1 sub index
2 time index: start --> end
3 text
3 text
4 <empty line>
*/
struct Subs *subs_load (char *path)
{
FILE *srtf;
struct Subs *head = NULL;
struct Subs *node = NULL;
if (NULL == (srtf = fopen (path, "r")))
{
Nprintf ("subs_load can't open SRT '%s'\n", path);
return NULL;
}
char *tmbuff = (char *)malloc (1024);
if (NULL == tmbuff)
{
Nprintf ("subs_load can't malloc 1024b\n");
return NULL;
}
/* load subtitles one by one */
int elemidx = 1; //element index: 1, 2, 3, 4
int subidx;
int subcnt = 0;
long sts, ets;
char *subtext = tmbuff + 512;
int stidx = 0;
int te[8];
//Nprintf ("subs_load processing SRT '%s'\n", path);
while (fgets (tmbuff, 511, srtf) != NULL)
{
char *ptp = tmbuff;
//if (subcnt > 20)
// break;
//Nprintf ("subs_load process line: '%s'\n", ptp);
switch (elemidx)
{
case 1:
//set sub index
subidx = atoi (ptp);
//Nprintf ("index: %d\n", subidx);
elemidx++;
break;
case 2:
//reformat time
if (sscanf (ptp, "%d:%d:%d,%d --> %d:%d:%d,%d", &te[0], &te[1], &te[2], &te[3], &te[4], &te[5], &te[6], &te[7]) == 8)
{
//Nprintf ("times: %d:%d:%d.%d > %d:%d:%d.%d\n", te[0], te[1], te[2], te[3], te[4], te[5], te[6], te[7]);
sts = (te[0] * 3600 + te[1] * 60 + te[2]) * 1000 + te[3];
ets = (te[4] * 3600 + te[5] * 60 + te[6]) * 1000 + te[7];
}
else
{
//this might never show
sts = 0; ets = 0;
}
elemidx++;
break;
default:
//add text from index 512
if (*ptp == 0x0D || *ptp == 0x0A)
{
elemidx = 1;
//add subtitle element to list
struct Subs *subs = (struct Subs *)malloc (sizeof (struct Subs));
if (subs)
{
subs->idx = subidx;
subs->smsts = sts;
subs->emsts = ets;
subs->text = strdup (subtext);
subs->prev = NULL;
subs->next = NULL;
//
if (!head)
head = subs;
else
{
subs->prev = node;
node->next = subs;
}
node = subs;
//
//Nprintf ("add sub: >%s<\n", subs->text);
++subcnt;
}
//Nprintf ("next sub number %d\n", subcnt);
stidx = 0;
}
else
{
stidx += snprintf (subtext + stidx, 510 - stidx, "%s\r\n", ptp);
//Nprintf ("added %dB to %s\n", stidx, subtext);
}
}
} /* while res game */
Nprintf ("subs_load END processing %dsubs SRT '%s'\n", subcnt, path);
//
free (tmbuff);
fclose (srtf);
//
return head;
}