comparison tools/xm2jss.c @ 2473:f5848606d5ad

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Apr 2020 15:16:39 +0300
parents bc05bcfc4598
children e2e94f9afe1b
comparison
equal deleted inserted replaced
2472:fd02e78f6fdc 2473:f5848606d5ad
1284 1284
1285 int main(int argc, char *argv[]) 1285 int main(int argc, char *argv[])
1286 { 1286 {
1287 DMResource *inFile = NULL; 1287 DMResource *inFile = NULL;
1288 FILE *outFile = NULL; 1288 FILE *outFile = NULL;
1289 JSSModule *sm, *dm; 1289 JSSModule *sm = NULL, *dm = NULL;
1290 int result; 1290 int res = DMERR_OK;
1291 1291
1292 dmInitProg("xm2jss", "XM to JSSMOD converter", "0.8", NULL, NULL); 1292 dmInitProg("xm2jss", "XM to JSSMOD converter", "0.8", NULL, NULL);
1293 dmVerbosity = 0; 1293 dmVerbosity = 0;
1294 1294
1295 // Parse arguments 1295 // Parse arguments
1298 exit(1); 1298 exit(1);
1299 1299
1300 // Check arguments 1300 // Check arguments
1301 if (optInFilename == NULL || optOutFilename == NULL) 1301 if (optInFilename == NULL || optOutFilename == NULL)
1302 { 1302 {
1303 dmErrorMsg("Input or output file not specified. Try --help.\n"); 1303 res = dmError(DMERR_INVALID_ARGS,
1304 return 1; 1304 "Input or output file not specified. Try --help.\n");
1305 goto out;
1305 } 1306 }
1306 1307
1307 // Read the source file 1308 // Read the source file
1308 if ((result = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) 1309 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
1309 { 1310 {
1310 dmErrorMsg("Error opening input file '%s', %d: %s\n", 1311 dmErrorMsg("Error opening input file '%s', %d: %s\n",
1311 optInFilename, result, dmErrorStr(result)); 1312 optInFilename, res, dmErrorStr(res));
1312 return 1; 1313 goto out;
1313 } 1314 }
1314 1315
1315 // Initialize miniJSS 1316 // Initialize miniJSS
1316 jssInit(); 1317 jssInit();
1317 1318
1318 // Read file 1319 // Read file
1319 dmMsg(1, "Reading XM-format file ...\n"); 1320 dmMsg(1, "Reading XM-format file ...\n");
1320 result = jssLoadXM(inFile, &sm, FALSE); 1321 res = jssLoadXM(inFile, &sm, FALSE);
1321 dmf_close(inFile); 1322 dmf_close(inFile);
1322 if (result != 0) 1323 if (res != 0)
1323 { 1324 {
1324 dmErrorMsg("Error while loading XM file (%d), ", result); 1325 dmErrorMsg("Error while loading XM file (%d), ", res);
1325 if (optIgnoreErrors) 1326 if (optIgnoreErrors)
1326 fprintf(stderr, "ignoring. This may cause problems.\n"); 1327 fprintf(stderr, "ignoring. This may cause problems.\n");
1327 else 1328 else
1328 { 1329 {
1329 fprintf(stderr, "giving up. Use --ignore if you want to try to convert anyway.\n"); 1330 fprintf(stderr, "giving up. Use --ignore if you want to try to convert anyway.\n");
1330 return 2; 1331 goto out;
1331 } 1332 }
1332 } 1333 }
1333 1334
1334 // Check stripping settings 1335 // Check stripping settings
1335 if (optStripExtInstr) optStripInstr = TRUE; 1336 if (optStripExtInstr) optStripInstr = TRUE;
1336 if (optStripInstr) optStripSamples = TRUE; 1337 if (optStripInstr) optStripSamples = TRUE;
1337 1338
1338 // Remove samples 1339 // Remove samples
1339 if (optStripSamples) 1340 if (optStripSamples)
1340 { 1341 {
1341 int i;
1342
1343 dmMsg(1, "Stripping samples...\n"); 1342 dmMsg(1, "Stripping samples...\n");
1344 for (i = 0; i < sm->ninstruments; i++) 1343 for (int i = 0; i < sm->ninstruments; i++)
1345 { 1344 {
1346 dmFree(sm->instruments[i]->data); 1345 dmFree(sm->instruments[i]->data);
1347 sm->instruments[i]->data = NULL; 1346 sm->instruments[i]->data = NULL;
1348 } 1347 }
1349 } 1348 }
1350 1349
1351 // Remove instruments 1350 // Remove instruments
1352 if (optStripInstr) 1351 if (optStripInstr)
1353 { 1352 {
1354 int i;
1355
1356 dmMsg(1, "Stripping instruments...\n"); 1353 dmMsg(1, "Stripping instruments...\n");
1357 for (i = 0; i < sm->ninstruments; i++) 1354 for (int i = 0; i < sm->ninstruments; i++)
1358 { 1355 {
1359 dmFree(sm->instruments[i]); 1356 dmFree(sm->instruments[i]);
1360 sm->instruments[i] = NULL; 1357 sm->instruments[i] = NULL;
1361 } 1358 }
1362 sm->ninstruments = 0; 1359 sm->ninstruments = 0;
1379 // Run the optimization procedure 1376 // Run the optimization procedure
1380 if (optOptimize) 1377 if (optOptimize)
1381 { 1378 {
1382 dmMsg(1, "Optimizing module data...\n"); 1379 dmMsg(1, "Optimizing module data...\n");
1383 dm = jssOptimizeModule(sm); 1380 dm = jssOptimizeModule(sm);
1384 } else 1381 }
1382 else
1385 dm = sm; 1383 dm = sm;
1386 1384
1387 // Write output file 1385 // Write output file
1388 if ((outFile = fopen(optOutFilename, "wb")) == NULL) 1386 if ((outFile = fopen(optOutFilename, "wb")) == NULL)
1389 { 1387 {
1394 } 1392 }
1395 1393
1396 dmMsg(1, "Writing JSSMOD-format file [patMode=0x%04x, samp8=0x%02x, samp16=0x%02x]\n", 1394 dmMsg(1, "Writing JSSMOD-format file [patMode=0x%04x, samp8=0x%02x, samp16=0x%02x]\n",
1397 optPatternMode, optSampMode8, optSampMode16); 1395 optPatternMode, optSampMode8, optSampMode16);
1398 1396
1399 result = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16); 1397 res = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16);
1400
1401 dmFree(sm);
1402 1398
1403 fclose(outFile); 1399 fclose(outFile);
1404 1400
1405 if (result != 0) 1401 if (res != 0)
1406 { 1402 {
1407 dmErrorMsg( 1403 dmErrorMsg(
1408 "Error while saving JSSMOD file, %d: %s\n" 1404 "Error while saving JSSMOD file: %s\n"
1409 "WARNING: The resulting file may be broken!\n", 1405 "WARNING: The resulting file may be broken!\n",
1410 result, dmErrorStr(result)); 1406 dmErrorStr(res));
1411 } 1407 }
1412 else 1408 else
1413 { 1409 {
1414 dmMsg(1, "Conversion complete.\n"); 1410 dmMsg(1, "Conversion complete.\n");
1415 } 1411 }
1416 return 0; 1412
1417 } 1413 out:
1414 jssFreeModule(sm);
1415 dmFree(dm);
1416
1417 return res;
1418 }