legal_vname.c 3.5 KB

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