parse_list.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /***********************************************************
  2. * parse_val_list (buf, list)
  3. * char *buf; int **list;
  4. *
  5. * buf is a comma separated list of values
  6. * or value ranges: 1,2,6-10,12
  7. *
  8. * actual usage is
  9. * char buf[300]; DCELL *list;
  10. *
  11. * count = parse_val_list (buf, &list);
  12. *
  13. * for (i = 0; i < count; i += 2)
  14. * {
  15. * min = list[i];
  16. * max = list[i+1];
  17. * }
  18. *
  19. * count will be negative if list is not valid
  20. ********************************************************/
  21. #include <grass/raster.h>
  22. int parse_val_list(char *buf, DCELL ** list)
  23. {
  24. int count;
  25. DCELL a, b;
  26. DCELL *lp;
  27. count = 0;
  28. lp = (DCELL *) G_malloc(sizeof(DCELL));
  29. while (*buf) {
  30. while (*buf == ' ' || *buf == '\t' || *buf == '\n' || *buf == ',')
  31. buf++;
  32. if (sscanf(buf, "%lf-%lf", &a, &b) == 2) {
  33. if (a > b) {
  34. DCELL t;
  35. t = a;
  36. a = b;
  37. b = t;
  38. }
  39. lp = (DCELL *) G_realloc(lp, (count + 2) * sizeof(DCELL));
  40. lp[count++] = a;
  41. lp[count++] = b;
  42. }
  43. else if (sscanf(buf, "%lf", &a) == 1) {
  44. lp = (DCELL *) G_realloc(lp, (count + 2) * sizeof(DCELL));
  45. lp[count++] = a;
  46. lp[count++] = a;
  47. }
  48. else {
  49. G_free(lp);
  50. return -1;
  51. }
  52. while (*buf && (*buf != ','))
  53. buf++;
  54. }
  55. *list = lp;
  56. return count;
  57. }