GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
get_window.c
Go to the documentation of this file.
1 
14 #include <stdlib.h>
15 
16 #include <grass/gis.h>
17 #include <grass/glocale.h>
18 
19 #include "G.h"
20 #include "local_proto.h"
21 
22 static struct state {
23  int initialized;
24  struct Cell_head dbwindow;
25 } state;
26 
27 static struct state *st = &state;
28 
47 void G_get_window(struct Cell_head *window)
48 {
49  const char *regvar;
50 
51  if (G_is_initialized(&st->initialized)) {
52  *window = st->dbwindow;
53  return;
54  }
55 
56  /* Optionally read the region from environment variable */
57  regvar = getenv("GRASS_REGION");
58 
59  if (regvar) {
60  char **tokens = G_tokenize(regvar, ";");
61  G__read_Cell_head_array(tokens, &st->dbwindow, 0);
62  G_free_tokens(tokens);
63  }
64  else {
65  char *wind = getenv("WIND_OVERRIDE");
66  if (wind)
67  G_get_element_window(&st->dbwindow, "windows", wind, G_mapset());
68  else
69  G_get_element_window(&st->dbwindow, "", "WIND", G_mapset());
70  }
71 
72  *window = st->dbwindow;
73 
74  if (!G__.window_set) {
75  G__.window_set = 1;
76  G__.window = st->dbwindow;
77  }
78 
79  G_initialize_done(&st->initialized);
80 }
81 
93 void G_get_default_window(struct Cell_head *window)
94 {
95  G_get_element_window(window, "", "DEFAULT_WIND", "PERMANENT");
96 }
97 
108 void G_get_element_window(struct Cell_head *window,
109  const char *element, const char *name, const char *mapset)
110 {
111  FILE *fp;
112 
113  G_zero(window, sizeof(struct Cell_head));
114 
115  /* Read from file */
116  fp = G_fopen_old(element, name, mapset);
117  if (!fp)
118  G_fatal_error(_("Unable to open element file <%s> for <%s@%s>"),
119  element, name, mapset);
120 
121  G_fseek(fp, 0, SEEK_END);
122  if (!G_ftell(fp))
123  G_fatal_error(_("Region file %s/%s/%s is empty"), mapset, element, name);
124  G_fseek(fp, 0, SEEK_SET);
125  G__read_Cell_head(fp, window, 0);
126  fclose(fp);
127 }
128 
133 {
134  st->initialized = 0;
135  G__.window_set = 0;
136 }
void G_get_window(struct Cell_head *window)
Get the current region.
Definition: get_window.c:47
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: zero.c:23
off_t G_ftell(FILE *fp)
Get the current file position of the stream.
Definition: gis/seek.c:27
void G__read_Cell_head(FILE *fd, struct Cell_head *cellhd, int is_cellhd)
Read cell header (for internal use only)
Definition: rd_cellhd.c:57
int G_is_initialized(int *p)
Definition: counter.c:59
void G_initialize_done(int *p)
Definition: counter.c:76
void G_unset_window()
Unset current region.
Definition: get_window.c:132
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
Definition: lidar.h:89
struct Cell_head window
Definition: G.h:6
Definition: G.h:4
struct state * st
Definition: parser.c:101
void G__read_Cell_head_array(char **array, struct Cell_head *cellhd, int is_cellhd)
Read window from NULL terminated array of strings (for internal use only)
Definition: rd_cellhd.c:97
int window_set
Definition: G.h:7
void G_free_tokens(char **tokens)
Free memory allocated to tokens.
Definition: token.c:204
void G_get_default_window(struct Cell_head *window)
Get the default region.
Definition: get_window.c:93
void G_fseek(FILE *fp, off_t offset, int whence)
Change the file position of the stream.
Definition: gis/seek.c:48
char ** G_tokenize(const char *buf, const char *delim)
Tokenize string.
Definition: token.c:48
void G_get_element_window(struct Cell_head *window, const char *element, const char *name, const char *mapset)
Get region for selected element (raster, vector, window, etc.)
Definition: get_window.c:108
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:240
const char * name
Definition: named_colr.c:7
struct state state
Definition: parser.c:100