file_name.c 2.6 KB

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