GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
get_window.c
Go to the documentation of this file.
1 /*
2  *************************************************************************
3  * G_get_window (window)
4  * struct Cell_head *window
5  *
6  * read the current mapset window
7  * dies if error
8  *
9  *************************************************************************
10  * G_get_default_window (window)
11  * struct Cell_head *window
12  *
13  * read the default window for the location
14  * dies if error
15  *
16  *************************************************************************
17  * char *
18  * G__get_window (window, element, name, mapset)
19  * read the window 'name' in 'element' in 'mapset'
20  * returns NULL if ok, error message if not
21  ************************************************************************/
22 
23 #include <stdlib.h>
24 #include "G.h"
25 #include <grass/gis.h>
26 #include <grass/glocale.h>
27 
28 
47 int G_get_window(struct Cell_head *window)
48 {
49  static int first = 1;
50  static struct Cell_head dbwindow;
51  char *regvar;
52 
53  /* Optionally read the region from environment variable */
54  regvar = getenv("GRASS_REGION");
55 
56  if (regvar) {
57  char **tokens, *delm = ";";
58  char *err;
59 
60  tokens = G_tokenize(regvar, delm);
61 
62  err = G__read_Cell_head_array(tokens, window, 0);
63 
64  G_free_tokens(tokens);
65 
66  if (err) {
67  G_fatal_error(_("region for current mapset %s\nrun \"g.region\""),
68  err);
69  G_free(err);
70  }
71 
72  return 1;
73  }
74 
75  if (first) {
76  char *wind, *err;
77 
78  wind = getenv("WIND_OVERRIDE");
79  if (wind)
80  err = G__get_window(&dbwindow, "windows", wind, G_mapset());
81  else
82  err = G__get_window(&dbwindow, "", "WIND", G_mapset());
83 
84  if (err) {
85  G_fatal_error(_("region for current mapset %s\nrun \"g.region\""),
86  err);
87  G_free(err);
88  }
89  }
90 
91  first = 0;
92  G_copy(window, &dbwindow, sizeof(dbwindow));
93 
94  if (!G__.window_set) {
95  G__.window_set = 1;
96  G_copy(&G__.window, &dbwindow, sizeof(dbwindow));
97  }
98 
99  return 1;
100 }
101 
102 
115 int G_get_default_window(struct Cell_head *window)
116 {
117  char *err;
118 
119  if ((err = G__get_window(window, "", "DEFAULT_WIND", "PERMANENT"))) {
120  G_fatal_error(_("default region %s"), err);
121  G_free(err);
122  }
123  return 1;
124 }
125 
126 char *G__get_window(struct Cell_head *window,
127  const char *element, const char *name, const char *mapset)
128 {
129  FILE *fd;
130  char *err;
131 
132  G_zero((char *)window, sizeof(struct Cell_head));
133 
134  /* Read from file */
135  if (!(fd = G_fopen_old(element, name, mapset))) {
136  /*
137  char path[GPATH_MAX];
138  G__file_name (path,element,name,mapset);
139  fprintf (stderr, "G__get_window(%s)\n",path);
140  */
141  return G_store(_("is not set"));
142  }
143 
144  err = G__read_Cell_head(fd, window, 0);
145  fclose(fd);
146 
147  if (err) {
148  char msg[1024];
149 
150  sprintf(msg, _("is invalid\n%s"), err);
151  G_free(err);
152  return G_store(msg);
153  }
154 
155  return NULL;
156 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
int G_free_tokens(char **tokens)
Free memory allocated to tokens.
Definition: gis/token.c:98
char * G__read_Cell_head_array(char **array, struct Cell_head *cellhd, int is_cellhd)
Definition: rd_cellhd.c:89
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
int G_copy(void *a, const void *b, int n)
Copies n bytes starting at address b into address a.
Definition: gis/copy.c:30
int G_get_default_window(struct Cell_head *window)
read the default region
Definition: get_window.c:115
char ** G_tokenize(const char *buf, const char *delim)
Tokenize string.
Definition: gis/token.c:33
const char * err
Definition: g3dcolor.c:50
char * getenv()
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
struct Cell_head window
Definition: G.h:78
Definition: G.h:74
tuple window
Definition: tools.py:543
int window_set
Definition: G.h:79
int G_get_window(struct Cell_head *window)
read the database region
Definition: get_window.c:47
int first
Definition: form/open.c:25
char * G__get_window(struct Cell_head *window, const char *element, const char *name, const char *mapset)
Definition: get_window.c:126
return NULL
Definition: dbfopen.c:1394
tuple msg
Definition: wxnviz.py:32
char * G__read_Cell_head(FILE *fd, struct Cell_head *cellhd, int is_cellhd)
Definition: rd_cellhd.c:53
fclose(fd)
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.