legal_vname.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 == '_' || *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 GV_FATAL_EXIT, GV_FATAL_PRINT, GV_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. if (Vect_legal_filename(output) == -1) {
  72. if (error == GV_FATAL_EXIT) {
  73. G_fatal_error(_("Output vector map name <%s> is not valid map name"),
  74. output);
  75. }
  76. else if (error == GV_FATAL_PRINT) {
  77. G_warning(_("Output vector map name <%s> is not valid map name"),
  78. output);
  79. return 1;
  80. }
  81. else { /* GV_FATAL_RETURN */
  82. return 1;
  83. }
  84. }
  85. mapset = G_find_vector2(input, "");
  86. if (mapset == NULL) {
  87. if (error == GV_FATAL_EXIT) {
  88. G_fatal_error(_("Vector map <%s> not found"), input);
  89. }
  90. else if (error == GV_FATAL_PRINT) {
  91. G_warning(_("Vector map <%s> not found"), input);
  92. return 1;
  93. }
  94. else { /* GV_FATAL_RETURN */
  95. return 1;
  96. }
  97. }
  98. if (strcmp(mapset, G_mapset()) == 0) {
  99. const char *in;
  100. char nm[GNAME_MAX], ms[GMAPSET_MAX];
  101. if (G_name_is_fully_qualified(input, nm, ms)) {
  102. in = nm;
  103. }
  104. else {
  105. in = input;
  106. }
  107. if (strcmp(in, output) == 0) {
  108. if (error == GV_FATAL_EXIT) {
  109. G_fatal_error(_("Output vector map <%s> is used as input"),
  110. output);
  111. }
  112. else if (error == GV_FATAL_PRINT) {
  113. G_warning(_("Output vector map <%s> is used as input"),
  114. output);
  115. return 1;
  116. }
  117. else { /* GV_FATAL_RETURN */
  118. return 1;
  119. }
  120. }
  121. }
  122. return 0;
  123. }