file_name.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. \file lib/gis/file_name.c
  3. \brief GIS library - Determine GRASS data base file name
  4. (C) 2001-2008, 2013 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 <grass/gis.h>
  11. #include "gis_local_proto.h"
  12. /*!
  13. \brief Builds full path names to GIS data files
  14. If name is of the form "nnn@ppp" then path is set as if name had
  15. been nnn and mapset had been ppp (mapset parameter itself is ignored
  16. in this case).
  17. \param[out] path buffer to hold resultant full path to file
  18. \param element database element (eg, "cell", "cellhd", etc)
  19. \param name name of file to build path to (fully qualified names allowed)
  20. \param mapset mapset name
  21. \return pointer to <i>path</i> buffer
  22. */
  23. char *G_file_name(char *path,
  24. const char *element, const char *name, const char *mapset)
  25. {
  26. char xname[GNAME_MAX];
  27. char xmapset[GMAPSET_MAX];
  28. const char *pname = name;
  29. char *location = G__location_path();
  30. /*
  31. * if a name is given, build a file name
  32. * must split the name into name, mapset if it is
  33. * in the name@mapset format
  34. */
  35. if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) {
  36. pname = xname;
  37. sprintf(path, "%s/%s", location, xmapset);
  38. }
  39. else if (mapset && *mapset)
  40. sprintf(path, "%s/%s", location, mapset);
  41. else
  42. sprintf(path, "%s/%s", location, G_mapset());
  43. G_free(location);
  44. if (!element && !pname)
  45. return path;
  46. if (element && *element) {
  47. strcat(path, "/");
  48. strcat(path, element);
  49. }
  50. if (pname && *pname) {
  51. strcat(path, "/");
  52. strcat(path, pname);
  53. }
  54. G_debug(2, "G_file_name(): path = %s", path);
  55. return path;
  56. }
  57. char *G_file_name_misc(char *path,
  58. const char *dir,
  59. const char *element,
  60. const char *name, const char *mapset)
  61. {
  62. char xname[GNAME_MAX];
  63. char xmapset[GMAPSET_MAX];
  64. const char *pname = name;
  65. char *location = G__location_path();
  66. /*
  67. * if a name is given, build a file name
  68. * must split the name into name, mapset if it is
  69. * in the name@mapset format
  70. */
  71. if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) {
  72. pname = xname;
  73. sprintf(path, "%s/%s", location, xmapset);
  74. }
  75. else if (mapset && *mapset)
  76. sprintf(path, "%s/%s", location, mapset);
  77. else
  78. sprintf(path, "%s/%s", location, G_mapset());
  79. G_free(location);
  80. if (dir && *dir) {
  81. strcat(path, "/");
  82. strcat(path, dir);
  83. }
  84. if (pname && *pname) {
  85. strcat(path, "/");
  86. strcat(path, pname);
  87. }
  88. if (element && *element) {
  89. strcat(path, "/");
  90. strcat(path, element);
  91. }
  92. return path;
  93. }