GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-3df7050671
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 #if 0
30 #ifdef GRASS_CMAKE_BUILD
31 #include <export/grass_gis_export.h>
32 #else
33 #define GRASS_GIS_EXPORT
34 #endif
35 #endif
36 
38 
39 /** initialized is set to 1 when engine is initialized */
40 /* GRASS_GIS_EXPORT static int initialized on windows msvc throws below error.
41 "Error C2201 'initialized': must have external linkage in order to be
42  exported/imported"
43 So we do an ifndef on msvc. without GRASS_GIS_EXPORT it will be exported in DLL.
44 */
45 #ifndef _MSC_VER
46 static int initialized = 0;
47 #else
48 GRASS_GIS_EXPORT int initialized;
49 #endif
50 
51 static int gisinit(void);
52 
53 /*!
54  \brief Initialize GIS Library and ensures a valid mapset is available.
55 
56  \param version
57  \param pgm program (module) name
58 
59  \return always returns 0 on success
60  \return G_fatal_error() is called on error
61  */
62 void G__gisinit(const char *version, const char *pgm)
63 {
64  const char *mapset;
65 
66  if (initialized)
67  return;
68 
69  G_set_program_name(pgm);
70 
71  /* verify version of GRASS headers (and anything else in include) */
72  if (strcmp(version, GIS_H_VERSION) != 0) {
73  char *envstr;
74  char *answer = "0";
75 
76  envstr = getenv("GRASS_COMPATIBILITY_TEST");
77  if (envstr && *envstr && strcmp(envstr, answer) == 0) {
78  G_warning(_("Module built against version %s but "
79  "trying to use version %s. "
80  "In case of errors you need to rebuild the module "
81  "against GRASS GIS version %s."),
83  }
84  else {
86  _("Module built against version %s but "
87  "trying to use version %s. "
88  "You need to rebuild GRASS GIS or untangle multiple "
89  "installations."),
90  version, GIS_H_VERSION);
91  }
92  }
93 
94  /* Make sure location and mapset are set */
96  mapset = G_mapset();
97  switch (G_mapset_permissions(mapset)) {
98  case 1:
99  break;
100  case 0:
101  G_fatal_error(_("MAPSET %s - permission denied"), mapset);
102  break;
103  default:
104  G_fatal_error(_("MAPSET %s not found at %s"), mapset,
105  G_location_path());
106  break;
107  }
108 
109  gisinit();
110 }
111 
112 /*!
113  \brief Initialize GIS Library
114 
115  Initializes GIS engine, but does not check for a valid mapset.
116  */
117 void G__no_gisinit(const char *version)
118 {
119  if (initialized)
120  return;
121 
122  /* verify version of GRASS headers (and anything else in include) */
123  if (strcmp(version, GIS_H_VERSION) != 0) {
124  char *envstr;
125  char *answer = "0";
126 
127  envstr = getenv("GRASS_COMPATIBILITY_TEST");
128  if (envstr && *envstr && strcmp(envstr, answer) == 0) {
129  G_warning(_("Module built against version %s but "
130  "trying to use version %s. "
131  "In case of errors you need to rebuild the module "
132  "against GRASS GIS version %s."),
134  }
135  else {
137  _("Module built against version %s but "
138  "trying to use version %s. "
139  "You need to rebuild GRASS GIS or untangle multiple "
140  "installations."),
141  version, GIS_H_VERSION);
142  }
143  }
144  gisinit();
145 }
146 
147 /*!
148  \brief Checks to see if GIS engine is initialized.
149  */
151 {
152  if (initialized)
153  return;
154  G_warning(
155  _("System not initialized. Programmer forgot to call G_gisinit()."));
156  G_sleep(3);
157  exit(EXIT_FAILURE);
158 }
159 
160 static int gisinit(void)
161 {
162  char *zlib;
163 
164 #if defined(_MSC_VER) || defined(__MINGW32__)
165  _fmode = O_BINARY;
166 #endif
167  /* Mark window as not set */
168  G__.window_set = 0;
169 
170  /* byte order */
172 
173  zlib = getenv("GRASS_ZLIB_LEVEL");
174  /* Valid zlib compression levels -1 - 9 */
175  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
176  * level 0 means no compression
177  * as used here, 1 gives the best compromise between speed and compression
178  */
179  G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : 1;
180  if (G__.compression_level < -1 || G__.compression_level > 9)
182 
183  initialized = 1;
184 
185  setlocale(LC_NUMERIC, "C");
186 
187  return 0;
188 }
189 
190 /*!
191  \brief Initialize environment
192  */
193 void G_init_all(void)
194 {
196  G_init_env();
197  G_init_logging();
198  G__init_window();
199  G_init_locale();
200  G_init_debug();
201  G_verbose();
202  G_init_tempfile();
204  G__home();
205  G__machine_name();
206  G_whoami();
209 }
#define GRASS_GIS_EXPORT
Definition: G.h:7
int G_mapset_permissions(const char *)
Check for user mapset permission.
Definition: mapset_msc.c:291
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void int G_is_little_endian(void)
Tests for little ENDIAN.
Definition: endian.c:24
const char * G_whoami(void)
Gets user's name.
Definition: gis/whoami.c:34
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void G_read_datum_table(void)
Definition: gis/datum.c:141
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:60
void G_sleep(unsigned int)
Definition: sleep.c:11
void G_set_program_name(const char *)
Set program name.
Definition: progrm_nme.c:61
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
void G__init_window(void)
Initialize window (region).
int G_read_ellipsoid_table(int)
Read ellipsoid table.
Definition: get_ellipse.c:246
void G_init_env(void)
Initialize variables.
Definition: env.c:83
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:29
void G_init_logging(void)
Definition: gis/error.c:355
void G_init_debug(void)
Initiate debugging.
Definition: debug.c:27
void G_init_locale(void)
Definition: locale.c:29
Header file for msvc/open.c and msvc/creat.c.
int _fmode
Definition: fmode.c:4
#define GIS_H_VERSION
Definition: gis.h:66
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:150
void G_init_all(void)
Initialize environment.
Definition: gisinit.c:193
void G__no_gisinit(const char *version)
Initialize GIS Library.
Definition: gisinit.c:117
void G__gisinit(const char *version, const char *pgm)
Initialize GIS Library and ensures a valid mapset is available.
Definition: gisinit.c:62
#define _(str)
Definition: glocale.h:10
const char * G__home(void)
Get user's home directory (internal use only)
Definition: home.c:53
const char * G__machine_name(void)
Definition: mach_name.c:17
void G__get_list_of_mapsets(void)
Fill list of mapsets from search path (internal use only)
Definition: mapset_nme.c:57
Definition: G.h:11
int compression_level
Definition: G.h:15
int little_endian
Definition: G.h:14
int window_set
Definition: G.h:13
#define GRASS_VERSION_STRING
Definition: version.h:1