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