GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
make_mapset.c
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * Project: libgrass
5  * Purpose: Function to create a new mapset within an existing location
6  * Author(s): Joel Pitt, joel.pitt@gmail.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2006, Joel Pitt
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  ******************************************************************************
25  *
26  */
27 
28 #include <grass/gis.h>
29 
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 
35 /*
36  * Returns 0 on success.
37  * Returns -1 to indicate a system error (check errno).
38  */
39 
40 
41 int G__make_mapset(const char *gisdbase_name, const char *location_name,
42  const char *mapset_name)
43 {
44  char path[GPATH_MAX];
45  struct Cell_head default_window;
46 
47  /* Get location */
48  if (location_name == NULL)
49  location_name = G_location();
50 
51  /* Get GISDBASE */
52  if (gisdbase_name == NULL)
53  gisdbase_name = G_gisdbase();
54 
55  /* TODO: Should probably check that user specified location and gisdbase are valid */
56 
57  /* Make the mapset. */
58  sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name);
59  if (G_mkdir(path) != 0)
60  return -1;
61 
63 
64  /* Get PERMANENT default window */
65  G__setenv("GISDBASE", gisdbase_name);
66  G__setenv("LOCATION", location_name);
67  G__setenv("MAPSET", "PERMANENT");
68  G_get_default_window(&default_window);
69 
70  /* Change to the new mapset */
71  G__setenv("MAPSET", mapset_name);
72 
73  /* Copy default window/regions to new mapset */
74  G__put_window(&default_window, "", "WIND");
75 
76  /* And switch back to original environment */
77  G__switch_env();
78 
79  return 0;
80 }
81 
82 
105 int G_make_mapset(const char *gisdbase_name, const char *location_name,
106  const char *mapset_name)
107 {
108  int err;
109 
110  err = G__make_mapset(gisdbase_name, location_name, mapset_name);
111 
112  if (err == 0)
113  return 0;
114 
115  if (err == -1) {
116  perror("G_make_mapset");
117  }
118 
119  G_fatal_error("G_make_mapset failed.");
120 
121  return 1;
122 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:17
int G__make_mapset(const char *gisdbase_name, const char *location_name, const char *mapset_name)
Definition: make_mapset.c:41
int G_get_default_window(struct Cell_head *window)
read the default region
Definition: get_window.c:115
const char * err
Definition: g3dcolor.c:50
char * G_gisdbase(void)
Get name of top level database directory.
Definition: gisdbase.c:29
char * G_location(void)
Get current location name.
Definition: location.c:61
int G_make_mapset(const char *gisdbase_name, const char *location_name, const char *mapset_name)
create a new mapset
Definition: make_mapset.c:105
return NULL
Definition: dbfopen.c:1394
int G__setenv(const char *name, const char *value)
Set environment name to value.
Definition: env.c:388
int G__put_window(const struct Cell_head *window, char *dir, char *name)
Definition: put_window.c:40
int G__create_alt_env(void)
Set up alternative environment variables.
Definition: env.c:537
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G__switch_env(void)
Switch environments.
Definition: env.c:561