comparison stream/stream_cue.c @ 30760:937621eaa290

Avoid code duplication and excessively deep nesting in cue_find_bin
author reimar
date Mon, 01 Mar 2010 19:20:32 +0000
parents 76a6338e5e71
children b41ac9f6e26f
comparison
equal deleted inserted replaced
30759:76a6338e5e71 30760:937621eaa290
176 /* FIXME: the string operations ( strcpy,strcat ) below depend 176 /* FIXME: the string operations ( strcpy,strcat ) below depend
177 * on the arrays to have the same size, thus we need to make 177 * on the arrays to have the same size, thus we need to make
178 * sure the sizes are in sync. 178 * sure the sizes are in sync.
179 */ 179 */
180 static int cue_find_bin (char *firstline) { 180 static int cue_find_bin (char *firstline) {
181 const char *cur_name;
181 int i,j; 182 int i,j;
182 char s[256]; 183 char s[256];
183 char t[256]; 184 char t[256];
184 185
185 /* get the filename out of that */ 186 /* get the filename out of that */
211 } 212 }
212 bin_filename[j+1] = '\0'; 213 bin_filename[j+1] = '\0';
213 214
214 } 215 }
215 216
217 fd_bin = -1;
218 for (i = 0; fd_bin == -1 && i < 6; i++) {
219 switch (i) {
220 case 0:
216 /* now try to open that file, without path */ 221 /* now try to open that file, without path */
217 fd_bin = open (bin_filename, O_RDONLY); 222 cur_name = bin_filename;
218 if (fd_bin == -1) 223 break;
219 { 224 case 1:
220 mp_msg(MSGT_OPEN,MSGL_STATUS, MSGTR_MPDEMUX_CUEREAD_BinFilenameTested,
221 bin_filename);
222
223 /* now try to find it with the path of the cue file */ 225 /* now try to find it with the path of the cue file */
224 snprintf(s,sizeof( s ),"%s/%s",bincue_path,bin_filename); 226 snprintf(s,sizeof( s ),"%s/%s",bincue_path,bin_filename);
225 fd_bin = open (s, O_RDONLY); 227 cur_name = s;
226 if (fd_bin == -1) 228 break;
227 { 229 case 2:
228 mp_msg(MSGT_OPEN,MSGL_STATUS,
229 MSGTR_MPDEMUX_CUEREAD_BinFilenameTested, s);
230 /* now I would say the whole filename is shit, build our own */ 230 /* now I would say the whole filename is shit, build our own */
231 strncpy(s, cue_filename, strlen(cue_filename) - 3 ); 231 strncpy(s, cue_filename, strlen(cue_filename) - 3 );
232 s[strlen(cue_filename) - 3] = '\0'; 232 s[strlen(cue_filename) - 3] = '\0';
233 strcat(s, "bin"); 233 strcat(s, "bin");
234 fd_bin = open (s, O_RDONLY); 234 cur_name = s;
235 if (fd_bin == -1) 235 break;
236 { 236 case 3:
237 mp_msg(MSGT_OPEN,MSGL_STATUS,
238 MSGTR_MPDEMUX_CUEREAD_BinFilenameTested, s);
239
240 /* ok try it with path */ 237 /* ok try it with path */
241 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s); 238 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s);
242 fd_bin = open (t, O_RDONLY); 239 fd_bin = open (t, O_RDONLY);
243 if (fd_bin == -1) 240 cur_name = t;
244 { 241 break;
245 mp_msg(MSGT_OPEN,MSGL_STATUS, 242 case 4:
246 MSGTR_MPDEMUX_CUEREAD_BinFilenameTested,t);
247 /* now I would say the whole filename is shit, build our own */ 243 /* now I would say the whole filename is shit, build our own */
248 strncpy(s, cue_filename, strlen(cue_filename) - 3 ); 244 strncpy(s, cue_filename, strlen(cue_filename) - 3 );
249 s[strlen(cue_filename) - 3] = '\0'; 245 s[strlen(cue_filename) - 3] = '\0';
250 strcat(s, "img"); 246 strcat(s, "img");
251 fd_bin = open (s, O_RDONLY); 247 cur_name = s;
252 if (fd_bin == -1) 248 break;
253 { 249 case 5:
254 mp_msg(MSGT_OPEN,MSGL_STATUS,
255 MSGTR_MPDEMUX_CUEREAD_BinFilenameTested, s);
256 /* ok try it with path */ 250 /* ok try it with path */
257 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s); 251 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s);
258 fd_bin = open (t, O_RDONLY); 252 cur_name = t;
253 break;
254 }
255 fd_bin = open(cur_name, O_RDONLY);
256 if (fd_bin == -1) {
257 mp_msg(MSGT_OPEN,MSGL_STATUS, MSGTR_MPDEMUX_CUEREAD_BinFilenameTested,
258 cur_name);
259 }
260 }
261
259 if (fd_bin == -1) 262 if (fd_bin == -1)
260 { 263 {
261 mp_msg(MSGT_OPEN,MSGL_STATUS,
262 MSGTR_MPDEMUX_CUEREAD_BinFilenameTested, s);
263
264 /* I'll give up */ 264 /* I'll give up */
265 mp_msg(MSGT_OPEN,MSGL_ERR, 265 mp_msg(MSGT_OPEN,MSGL_ERR,
266 MSGTR_MPDEMUX_CUEREAD_CannotFindBinFile); 266 MSGTR_MPDEMUX_CUEREAD_CannotFindBinFile);
267 return -1; 267 return -1;
268 } 268 }
269 } 269
270 } else strcpy(bin_filename, t); 270 if (cur_name != bin_filename)
271 271 strcpy(bin_filename, cur_name);
272 } else strcpy(bin_filename, s);
273
274 } else strcpy(bin_filename, s);
275
276 }
277 272
278 mp_msg(MSGT_OPEN,MSGL_INFO, 273 mp_msg(MSGT_OPEN,MSGL_INFO,
279 MSGTR_MPDEMUX_CUEREAD_UsingBinFile, bin_filename); 274 MSGTR_MPDEMUX_CUEREAD_UsingBinFile, bin_filename);
280 return 0; 275 return 0;
281 } 276 }