file_name.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. \file lib/gis/file_name.c
  3. \brief GIS library - Determine GRASS data base file name
  4. (C) 2001-2015 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 <stdlib.h>
  11. #include <grass/gis.h>
  12. #include "gis_local_proto.h"
  13. static char *file_name(char *, const char *, const char *,
  14. const char *, const char *, const char *);
  15. static void append_char(char*, char);
  16. /*!
  17. \brief Builds full path names to GIS data files
  18. If <i>name</i> is of the form "nnn@ppp" then path is set as if name
  19. had been "nnn" and mapset had been "ppp" (mapset parameter itself is
  20. ignored in this case).
  21. \param[out] path buffer to hold resultant full path to file
  22. \param element database element (eg, "cell", "cellhd", "vector", etc)
  23. \param name name of file to build path to (fully qualified names allowed)
  24. \param mapset mapset name
  25. \return pointer to <i>path</i> buffer
  26. */
  27. char *G_file_name(char *path,
  28. const char *element, const char *name, const char *mapset)
  29. {
  30. return file_name(path, NULL, element, name, mapset, NULL);
  31. }
  32. /*!
  33. \brief Builds full path names to GIS misc data files
  34. \param[out] path buffer to hold resultant full path to file
  35. \param dir misc directory
  36. \param element database element (eg, "cell", "cellhd", "vector", etc)
  37. \param name name of file to build path to (fully qualified names allowed)
  38. \param mapset mapset name
  39. \return pointer to <i>path</i> buffer
  40. */
  41. char *G_file_name_misc(char *path,
  42. const char *dir,
  43. const char *element,
  44. const char *name, const char *mapset)
  45. {
  46. return file_name(path, dir, element, name, mapset, NULL);
  47. }
  48. /*!
  49. \brief Builds full path names to GIS data files in temporary directory (for internal use only)
  50. By default temporary directory is located
  51. $LOCATION/$MAPSET/.tmp/$HOSTNAME. If GRASS_VECTOR_TMPDIR_MAPSET is
  52. set to "0", the temporary directory is located in TMPDIR
  53. (environmental variable defined by the user or GRASS initialization
  54. script if not given). Note that GRASS_VECTOR_TMPDIR_MAPSET variable
  55. is currently used only by vector library.
  56. \param[out] path buffer to hold resultant full path to file
  57. \param element database element (eg, "cell", "cellhd", "vector", etc)
  58. \param name name of file to build path to (fully qualified names allowed)
  59. \param mapset mapset name
  60. \return pointer to <i>path</i> buffer
  61. */
  62. char *G_file_name_tmp(char *path,
  63. const char *element,
  64. const char *name, const char *mapset)
  65. {
  66. const char *env, *tmp_path;
  67. tmp_path = NULL;
  68. env = getenv("GRASS_VECTOR_TMPDIR_MAPSET");
  69. if (env && strcmp(env, "0") == 0) {
  70. tmp_path = getenv("TMPDIR");
  71. }
  72. return file_name(path, NULL, element, name, mapset, tmp_path);
  73. }
  74. char *file_name(char *path,
  75. const char *dir, const char *element, const char *name,
  76. const char *mapset, const char *base)
  77. {
  78. const char *pname = name;
  79. if (base && *base) {
  80. sprintf(path, "%s", base);
  81. }
  82. else {
  83. char xname[GNAME_MAX];
  84. char xmapset[GMAPSET_MAX];
  85. char *location = G__location_path();
  86. /*
  87. * if a name is given, build a file name
  88. * must split the name into name, mapset if it is
  89. * in the name@mapset format
  90. */
  91. if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) {
  92. pname = xname;
  93. sprintf(path, "%s%c%s", location, HOST_DIRSEP, xmapset);
  94. }
  95. else if (mapset && *mapset)
  96. sprintf(path, "%s%c%s", location, HOST_DIRSEP, mapset);
  97. else
  98. sprintf(path, "%s%c%s", location, HOST_DIRSEP, G_mapset());
  99. G_free(location);
  100. }
  101. if (dir && *dir) { /* misc element */
  102. append_char(path, HOST_DIRSEP);
  103. strcat(path, dir);
  104. if (pname && *pname) {
  105. append_char(path, HOST_DIRSEP);
  106. strcat(path, pname);
  107. }
  108. if (element && *element) {
  109. append_char(path, HOST_DIRSEP);
  110. strcat(path, element);
  111. }
  112. }
  113. else {
  114. if (element && *element) {
  115. append_char(path, HOST_DIRSEP);
  116. strcat(path, element);
  117. }
  118. if (pname && *pname) {
  119. append_char(path, HOST_DIRSEP);
  120. strcat(path, pname);
  121. }
  122. }
  123. G_debug(2, "G_file_name(): path = %s", path);
  124. return path;
  125. }
  126. void append_char(char* s, char c)
  127. {
  128. int len = strlen(s);
  129. s[len] = c;
  130. s[len+1] = '\0';
  131. }