location.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*!
  2. \file gis/location.c
  3. \brief GIS library - environment routines (location)
  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 <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. * \brief Get current location name
  19. *
  20. * Returns the name of the current database location. This routine
  21. * should be used by modules that need to display the current location
  22. * to the user. See Locations for an explanation of locations.
  23. *
  24. * \return char* tolocation name
  25. */
  26. const char *G_location(void)
  27. {
  28. return G_getenv("LOCATION_NAME");
  29. }
  30. /*!
  31. * \brief Get current location directory
  32. *
  33. * Returns the full UNIX path name of the current database
  34. * location. For example, if the user is working in location
  35. * <i>spearfish</i> in the <i>/home/user/grassdata</i> database
  36. * directory, this routine will return a string which looks like
  37. * <i>/home/user/grassdata/spearfish</i>.
  38. *
  39. * \return char *
  40. */
  41. char *G_location_path(void)
  42. {
  43. char *location;
  44. location = G__location_path();
  45. if (access(location, F_OK) != 0) {
  46. perror("access");
  47. G_fatal_error(_("LOCATION << %s >> not available"), location);
  48. }
  49. return location;
  50. }
  51. /*!
  52. * \brief Get current location path
  53. *
  54. * \return char* to location path
  55. */
  56. char *G__location_path(void)
  57. {
  58. const char *name = G_location();
  59. const char *base = G_gisdbase();
  60. char *location = G_malloc(strlen(base) + strlen(name) + 2);
  61. sprintf(location, "%s/%s", base, name);
  62. return location;
  63. }