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