nme_in_mps.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. \file nme_in_mps.c
  3. \brief GIS Library - check 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 Original author CERL
  10. */
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. /*!
  14. \brief Check if map name is fully qualified (map @ mapset)
  15. Note:
  16. - <b>name</b> is char array of size GNAME_MAX
  17. - <b>mapset</b> is char array of size GMAPSET_MAX
  18. \param fullname full map name
  19. \param[out] name map name
  20. \param[out] mapset mapset name
  21. \return 1 if input map name is fully qualified
  22. \return 0 if input map name is not fully qualified
  23. */
  24. int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
  25. {
  26. const char *p;
  27. char *q;
  28. /* search for name@mapset */
  29. *name = *mapset = 0;
  30. for (p = fullname; *p; p++)
  31. if (*p == '@')
  32. break;
  33. if (*p == 0)
  34. return 0;
  35. /* copy the name part */
  36. q = name;
  37. while (fullname != p)
  38. *q++ = *fullname++;
  39. *q = 0;
  40. /* copy the mapset part */
  41. p++; /* skip the @ */
  42. q = mapset;
  43. while ((*q++ = *p++)) ;
  44. return (*name && *mapset);
  45. }
  46. /*!
  47. \brief fully qualified file name
  48. Returns a fully qualified name for the file <b>name</b> in
  49. <b>mapset.</b> Currently this string is in the form <i>name @ mapset</i>,
  50. but the programmer should pretend not to know this and always call this
  51. routine to get the fully qualified name.
  52. The following example shows how an interactive version of <i>d.rast</i>
  53. interfaces with the command-line version of <i>d.rast</i>:
  54. \code
  55. #include "gis.h"
  56. int main(char *argc, char **argv)
  57. {
  58. char name[GNAME_MAX], *mapset, *fqn;
  59. char command[1024];
  60. G_gisinit(argv[0]);
  61. mapset = G_ask_cell_old ("", name, "");
  62. if (mapset == NULL) exit(EXIT_SUCCESS);
  63. fqn = G_fully_qualified_name (name, mapset);
  64. sprintf (command, "d.rast map='%s'", fqn);
  65. system(command);
  66. }
  67. \endcode
  68. \param name map name
  69. \param mapset mapset name
  70. \return pointer to full map name (map @ mapset)
  71. */
  72. char *G_fully_qualified_name(const char *name, const char *mapset)
  73. {
  74. char fullname[GNAME_MAX + GMAPSET_MAX];
  75. if (strchr(name, '@'))
  76. sprintf(fullname, "%s", name);
  77. else
  78. sprintf(fullname, "%s@%s", name, mapset);
  79. return G_store(fullname);
  80. }
  81. int G__unqualified_name(const char *name, const char *mapset,
  82. char *xname, char *xmapset)
  83. {
  84. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  85. if (mapset && *mapset && strcmp(mapset, xmapset) != 0)
  86. return -1;
  87. return 1;
  88. }
  89. strcpy(xname, name);
  90. strcpy(xmapset, mapset);
  91. return 0;
  92. }