comparison src/xs_stil.c @ 657:acaba070cf49

Lots of cosmetic code cleanups; synced the de-gettextification from Audacious-SID, I suppose it makes some sense ...
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 02 Apr 2008 19:46:59 +0300
parents a8ceae9ae8e3
children b0743dc9165d
comparison
equal deleted inserted replaced
656:e9257f006f41 657:acaba070cf49
28 #include <stdarg.h> 28 #include <stdarg.h>
29 29
30 30
31 /* Database handling functions 31 /* Database handling functions
32 */ 32 */
33 static gboolean xs_stildb_node_realloc(t_xs_stil_node *pNode, gint nsubTunes) 33 static gboolean xs_stildb_node_realloc(stil_node_t *node, gint nsubTunes)
34 { 34 {
35 if (!pNode) return FALSE; 35 if (!node) return FALSE;
36 36
37 /* Re-allocate subTune structure if needed */ 37 /* Re-allocate subTune structure if needed */
38 if (nsubTunes > pNode->nsubTunes) { 38 if (nsubTunes > node->nsubTunes) {
39 gint clearIndex, clearLength; 39 gint clearIndex, clearLength;
40 40
41 pNode->subTunes = 41 node->subTunes =
42 (t_xs_stil_subnode **) g_realloc(pNode->subTunes, 42 (stil_subnode_t **) g_realloc(node->subTunes,
43 (nsubTunes + 1) * sizeof(t_xs_stil_subnode **)); 43 (nsubTunes + 1) * sizeof(stil_subnode_t **));
44 44
45 if (!pNode->subTunes) { 45 if (!node->subTunes) {
46 xs_error(_("SubTune pointer structure realloc failed.\n")); 46 xs_error("SubTune pointer structure realloc failed.\n");
47 return FALSE; 47 return FALSE;
48 } 48 }
49 49
50 /* Clear the newly allocated memory */ 50 /* Clear the newly allocated memory */
51 if (pNode->nsubTunes == 0) { 51 if (node->nsubTunes == 0) {
52 clearIndex = 0; 52 clearIndex = 0;
53 clearLength = nsubTunes + 1; 53 clearLength = nsubTunes + 1;
54 } else { 54 } else {
55 clearIndex = pNode->nsubTunes + 1; 55 clearIndex = node->nsubTunes + 1;
56 clearLength = (nsubTunes - clearIndex + 1); 56 clearLength = (nsubTunes - clearIndex + 1);
57 } 57 }
58 xs_memset(&(pNode->subTunes[clearIndex]), 0, clearLength * sizeof(t_xs_stil_subnode **)); 58 memset(&(node->subTunes[clearIndex]), 0, clearLength * sizeof(stil_subnode_t **));
59 59
60 pNode->nsubTunes = nsubTunes; 60 node->nsubTunes = nsubTunes;
61 } 61 }
62 62
63 /* Allocate memory for subTune */ 63 /* Allocate memory for subTune */
64 if (!pNode->subTunes[nsubTunes]) { 64 if (!node->subTunes[nsubTunes]) {
65 pNode->subTunes[nsubTunes] = (t_xs_stil_subnode *) 65 node->subTunes[nsubTunes] = (stil_subnode_t *)
66 g_malloc0(sizeof(t_xs_stil_subnode)); 66 g_malloc0(sizeof(stil_subnode_t));
67 67
68 if (!pNode->subTunes[nsubTunes]) { 68 if (!node->subTunes[nsubTunes]) {
69 xs_error(_("SubTune structure malloc failed!\n")); 69 xs_error("SubTune structure malloc failed!\n");
70 return FALSE; 70 return FALSE;
71 } 71 }
72 } 72 }
73 73
74 return TRUE; 74 return TRUE;
75 } 75 }
76 76
77 77
78 static void xs_stildb_node_free(t_xs_stil_node *pNode) 78 static void xs_stildb_node_free(stil_node_t *node)
79 { 79 {
80 gint i; 80 gint i;
81 t_xs_stil_subnode *pSub; 81 stil_subnode_t *subnode;
82 82
83 if (pNode) { 83 if (!node) return;
84 /* Free subtune information */ 84
85 for (i = 0; i <= pNode->nsubTunes; i++) { 85 /* Free subtune information */
86 pSub = pNode->subTunes[i]; 86 for (i = 0; i <= node->nsubTunes; i++) {
87 if (pSub) { 87 subnode = node->subTunes[i];
88 g_free(pSub->pName); 88 if (subnode) {
89 g_free(pSub->pAuthor); 89 g_free(subnode->name);
90 g_free(pSub->pInfo); 90 g_free(subnode->author);
91 g_free(pSub->pTitle); 91 g_free(subnode->info);
92 92 g_free(subnode->title);
93 g_free(pSub); 93 g_free(subnode);
94 } 94 }
95 } 95 }
96 96 g_free(node->subTunes);
97 g_free(pNode->subTunes); 97 g_free(node->filename);
98 g_free(pNode->pcFilename); 98 g_free(node);
99 g_free(pNode); 99 }
100 } 100
101 } 101
102 102 static stil_node_t *xs_stildb_node_new(gchar *filename)
103 103 {
104 static t_xs_stil_node *xs_stildb_node_new(gchar *pcFilename) 104 stil_node_t *result;
105 {
106 t_xs_stil_node *pResult;
107 105
108 /* Allocate memory for new node */ 106 /* Allocate memory for new node */
109 pResult = (t_xs_stil_node *) g_malloc0(sizeof(t_xs_stil_node)); 107 result = (stil_node_t *) g_malloc0(sizeof(stil_node_t));
110 if (!pResult) 108 if (!result)
111 return NULL; 109 return NULL;
112 110
113 /* Allocate filename and initial space for one subtune */ 111 /* Allocate filename and initial space for one subtune */
114 pResult->pcFilename = g_strdup(pcFilename); 112 result->filename = g_strdup(filename);
115 if (!pResult->pcFilename || !xs_stildb_node_realloc(pResult, 1)) { 113 if (!result->filename || !xs_stildb_node_realloc(result, 1)) {
116 xs_stildb_node_free(pResult); 114 xs_stildb_node_free(result);
117 return NULL; 115 return NULL;
118 } 116 }
119 117
120 return pResult; 118 return result;
121 } 119 }
122 120
123 121
124 /* Insert given node to db linked list 122 /* Insert given node to db linked list
125 */ 123 */
126 static void xs_stildb_node_insert(t_xs_stildb *db, t_xs_stil_node *pNode) 124 static void xs_stildb_node_insert(xs_stildb_t *db, stil_node_t *node)
127 { 125 {
128 assert(db); 126 assert(db != NULL);
129 127
130 if (db->pNodes) { 128 if (db->nodes) {
131 /* The first node's pPrev points to last node */ 129 /* The first node's pPrev points to last node */
132 LPREV = db->pNodes->pPrev; /* New node's prev = Previous last node */ 130 LPREV = db->nodes->prev; /* New node's prev = Previous last node */
133 db->pNodes->pPrev->pNext = pNode; /* Previous last node's next = New node */ 131 db->nodes->prev->next = node; /* Previous last node's next = New node */
134 db->pNodes->pPrev = pNode; /* New last node = New node */ 132 db->nodes->prev = node; /* New last node = New node */
135 LNEXT = NULL; /* But next is NULL! */ 133 LNEXT = NULL; /* But next is NULL! */
136 } else { 134 } else {
137 db->pNodes = pNode; /* First node ... */ 135 db->nodes = node; /* First node ... */
138 LPREV = pNode; /* ... it's also last */ 136 LPREV = node; /* ... it's also last */
139 LNEXT = NULL; /* But next is NULL! */ 137 LNEXT = NULL; /* But next is NULL! */
140 } 138 }
141 } 139 }
142 140
143 141
144 /* Read database (additively) to given db-structure 142 /* Read database (additively) to given db-structure
145 */ 143 */
146 #define XS_STILDB_MULTI \ 144 #define XS_STILDB_MULTI \
147 if (isMulti) { \ 145 if (isMulti) { \
148 isMulti = FALSE; \ 146 isMulti = FALSE; \
149 xs_pstrcat(&(tmpNode->subTunes[subEntry]->pInfo), "\n");\ 147 xs_pstrcat(&(tmnode->subTunes[subEntry]->info), "\n");\
150 } 148 }
151 149
152 static void XS_STILDB_ERR(gint lineNum, gchar *inLine, const char *fmt, ...) 150 static void XS_STILDB_ERR(gint lineNum, gchar *inLine, const char *fmt, ...)
153 { 151 {
154 va_list ap; 152 va_list ap;
158 va_end(ap); 156 va_end(ap);
159 157
160 fprintf(stderr, "#%d: '%s'\n", lineNum, inLine); 158 fprintf(stderr, "#%d: '%s'\n", lineNum, inLine);
161 } 159 }
162 160
163 gint xs_stildb_read(t_xs_stildb *db, gchar *dbFilename) 161 gint xs_stildb_read(xs_stildb_t *db, gchar *dbFilename)
164 { 162 {
165 FILE *inFile; 163 FILE *inFile;
166 gchar inLine[XS_BUF_SIZE + 16]; /* Since we add some chars here and there */ 164 gchar inLine[XS_BUF_SIZE + 16]; /* Since we add some chars here and there */
167 size_t lineNum; 165 size_t lineNum;
168 t_xs_stil_node *tmpNode; 166 stil_node_t *tmnode;
169 gboolean isError, isMulti; 167 gboolean isError, isMulti;
170 gint subEntry; 168 gint subEntry;
171 gchar *tmpLine = inLine; 169 gchar *tmpLine = inLine;
172 assert(db); 170 assert(db != NULL);
173 171
174 /* Try to open the file */ 172 /* Try to open the file */
175 if ((inFile = fopen(dbFilename, "ra")) == NULL) { 173 if ((inFile = fopen(dbFilename, "ra")) == NULL) {
176 xs_error(_("Could not open STILDB '%s'\n"), 174 xs_error("Could not open STILDB '%s'\n", dbFilename);
177 dbFilename);
178 return -1; 175 return -1;
179 } 176 }
180 177
181 /* Read and parse the data */ 178 /* Read and parse the data */
182 lineNum = 0; 179 lineNum = 0;
183 isError = FALSE; 180 isError = FALSE;
184 isMulti = FALSE; 181 isMulti = FALSE;
185 tmpNode = NULL; 182 tmnode = NULL;
186 subEntry = 0; 183 subEntry = 0;
187 184
188 while (!isError && fgets(inLine, XS_BUF_SIZE, inFile) != NULL) { 185 while (!isError && fgets(inLine, XS_BUF_SIZE, inFile) != NULL) {
189 size_t linePos = 0, eolPos = 0; 186 size_t linePos = 0, eolPos = 0;
190 xs_findeol(inLine, &eolPos); 187 xs_findeol(inLine, &eolPos);
195 192
196 switch (tmpLine[0]) { 193 switch (tmpLine[0]) {
197 case '/': 194 case '/':
198 /* Check if we are already parsing entry */ 195 /* Check if we are already parsing entry */
199 isMulti = FALSE; 196 isMulti = FALSE;
200 if (tmpNode) { 197 if (tmnode) {
201 XS_STILDB_ERR(lineNum, tmpLine, 198 XS_STILDB_ERR(lineNum, tmpLine,
202 "New entry found before end of current ('%s')!\n", 199 "New entry found before end of current ('%s')!\n",
203 tmpNode->pcFilename); 200 tmnode->filename);
204 xs_stildb_node_free(tmpNode); 201 xs_stildb_node_free(tmnode);
205 } 202 }
206 203
207 /* A new node */ 204 /* A new node */
208 subEntry = 0; 205 subEntry = 0;
209 tmpNode = xs_stildb_node_new(tmpLine); 206 tmnode = xs_stildb_node_new(tmpLine);
210 if (!tmpNode) { 207 if (!tmnode) {
211 /* Allocation failed */ 208 /* Allocation failed */
212 XS_STILDB_ERR(lineNum, tmpLine, 209 XS_STILDB_ERR(lineNum, tmpLine,
213 "Could not allocate new STILdb-node!\n"); 210 "Could not allocate new STILdb-node!\n");
214 isError = TRUE; 211 isError = TRUE;
215 } 212 }
229 226
230 /* Sanity check */ 227 /* Sanity check */
231 if (subEntry < 1) { 228 if (subEntry < 1) {
232 XS_STILDB_ERR(lineNum, tmpLine, 229 XS_STILDB_ERR(lineNum, tmpLine,
233 "Number of subEntry (%i) for '%s' is invalid\n", 230 "Number of subEntry (%i) for '%s' is invalid\n",
234 subEntry, tmpNode->pcFilename); 231 subEntry, tmnode->filename);
235 subEntry = 0; 232 subEntry = 0;
236 } 233 }
237 } else { 234 } else {
238 XS_STILDB_ERR(lineNum, tmpLine, 235 XS_STILDB_ERR(lineNum, tmpLine,
239 "Syntax error, expected subEntry number.\n"); 236 "Syntax error, expected subEntry number.\n");
251 case '#': 248 case '#':
252 case '\n': 249 case '\n':
253 case '\r': 250 case '\r':
254 /* End of entry/field */ 251 /* End of entry/field */
255 isMulti = FALSE; 252 isMulti = FALSE;
256 if (tmpNode) { 253 if (tmnode) {
257 /* Insert to database */ 254 /* Insert to database */
258 xs_stildb_node_insert(db, tmpNode); 255 xs_stildb_node_insert(db, tmnode);
259 tmpNode = NULL; 256 tmnode = NULL;
260 } 257 }
261 break; 258 break;
262 259
263 default: 260 default:
264 /* Check if we are parsing an entry */ 261 /* Check if we are parsing an entry */
265 xs_findnext(tmpLine, &linePos); 262 xs_findnext(tmpLine, &linePos);
266 263
267 if (!tmpNode) { 264 if (!tmnode) {
268 XS_STILDB_ERR(lineNum, tmpLine, 265 XS_STILDB_ERR(lineNum, tmpLine,
269 "Entry data encountered outside of entry or syntax error!\n"); 266 "Entry data encountered outside of entry or syntax error!\n");
270 break; 267 break;
271 } 268 }
272 269
273 if (!xs_stildb_node_realloc(tmpNode, subEntry)) { 270 if (!xs_stildb_node_realloc(tmnode, subEntry)) {
274 XS_STILDB_ERR(lineNum, tmpLine, 271 XS_STILDB_ERR(lineNum, tmpLine,
275 "Could not (re)allocate memory for subEntries!\n"); 272 "Could not (re)allocate memory for subEntries!\n");
276 isError = TRUE; 273 isError = TRUE;
277 break; 274 break;
278 } 275 }
279 276
280 /* Some other type */ 277 /* Some other type */
281 if (strncmp(tmpLine, " NAME:", 8) == 0) { 278 if (strncmp(tmpLine, " NAME:", 8) == 0) {
282 XS_STILDB_MULTI; 279 XS_STILDB_MULTI;
283 g_free(tmpNode->subTunes[subEntry]->pName); 280 g_free(tmnode->subTunes[subEntry]->name);
284 tmpNode->subTunes[subEntry]->pName = g_strdup(&tmpLine[9]); 281 tmnode->subTunes[subEntry]->name = g_strdup(&tmpLine[9]);
285 } else if (strncmp(tmpLine, " TITLE:", 8) == 0) { 282 } else if (strncmp(tmpLine, " TITLE:", 8) == 0) {
286 XS_STILDB_MULTI; 283 XS_STILDB_MULTI;
287 isMulti = TRUE; 284 isMulti = TRUE;
288 if (!tmpNode->subTunes[subEntry]->pTitle) 285 if (!tmnode->subTunes[subEntry]->title)
289 tmpNode->subTunes[subEntry]->pTitle = g_strdup(&tmpLine[9]); 286 tmnode->subTunes[subEntry]->title = g_strdup(&tmpLine[9]);
290 xs_pstrcat(&(tmpNode->subTunes[subEntry]->pInfo), &tmpLine[2]); 287 xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[2]);
291 } else if (strncmp(tmpLine, " AUTHOR:", 8) == 0) { 288 } else if (strncmp(tmpLine, " AUTHOR:", 8) == 0) {
292 XS_STILDB_MULTI; 289 XS_STILDB_MULTI;
293 g_free(tmpNode->subTunes[subEntry]->pAuthor); 290 g_free(tmnode->subTunes[subEntry]->author);
294 tmpNode->subTunes[subEntry]->pAuthor = g_strdup(&tmpLine[9]); 291 tmnode->subTunes[subEntry]->author = g_strdup(&tmpLine[9]);
295 } else if (strncmp(tmpLine, " ARTIST:", 8) == 0) { 292 } else if (strncmp(tmpLine, " ARTIST:", 8) == 0) {
296 XS_STILDB_MULTI; 293 XS_STILDB_MULTI;
297 isMulti = TRUE; 294 isMulti = TRUE;
298 xs_pstrcat(&(tmpNode->subTunes[subEntry]->pInfo), &tmpLine[1]); 295 xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[1]);
299 } else if (strncmp(tmpLine, "COMMENT:", 8) == 0) { 296 } else if (strncmp(tmpLine, "COMMENT:", 8) == 0) {
300 XS_STILDB_MULTI; 297 XS_STILDB_MULTI;
301 isMulti = TRUE; 298 isMulti = TRUE;
302 xs_pstrcat(&(tmpNode->subTunes[subEntry]->pInfo), tmpLine); 299 xs_pstrcat(&(tmnode->subTunes[subEntry]->info), tmpLine);
303 } else { 300 } else {
304 if (isMulti) { 301 if (isMulti) {
305 xs_pstrcat(&(tmpNode->subTunes[subEntry]->pInfo), " "); 302 xs_pstrcat(&(tmnode->subTunes[subEntry]->info), " ");
306 xs_pstrcat(&(tmpNode->subTunes[subEntry]->pInfo), &tmpLine[linePos]); 303 xs_pstrcat(&(tmnode->subTunes[subEntry]->info), &tmpLine[linePos]);
307 } else { 304 } else {
308 XS_STILDB_ERR(lineNum, tmpLine, 305 XS_STILDB_ERR(lineNum, tmpLine,
309 "Entry continuation found when isMulti == FALSE.\n"); 306 "Entry continuation found when isMulti == FALSE.\n");
310 } 307 }
311 } 308 }
315 XS_CS_FREE(tmpLine); 312 XS_CS_FREE(tmpLine);
316 313
317 } /* while */ 314 } /* while */
318 315
319 /* Check if there is one remaining node */ 316 /* Check if there is one remaining node */
320 if (tmpNode) 317 if (tmnode)
321 xs_stildb_node_insert(db, tmpNode); 318 xs_stildb_node_insert(db, tmnode);
322 319
323 /* Close the file */ 320 /* Close the file */
324 fclose(inFile); 321 fclose(inFile);
325 322
326 return 0; 323 return 0;
327 } 324 }
328 325
329 326
330 /* Compare two nodes 327 /* Compare two nodes
331 */ 328 */
332 static gint xs_stildb_cmp(const void *pNode1, const void *pNode2) 329 static gint xs_stildb_cmp(const void *node1, const void *node2)
333 { 330 {
334 /* We assume here that we never ever get NULL-pointers or similar */ 331 /* We assume here that we never ever get NULL-pointers or similar */
335 return strcmp( 332 return strcmp(
336 (*(t_xs_stil_node **) pNode1)->pcFilename, 333 (*(stil_node_t **) node1)->filename,
337 (*(t_xs_stil_node **) pNode2)->pcFilename); 334 (*(stil_node_t **) node2)->filename);
338 } 335 }
339 336
340 337
341 /* (Re)create index 338 /* (Re)create index
342 */ 339 */
343 gint xs_stildb_index(t_xs_stildb *db) 340 gint xs_stildb_index(xs_stildb_t *db)
344 { 341 {
345 t_xs_stil_node *pCurr; 342 stil_node_t *curr;
346 size_t i; 343 size_t i;
347 344
348 /* Free old index */ 345 /* Free old index */
349 if (db->ppIndex) { 346 if (db->pindex) {
350 g_free(db->ppIndex); 347 g_free(db->pindex);
351 db->ppIndex = NULL; 348 db->pindex = NULL;
352 } 349 }
353 350
354 /* Get size of db */ 351 /* Get size of db */
355 pCurr = db->pNodes; 352 curr = db->nodes;
356 db->n = 0; 353 db->n = 0;
357 while (pCurr) { 354 while (curr) {
358 db->n++; 355 db->n++;
359 pCurr = pCurr->pNext; 356 curr = curr->next;
360 } 357 }
361 358
362 /* Check number of nodes */ 359 /* Check number of nodes */
363 if (db->n > 0) { 360 if (db->n > 0) {
364 /* Allocate memory for index-table */ 361 /* Allocate memory for index-table */
365 db->ppIndex = (t_xs_stil_node **) g_malloc(sizeof(t_xs_stil_node *) * db->n); 362 db->pindex = (stil_node_t **) g_malloc(sizeof(stil_node_t *) * db->n);
366 if (!db->ppIndex) 363 if (!db->pindex)
367 return -1; 364 return -1;
368 365
369 /* Get node-pointers to table */ 366 /* Get node-pointers to table */
370 i = 0; 367 i = 0;
371 pCurr = db->pNodes; 368 curr = db->nodes;
372 while (pCurr && (i < db->n)) { 369 while (curr && (i < db->n)) {
373 db->ppIndex[i++] = pCurr; 370 db->pindex[i++] = curr;
374 pCurr = pCurr->pNext; 371 curr = curr->next;
375 } 372 }
376 373
377 /* Sort the indexes */ 374 /* Sort the indexes */
378 qsort(db->ppIndex, db->n, sizeof(t_xs_stil_node *), xs_stildb_cmp); 375 qsort(db->pindex, db->n, sizeof(stil_node_t *), xs_stildb_cmp);
379 } 376 }
380 377
381 return 0; 378 return 0;
382 } 379 }
383 380
384 381
385 /* Free a given STIL database 382 /* Free a given STIL database
386 */ 383 */
387 void xs_stildb_free(t_xs_stildb *db) 384 void xs_stildb_free(xs_stildb_t *db)
388 { 385 {
389 t_xs_stil_node *pCurr, *pNext; 386 stil_node_t *curr, *next;
390 387
391 if (!db) 388 if (!db)
392 return; 389 return;
393 390
394 /* Free the memory allocated for nodes */ 391 /* Free the memory allocated for nodes */
395 pCurr = db->pNodes; 392 curr = db->nodes;
396 while (pCurr) { 393 while (curr) {
397 pNext = pCurr->pNext; 394 next = curr->next;
398 xs_stildb_node_free(pCurr); 395 xs_stildb_node_free(curr);
399 pCurr = pNext; 396 curr = next;
400 } 397 }
401 398
402 db->pNodes = NULL; 399 db->nodes = NULL;
403 400
404 /* Free memory allocated for index */ 401 /* Free memory allocated for index */
405 if (db->ppIndex) { 402 if (db->pindex) {
406 g_free(db->ppIndex); 403 g_free(db->pindex);
407 db->ppIndex = NULL; 404 db->pindex = NULL;
408 } 405 }
409 406
410 /* Free structure */ 407 /* Free structure */
411 db->n = 0; 408 db->n = 0;
412 g_free(db); 409 g_free(db);
413 } 410 }
414 411
415 412
416 /* Get STIL information node from database 413 /* Get STIL information node from database
417 */ 414 */
418 t_xs_stil_node *xs_stildb_get_node(t_xs_stildb *db, gchar *pcFilename) 415 stil_node_t *xs_stildb_get_node(xs_stildb_t *db, gchar *filename)
419 { 416 {
420 t_xs_stil_node keyItem, *key, **item; 417 stil_node_t keyItem, *key, **item;
421 418
422 /* Check the database pointers */ 419 /* Check the database pointers */
423 if (!db || !db->pNodes || !db->ppIndex) 420 if (!db || !db->nodes || !db->pindex)
424 return NULL; 421 return NULL;
425 422
426 /* Look-up index using binary search */ 423 /* Look-up index using binary search */
427 keyItem.pcFilename = pcFilename; 424 keyItem.filename = filename;
428 key = &keyItem; 425 key = &keyItem;
429 item = bsearch(&key, db->ppIndex, db->n, sizeof(t_xs_stil_node *), xs_stildb_cmp); 426 item = bsearch(&key, db->pindex, db->n, sizeof(stil_node_t *), xs_stildb_cmp);
430 if (item) 427 if (item)
431 return *item; 428 return *item;
432 else 429 else
433 return NULL; 430 return NULL;
434 } 431 }