file_name.c 4.3 KB

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