GteEnvironment.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
9 #include <LowLevel/GteLogger.h>
11 #include <cstdlib>
12 #include <fstream>
13 using namespace gte;
14 
16 {
17 }
18 
20 {
21 }
22 
24 {
25 #if defined(__MSWINDOWS__)
26  size_t size;
27  getenv_s(&size, nullptr, 0, name.c_str());
28  if (size > 0)
29  {
30  std::vector<char> tmpvar(size);
31  errno_t result = getenv_s(&size, tmpvar.data(), size, name.c_str());
32  std::string var = (result == 0 ? std::string(tmpvar.data()) : "");
33  return var;
34  }
35  else
36  {
37  return "";
38  }
39 #endif
40 
41 #if defined(__LINUX__)
42  char const* variable = getenv(name.c_str());
43  return variable ? std::string(variable) : std::string("");
44 #endif
45 }
46 
48 {
49  return static_cast<int>(mDirectories.size());
50 }
51 
53 {
54  if (0 <= i && i < static_cast<int>(mDirectories.size()))
55  {
56  return mDirectories[i];
57  }
58 
59  LogError("Invalid index.");
60  return "";
61 }
62 
63 bool Environment::Insert(std::string const& directory)
64 {
65  if (directory.size() > 0)
66  {
67  for (auto& d : mDirectories)
68  {
69  if (directory == d
70  || directory + "/" == d
71  || directory + "\\" == d)
72  {
73  return false;
74  }
75  }
76 
77  // Ensure all directories are terminated with a slash.
78  char lastChar = directory[directory.size() - 1];
79  if (lastChar == '\\' || lastChar == '/')
80  {
81  mDirectories.push_back(directory);
82  }
83  else
84  {
85  mDirectories.push_back(directory + "/");
86  }
87  return true;
88  }
89 
90  LogError("Insert expects non-empty inputs.");
91  return false;
92 }
93 
94 bool Environment::Remove(std::string const& directory)
95 {
96  auto iter = mDirectories.begin(), end = mDirectories.end();
97  for (; iter != end; ++iter)
98  {
99  if (directory == *iter)
100  {
101  mDirectories.erase(iter);
102  return true;
103  }
104  }
105  return false;
106 }
107 
109 {
110  mDirectories.clear();
111 }
112 
114 {
115  for (auto const& directory : mDirectories)
116  {
117  std::string decorated = directory + name;
118  std::ifstream input(decorated, std::ios::binary);
119  if (input)
120  {
121  input.close();
122  return decorated;
123  }
124  }
125  return "";
126 }
bool Remove(std::string const &directory)
std::vector< std::string > mDirectories
std::string GetPath(std::string const &name) const
const GLuint GLenum const void * binary
Definition: glcorearb.h:1919
GLuint const GLchar * name
Definition: glcorearb.h:781
GLsizeiptr size
Definition: glcorearb.h:659
std::string GetVariable(std::string const &name)
GLuint GLuint end
Definition: glcorearb.h:470
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
GLenum GLenum variable
Definition: glext.h:9913
#define LogError(message)
Definition: GteLogger.h:92
GLenum GLenum GLenum input
Definition: glext.h:9913
GLuint64EXT * result
Definition: glext.h:10003
std::string Get(int i) const
bool Insert(std::string const &directory)
int GetNumDirectories() const


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59