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