file_name.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. Paths to files are currently in form:
  22. /path/to/location/mapset/element/name
  23. path input buffer memory must be allocated by caller.
  24. C:
  25. @code{.c}
  26. char path[GPATH_MAX];
  27. G_file_name(path, "fcell", "my_raster", "my_mapset");
  28. // path now is "/full/path/to/my_mapset/fcell/my_raster"
  29. @endcode
  30. Python:
  31. @code{.py}
  32. import ctypes
  33. from grass.pygrass.utils import decode
  34. from grass.lib.gis import G_file_name, GPATH_MAX
  35. path = ctypes.create_string_buffer(GPATH_MAX)
  36. path_str = decode(G_file_name(path, "elem", "name", "mapset"))
  37. print(path_str)
  38. >>> /full/path/to/mapset/elem/name
  39. @endcode
  40. \param[out] path allocated buffer to hold resultant full path to file
  41. \param element database element (eg, "cell", "cellhd", "vector", etc)
  42. \param name name of file to build path to (fully qualified names allowed)
  43. \param mapset mapset name
  44. \return pointer to <i>path</i> buffer
  45. */
  46. char *G_file_name(char *path,
  47. const char *element, const char *name, const char *mapset)
  48. {
  49. return file_name(path, NULL, element, name, mapset, NULL);
  50. }
  51. /*!
  52. \brief Builds full path names to GIS misc data files
  53. Paths to misc files are currently in form:
  54. /path/to/location/mapset/dir/name/element
  55. path input buffer memory must be allocated by caller.
  56. C:
  57. @code{.c}
  58. char path[GPATH_MAX];
  59. G_file_name_misc(path, "cell_misc", "history", "my_raster", "my_mapset");
  60. // path now contains "/full/path/to/my_mapset/cell_misc/my_raster/history"
  61. @endcode
  62. Python:
  63. @code{.py}
  64. import ctypes
  65. from grass.pygrass.utils import decode
  66. from grass.lib.gis import G_file_name_misc, GPATH_MAX
  67. path = ctypes.create_string_buffer(GPATH_MAX)
  68. path_str = decode(G_file_name_misc(path, "dir", "elem", "name", "mapset"))
  69. print(path_str)
  70. >>> /full/path/to/mapset/dir/name/elem
  71. @endcode
  72. \param[out] path allocated buffer to hold resultant full path to file
  73. \param dir misc directory (e.g., "cell_misc", "group")
  74. \param element database element (in this case – file to build path to e.g., "history", "REF")
  75. \param name name of object (raster, group; fully qualified names allowed e.g., "my_raster@PERMANENT")
  76. \param mapset mapset name
  77. \return pointer to <i>path</i> buffer
  78. */
  79. char *G_file_name_misc(char *path,
  80. const char *dir,
  81. const char *element,
  82. const char *name, const char *mapset)
  83. {
  84. return file_name(path, dir, element, name, mapset, NULL);
  85. }
  86. /*!
  87. \brief Builds full path names to GIS data files in temporary directory (for internal use only)
  88. By default temporary directory is located
  89. $LOCATION/$MAPSET/.tmp/$HOSTNAME. If GRASS_VECTOR_TMPDIR_MAPSET is
  90. set to "0", the temporary directory is located in TMPDIR
  91. (environmental variable defined by the user or GRASS initialization
  92. script if not given). Note that GRASS_VECTOR_TMPDIR_MAPSET variable
  93. is currently used only by vector library.
  94. \param[out] path buffer to hold resultant full path to file
  95. \param element database element (eg, "cell", "cellhd", "vector", etc)
  96. \param name name of file to build path to (fully qualified names allowed)
  97. \param mapset mapset name
  98. \return pointer to <i>path</i> buffer
  99. */
  100. char *G_file_name_tmp(char *path,
  101. const char *element,
  102. const char *name, const char *mapset)
  103. {
  104. const char *env, *tmp_path;
  105. tmp_path = NULL;
  106. env = getenv("GRASS_VECTOR_TMPDIR_MAPSET");
  107. if (env && strcmp(env, "0") == 0) {
  108. tmp_path = getenv("TMPDIR");
  109. }
  110. return file_name(path, NULL, element, name, mapset, tmp_path);
  111. }
  112. char *file_name(char *path,
  113. const char *dir, const char *element, const char *name,
  114. const char *mapset, const char *base)
  115. {
  116. const char *pname = name;
  117. if (base && *base) {
  118. sprintf(path, "%s", base);
  119. }
  120. else {
  121. char xname[GNAME_MAX];
  122. char xmapset[GMAPSET_MAX];
  123. char *location = G__location_path();
  124. /*
  125. * if a name is given, build a file name
  126. * must split the name into name, mapset if it is
  127. * in the name@mapset format
  128. */
  129. if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) {
  130. pname = xname;
  131. sprintf(path, "%s%c%s", location, HOST_DIRSEP, xmapset);
  132. }
  133. else if (mapset && *mapset)
  134. sprintf(path, "%s%c%s", location, HOST_DIRSEP, mapset);
  135. else
  136. sprintf(path, "%s%c%s", location, HOST_DIRSEP, G_mapset());
  137. G_free(location);
  138. }
  139. if (dir && *dir) { /* misc element */
  140. append_char(path, HOST_DIRSEP);
  141. strcat(path, dir);
  142. if (pname && *pname) {
  143. append_char(path, HOST_DIRSEP);
  144. strcat(path, pname);
  145. }
  146. if (element && *element) {
  147. append_char(path, HOST_DIRSEP);
  148. strcat(path, element);
  149. }
  150. }
  151. else {
  152. if (element && *element) {
  153. append_char(path, HOST_DIRSEP);
  154. strcat(path, element);
  155. }
  156. if (pname && *pname) {
  157. append_char(path, HOST_DIRSEP);
  158. strcat(path, pname);
  159. }
  160. }
  161. G_debug(2, "G_file_name(): path = %s", path);
  162. return path;
  163. }
  164. void append_char(char* s, char c)
  165. {
  166. int len = strlen(s);
  167. s[len] = c;
  168. s[len+1] = '\0';
  169. }