nme_in_mps.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*!
  2. \file gis/nme_in_mps.c
  3. \brief GIS Library - check 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 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 fully qualified file name
  50. Returns a fully qualified name for the file <b>name</b> in
  51. <b>mapset.</b> Currently this string is in the form <i>name @ mapset</i>,
  52. but the programmer should pretend not to know this and always call this
  53. routine to get the fully qualified name.
  54. The following example shows how an interactive version of <i>d.rast</i>
  55. interfaces with the command-line version of <i>d.rast</i>:
  56. \code
  57. #include "gis.h"
  58. int main(char *argc, char **argv)
  59. {
  60. char name[GNAME_MAX], *mapset, *fqn;
  61. char command[1024];
  62. G_gisinit(argv[0]);
  63. mapset = G_ask_cell_old ("", name, "");
  64. if (mapset == NULL) exit(EXIT_SUCCESS);
  65. fqn = G_fully_qualified_name (name, mapset);
  66. sprintf (command, "d.rast map='%s'", fqn);
  67. system(command);
  68. }
  69. \endcode
  70. \param name map name
  71. \param mapset mapset name
  72. \return pointer to full map 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, '@'))
  78. sprintf(fullname, "%s", name);
  79. else
  80. sprintf(fullname, "%s@%s", name, mapset);
  81. return G_store(fullname);
  82. }
  83. /*!
  84. \brief Returns unqualified map name (without @ mapset)
  85. Returns an unqualified name for the file <i>name</i> in
  86. <i>mapset</i>.
  87. Note:
  88. - <i>name, xname</i> are char array of size GNAME_MAX
  89. - <i>mapset, xmapset</i> are char array of size GMAPSET_MAX
  90. \param fullname map name
  91. \param fullname map mapset
  92. \param[out] name map name
  93. \param[out] mapset mapset name
  94. \return 1 if input map name is fully qualified
  95. \return 0 if ...
  96. \return -1 if input mapset invalid
  97. */
  98. int G_unqualified_name(const char *name, const char *mapset,
  99. char *xname, char *xmapset)
  100. {
  101. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  102. if (mapset && *mapset && strcmp(mapset, xmapset) != 0)
  103. return -1;
  104. return 1;
  105. }
  106. strcpy(xname, name);
  107. strcpy(xmapset, mapset);
  108. return 0;
  109. }