Boost.Nowide
cenv.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_NOWIDE_CENV_H_INCLUDED
9 #define BOOST_NOWIDE_CENV_H_INCLUDED
10 
11 #include <string>
12 #include <stdexcept>
13 #include <stdlib.h>
14 #include <boost/config.hpp>
15 #include <boost/nowide/stackstring.hpp>
16 #include <vector>
17 
18 #ifdef BOOST_WINDOWS
19 #include <boost/nowide/windows.hpp>
20 #endif
21 
22 namespace boost {
23 namespace nowide {
24  #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_DOXYGEN)
25  using ::getenv;
26  using ::setenv;
27  using ::unsetenv;
28  using ::putenv;
29  #else
30  inline char *getenv(char const *key)
36  {
37  static stackstring value;
38 
39  wshort_stackstring name;
40  if(!name.convert(key))
41  return 0;
42 
43  static const size_t buf_size = 64;
44  wchar_t buf[buf_size];
45  std::vector<wchar_t> tmp;
46  wchar_t *ptr = buf;
47  size_t n = GetEnvironmentVariableW(name.c_str(),buf,buf_size);
48  if(n == 0 && GetLastError() == 203) // ERROR_ENVVAR_NOT_FOUND
49  return 0;
50  if(n >= buf_size) {
51  tmp.resize(n+1,L'\0');
52  n = GetEnvironmentVariableW(name.c_str(),&tmp[0],static_cast<unsigned>(tmp.size() - 1));
53  // The size may have changed
54  if(n >= tmp.size() - 1)
55  return 0;
56  ptr = &tmp[0];
57  }
58  if(!value.convert(ptr))
59  return 0;
60  return value.c_str();
61  }
68  inline int setenv(char const *key,char const *value,int override)
69  {
70  wshort_stackstring name;
71  if(!name.convert(key))
72  return -1;
73  if(!override) {
74  wchar_t unused[2];
75  if(!(GetEnvironmentVariableW(name.c_str(),unused,2)==0 && GetLastError() == 203)) // ERROR_ENVVAR_NOT_FOUND
76  return 0;
77  }
78  wstackstring wval;
79  if(!wval.convert(value))
80  return -1;
81  if(SetEnvironmentVariableW(name.c_str(),wval.c_str()))
82  return 0;
83  return -1;
84  }
88  inline int unsetenv(char const *key)
89  {
90  wshort_stackstring name;
91  if(!name.convert(key))
92  return -1;
93  if(SetEnvironmentVariableW(name.c_str(),0))
94  return 0;
95  return -1;
96  }
100  inline int putenv(char *string)
101  {
102  char const *key = string;
103  char const *key_end = string;
104  while(*key_end!='=' && key_end!='\0')
105  key_end++;
106  if(*key_end == '\0')
107  return -1;
108  wshort_stackstring wkey;
109  if(!wkey.convert(key,key_end))
110  return -1;
111 
112  wstackstring wvalue;
113  if(!wvalue.convert(key_end+1))
114  return -1;
115 
116  if(SetEnvironmentVariableW(wkey.c_str(),wvalue.c_str()))
117  return 0;
118  return -1;
119  }
120  #endif
121 } // nowide
122 } // namespace boost
123 
124 #endif
125 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Definition: args.hpp:18
basic_stackstring< wchar_t, char, 16 > wshort_stackstring
Definition: stackstring.hpp:142
int setenv(char const *key, char const *value, int override)
UTF-8 aware setenv, key - the variable name, value is a new UTF-8 value,.
Definition: cenv.hpp:68
int putenv(char *string)
UTF-8 aware putenv implementation, expects string in format KEY=VALUE.
Definition: cenv.hpp:100
int unsetenv(char const *key)
Remove enviroment variable key.
Definition: cenv.hpp:88
char * getenv(char const *key)
UTF-8 aware getenv. Returns 0 if the variable is not set.
Definition: cenv.hpp:35
basic_stackstring< char, wchar_t, 256 > stackstring
Definition: stackstring.hpp:138
A class that allows to create a temporary wide or narrow UTF strings from wide or narrow UTF source...
Definition: stackstring.hpp:25