make_mapset.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*!
  2. * \file lib/gis/make_mapset.c
  3. *
  4. * \brief GIS Library - Functions to create a new mapset within an
  5. * existing location
  6. *
  7. * (C) 2006-2013 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public License
  10. * (>=v2). Read the file COPYING that comes with GRASS for details.
  11. *
  12. * \author Joel Pitt, joel.pitt@gmail.com
  13. */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/stat.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. /*!
  21. * \brief Create a new mapset
  22. *
  23. * This function creates a new mapset in the given location,
  24. * initializes default window and the current window.
  25. *
  26. * Calls G_fatal_error() if location doesn't exist.
  27. *
  28. * \param gisdbase_name full path of GISDBASE to create mapset in
  29. * (NULL for the current GISDBASE)
  30. * \param location_name name of location to create mapset in
  31. * (NULL for the current location)
  32. * \param mapset_name Name of the new mapset. Should not include
  33. * the full path, the mapset will be created within
  34. * the specified database and location.
  35. *
  36. * \return 0 on success
  37. * \return -1 to indicate a system error (check errno).
  38. * \return -2 illegal name
  39. */
  40. int G_make_mapset(const char *gisdbase_name, const char *location_name,
  41. const char *mapset_name)
  42. {
  43. char path[GPATH_MAX];
  44. struct Cell_head default_window;
  45. /* Get location */
  46. if (location_name == NULL)
  47. location_name = G_location();
  48. /* Get GISDBASE */
  49. if (gisdbase_name == NULL)
  50. gisdbase_name = G_gisdbase();
  51. /* TODO: Should probably check that user specified location and gisdbase are valid */
  52. /* check if mapset name is legal */
  53. if (G_legal_filename(mapset_name) != 1)
  54. return -2;
  55. /* Check if location exists */
  56. sprintf(path, "%s/%s", gisdbase_name, location_name);
  57. if (access(path, F_OK ) == -1)
  58. G_fatal_error(_("Location <%s> doesn't exist"), location_name);
  59. /* Make the mapset */
  60. sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
  61. if (G_mkdir(path) != 0) {
  62. perror("G_make_mapset");
  63. return -1;
  64. }
  65. G_create_alt_env();
  66. /* Get PERMANENT default window */
  67. G_setenv_nogisrc("GISDBASE", gisdbase_name);
  68. G_setenv_nogisrc("LOCATION", location_name);
  69. G_setenv_nogisrc("MAPSET", "PERMANENT");
  70. G_get_default_window(&default_window);
  71. /* Change to the new mapset */
  72. G_setenv_nogisrc("MAPSET", mapset_name);
  73. /* Copy default window/regions to new mapset */
  74. G_put_element_window(&default_window, "", "WIND");
  75. /* And switch back to original environment */
  76. G_switch_env();
  77. return 0;
  78. }