GRASS Programmer's Manual 6.4.1(2011)
get_window.c
Go to the documentation of this file.
00001 /*
00002  *************************************************************************
00003  * G_get_window (window)
00004  *     struct Cell_head *window
00005  *
00006  *      read the current mapset window
00007  *      dies if error
00008  *
00009  *************************************************************************
00010  * G_get_default_window (window)
00011  *     struct Cell_head *window
00012  *
00013  *      read the default window for the location
00014  *      dies if error
00015  *
00016  *************************************************************************
00017  * char *
00018  * G__get_window (window, element, name, mapset)
00019  *      read the window 'name' in 'element' in 'mapset'
00020  *      returns NULL if ok, error message if not
00021  ************************************************************************/
00022 
00023 #include <stdlib.h>
00024 #include "G.h"
00025 #include <grass/gis.h>
00026 #include <grass/glocale.h>
00027 
00028 
00047 int G_get_window(struct Cell_head *window)
00048 {
00049     static int first = 1;
00050     static struct Cell_head dbwindow;
00051     char *regvar;
00052 
00053     /* Optionally read the region from environment variable */
00054     regvar = getenv("GRASS_REGION");
00055 
00056     if (regvar) {
00057         char **tokens, *delm = ";";
00058         char *err;
00059 
00060         tokens = G_tokenize(regvar, delm);
00061 
00062         err = G__read_Cell_head_array(tokens, window, 0);
00063 
00064         G_free_tokens(tokens);
00065 
00066         if (err) {
00067             G_fatal_error(_("region for current mapset %s\nrun \"g.region\""),
00068                           err);
00069             G_free(err);
00070         }
00071 
00072         return 1;
00073     }
00074 
00075     if (first) {
00076         char *wind, *err;
00077 
00078         wind = getenv("WIND_OVERRIDE");
00079         if (wind)
00080             err = G__get_window(&dbwindow, "windows", wind, G_mapset());
00081         else
00082             err = G__get_window(&dbwindow, "", "WIND", G_mapset());
00083 
00084         if (err) {
00085             G_fatal_error(_("region for current mapset %s\nrun \"g.region\""),
00086                           err);
00087             G_free(err);
00088         }
00089     }
00090 
00091     first = 0;
00092     G_copy(window, &dbwindow, sizeof(dbwindow));
00093 
00094     if (!G__.window_set) {
00095         G__.window_set = 1;
00096         G_copy(&G__.window, &dbwindow, sizeof(dbwindow));
00097     }
00098 
00099     return 1;
00100 }
00101 
00102 
00115 int G_get_default_window(struct Cell_head *window)
00116 {
00117     char *err;
00118 
00119     if ((err = G__get_window(window, "", "DEFAULT_WIND", "PERMANENT"))) {
00120         G_fatal_error(_("default region %s"), err);
00121         G_free(err);
00122     }
00123     return 1;
00124 }
00125 
00126 char *G__get_window(struct Cell_head *window,
00127                     const char *element, const char *name, const char *mapset)
00128 {
00129     FILE *fd;
00130     char *err;
00131 
00132     G_zero((char *)window, sizeof(struct Cell_head));
00133 
00134     /* Read from file */
00135     if (!(fd = G_fopen_old(element, name, mapset))) {
00136         /*
00137            char path[GPATH_MAX];
00138            G__file_name (path,element,name,mapset);
00139            fprintf (stderr, "G__get_window(%s)\n",path);
00140          */
00141         return G_store(_("is not set"));
00142     }
00143 
00144     err = G__read_Cell_head(fd, window, 0);
00145     fclose(fd);
00146 
00147     if (err) {
00148         char msg[1024];
00149 
00150         sprintf(msg, _("is invalid\n%s"), err);
00151         G_free(err);
00152         return G_store(msg);
00153     }
00154 
00155     return NULL;
00156 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines