comparison src/xs_length.c @ 920:560cdf7f1fec

Fix a silly bug in the SLDB parser caused by recent changes. Cleaned up the code a bit as well.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 10 Nov 2012 19:06:55 +0200
parents 84394ee26545
children 86f914bb48a7
comparison
equal deleted inserted replaced
919:8ca7f33635fc 920:560cdf7f1fec
103 103
104 /* Parse one SLDB definition line, return SLDB node 104 /* Parse one SLDB definition line, return SLDB node
105 */ 105 */
106 XSSLDBNode * xs_sldb_read_entry(gchar *inLine) 106 XSSLDBNode * xs_sldb_read_entry(gchar *inLine)
107 { 107 {
108 size_t linePos; 108 XSSLDBNode *tmnode = NULL;
109 size_t linePos, tmpLen, savePos;
110 gboolean isOK;
109 gint i; 111 gint i;
110 gboolean isOK;
111 XSSLDBNode *tmnode;
112 112
113 /* Allocate new node */ 113 /* Allocate new node */
114 tmnode = (XSSLDBNode *) g_malloc0(sizeof(XSSLDBNode)); 114 tmnode = (XSSLDBNode *) g_malloc0(sizeof(XSSLDBNode));
115 if (!tmnode) 115 if (tmnode == NULL)
116 { 116 {
117 xs_error("Error allocating new node. Fatal error.\n"); 117 xs_error("Error allocating new node. Fatal error.\n");
118 return NULL; 118 return NULL;
119 } 119 }
120 120
126 sscanf(&inLine[linePos], "%2x", &tmpu); 126 sscanf(&inLine[linePos], "%2x", &tmpu);
127 tmnode->md5Hash[i] = tmpu; 127 tmnode->md5Hash[i] = tmpu;
128 } 128 }
129 129
130 /* Get playtimes */ 130 /* Get playtimes */
131 if (inLine[linePos] != 0) 131 xs_findnext(inLine, &linePos);
132 { 132 if (inLine[linePos] != '=')
133 if (inLine[linePos] != '=') 133 {
134 { 134 xs_error("'=' expected on column #%d.\n", linePos);
135 xs_error("'=' expected on column #%d.\n", linePos); 135 goto error;
136 xs_sldb_node_free(tmnode); 136 }
137 return NULL; 137
138 } 138 /* First playtime is after '=' */
139 savePos = ++linePos;
140 tmpLen = strlen(inLine);
141
142 /* Get number of sub-tune lengths */
143 isOK = TRUE;
144 while (linePos < tmpLen && isOK)
145 {
146 xs_findnext(inLine, &linePos);
147
148 if (xs_sldb_gettime(inLine, &linePos) >= 0)
149 tmnode->nlengths++;
139 else 150 else
140 { 151 isOK = FALSE;
141 size_t tmpLen, savePos; 152 }
142 153
143 /* First playtime is after '=' */ 154 /* Allocate memory for lengths */
144 savePos = ++linePos; 155 if (tmnode->nlengths == 0)
145 tmpLen = strlen(inLine); 156 goto error;
146 157
147 /* Get number of sub-tune lengths */ 158 tmnode->lengths = (gint *) g_malloc0(tmnode->nlengths * sizeof(gint));
148 isOK = TRUE; 159 if (tmnode->lengths == NULL)
149 while (linePos < tmpLen && isOK) 160 {
150 { 161 xs_error("Could not allocate memory for node.\n");
151 xs_findnext(inLine, &linePos); 162 goto error;
152 163 }
153 if (xs_sldb_gettime(inLine, &linePos) >= 0) 164
154 tmnode->nlengths++; 165 /* Read lengths in */
155 else 166 for (i = 0, linePos = savePos, isOK = TRUE;
156 isOK = FALSE; 167 linePos < tmpLen && i < tmnode->nlengths && isOK; i++)
157 } 168 {
158 169 gint l;
159 /* Allocate memory for lengths */ 170 xs_findnext(inLine, &linePos);
160 if (tmnode->nlengths > 0) 171
161 { 172 l = xs_sldb_gettime(inLine, &linePos);
162 tmnode->lengths = (gint *) g_malloc0(tmnode->nlengths * sizeof(gint)); 173 if (l >= 0)
163 if (!tmnode->lengths) 174 tmnode->lengths[i] = l;
164 { 175 else
165 xs_error("Could not allocate memory for node.\n"); 176 isOK = FALSE;
166 xs_sldb_node_free(tmnode); 177 }
167 return NULL; 178
168 } 179 return tmnode;
169 } 180
170 else 181 error:
171 { 182 xs_sldb_node_free(tmnode);
172 xs_sldb_node_free(tmnode);
173 return NULL;
174 }
175
176 /* Read lengths in */
177 for (i = 0, linePos = savePos, isOK = TRUE;
178 linePos < tmpLen && i < tmnode->nlengths && isOK; i++)
179 {
180 gint l;
181
182 xs_findnext(inLine, &linePos);
183
184 l = xs_sldb_gettime(inLine, &linePos);
185 if (l >= 0)
186 tmnode->lengths[i] = l;
187 else
188 isOK = FALSE;
189
190 i++;
191 }
192
193 if (!isOK)
194 {
195 xs_sldb_node_free(tmnode);
196 return NULL;
197 }
198 else
199 return tmnode;
200 }
201 }
202
203 return NULL; 183 return NULL;
204 } 184 }
205 185
206 186
207 /* Read database to memory 187 /* Read database to memory
261 if (inLine[linePos] != ';' && inLine[linePos] != '[' && inLine[linePos] != 0) 241 if (inLine[linePos] != ';' && inLine[linePos] != '[' && inLine[linePos] != 0)
262 { 242 {
263 xs_error("Invalid line in SongLengthDB file '%s' line #%d:\n%s\n", 243 xs_error("Invalid line in SongLengthDB file '%s' line #%d:\n%s\n",
264 dbFilename, lineNum, inLine); 244 dbFilename, lineNum, inLine);
265 } 245 }
266
267 } 246 }
268 247
269 fclose(inFile); 248 fclose(inFile);
270 return 0; 249 return 0;
271 } 250 }
533 { 512 {
534 key = &keyItem; 513 key = &keyItem;
535 item = bsearch(&key, db->pindex, db->n, 514 item = bsearch(&key, db->pindex, db->n,
536 sizeof(db->pindex[0]), xs_sldb_cmp); 515 sizeof(db->pindex[0]), xs_sldb_cmp);
537 516
517 if (item == NULL)
518 {
519 gint i;
520 xs_error("No matching hash in SLDB: %s\n");
521 for (i = 0; i < XS_MD5HASH_LENGTH; i++)
522 fprintf(stderr, "%02x", keyItem.md5Hash[i]);
523 fprintf(stderr, "\n");
524 }
538 return (item != NULL) ? *item : NULL; 525 return (item != NULL) ? *item : NULL;
539 } 526 }
540 else 527 else
541 return NULL; 528 return NULL;
542 } 529 }