File: os_utils

package info (click to toggle)
openscenegraph 3.6.5%2Bdfsg1-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,100 kB
  • sloc: cpp: 392,065; ansic: 21,495; java: 1,020; yacc: 548; makefile: 431; objc: 406; xml: 155; lex: 151; javascript: 34
file content (147 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2017 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_os_utils
#define OSG_os_utils 1

#include <osg/Export>

#ifdef __cplusplus
extern "C" {
#endif

/** Cross platform version of C system() function. */
extern OSG_EXPORT int osg_system(const char* str);

#ifdef __cplusplus
}
#endif


#ifdef __cplusplus

#include <string>

#if defined(OSG_ENVVAR_SUPPORTED)
    #include <stdlib.h>
    #include <sstream>
#endif

namespace osg {


inline unsigned int getClampedLength(const char* str, unsigned int maxNumChars=4096)
{
    unsigned int i = 0;
    while(i<maxNumChars && str[i]!=0) { ++i; }
    return i;
}

inline std::string getEnvVar(const char* name)
{
#ifdef OSG_ENVVAR_SUPPORTED
    std::string value;
    const char* ptr = getenv(name);
    if (ptr) value.assign(ptr, getClampedLength(ptr));
    return value;
#else
    OSG_UNUSED(name);
    return std::string();
#endif
}


template<typename T>
inline bool getEnvVar(const char* name, T& value)
{
#ifdef OSG_ENVVAR_SUPPORTED
    const char* ptr = getenv(name);
    if (!ptr) return false;

    std::istringstream str(std::string(ptr, getClampedLength(ptr)));
    str >> value;
    return !str.fail();
#else
    OSG_UNUSED2(name, value);
    return false;
#endif
}

template<>
inline bool getEnvVar(const char* name, std::string& value)
{
#ifdef OSG_ENVVAR_SUPPORTED
    const char* ptr = getenv(name);
    if (!ptr) return false;

    value.assign(ptr, getClampedLength(ptr));
    return true;
#else
    OSG_UNUSED2(name, value);
    return false;
#endif
}

template<typename T1, typename T2>
inline bool getEnvVar(const char* name, T1& value1, T2& value2)
{
#ifdef OSG_ENVVAR_SUPPORTED
    const char* ptr = getenv(name);
    if (!ptr) return false;

    std::istringstream str(std::string(ptr, getClampedLength(ptr)));
    str >> value1 >> value2;
    return !str.fail();
#else
    OSG_UNUSED3(name, value1, value2);
    return false;
#endif
}

template<typename T1, typename T2, typename T3>
inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3)
{
#ifdef OSG_ENVVAR_SUPPORTED
    const char* ptr = getenv(name);
    if (!ptr) return false;

    std::istringstream str(std::string(ptr, getClampedLength(ptr)));
    str >> value1 >> value2 >> value3;
    return !str.fail();
#else
    OSG_UNUSED4(name, value1, value2, value3);
    return false;
#endif
}

template<typename T1, typename T2, typename T3, typename T4>
inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3, T4& value4)
{
#ifdef OSG_ENVVAR_SUPPORTED
    const char* ptr = getenv(name);
    if (!ptr) return false;

    std::istringstream str(std::string(ptr, getClampedLength(ptr)));
    str >> value1 >> value2 >> value3 >> value4;
    return !str.fail();
#else
    OSG_UNUSED5(name, value1, value2, value3, value4);
    return false;
#endif
}

}

#endif // _cplusplus

# endif