GRASS GIS 8 Programmer's Manual  8.3.1(2023)-exported
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  /* verify version of GRASS headers (and anything else in include) */
53  if (strcmp(version, GIS_H_VERSION) != 0)
54  G_fatal_error(_("Module built against version %s but "
55  "trying to use version %s. "
56  "You need to rebuild GRASS GIS or untangle multiple "
57  "installations."),
58  version, GIS_H_VERSION);
59 
60  /* Make sure location and mapset are set */
62  mapset = G_mapset();
63  switch (G_mapset_permissions(mapset)) {
64  case 1:
65  break;
66  case 0:
67  G_fatal_error(_("MAPSET %s - permission denied"), mapset);
68  break;
69  default:
70  G_fatal_error(_("MAPSET %s not found at %s"), mapset,
71  G_location_path());
72  break;
73  }
74 
75  gisinit();
76 }
77 
78 /*!
79  \brief Initialize GIS Library
80 
81  Initializes GIS engine, but does not check for a valid mapset.
82  */
83 void G__no_gisinit(const char *version)
84 {
85  if (initialized)
86  return;
87 
88  /* verify version of GRASS headers (and anything else in include) */
89  if (strcmp(version, GIS_H_VERSION) != 0)
90  G_fatal_error(_("Module built against version %s but "
91  "trying to use version %s. "
92  "You need to rebuild GRASS GIS or untangle multiple "
93  "installations."),
94  version, GIS_H_VERSION);
95  gisinit();
96 }
97 
98 /*!
99  \brief Checks to see if GIS engine is initialized.
100  */
102 {
103  if (initialized)
104  return;
105  G_warning(
106  _("System not initialized. Programmer forgot to call G_gisinit()."));
107  G_sleep(3);
108  exit(EXIT_FAILURE);
109 }
110 
111 static int gisinit(void)
112 {
113  char *zlib;
114 
115 #ifdef __MINGW32__
116  _fmode = O_BINARY;
117 #endif
118  /* Mark window as not set */
119  G__.window_set = 0;
120 
121  /* byte order */
123 
124  zlib = getenv("GRASS_ZLIB_LEVEL");
125  /* Valid zlib compression levels -1 - 9 */
126  /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6
127  * level 0 means no compression
128  * as used here, 1 gives the best compromise between speed and compression
129  */
130  G__.compression_level = (zlib && *zlib && isdigit(*zlib)) ? atoi(zlib) : 1;
131  if (G__.compression_level < -1 || G__.compression_level > 9)
133 
134  initialized = 1;
135 
136  setlocale(LC_NUMERIC, "C");
137 
138  return 0;
139 }
140 
141 /*!
142  \brief Initialize environment
143  */
144 void G_init_all(void)
145 {
147  G_init_env();
148  G_init_logging();
149  G__init_window();
150  G_init_locale();
151  G_init_debug();
152  G_verbose();
153  G_init_tempfile();
155  G__home();
156  G__machine_name();
157  G_whoami();
160 }
void G_init_debug(void)
Initiate debugging.
Definition: debug.c:27
int G_is_little_endian(void)
Tests for little ENDIAN.
Definition: endian.c:24
void G_init_env(void)
Initialize variables.
Definition: env.c:83
int _fmode
Definition: fmode.c:4
int G_read_ellipsoid_table(int fatal)
Read ellipsoid table.
Definition: get_ellipse.c:246
void G_read_datum_table(void)
Definition: gis/datum.c:141
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
void G_init_logging(void)
Definition: gis/error.c:355
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:101
void G_init_all(void)
Initialize environment.
Definition: gisinit.c:144
void G__no_gisinit(const char *version)
Initialize GIS Library.
Definition: gisinit.c:83
void G__gisinit(const char *version, const char *pgm)
Initialize GIS Library and ensures a valid mapset is available.
Definition: gisinit.c:43
const char * G__home(void)
Get user's home directory (internal use only)
Definition: home.c:53
void G_init_locale(void)
Definition: locale.c:29
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
const char * G__machine_name(void)
Definition: mach_name.c:17
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
int G_mapset_permissions(const char *mapset)
Check for user mapset permission.
Definition: mapset_msc.c:290
void G__get_list_of_mapsets(void)
Fill list of mapsets from search path (internal use only)
Definition: mapset_nme.c:57
void G_set_program_name(const char *s)
Set program name.
Definition: progrm_nme.c:61
void G_sleep(unsigned int seconds)
Definition: sleep.c:11
Definition: G.h:5
int compression_level
Definition: G.h:9
int little_endian
Definition: G.h:8
int window_set
Definition: G.h:7
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:29
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:60
const char * G_whoami(void)
Gets user's name.
Definition: whoami.c:35
void G__init_window(void)
Initialize window (region).
Definition: window_map.c:76