os_utils 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef OSG_os_utils
  14. #define OSG_os_utils 1
  15. #include <osg/Export>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /** Cross platform version of C system() function. */
  20. extern OSG_EXPORT int osg_system(const char* str);
  21. #ifdef __cplusplus
  22. }
  23. #endif
  24. #ifdef __cplusplus
  25. #include <string>
  26. #if defined(OSG_ENVVAR_SUPPORTED)
  27. #include <stdlib.h>
  28. #include <sstream>
  29. #endif
  30. namespace osg {
  31. inline unsigned int getClampedLength(const char* str, unsigned int maxNumChars=4096)
  32. {
  33. unsigned int i = 0;
  34. while(i<maxNumChars && str[i]!=0) { ++i; }
  35. return i;
  36. }
  37. inline std::string getEnvVar(const char* name)
  38. {
  39. #ifdef OSG_ENVVAR_SUPPORTED
  40. std::string value;
  41. const char* ptr = getenv(name);
  42. if (ptr) value.assign(ptr, getClampedLength(ptr));
  43. return value;
  44. #else
  45. OSG_UNUSED(name);
  46. return std::string();
  47. #endif
  48. }
  49. template<typename T>
  50. inline bool getEnvVar(const char* name, T& value)
  51. {
  52. #ifdef OSG_ENVVAR_SUPPORTED
  53. const char* ptr = getenv(name);
  54. if (!ptr) return false;
  55. std::istringstream str(std::string(ptr, getClampedLength(ptr)));
  56. str >> value;
  57. return !str.fail();
  58. #else
  59. OSG_UNUSED2(name, value);
  60. return false;
  61. #endif
  62. }
  63. template<>
  64. inline bool getEnvVar(const char* name, std::string& value)
  65. {
  66. #ifdef OSG_ENVVAR_SUPPORTED
  67. const char* ptr = getenv(name);
  68. if (!ptr) return false;
  69. value.assign(ptr, getClampedLength(ptr));
  70. return true;
  71. #else
  72. OSG_UNUSED2(name, value);
  73. return false;
  74. #endif
  75. }
  76. template<typename T1, typename T2>
  77. inline bool getEnvVar(const char* name, T1& value1, T2& value2)
  78. {
  79. #ifdef OSG_ENVVAR_SUPPORTED
  80. const char* ptr = getenv(name);
  81. if (!ptr) return false;
  82. std::istringstream str(std::string(ptr, getClampedLength(ptr)));
  83. str >> value1 >> value2;
  84. return !str.fail();
  85. #else
  86. OSG_UNUSED3(name, value1, value2);
  87. return false;
  88. #endif
  89. }
  90. template<typename T1, typename T2, typename T3>
  91. inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3)
  92. {
  93. #ifdef OSG_ENVVAR_SUPPORTED
  94. const char* ptr = getenv(name);
  95. if (!ptr) return false;
  96. std::istringstream str(std::string(ptr, getClampedLength(ptr)));
  97. str >> value1 >> value2 >> value3;
  98. return !str.fail();
  99. #else
  100. OSG_UNUSED4(name, value1, value2, value3);
  101. return false;
  102. #endif
  103. }
  104. template<typename T1, typename T2, typename T3, typename T4>
  105. inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3, T4& value4)
  106. {
  107. #ifdef OSG_ENVVAR_SUPPORTED
  108. const char* ptr = getenv(name);
  109. if (!ptr) return false;
  110. std::istringstream str(std::string(ptr, getClampedLength(ptr)));
  111. str >> value1 >> value2 >> value3 >> value4;
  112. return !str.fail();
  113. #else
  114. OSG_UNUSED5(name, value1, value2, value3, value4);
  115. return false;
  116. #endif
  117. }
  118. }
  119. #endif // _cplusplus
  120. # endif