legal_vname.c 3.5 KB

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