comparison src/xs_length.c @ 389:3c239e4160d5

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 29 May 2006 23:46:04 +0000
parents 7dc7e7dbbf90
children b09d74eb71e6
comparison
equal deleted inserted replaced
388:52f71f323555 389:3c239e4160d5
73 } 73 }
74 74
75 75
76 /* Parses a time-entry in SLDB format 76 /* Parses a time-entry in SLDB format
77 */ 77 */
78 static gint32 xs_sldb_gettime(gchar * pcStr, size_t * piPos) 78 static gint xs_sldb_gettime(gchar * pcStr, size_t * piPos)
79 { 79 {
80 gint32 iResult, iTemp; 80 gint iResult, iTemp;
81 81
82 /* Check if it starts with a digit */ 82 /* Check if it starts with a digit */
83 if (isdigit(pcStr[*piPos])) { 83 if (isdigit(pcStr[*piPos])) {
84 /* Get minutes-field */ 84 /* Get minutes-field */
85 iResult = 0; 85 iResult = 0;
109 109
110 return iResult; 110 return iResult;
111 } 111 }
112 112
113 113
114 t_xs_sldb_node * xs_sldb_read_entry(gchar *inLine)
115 {
116 size_t linePos, savePos, tmpI, tmpLen;
117 t_xs_sldb_node *tmpNode;
118
119 /* Allocate new node */
120 if ((tmpNode = xs_sldb_node_new()) == NULL) {
121 XSERR("Error allocating new node. Fatal error.\n");
122 return NULL;
123 }
124
125 /* Get hash value */
126 #if (XS_MD5HASH_LENGTH != 16)
127 #error Mismatch in hashcode length. Check against xs_md5.h and fix here.
128 #endif
129 sscanf(&inLine[0], "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
130 (guint *) & (tmpNode->md5Hash[0]), (guint *) & (tmpNode->md5Hash[1]),
131 (guint *) & (tmpNode->md5Hash[2]), (guint *) & (tmpNode->md5Hash[3]),
132 (guint *) & (tmpNode->md5Hash[4]), (guint *) & (tmpNode->md5Hash[5]),
133 (guint *) & (tmpNode->md5Hash[6]), (guint *) & (tmpNode->md5Hash[7]),
134 (guint *) & (tmpNode->md5Hash[8]), (guint *) & (tmpNode->md5Hash[9]),
135 (guint *) & (tmpNode->md5Hash[10]), (guint *) & (tmpNode->md5Hash[11]),
136 (guint *) & (tmpNode->md5Hash[12]), (guint *) & (tmpNode->md5Hash[13]),
137 (guint *) & (tmpNode->md5Hash[14]), (guint *) & (tmpNode->md5Hash[15]));
138
139 /* Get playtimes */
140 if (inLine[linePos] != 0) {
141 if (inLine[linePos] != '=') {
142 XSERR("'=' expected in SongLengthDB file '%s' line #%d, column #%d\n",
143 dbFilename, lineNum, linePos);
144
145 xs_sldb_node_free(tmpNode);
146 return NULL;
147 } else {
148 /* First playtime is after '=' */
149 savePos = ++linePos;
150 tmpLen = strlen(inLine);
151
152 /* Get number of sub-tune lengths */
153 iOK = TRUE;
154 while ((linePos < tmpLen) && iOK) {
155 xs_findnext(inLine, &linePos);
156 if (xs_sldb_gettime(inLine, &linePos) >= 0)
157 tmpNode->nLengths++;
158 else
159 iOK = FALSE;
160 }
161
162 if (!iOK) {
163 XSERR("Invalid plaaplaa\n");
164 xs_sldb_node_free(tmpNode);
165 return NULL;
166 }
167
168 /* Allocate memory for lengths */
169 tmpNode->sLengths = (gint *) g_malloc0(tmpNode->nLengths * sizeof(gint));
170 if (!tmpNode->sLengths) {
171 XSERR("Could not allocate memory for SongLengthDB entry.\n");
172 xs_sldb_node_free(tmpNode);
173 return NULL;
174 }
175
176 /* Read lengths in */
177 tmpI = 0; linePos = savePos; iOK = TRUE;
178 while ((linePos < tmpLen) && iOK) {
179 xs_findnext(inLine, &linePos);
180
181 if (tmpI < tmpNode->nLengths) {
182 tmpNode->sLengths[tmpI] = xs_sldb_gettime(inLine, &linePos);
183 tmpI++;
184 } else
185 iOK = FALSE;
186 }
187
188 if (!iOK) {
189 xs_sldb_node_free(tmpNode);
190 return NULL;
191 } else
192 return tmpNode;
193 }
194 }
195
196 return NULL;
197 }
198
199
114 /* Read database to memory 200 /* Read database to memory
115 */ 201 */
116 gint xs_sldb_read(t_xs_sldb * db, gchar * dbFilename) 202 gint xs_sldb_read(t_xs_sldb * db, gchar * dbFilename)
117 { 203 {
118 FILE *inFile; 204 FILE *inFile;
132 lineNum = 0; 218 lineNum = 0;
133 219
134 while (!feof(inFile)) { 220 while (!feof(inFile)) {
135 fgets(inLine, XS_BUF_SIZE, inFile); 221 fgets(inLine, XS_BUF_SIZE, inFile);
136 inLine[XS_BUF_SIZE - 1] = 0; 222 inLine[XS_BUF_SIZE - 1] = 0;
223 linePos = 0;
137 lineNum++; 224 lineNum++;
138 225
139 /* Check if it is datafield */ 226 /* Check if it is datafield */
140 if (isxdigit(inLine[0])) { 227 if (isxdigit(inLine[linePos])) {
228 size_t tmpI;
229
141 /* Check the length of the hash */ 230 /* Check the length of the hash */
142 linePos = 0; 231 for (tmpI = 0; isxdigit(inLine[linePos]); linePos++, tmpI++);
143 while (isxdigit(inLine[linePos])) 232
144 linePos++; 233 if (tmpI != XS_MD5HASH_LENGTH_CH) {
145
146 if (linePos != XS_MD5HASH_LENGTH_CH) {
147 XSERR("Invalid hash in SongLengthDB file '%s' line #%d!\n", dbFilename, lineNum); 234 XSERR("Invalid hash in SongLengthDB file '%s' line #%d!\n", dbFilename, lineNum);
148 } else { 235 } else {
149 /* Allocate new node */ 236 /* Parse and add node to db */
150 if ((tmpNode = xs_sldb_node_new()) == NULL) { 237 if ((tmpNode = xs_sldb_read_entry(inLine)) != NULL)
151 XSERR("Error allocating new node. Fatal error.\n"); 238 xs_sldb_node_insert(db, tmpNode);
152 exit(5);
153 }
154
155 /* Get hash value */
156 #if (XS_MD5HASH_LENGTH != 16)
157 #error Mismatch in hashcode length. Fix here.
158 #endif
159 sscanf(&inLine[0], "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
160 (guint *) & (tmpNode->md5Hash[0]), (guint *) & (tmpNode->md5Hash[1]),
161 (guint *) & (tmpNode->md5Hash[2]), (guint *) & (tmpNode->md5Hash[3]),
162 (guint *) & (tmpNode->md5Hash[4]), (guint *) & (tmpNode->md5Hash[5]),
163 (guint *) & (tmpNode->md5Hash[6]), (guint *) & (tmpNode->md5Hash[7]),
164 (guint *) & (tmpNode->md5Hash[8]), (guint *) & (tmpNode->md5Hash[9]),
165 (guint *) & (tmpNode->md5Hash[10]), (guint *) & (tmpNode->md5Hash[11]),
166 (guint *) & (tmpNode->md5Hash[12]), (guint *) & (tmpNode->md5Hash[13]),
167 (guint *) & (tmpNode->md5Hash[14]), (guint *) & (tmpNode->md5Hash[15]));
168
169 /* Get playtimes */
170 if (inLine[linePos] != 0) {
171 if (inLine[linePos] != '=') {
172 XSERR("'=' expected in SongLengthDB file '%s' line #%d, column #%d\n",
173 dbFilename, lineNum, linePos);
174
175 xs_sldb_node_free(tmpNode);
176 } else {
177 /* First playtime is after '=' */
178 linePos++;
179 iOK = TRUE;
180
181 while ((linePos < strlen(inLine)) && iOK) {
182 xs_findnext(inLine, &linePos);
183
184 if (tmpNode->nLengths < XS_STIL_MAXENTRY) {
185 tmpNode->sLengths[tmpNode->nLengths] =
186 xs_sldb_gettime(inLine, &linePos);
187 tmpNode->nLengths++;
188 } else
189 iOK = FALSE;
190 }
191
192 /* Add an node to db in memory */
193 if (iOK)
194 xs_sldb_node_insert(db, tmpNode);
195 else
196 xs_sldb_node_free(tmpNode);
197 }
198 }
199 } 239 }
200 } else if ((inLine[0] != ';') && (inLine[0] != '[')) { 240 } else if ((inLine[0] != ';') && (inLine[0] != '[')) {
201 XSERR("Invalid line in SongLengthDB file '%s' line #%d\n", dbFilename, lineNum); 241 XSERR("Invalid line in SongLengthDB file '%s' line #%d\n", dbFilename, lineNum);
202 } 242 }
203 243
204 } /* while */ 244 }
205 245
206 /* Close the file */ 246 /* Close the file */
207 fclose(inFile); 247 fclose(inFile);
208 248
209 return 0; 249 return 0;