GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71746
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gisinit.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/gisinit.c
3 
4  \brief GIS Library - Handles program initialization.
5 
6  (C) 2001-2008, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author GRASS GIS Development Team
12 */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <locale.h>
22 
23 #include <grass/gis.h>
24 #include <grass/glocale.h>
25 
26 #include "G.h"
27 #include "gis_local_proto.h"
28 
29 struct G__ G__;
30 
31 static int initialized = 0; /** Is set when engine is initialized */
32 static int gisinit(void);
33 
34 /*!
35  \brief Initialize GIS Library and ensures a valid mapset is available.
36 
37  \param version
38  \param pgm program (module) name
39 
40  \return always returns 0 on success
41  \return G_fatal_error() is called on error
42 */
43 void G__gisinit(const char *version, const char *pgm)
44 {
45  const char *mapset;
46 
47  if (initialized)
48  return;
49 
50  G_set_program_name(pgm);
51 
52  if (strcmp(version, GIS_H_VERSION) != 0)
53  G_fatal_error(_("Module built against version %s but "
54  "trying to use version %s. "
55  "You need to rebuild GRASS GIS or untangle multiple installations."),
56  version, GIS_H_VERSION);
57 
58  /* Make sure location and mapset are set */
60  mapset = G_mapset();
61  switch (G_mapset_permissions(mapset)) {
62  case 1:
63  break;
64  case 0:
65  G_fatal_error(_("MAPSET %s - permission denied"), mapset);
66  break;
67  default:
68  G_fatal_error(_("MAPSET %s not found at %s"), mapset, G_location_path());
69  break;
70  }
71 
72  gisinit();
73 }
74 
75 
76 /*!
77  \brief Initialize GIS Library
78 
79  Initializes GIS engine, but does not check for a valid mapset.
80 */
81 void G__no_gisinit(const char *version)
82 {
83  if (initialized)
84  return;
85 
86  if (strcmp(version, GIS_H_VERSION) != 0)
87  G_fatal_error(_("Module built against version %s but "
88  "trying to use version %s. "
89  "You need to rebuild GRASS GIS or untangle multiple installations."),
90  version, GIS_H_VERSION);
91  gisinit();
92 }
93 
94 
95 /*!
96  \brief Checks to see if GIS engine is initialized.
97 */
98 void G__check_gisinit(void)
99 {
100  if (initialized)
101  return;
102  G_warning(_("System not initialized. Programmer forgot to call G_gisinit()."));
103  G_sleep(3);
104  exit(EXIT_FAILURE);
105 }
106 
107 
108 static int gisinit(void)
109 {
110  char *zlib;
111 
112 #ifdef __MINGW32__
113  _fmode = O_BINARY;
114 #endif
115  /* Mark window as not set */
116  G__.window_set = 0;
117 
118  /* byte order */
120 
121  zlib = getenv("GRASS_ZLIB_LEVEL");
122  /* Valid zlib compression levels -1 - 9 */
123  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
124  * level 0 means no compression
125  * as used here, 1 gives the best compromise between speed and compression */
126  G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : 1;
127  if (G__.compression_level < -1 || G__.compression_level > 9)
129 
130  initialized = 1;
131 
132  setlocale(LC_NUMERIC, "C");
133 
134  return 0;
135 }
136 
137 /*!
138  \brief Initialize environment
139 */
140 void G_init_all(void)
141 {
143  G_init_env();
144  G_init_logging();
145  G__init_window();
146  G_init_locale();
147  G_init_debug();
148  G_verbose();
149  G_init_tempfile();
151  G__home();
152  G__machine_name();
153  G_whoami();
156 }
const char * G__home(void)
Get user&#39;s home directory (internal use only)
Definition: home.c:53
void G__no_gisinit(const char *version)
Initialize GIS Library.
Definition: gisinit.c:81
void G_init_debug(void)
Initiate debugging.
Definition: debug.c:27
int G_mapset_permissions(const char *mapset)
Check for user mapset permission.
Definition: mapset_msc.c:145
void G_read_datum_table(void)
Definition: gis/datum.c:146
void G__init_window(void)
Initialize window (region).
void G__gisinit(const char *version, const char *pgm)
Initialize GIS Library and ensures a valid mapset is available.
Definition: gisinit.c:43
void G_set_program_name(const char *s)
Set program name.
Definition: progrm_nme.c:61
int compression_level
Definition: G.h:9
int little_endian
Definition: G.h:8
const char * G_whoami(void)
Gets user&#39;s name.
Definition: gis/whoami.c:35
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
Definition: G.h:4
const char * G__machine_name(void)
Definition: mach_name.c:17
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
void G__get_list_of_mapsets(void)
Fill list of mapsets from search path (internal use only)
Definition: mapset_nme.c:57
void G_sleep(unsigned int seconds)
Definition: sleep.c:11
int window_set
Definition: G.h:7
void G_init_all(void)
Initialize environment.
Definition: gisinit.c:140
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:29
int G_is_little_endian(void)
Tests for little ENDIAN.
Definition: endian.c:24
void G_init_locale(void)
Definition: locale.c:24
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:55
void G_init_logging(void)
Definition: gis/error.c:350
void G_init_env(void)
Initialize variables.
Definition: env.c:83
#define _(str)
Definition: glocale.h:13
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:98
int _fmode
Definition: fmode.c:4
#define GIS_H_VERSION
Definition: gis.h:41
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
char * getenv()
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
int G_read_ellipsoid_table(int fatal)
Read ellipsoid table.
Definition: get_ellipse.c:247