construct_pattern.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/glocale.h>
  5. char *construct_pattern(char **names)
  6. {
  7. char *pattern, *p;
  8. int i, j, len, found_illegal_names;
  9. const char *mapset;
  10. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  11. len = 0;
  12. for (i = 0; names[i]; i++) {
  13. if ((p = strchr(names[i], '@')))
  14. len += p - names[i];
  15. else
  16. len += strlen(names[i]);
  17. /* make room for escaping special characters */
  18. for (j = 0; names[i][j]; j++)
  19. len += !isalnum(names[i][j]);
  20. }
  21. len += i; /* # names - 1 commas + \0 */
  22. pattern = p = (char *)G_malloc(len);
  23. mapset = G_mapset();
  24. found_illegal_names = 0;
  25. for (i = 0; names[i]; i++) {
  26. char *name;
  27. name = names[i];
  28. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  29. if (strcmp(xmapset, mapset) != 0)
  30. G_fatal_error(_("%s: Cannot remove or exclude files not in "
  31. "the current mapset."), name);
  32. name = xname;
  33. }
  34. if (G_legal_filename(name) == -1)
  35. found_illegal_names = 1;
  36. if (i)
  37. *p++ = ',';
  38. for (j = 0; name[j]; j++) {
  39. if (!isalnum(name[j]))
  40. *p++ = '\\';
  41. *p++ = name[j];
  42. }
  43. }
  44. *p = '\0';
  45. if (found_illegal_names)
  46. G_fatal_error(_("Illegal filenames not allowed in the name or ignore "
  47. "option."));
  48. return pattern;
  49. }