comparison tools/gfxconv.c @ 2488:87c8be2bc8ba

Rework some option parsing to be more robust.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 28 Apr 2020 18:09:32 +0300
parents 14a4f8d78fe6
children ed2f56abda22
comparison
equal deleted inserted replaced
2487:14a4f8d78fe6 2488:87c8be2bc8ba
910 } 910 }
911 optSpec.compression = tmpUInt; 911 optSpec.compression = tmpUInt;
912 break; 912 break;
913 913
914 case 40: 914 case 40:
915 { 915 if (strcasecmp(optArg, "auto") == 0)
916 int tx0, ty0, tx1, ty1; 916 {
917 if (strcasecmp(optArg, "auto") == 0) 917 optCropMode = CROP_AUTO;
918 { 918 }
919 optCropMode = CROP_AUTO; 919 else
920 } 920 {
921 else 921 unsigned int tx0, ty0, tx1, ty1;
922 if (sscanf(optArg, "%d:%d-%d:%d", &tx0, &ty0, &tx1, &ty1) == 4) 922 char sep, modeSep = 0, *tmpTok, *tmpStr;
923 BOOL ok;
924 if ((tmpTok = tmpStr = dm_strdup(optArg)) == NULL)
925 {
926 dmErrorMsg("Could not allocate memory for temporary string.\n");
927 return FALSE;
928 }
929
930 // Check for 'x0:y0-x1:y1' pattern
931 ok = dmParseIntValWithSep(&tmpTok, &tx0, &sep, ":") &&
932 sep == ':' &&
933 dmParseIntValWithSep(&tmpTok, &ty0, &modeSep, ":-") &&
934 (sep == ':' || sep == '-') &&
935 dmParseIntValWithSep(&tmpTok, &tx1, &sep, ":") &&
936 sep == ':' &&
937 dmParseIntValWithSep(&tmpTok, &ty1, &sep, "");
938
939 dmFree(tmpStr);
940
941 if (ok)
923 { 942 {
924 optCropMode = CROP_SIZE; 943 optCropMode = CROP_SIZE;
925 optCropX0 = tx0; 944 optCropX0 = tx0;
926 optCropY0 = ty0; 945 optCropY0 = ty0;
927 optCropW = tx1 - tx0 + 1; 946
928 optCropH = ty1 - ty0 + 1; 947 if (modeSep == '-')
929 } 948 {
930 else 949 DM_SWAP_IF(unsigned int, tx0, tx1);
931 if (sscanf(optArg, "%d:%d:%d:%d", &tx0, &ty0, &tx1, &ty1) == 4) 950 DM_SWAP_IF(unsigned int, ty0, ty1);
932 { 951
933 optCropMode = CROP_SIZE; 952 optCropW = tx1 - tx0;
934 optCropX0 = tx0; 953 optCropH = ty1 - ty0;
935 optCropY0 = ty0; 954
936 optCropW = tx1; 955 dmMsg(1, "Crop coordinates %d, %d - %d, %d [dim %d, %d]\n",
937 optCropH = ty1; 956 tx0, ty0, tx1, ty1, optCropW, optCropH);
957 }
958 else
959 {
960 optCropW = tx1;
961 optCropH = ty1;
962
963 dmMsg(1, "Crop coordinates %d, %d - %d, %d [dim %d, %d]\n",
964 tx0, ty0, tx0 + tx1, ty0 + ty1, optCropW, optCropH);
965 }
938 } 966 }
939 else 967 else
940 { 968 {
941 dmErrorMsg("Invalid crop mode / argument '%s'.\n", optArg); 969 dmErrorMsg("Invalid crop mode / argument '%s'.\n", optArg);
942 return FALSE; 970 return FALSE;