Home2L - C/C++ API v1.4-0-g38cc (2024-05-25)
Smart Tools for a Private Home
env.H
Go to the documentation of this file.
1/*
2 * This file is part of the Home2L project.
3 *
4 * (C) 2015-2021 Gundolf Kiefer
5 *
6 * Home2L is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Home2L is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Home2L. If not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20
21
22#ifndef _ENV_
23#define _ENV_
24
25// This file is SWIG-readable and part of the Python library.
26
27
36#ifdef SWIG
37%pythoncode %{
38## @defgroup env Environment
39## @brief Access configuration parameters and query environment properties.
40##
41## More detailed information on these functions can be found in the
42## [*C/C++ API documentation*](../home2l-api_c/group__env.html).
43## @{
44%}
45#endif
46
47
48#ifndef SWIG
49
50#include "base.H"
51
52#include <stdio.h>
53
54#endif // SWIG
55
56
57
58// ********************** Global settings **************************************
59
60
64
65
66#define HOME2L_USER "home2l"
67
68
70
71
72
73
74// ***************** Basic Environment Information and Helpers *****************
75
76
80
81
84
85const char *EnvBuildOS ();
86const char *EnvBuildArch ();
87
88const char *EnvMachineName ();
89
90const char *EnvExecPathName ();
95const char *EnvExecName ();
96
97const char *EnvInstanceName ();
120
121const char *EnvDroidId ();
122
123int EnvPid ();
125
126void EnvReadIniFile (const char *fileName, CDictCompact<CString> *map);
129
131
132
133
136
137const char *EnvHome2lRoot ();
138const char *EnvHome2lEtc ();
139const char *EnvHome2lVar ();
140const char *EnvHome2lTmp ();
141
142#ifndef SWIG
143
144const char *EnvGetHome2lRootPath (CString *ret, const char *relOrAbsPath);
147const char *EnvGetHome2lEtcPath (CString *ret, const char *relOrAbsPath);
149const char *EnvGetHome2lVarPath (CString *ret, const char *relOrAbsPath);
151const char *EnvGetHome2lTmpPath (CString *ret, const char *relOrAbsPath);
153
154#else // SWIG
155
156%feature("docstring") EnvGetHome2lRootPath "Return an absolute path name in the root domain.\n\n" \
157 "Path starting with '/' are considered already absolute and are\n" \
158 "returned unchanged, but normalized.\n"
159%feature("docstring") EnvGetHome2lEtcPath "Return an absolute path name in the 'etc' domain.\n\n" \
160 "Path starting with '/' are considered already absolute and are\n" \
161 "returned unchanged, but normalized.\n"
162%feature("docstring") EnvGetHome2lVarPath "Return an absolute path name in the 'var' domain.\n\n" \
163 "Path starting with '/' are considered already absolute and are\n" \
164 "returned unchanged, but normalized.\n"
165%feature("docstring") EnvGetHome2lTmpPath "Return an absolute path name in the 'tmp' domain.\n\n" \
166 "Path starting with '/' are considered already absolute and are\n" \
167 "returned unchanged, but normalized.\n"
168
169%newobject EnvGetHome2lRootPath (const char *);
170%newobject EnvGetHome2lEtcPath (const char *);
171%newobject EnvGetHome2lVarPath (const char *);
172%newobject EnvGetHome2lTmpPath (const char *);
173%inline %{
174static inline const char *EnvGetHome2lRootPath (const char *relOrAbsPath) { CString s; EnvGetHome2lRootPath (&s, relOrAbsPath); return s.Disown (); }
175static inline const char *EnvGetHome2lEtcPath (const char *relOrAbsPath) { CString s; EnvGetHome2lEtcPath (&s, relOrAbsPath); return s.Disown (); }
176static inline const char *EnvGetHome2lVarPath (const char *relOrAbsPath) { CString s; EnvGetHome2lVarPath (&s, relOrAbsPath); return s.Disown (); }
177static inline const char *EnvGetHome2lTmpPath (const char *relOrAbsPath) { CString s; EnvGetHome2lTmpPath (&s, relOrAbsPath); return s.Disown (); }
178%} // inline
179
180#endif // SWIG
181
182bool EnvMkVarDir (const char *relOrAbsPath);
187bool EnvMkTmpDir (const char *relOrAbsPath);
189
191
192
193
196
197bool EnvNetResolve (const char *hostAndPort, CString *retHost, int *retPort = NULL, int defaultPort = 0, bool warn = true);
208
210
211
214
219
220
221
223
224
225
226
228
229
230
231
232
233// ********************** Environment dictionary *****************************
234
235
239
240
243void EnvInit (int argc, char **argv, const char *specOptionsUsage = NULL, const char *instanceName = NULL, bool noBanner = false);
252void EnvDone ();
255
256
259void EnvEnablePersistence (bool writeThrough = true, const char *_varFileName = NULL);
279
280void EnvFlush ();
282
283
285
286
287
291
292
295
296#ifdef SWIG
297%feature("docstring") EnvGet "Get a Home2L configuration variable from the environment."
298#endif
299const char *EnvGet (const char *key);
304
305const char *EnvPut (const char *key, const char *value);
310const char *EnvPut (const char *key, int value);
313static inline void EnvDel (const char *key) { EnvPut (key, (const char *) NULL); }
315
317
318
319#ifndef SWIG
320
337
338bool EnvGetString (const char *key, const char **ret, bool warnIfMissing = false);
340const char *EnvGetString (const char *key, const char *defaultVal, bool warnIfMissing = false);
342static inline const char *EnvGetString (const char *key, bool warnIfMissing) { return EnvGetString (key, (const char *) NULL, warnIfMissing); }
344static inline const char *EnvGetString (const char *key) { return EnvGet (key); }
346
347bool EnvGetPath (const char *key, const char **ret, const char *path = NULL, bool warnIfMissing = false);
350const char *EnvGetPath (const char *key, const char *path = NULL, bool warnIfMissing = false);
352bool EnvGetHostAndPort (const char *key, CString *retHost, int *retPort = NULL, int defaultPort = 0, bool warnIfMissing = false);
354static inline bool EnvGetHost (const char *key, CString *retHost, bool warnIfMissing = false) { return EnvGetHostAndPort (key, retHost, NULL, 0, warnIfMissing); }
356
357
358bool EnvGetInt (const char *key, int *ret, bool warnIfMissing = false);
359int EnvGetInt (const char *key, int defaultVal = 0, bool warnIfMissing = false);
360
361bool EnvGetFloat (const char *key, float *ret, bool warnIfMissing = false);
362float EnvGetFloat (const char *key, float defaultVal = 0.0, bool warnIfMissing = false);
363
364bool EnvGetBool (const char *key, bool *ret, bool warnIfMissing = false);
365bool EnvGetBool (const char *key, bool defaultVal = false, bool warnIfMissing = false);
366
368
369#endif // SWIG
370
371
372
373
374#ifndef SWIG
375
378void EnvGetPrefixInterval (const char *prefix, int *retIdx0, int *retIdx1);
379const char *EnvGetKey (int idx);
380const char *EnvGetVal (int idx);
381
387
388#endif // SWIG
389
390
392
393
394
395
396
397// ************ Parameter declaration with automatic documentation *************
398
399
430enum EEnvParaType {
431 eptString = 0,
432 eptPath,
433 eptInt,
434 eptFloat,
435 eptBool
436};
437
438
439class CEnvPara {
440 public:
441 CEnvPara (const char *_key, EEnvParaType _type, void *_pVar);
442
443 static void GetAll (bool withVarKeys = true);
444
445 protected:
446 const char *key;
447 EEnvParaType type;
448 void *pVar;
449
450 CEnvPara *next;
451 static CEnvPara *first;
452};
453
454
457#define ENV_PARA_STRING(key,varName,varDefault) const char *varName = varDefault; CEnvPara varName##PreInit (key, eptString, &varName); const char *varName##Key = key;
458#define ENV_PARA_PATH(key,varName,varDefault) const char *varName = varDefault; CEnvPara varName##PreInit (key, eptPath, &varName); const char *varName##Key = key;
460#define ENV_PARA_INT(key,varName,varDefault) int varName = varDefault; CEnvPara varName##PreInit (key, eptInt, &varName); const char *varName##Key = key;
461#define ENV_PARA_FLOAT(key,varName,varDefault) float varName = varDefault; CEnvPara varName##PreInit (key, eptFloat, &varName); const char *varName##Key = key;
462#define ENV_PARA_BOOL(key,varName,varDefault) bool varName = varDefault; CEnvPara varName##PreInit (key, eptBool, &varName); const char *varName##Key = key;
464
468#define ENV_PARA_VAR(key,varType,varName,varDefault) varType varName = varDefault; const char *varName##Key = key;
470
474#define ENV_PARA_NOVAR(key,varType,varName,varDefault) const char *varName##Key = key;
481#define ENV_PARA_SPECIAL(keyPattern, varType, varDefault)
484
487
488
490#ifdef SWIG
491%pythoncode %{
492## @}
493%}
494#endif
495
496
497#endif
Compact dictionary.
Definition: base.H:1250
Raw dictionary (base class for other dictionary variants).
Definition: base.H:1139
Dynamically allocated string.
Definition: base.H:635
char * Disown()
Return current string as a dynamic object and clear 'this'.
bool EnvGetString(const char *key, const char **ret, bool warnIfMissing=false)
Read a string (advanced variant).
const CDictRaw * EnvGetKeySet()
Get the complete key set for direct key listing.
bool EnvGetHostAndPort(const char *key, CString *retHost, int *retPort=NULL, int defaultPort=0, bool warnIfMissing=false)
Get a host + port combination; host names are translated using the 'net.resolve.<name>' settings.
const char * EnvPut(const char *key, const char *value)
Generically put a string into the dictionary; Passing 'value == NULL' removes the entry.
bool EnvGetFloat(const char *key, float *ret, bool warnIfMissing=false)
Read a float value (advanced).
const char * EnvGet(const char *key)
Generically read a setting; Returns 'NULL' on non-existence and never prints warnings.
bool EnvGetInt(const char *key, int *ret, bool warnIfMissing=false)
Read an integer (advanced).
static void EnvDel(const char *key)
Remove an entry from the environment dictionary.
Definition: env.H:313
bool EnvGetPath(const char *key, const char **ret, const char *path=NULL, bool warnIfMissing=false)
Read a path, eventually convert value to an absolute path before (advanced). The returned path is alw...
bool EnvGetBool(const char *key, bool *ret, bool warnIfMissing=false)
Read a boolean value (advanced).
static bool EnvGetHost(const char *key, CString *retHost, bool warnIfMissing=false)
Get a host name; host names are translated using the 'net.resolve.<name>' settings.
Definition: env.H:354
void EnvPrintBanner()
Print the tool banner (only useful if banner was suppressed in the EnvInit() call).
void EnvFlush()
Write back any persistent variables now.
void EnvDone()
Shutdown the environment module.
void EnvInit(int argc, char **argv, const char *specOptionsUsage=NULL, const char *instanceName=NULL, bool noBanner=false)
Initialize the environment module.
void EnvEnablePersistence(bool writeThrough=true, const char *_varFileName=NULL)
Enable the persistence of all environment variables starting with "var.*".
const char * EnvGetHome2lEtcPath(CString *ret, const char *relOrAbsPath)
Return an absolute path name in the 'etc' domain.
const char * EnvExecName()
File name of the executable without path.
const char * EnvHome2lTmp()
'tmp' root path: dynamic data (not need to save over reboots).
bool EnvHaveTerminal()
'true', if the application has been started from an interactive terminal.
const char * EnvBuildArch()
Operating architecture (e.g. "i386", "armhf", ...).
void EnvReadIniFile(const char *fileName, CDictCompact< CString > *map)
Read a .ini file. 'fileName' must be an absolute path name.
const char * EnvGetHome2lRootPath(CString *ret, const char *relOrAbsPath)
Return an absolute path name in the root domain. A path starting with '/' is considered already absol...
const char * EnvHome2lVar()
'var' root path: dynamic data (saved over reboots).
const char * EnvDroidId()
Droid ID.
bool EnvMkVarDir(const char *relOrAbsPath)
Prepare (create) a directory in the 'var' domain.
const char * EnvInstanceName()
Instance name - the identifier for the running process.
const char * EnvBuildOS()
Operating software ("Debian", "Android", ...).
const char * EnvExecPathName()
Full path name of the executable (== argv[0]).
const char * EnvGetHome2lVarPath(CString *ret, const char *relOrAbsPath)
Return an absolute path name in the 'var' domain.
float EnvLocationLatitudeN()
Get the WGS84 latitude north coordinate of the home (the 'location.latitudeN' setting).
bool EnvNetResolve(const char *hostAndPort, CString *retHost, int *retPort=NULL, int defaultPort=0, bool warn=true)
Get a resolved host + port combination.
const char * EnvHome2lEtc()
'etc' root path: configuration data.
bool EnvMkTmpDir(const char *relOrAbsPath)
Prepare (create) a directory in the 'tmp' domain.
float EnvLocationLongitudeE()
Get the WGS84 longitude east coordinate of the home (the 'location.longitudeE' setting).
const char * EnvHome2lRoot()
Root installation path: all static data (blob + etc).
int EnvPid()
Operating system's process identifier (PID).
const char * EnvGetHome2lTmpPath(CString *ret, const char *relOrAbsPath)
Return an absolute path name in the 'tmp' domain.
const char * EnvMachineName()
Machine name (network host name).