legal_vname.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*!
  2. \file lib/vector/Vlib/legal_vname.c
  3. \brief Vector library - Check if map name is legal vector map name
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Radim Blazek
  8. */
  9. #include <string.h>
  10. #include <grass/vector.h>
  11. #include <grass/glocale.h>
  12. /*!
  13. \brief Check if output is legal vector name.
  14. Rule: [A-Za-z][A-Za-z0-9_]*
  15. Check also for SQL keywords.
  16. \param s filename to be checked
  17. \return 1 OK
  18. \return -1 if name does not start with letter A..Za..z or if name does not continue with A..Za..z0..9_
  19. */
  20. int Vect_legal_filename(const char *s)
  21. {
  22. /* full list of SQL keywords available at
  23. http://www.postgresql.org/docs/8.2/static/sql-keywords-appendix.html
  24. */
  25. static const char *keywords[] = { "and", "or", "not", NULL };
  26. char buf[GNAME_MAX];
  27. int i;
  28. sprintf(buf, "%s", s);
  29. if (*s == '.' || *s == 0) {
  30. G_warning(_("Illegal vector map name <%s>. May not contain '.' or 'NULL'."),
  31. buf);
  32. return -1;
  33. }
  34. /* file name must start with letter */
  35. if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
  36. G_warning(_("Illegal vector map name <%s>. Must start with a letter."),
  37. buf);
  38. return -1;
  39. }
  40. for (s++; *s; s++)
  41. if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
  42. (*s >= '0' && *s <= '9') || *s == '_')) {
  43. G_warning(_("Illegal vector map name <%s>. Character '%c' not allowed."),
  44. buf, *s);
  45. return -1;
  46. }
  47. for (i = 0; keywords[i]; i++)
  48. if (G_strcasecmp(buf, keywords[i]) == 0) {
  49. G_warning(_("Illegal vector map name <%s>. SQL keyword cannot be used as vector map name."),
  50. buf);
  51. return -1;
  52. }
  53. return 1;
  54. }
  55. /*!
  56. \brief Check for input and output vector map name.
  57. Check
  58. - output is legal vector name
  59. - if can find input map
  60. - if input was found in current mapset, check if input != output
  61. \param input input name
  62. \param output output name
  63. \param error error type G_FATAL_EXIT, G_FATAL_PRINT, G_FATAL_RETURN
  64. \return 0 OK
  65. \return 1 error
  66. */
  67. int Vect_check_input_output_name(const char *input, const char *output,
  68. int error)
  69. {
  70. const char *mapset;
  71. char inm[GNAME_MAX], ims[GMAPSET_MAX];
  72. char onm[GNAME_MAX], oms[GMAPSET_MAX];
  73. /* check for fully-qualified map name */
  74. if (G_name_is_fully_qualified(output, onm, oms)) {
  75. if (strcmp(oms, G_mapset()) != 0) {
  76. if (error == G_FATAL_EXIT) {
  77. G_fatal_error(_("Output vector map name <%s> is not in the current mapset (%s)"),
  78. output, G_mapset());
  79. }
  80. else if (error == G_FATAL_PRINT) {
  81. G_warning(_("Output vector map name <%s> is not in the current mapset (%s)"),
  82. output, G_mapset());
  83. return 1;
  84. }
  85. else { /* GV_FATAL_RETURN */
  86. return 1;
  87. }
  88. }
  89. output = onm;
  90. }
  91. if (Vect_legal_filename(output) == -1) {
  92. if (error == G_FATAL_EXIT) {
  93. G_fatal_error(_("Output vector map name <%s> is not SQL compliant"),
  94. output);
  95. }
  96. else if (error == G_FATAL_PRINT) {
  97. G_warning(_("Output vector map name <%s> is not SQL compliant"),
  98. output);
  99. return 1;
  100. }
  101. else { /* GV_FATAL_RETURN */
  102. return 1;
  103. }
  104. }
  105. if (G_name_is_fully_qualified(input, inm, ims)) {
  106. if (strcasecmp(ims, "ogr") != 0)
  107. mapset = G_find_vector2(input, "");
  108. else
  109. mapset = ims;
  110. }
  111. else
  112. mapset = G_find_vector2(input, "");
  113. if (mapset == NULL) {
  114. if (error == G_FATAL_EXIT) {
  115. G_fatal_error(_("Vector map <%s> not found"), input);
  116. }
  117. else if (error == G_FATAL_PRINT) {
  118. G_warning(_("Vector map <%s> not found"), input);
  119. return 1;
  120. }
  121. else { /* GV_FATAL_RETURN */
  122. return 1;
  123. }
  124. }
  125. if (strcmp(mapset, G_mapset()) == 0) {
  126. if (G_name_is_fully_qualified(input, inm, ims)) {
  127. input = inm;
  128. }
  129. if (strcmp(input, output) == 0) {
  130. if (error == G_FATAL_EXIT) {
  131. G_fatal_error(_("Output vector map <%s> is used as input"),
  132. output);
  133. }
  134. else if (error == G_FATAL_PRINT) {
  135. G_warning(_("Output vector map <%s> is used as input"),
  136. output);
  137. return 1;
  138. }
  139. else { /* GV_FATAL_RETURN */
  140. return 1;
  141. }
  142. }
  143. }
  144. return 0;
  145. }