file_name.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**********************************************************************
  2. * char *
  3. * G__file_name (path, element, name, maps)
  4. * char path[] buffer to hold resultant full path to file.
  5. * const char *element database element (eg, "cell", "cellhd", etc)
  6. * const char *name name of file to build path to
  7. * const char *mapset mapset name
  8. *
  9. * builds full path names to GIS data files
  10. *
  11. * returns:
  12. * pointer to 'path'
  13. *
  14. * note:
  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. *********************************************************************/
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. char *G__file_name(char *path,
  22. const char *element, const char *name, const char *mapset)
  23. {
  24. char xname[GNAME_MAX];
  25. char xmapset[GMAPSET_MAX];
  26. const char *pname = name;
  27. char *location = G__location_path();
  28. /*
  29. * if a name is given, build a file name
  30. * must split the name into name, mapset if it is
  31. * in the name@mapset format
  32. */
  33. if (name && *name && G__name_is_fully_qualified(name, xname, xmapset)) {
  34. pname = xname;
  35. sprintf(path, "%s/%s", location, xmapset);
  36. }
  37. else if (mapset && *mapset)
  38. sprintf(path, "%s/%s", location, mapset);
  39. else
  40. sprintf(path, "%s/%s", location, G_mapset());
  41. G_free(location);
  42. if (element && *element) {
  43. strcat(path, "/");
  44. strcat(path, element);
  45. }
  46. if (pname && *pname) {
  47. strcat(path, "/");
  48. strcat(path, pname);
  49. }
  50. return path;
  51. }
  52. char *G__file_name_misc(char *path,
  53. const char *dir,
  54. const char *element,
  55. const char *name, const char *mapset)
  56. {
  57. char xname[GNAME_MAX];
  58. char xmapset[GMAPSET_MAX];
  59. const char *pname = name;
  60. char *location = G__location_path();
  61. /*
  62. * if a name is given, build a file name
  63. * must split the name into name, mapset if it is
  64. * in the name@mapset format
  65. */
  66. if (name && *name && G__name_is_fully_qualified(name, xname, xmapset)) {
  67. pname = xname;
  68. sprintf(path, "%s/%s", location, xmapset);
  69. }
  70. else if (mapset && *mapset)
  71. sprintf(path, "%s/%s", location, mapset);
  72. else
  73. sprintf(path, "%s/%s", location, G_mapset());
  74. G_free(location);
  75. if (dir && *dir) {
  76. strcat(path, "/");
  77. strcat(path, dir);
  78. }
  79. if (pname && *pname) {
  80. strcat(path, "/");
  81. strcat(path, pname);
  82. }
  83. if (element && *element) {
  84. strcat(path, "/");
  85. strcat(path, element);
  86. }
  87. return path;
  88. }