comparison gfxconv.c @ 481:c3f0fca5b596

Improve remapping option parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Nov 2012 04:25:16 +0200
parents d7fc7e011c90
children 6fdee3ec2894
comparison
equal deleted inserted replaced
480:d7fc7e011c90 481:c3f0fca5b596
242 } 242 }
243 return FALSE; 243 return FALSE;
244 } 244 }
245 245
246 246
247 static BOOL dmParseMapOptionMapItem(char *opt, DMMapValue *value, const int nmax, const char *msg) 247 static BOOL dmParseMapOptionMapItem(const char *popt, DMMapValue *value, const int nmax, const char *msg)
248 { 248 {
249 char *split = strchr(opt, ':'); 249 char *end, *split, *opt = dm_strdup(popt);
250 250
251 if (split == NULL) 251 if (opt == NULL)
252 goto error;
253
254 if ((end = split = strchr(opt, ':')) == NULL)
252 { 255 {
253 dmError("Invalid %s value '%s', expected <(#|%)RRGGBB|[$|0x]index>:<[$|0x]index>.\n", msg, opt); 256 dmError("Invalid %s value '%s', expected <(#|%)RRGGBB|[$|0x]index>:<[$|0x]index>.\n", msg, opt);
254 return FALSE; 257 goto error;
255 } 258 }
256 259
257 *split = 0; 260 // Trim whitespace
258 261 *end = 0;
262 for (end--; end > opt && *end && isspace(*end); end--)
263 *end = 0;
264
265 // Parse either a hex triplet color definition or a normal value
259 if (*opt == '#' || *opt == '%') 266 if (*opt == '#' || *opt == '%')
260 { 267 {
261 int colR, colG, colB, colA; 268 int colR, colG, colB, colA;
262 269
263 if (sscanf(opt + 1, "%2x%2x%2x%2x", &colR, &colG, &colB, &colA) == 4 || 270 if (sscanf(opt + 1, "%2x%2x%2x%2x", &colR, &colG, &colB, &colA) == 4 ||
268 } 275 }
269 else 276 else
270 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 && 277 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 &&
271 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3) 278 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3)
272 { 279 {
273 dmError("Invalid %s value '%s', expected a hex triplet after #.\n", msg, opt); 280 dmError("Invalid %s value '%s', expected a hex triplet, got '%s'.\n", msg, popt, opt + 1);
274 return FALSE; 281 goto error;
275 } 282 }
276 283
277 value->color.r = colR; 284 value->color.r = colR;
278 value->color.g = colG; 285 value->color.g = colG;
279 value->color.b = colB; 286 value->color.b = colB;
281 } 288 }
282 else 289 else
283 { 290 {
284 if (!dmGetIntVal(opt, &value->from)) 291 if (!dmGetIntVal(opt, &value->from))
285 { 292 {
286 dmError("Invalid %s value '%s', could not parse.\n", msg, opt); 293 dmError("Invalid %s value '%s', could not parse source value '%s'.\n", msg, popt, opt);
287 return FALSE; 294 goto error;
288 } 295 }
289 value->triplet = FALSE; 296 value->triplet = FALSE;
290 } 297 }
291 298
292 if (!dmGetIntVal(split + 1, &value->to)) 299 // Trim whitespace
293 { 300 split++;
294 dmError("Invalid %s value '%s', could not parse.\n", msg, opt); 301 while (*split && isspace(*split)) split++;
295 return FALSE; 302
303 // Parse destination value
304 if (!dmGetIntVal(split, &value->to))
305 {
306 dmError("Invalid %s value '%s', could not parse destination value '%s'.\n", msg, popt, split);
307 goto error;
296 } 308 }
297 309
298 if (!value->triplet && (value->from < 0 || value->from > 255)) 310 if (!value->triplet && (value->from < 0 || value->from > 255))
299 { 311 {
300 dmError("Invalid %s map source color index value %d.\n", msg, value->from); 312 dmError("Invalid %s map source color index value %d, must be [0..255].\n", msg, value->from);
301 return FALSE; 313 goto error;
302 } 314 }
303 315
304 if (value->to < 0 || value->to > nmax) 316 if (value->to < 0 || value->to > nmax)
305 { 317 {
306 dmError("Invalid %s map destination color index value %d.\n", msg, value->to); 318 dmError("Invalid %s map destination color index value %d, must be [0..%d].\n", msg, value->to, nmax);
307 return FALSE; 319 goto error;
308 } 320 }
309 321
322 dmFree(opt);
310 return TRUE; 323 return TRUE;
324
325 error:
326 dmFree(opt);
327 return FALSE;
311 } 328 }
312 329
313 330
314 static BOOL dmParseMapOptionItem(char *opt, char *end, void *pvalue, const int index, const int nmax, const BOOL requireIndex, const char *msg) 331 static BOOL dmParseMapOptionItem(char *opt, char *end, void *pvalue, const int index, const int nmax, const BOOL requireIndex, const char *msg)
315 { 332 {