# HG changeset patch # User Matti Hamalainen # Date 1588086572 -10800 # Node ID 87c8be2bc8ba2544deb5929e5d36ad2c3b5841e7 # Parent 14a4f8d78fe65024caabb317798b243424267e7f Rework some option parsing to be more robust. diff -r 14a4f8d78fe6 -r 87c8be2bc8ba tools/gfxconv.c --- a/tools/gfxconv.c Tue Apr 28 18:09:07 2020 +0300 +++ b/tools/gfxconv.c Tue Apr 28 18:09:32 2020 +0300 @@ -912,29 +912,57 @@ break; case 40: + if (strcasecmp(optArg, "auto") == 0) { - int tx0, ty0, tx1, ty1; - if (strcasecmp(optArg, "auto") == 0) + optCropMode = CROP_AUTO; + } + else + { + unsigned int tx0, ty0, tx1, ty1; + char sep, modeSep = 0, *tmpTok, *tmpStr; + BOOL ok; + if ((tmpTok = tmpStr = dm_strdup(optArg)) == NULL) { - optCropMode = CROP_AUTO; + dmErrorMsg("Could not allocate memory for temporary string.\n"); + return FALSE; } - else - if (sscanf(optArg, "%d:%d-%d:%d", &tx0, &ty0, &tx1, &ty1) == 4) + + // Check for 'x0:y0-x1:y1' pattern + ok = dmParseIntValWithSep(&tmpTok, &tx0, &sep, ":") && + sep == ':' && + dmParseIntValWithSep(&tmpTok, &ty0, &modeSep, ":-") && + (sep == ':' || sep == '-') && + dmParseIntValWithSep(&tmpTok, &tx1, &sep, ":") && + sep == ':' && + dmParseIntValWithSep(&tmpTok, &ty1, &sep, ""); + + dmFree(tmpStr); + + if (ok) { optCropMode = CROP_SIZE; optCropX0 = tx0; optCropY0 = ty0; - optCropW = tx1 - tx0 + 1; - optCropH = ty1 - ty0 + 1; - } - else - if (sscanf(optArg, "%d:%d:%d:%d", &tx0, &ty0, &tx1, &ty1) == 4) - { - optCropMode = CROP_SIZE; - optCropX0 = tx0; - optCropY0 = ty0; - optCropW = tx1; - optCropH = ty1; + + if (modeSep == '-') + { + DM_SWAP_IF(unsigned int, tx0, tx1); + DM_SWAP_IF(unsigned int, ty0, ty1); + + optCropW = tx1 - tx0; + optCropH = ty1 - ty0; + + dmMsg(1, "Crop coordinates %d, %d - %d, %d [dim %d, %d]\n", + tx0, ty0, tx1, ty1, optCropW, optCropH); + } + else + { + optCropW = tx1; + optCropH = ty1; + + dmMsg(1, "Crop coordinates %d, %d - %d, %d [dim %d, %d]\n", + tx0, ty0, tx0 + tx1, ty0 + ty1, optCropW, optCropH); + } } else {