file_name.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  49. By default temporary directory is located
  50. $LOCATION/$MAPSET/.tmp/$HOSTNAME. If GRASS_TMPDIR_MAPSET is set to
  51. "0", the temporary directory is located in TMPDIR (environmental
  52. variable defined by the user or GRASS initialization script if not
  53. given).
  54. \param[out] path buffer to hold resultant full path to file
  55. \param element database element (eg, "cell", "cellhd", "vector", etc)
  56. \param name name of file to build path to (fully qualified names allowed)
  57. \param mapset mapset name
  58. \return pointer to <i>path</i> buffer
  59. */
  60. char *G_file_name_tmp(char *path,
  61. const char *element,
  62. const char *name, const char *mapset)
  63. {
  64. const char *env, *tmp_path;
  65. tmp_path = NULL;
  66. env = getenv("GRASS_TMPDIR_MAPSET");
  67. if (env && strcmp(env, "0") == 0) {
  68. tmp_path = getenv("TMPDIR");
  69. }
  70. return file_name(path, NULL, element, name, mapset, tmp_path);
  71. }
  72. char *file_name(char *path,
  73. const char *dir, const char *element, const char *name,
  74. const char *mapset, const char *base)
  75. {
  76. const char *pname = name;
  77. if (base && *base) {
  78. sprintf(path, "%s", base);
  79. }
  80. else {
  81. char xname[GNAME_MAX];
  82. char xmapset[GMAPSET_MAX];
  83. char *location = G__location_path();
  84. /*
  85. * if a name is given, build a file name
  86. * must split the name into name, mapset if it is
  87. * in the name@mapset format
  88. */
  89. if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) {
  90. pname = xname;
  91. sprintf(path, "%s/%s", location, xmapset);
  92. }
  93. else if (mapset && *mapset)
  94. sprintf(path, "%s/%s", location, mapset);
  95. else
  96. sprintf(path, "%s/%s", location, G_mapset());
  97. G_free(location);
  98. }
  99. if (dir && *dir) { /* misc element */
  100. strcat(path, "/");
  101. strcat(path, dir);
  102. if (pname && *pname) {
  103. strcat(path, "/");
  104. strcat(path, pname);
  105. }
  106. if (element && *element) {
  107. strcat(path, "/");
  108. strcat(path, element);
  109. }
  110. }
  111. else {
  112. if (element && *element) {
  113. strcat(path, "/");
  114. strcat(path, element);
  115. }
  116. if (pname && *pname) {
  117. strcat(path, "/");
  118. strcat(path, pname);
  119. }
  120. }
  121. G_debug(2, "G_file_name(): path = %s", path);
  122. return path;
  123. }