# HG changeset patch # User Matti Hamalainen # Date 1425039432 -7200 # Node ID 6d5edc6af2ad0127915b5e75ea2a598aa8e333c9 # Parent f654435df15e5dba0dbf121e68e6f15ddcf6536b Error handling cleanups/fixes. diff -r f654435df15e -r 6d5edc6af2ad tools/gfxconv.c --- a/tools/gfxconv.c Fri Feb 27 06:10:16 2015 +0200 +++ b/tools/gfxconv.c Fri Feb 27 14:17:12 2015 +0200 @@ -467,7 +467,7 @@ if ((fp = fopen(filename, "r")) == NULL) { res = dmGetErrno(); - dmErrorMsg("Could not open color remap file '%s' for reading, %d: %s\n", + dmError(res, "Could not open color remap file '%s' for reading, %d: %s\n", res, dmErrorStr(res)); return res; } @@ -865,8 +865,8 @@ if (npal == NULL || mapping == NULL || mapped == NULL || used == NULL) { - dmErrorMsg("Could not allocate memory for reused palette.\n"); - return DMERR_MALLOC; + return dmError(DMERR_MALLOC, + "Could not allocate memory for reused palette.\n"); } for (index = 0; index < image->ncolors; index++) @@ -1040,7 +1040,7 @@ if ((outFile = fopen(filename, "wb")) == NULL) { res = dmGetErrno(); - dmErrorMsg("Error opening output file '%s', %d: %s\n", + dmError(res, "Error opening output file '%s', %d: %s\n", filename, res, dmErrorStr(res)); goto error; } @@ -1048,7 +1048,7 @@ if (!dm_fwrite_str(outFile, buf, bufSize)) { res = dmGetErrno(); - dmErrorMsg("Error writing image data to '%s', %d: %s\n", + dmError(res, "Error writing image data to '%s', %d: %s\n", filename, res, dmErrorStr(res)); } @@ -1270,15 +1270,15 @@ break; default: - ret = DMERR_INVALID_ARGS; - dmErrorMsg("Invalid output format %d, internal error.\n", outFormat); + ret = dmError(DMERR_INVALID_ARGS, + "Invalid output format %d, internal error.\n", outFormat); goto error; } if (outBlockW < 1 || outBlockH < 1) { - ret = DMERR_INVALID_ARGS; - dmErrorMsg("Source image dimensions too small for conversion, block dimensions %d x %d.\n", + ret = dmError(DMERR_INVALID_ARGS, + "Source image dimensions too small for conversion, block dimensions %d x %d.\n", outBlockW, outBlockH); goto error; } @@ -1330,7 +1330,7 @@ if (!dm_fwrite_str(outFile, buf, outBufSize)) { ret = dmGetErrno(); - dmErrorMsg("Error writing data block %d,%d to '%s', %d: %s\n", + dmError(ret, "Error writing data block %d,%d to '%s', %d: %s\n", bx, by, filename, ret, dmErrorStr(ret)); goto error; } @@ -1350,7 +1350,7 @@ int dmDumpSpritesAndChars(FILE *inFile) { - int dataOffs, itemCount, outWidth, outWidthPX, outHeight; + int dataOffs, itemCount, outWidth, outWidthPX, outHeight, ret; size_t bufSize; Uint8 *bufData; @@ -1371,14 +1371,14 @@ break; default: - dmErrorMsg("Invalid input format %d, internal error.\n", optInFormat); - return -1; + return dmError(DMERR_INTERNAL, + "Invalid input format %d, internal error.\n", optInFormat); } if ((bufData = dmMalloc(bufSize)) == NULL) { - dmErrorMsg("Could not allocate temporary buffer of %d bytes.\n", bufSize); - return -2; + return dmError(DMERR_INTERNAL, + "Could not allocate temporary buffer of %d bytes.\n", bufSize); } @@ -1395,9 +1395,9 @@ else if ((outFile = fopen(optOutFilename, "w")) == NULL) { - int res = dmGetErrno(); - dmErrorMsg("Error opening output file '%s', %d: %s\n", - optOutFilename, res, dmErrorStr(res)); + ret = dmGetErrno(); + dmError(ret, "Error opening output file '%s', %d: %s\n", + optOutFilename, ret, dmErrorStr(ret)); goto error; } @@ -1491,8 +1491,6 @@ if (optSequential) { - int eres; - outFilename = dm_strdup_printf("%s%04d.%s", optOutFilename, itemCount, convFormatList[optOutFormat].fext); if (outFilename == NULL) { @@ -1500,11 +1498,11 @@ goto error; } - eres = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE); - if (eres != DMERR_OK) + ret = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE); + if (ret != DMERR_OK) { - dmErrorMsg("Error writing output image, %s.\n", - dmErrorStr(eres)); + dmErrorMsg("Error writing output image, %d: %s.\n", + ret, dmErrorStr(ret)); } dmFree(outFilename); @@ -1523,11 +1521,11 @@ if (!optSequential) { - int eres = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE); - if (eres != DMERR_OK) + ret = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE); + if (ret != DMERR_OK) { - dmErrorMsg("Error writing output image, %s.\n", - dmErrorStr(eres)); + dmError(ret, "Error writing output image, %d: %s.\n", + ret, dmErrorStr(ret)); } } @@ -1538,17 +1536,18 @@ { if (optSequential) { - dmErrorMsg("Sequential output not supported for spr/char -> bitmap conversion.\n"); + ret = dmError(DMERR_INVALID_ARGS, + "Sequential output not supported for spr/char -> bitmap conversion.\n"); goto error; } } dmFree(bufData); - return 0; + return DMERR_OK; error: dmFree(bufData); - return -1; + return ret; }