legal_vname.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*!
  2. \file 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/gis.h>
  11. #include <grass/Vect.h>
  12. #include <grass/glocale.h>
  13. /*!
  14. \brief Check if output is legal vector name.
  15. Rule: [A-Za-z][A-Za-z0-9_@]*
  16. Check also for SQL keywords.
  17. \param s filename to be checked
  18. \return 1 OK
  19. \return -1 if name does not start with letter A..Za..z or if name does not continue with A..Za..z0..9_@
  20. */
  21. int Vect_legal_filename(const char *s)
  22. {
  23. /* full list of SQL keywords available at
  24. http://www.postgresql.org/docs/8.2/static/sql-keywords-appendix.html
  25. */
  26. static const char *keywords[] = { "and", "or", "not", NULL };
  27. char buf[GNAME_MAX];
  28. int i;
  29. sprintf(buf, "%s", s);
  30. if (*s == '.' || *s == 0) {
  31. G_warning(_("Illegal vector map name <%s>. May not contain '.' or 'NULL'."),
  32. buf);
  33. return -1;
  34. }
  35. /* file name must start with letter */
  36. if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
  37. G_warning(_("Illegal vector map name <%s>. Must start with a letter."),
  38. buf);
  39. return -1;
  40. }
  41. for (s++; *s; s++)
  42. if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
  43. (*s >= '0' && *s <= '9') || *s == '_' || *s == '@')) {
  44. G_warning(_("Illegal vector map name <%s>. Character '%c' not allowed."),
  45. buf, *s);
  46. return -1;
  47. }
  48. for (i = 0; keywords[i]; i++)
  49. if (G_strcasecmp(buf, keywords[i]) == 0) {
  50. G_warning(_("Illegal vector map name <%s>. SQL keyword cannot be used as vector map name."),
  51. buf);
  52. return -1;
  53. }
  54. return 1;
  55. }
  56. /*!
  57. \brief Check for input and output vector map name.
  58. Check
  59. - output is legal vector name
  60. - if can find input map
  61. - if input was found in current mapset, check if input != output
  62. \param input input name
  63. \param output output name
  64. \param error error type GV_FATAL_EXIT, GV_FATAL_PRINT, GV_FATAL_RETURN
  65. \return 0 OK
  66. \return 1 error
  67. */
  68. int Vect_check_input_output_name(const char *input, const char *output,
  69. int error)
  70. {
  71. const char *mapset;
  72. if (Vect_legal_filename(output) == -1) {
  73. if (error == GV_FATAL_EXIT) {
  74. G_fatal_error(_("Output vector map name <%s> is not valid map name"),
  75. output);
  76. }
  77. else if (error == GV_FATAL_PRINT) {
  78. G_warning(_("Output vector map name <%s> is not valid map name"),
  79. output);
  80. return 1;
  81. }
  82. else { /* GV_FATAL_RETURN */
  83. return 1;
  84. }
  85. }
  86. mapset = G_find_vector2(input, "");
  87. if (mapset == NULL) {
  88. if (error == GV_FATAL_EXIT) {
  89. G_fatal_error(_("Vector map <%s> not found"), input);
  90. }
  91. else if (error == GV_FATAL_PRINT) {
  92. G_warning(_("Vector map <%s> not found"), input);
  93. return 1;
  94. }
  95. else { /* GV_FATAL_RETURN */
  96. return 1;
  97. }
  98. }
  99. if (strcmp(mapset, G_mapset()) == 0) {
  100. const char *in;
  101. char nm[GNAME_MAX], ms[GMAPSET_MAX];
  102. if (G__name_is_fully_qualified(input, nm, ms)) {
  103. in = nm;
  104. }
  105. else {
  106. in = input;
  107. }
  108. if (strcmp(in, output) == 0) {
  109. if (error == GV_FATAL_EXIT) {
  110. G_fatal_error(_("Output vector map <%s> is used as input"),
  111. output);
  112. }
  113. else if (error == GV_FATAL_PRINT) {
  114. G_warning(_("Output vector map <%s> is used as input"),
  115. output);
  116. return 1;
  117. }
  118. else { /* GV_FATAL_RETURN */
  119. return 1;
  120. }
  121. }
  122. }
  123. return 0;
  124. }