legal_vname.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 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 nm[GNAME_MAX], ms[GMAPSET_MAX];
  72. if (Vect_legal_filename(output) == -1) {
  73. if (error == G_FATAL_EXIT) {
  74. G_fatal_error(_("Output vector map name <%s> is not valid map name"),
  75. output);
  76. }
  77. else if (error == G_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. if (G_name_is_fully_qualified(input, nm, ms)) {
  87. if (strcasecmp(ms, "ogr") != 0)
  88. mapset = G_find_vector2(input, "");
  89. else
  90. mapset = ms;
  91. }
  92. else
  93. mapset = G_find_vector2(input, "");
  94. if (mapset == NULL) {
  95. if (error == G_FATAL_EXIT) {
  96. G_fatal_error(_("Vector map <%s> not found"), input);
  97. }
  98. else if (error == G_FATAL_PRINT) {
  99. G_warning(_("Vector map <%s> not found"), input);
  100. return 1;
  101. }
  102. else { /* GV_FATAL_RETURN */
  103. return 1;
  104. }
  105. }
  106. if (strcmp(mapset, G_mapset()) == 0) {
  107. const char *in;
  108. if (G_name_is_fully_qualified(input, nm, ms)) {
  109. in = nm;
  110. }
  111. else {
  112. in = input;
  113. }
  114. if (strcmp(in, output) == 0) {
  115. if (error == G_FATAL_EXIT) {
  116. G_fatal_error(_("Output vector map <%s> is used as input"),
  117. output);
  118. }
  119. else if (error == G_FATAL_PRINT) {
  120. G_warning(_("Output vector map <%s> is used as input"),
  121. output);
  122. return 1;
  123. }
  124. else { /* GV_FATAL_RETURN */
  125. return 1;
  126. }
  127. }
  128. }
  129. return 0;
  130. }