legal_name.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*!
  2. * \file gis/legal_name.c
  3. *
  4. * \brief GIS Library - Functions to handle file name legality.
  5. *
  6. * (C) 2001-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. * \brief Check for legal database file name.
  19. *
  20. * Legal file names will <b>not</b> begin with '.' or NULL and must
  21. * not contain the characters, ' ' (space), '/', '"'. '\'' (single
  22. * quote), '@', ',', '=', '*', and all other non-alphanumeric
  23. * characters within.
  24. *
  25. * \param s file name to check
  26. *
  27. * \return 1 success
  28. * \return -1 failure
  29. */
  30. int G_legal_filename(const char *s)
  31. {
  32. const char *name = s;
  33. if (*s == '.' || *s == 0) {
  34. G_warning(_("Illegal filename <%s>. Cannot be '.' or 'NULL'."), name);
  35. return -1;
  36. }
  37. for (; *s; s++)
  38. if (*s == '/' || *s == '"' || *s == '\'' || *s <= ' ' ||
  39. *s == '@' || *s == ',' || *s == '=' || *s == '*' || *s > 0176) {
  40. G_warning(_("Illegal filename <%s>. Character <%c> not allowed.\n"), name, *s);
  41. return -1;
  42. }
  43. return 1;
  44. }
  45. /*!
  46. * \brief Check input and output file names.
  47. *
  48. * Check: 1) output is legal map name, 2) if can find input map, and
  49. * 3) if input was found in current mapset, check if input != output.
  50. *
  51. * \param input input map name
  52. * \param output output map name
  53. * \param error error type: GR_FATAL_EXIT, GR_FATAL_PRINT, GR_FATAL_RETURN
  54. *
  55. * \return 0 OK
  56. * \return 1 error
  57. */
  58. int G_check_input_output_name(const char *input, const char *output,
  59. int error)
  60. {
  61. const char *mapset;
  62. if (output == NULL)
  63. return 0; /* don't die on undefined parameters */
  64. if (G_legal_filename(output) == -1) {
  65. if (error == GR_FATAL_EXIT) {
  66. G_fatal_error(_("Output raster map name <%s> is not valid map name"),
  67. output);
  68. }
  69. else if (error == GR_FATAL_PRINT) {
  70. G_warning(_("Output raster map name <%s> is not valid map name"),
  71. output);
  72. return 1;
  73. }
  74. else { /* GR_FATAL_RETURN */
  75. return 1;
  76. }
  77. }
  78. mapset = G_find_raster2(input, "");
  79. if (mapset == NULL) {
  80. if (error == GR_FATAL_EXIT) {
  81. G_fatal_error(_("Raster map <%s> not found"), input);
  82. }
  83. else if (error == GR_FATAL_PRINT) {
  84. G_warning(_("Raster map <%s> not found"), input);
  85. return 1;
  86. }
  87. else { /* GR_FATAL_RETURN */
  88. return 1;
  89. }
  90. }
  91. if (strcmp(mapset, G_mapset()) == 0) {
  92. char nm[1000], ms[1000];
  93. const char *in;
  94. if (G_name_is_fully_qualified(input, nm, ms)) {
  95. in = nm;
  96. }
  97. else {
  98. in = input;
  99. }
  100. if (strcmp(in, output) == 0) {
  101. if (error == GR_FATAL_EXIT) {
  102. G_fatal_error(_("Output raster map <%s> is used as input"),
  103. output);
  104. }
  105. else if (error == GR_FATAL_PRINT) {
  106. G_warning(_("Output raster map <%s> is used as input"),
  107. output);
  108. return 1;
  109. }
  110. else { /* GR_FATAL_RETURN */
  111. return 1;
  112. }
  113. }
  114. }
  115. return 0;
  116. }