nme_in_mps.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*!
  2. \file lib/gis/nme_in_mps.c
  3. \brief GIS Library - check map name
  4. (C) 2001-2009, 2013 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 Original author CERL
  8. */
  9. #include <string.h>
  10. #include <grass/gis.h>
  11. /*!
  12. \brief Check if map name is fully qualified (map @ mapset)
  13. Returns a fully qualified name for the file <i>name</i> in
  14. <i>mapset</i>. Currently this string is in the form
  15. <i>name@mapset</i>, but the programmer should pretend not to know this
  16. and always call this routine to get the fully qualified name.
  17. Note:
  18. - <i>name</i> is char array of size GNAME_MAX
  19. - <i>mapset</i> is char array of size GMAPSET_MAX
  20. \param fullname full map name
  21. \param[out] name map name
  22. \param[out] mapset mapset name
  23. \return 1 if input map name is fully qualified
  24. \return 0 if input map name is not fully qualified
  25. */
  26. int G_name_is_fully_qualified(const char *fullname, char *name, char *mapset)
  27. {
  28. const char *p;
  29. char *q;
  30. /* search for name@mapset */
  31. *name = *mapset = 0;
  32. for (p = fullname; *p; p++)
  33. if (*p == '@')
  34. break;
  35. if (*p == 0)
  36. return 0;
  37. /* copy the name part */
  38. q = name;
  39. while (fullname != p)
  40. *q++ = *fullname++;
  41. *q = 0;
  42. /* copy the mapset part */
  43. p++; /* skip the @ */
  44. q = mapset;
  45. while ((*q++ = *p++)) ;
  46. return (*name && *mapset);
  47. }
  48. /*!
  49. \brief Get fully qualified element name
  50. Returns a fully qualified name for GIS element <i>name</i> in
  51. <i>mapset</i>. Currently this string is in the form
  52. <b>name@mapset</b>, but the programmer should pretend not to know
  53. this and always call this routine to get the fully qualified name.
  54. String is allocated by G_store().
  55. \code
  56. #include <grass/gis.h>
  57. int main(char *argc, char **argv)
  58. {
  59. char name[GNAME_MAX], *mapset, *fqn;
  60. char command[1024];
  61. G_gisinit(argv[0]);
  62. mapset = G_find_rast(name, "");
  63. if (mapset == NULL)
  64. exit(EXIT_SUCCESS);
  65. fqn = G_fully_qualified_name (name, mapset);
  66. printf (stdout, "map='%s'", fqn);
  67. exit(EXIT_SUCCESS);
  68. }
  69. \endcode
  70. \param name element name
  71. \param mapset mapset name
  72. \return pointer to full element name (map@mapset)
  73. */
  74. char *G_fully_qualified_name(const char *name, const char *mapset)
  75. {
  76. char fullname[GNAME_MAX + GMAPSET_MAX];
  77. if (strchr(name, '@') || strlen(mapset) < 1) {
  78. sprintf(fullname, "%s", name);
  79. }
  80. else {
  81. sprintf(fullname, "%s@%s", name, mapset);
  82. }
  83. return G_store(fullname);
  84. }
  85. /*!
  86. \brief Returns unqualified map name (without @ mapset)
  87. Returns an unqualified name for the file <i>name</i> in
  88. <i>mapset</i>.
  89. Note:
  90. - <i>name, xname</i> are char array of size GNAME_MAX
  91. - <i>mapset, xmapset</i> are char array of size GMAPSET_MAX
  92. \param name map name
  93. \param mapset mapset to check or NULL
  94. \param[out] xname map name
  95. \param[out] xmapset mapset name
  96. \return 1 if input map name is fully qualified
  97. \return 0 if name is not fully qualified
  98. \return -1 if input mapset invalid (mapset != xmapset)
  99. */
  100. int G_unqualified_name(const char *name, const char *mapset,
  101. char *xname, char *xmapset)
  102. {
  103. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  104. /* name is fully qualified */
  105. if (mapset && *mapset && strcmp(mapset, xmapset) != 0)
  106. return -1;
  107. return 1;
  108. }
  109. /* name is not fully qualified */
  110. strcpy(xname, name);
  111. if (mapset)
  112. strcpy(xmapset, mapset);
  113. else
  114. xmapset[0] = '\0';
  115. return 0;
  116. }