openscenegraph
os_utils
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2017 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_os_utils
15#define OSG_os_utils 1
16
17#include <osg/Export>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23/** Cross platform version of C system() function. */
24extern OSG_EXPORT int osg_system(const char* str);
25
26#ifdef __cplusplus
27}
28#endif
29
30
31#ifdef __cplusplus
32
33#include <string>
34
35#if defined(OSG_ENVVAR_SUPPORTED)
36 #include <stdlib.h>
37 #include <sstream>
38#endif
39
40namespace osg {
41
42
43inline unsigned int getClampedLength(const char* str, unsigned int maxNumChars=4096)
44{
45 unsigned int i = 0;
46 while(i<maxNumChars && str[i]!=0) { ++i; }
47 return i;
48}
49
50inline std::string getEnvVar(const char* name)
51{
52#ifdef OSG_ENVVAR_SUPPORTED
53 std::string value;
54 const char* ptr = getenv(name);
55 if (ptr) value.assign(ptr, getClampedLength(ptr));
56 return value;
57#else
58 OSG_UNUSED(name);
59 return std::string();
60#endif
61}
62
63
64template<typename T>
65inline bool getEnvVar(const char* name, T& value)
66{
67#ifdef OSG_ENVVAR_SUPPORTED
68 const char* ptr = getenv(name);
69 if (!ptr) return false;
70
71 std::istringstream str(std::string(ptr, getClampedLength(ptr)));
72 str >> value;
73 return !str.fail();
74#else
75 OSG_UNUSED2(name, value);
76 return false;
77#endif
78}
79
80template<>
81inline bool getEnvVar(const char* name, std::string& value)
82{
83#ifdef OSG_ENVVAR_SUPPORTED
84 const char* ptr = getenv(name);
85 if (!ptr) return false;
86
87 value.assign(ptr, getClampedLength(ptr));
88 return true;
89#else
90 OSG_UNUSED2(name, value);
91 return false;
92#endif
93}
94
95template<typename T1, typename T2>
96inline bool getEnvVar(const char* name, T1& value1, T2& value2)
97{
98#ifdef OSG_ENVVAR_SUPPORTED
99 const char* ptr = getenv(name);
100 if (!ptr) return false;
101
102 std::istringstream str(std::string(ptr, getClampedLength(ptr)));
103 str >> value1 >> value2;
104 return !str.fail();
105#else
106 OSG_UNUSED3(name, value1, value2);
107 return false;
108#endif
109}
110
111template<typename T1, typename T2, typename T3>
112inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3)
113{
114#ifdef OSG_ENVVAR_SUPPORTED
115 const char* ptr = getenv(name);
116 if (!ptr) return false;
117
118 std::istringstream str(std::string(ptr, getClampedLength(ptr)));
119 str >> value1 >> value2 >> value3;
120 return !str.fail();
121#else
122 OSG_UNUSED4(name, value1, value2, value3);
123 return false;
124#endif
125}
126
127template<typename T1, typename T2, typename T3, typename T4>
128inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3, T4& value4)
129{
130#ifdef OSG_ENVVAR_SUPPORTED
131 const char* ptr = getenv(name);
132 if (!ptr) return false;
133
134 std::istringstream str(std::string(ptr, getClampedLength(ptr)));
135 str >> value1 >> value2 >> value3 >> value4;
136 return !str.fail();
137#else
138 OSG_UNUSED5(name, value1, value2, value3, value4);
139 return false;
140#endif
141}
142
143}
144
145#endif // _cplusplus
146
147# endif