GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 }