Boost.Nowide
boost/nowide/cenv.hpp
00001 //
00002 //  Copyright (c) 2012 Artyom Beilis (Tonkikh)
00003 //
00004 //  Distributed under the Boost Software License, Version 1.0. (See
00005 //  accompanying file LICENSE_1_0.txt or copy at
00006 //  http://www.boost.org/LICENSE_1_0.txt)
00007 //
00008 #ifndef BOOST_NOWIDE_CENV_H_INCLUDED
00009 #define BOOST_NOWIDE_CENV_H_INCLUDED
00010 
00011 #include <string>
00012 #include <stdexcept>
00013 #include <stdlib.h>
00014 #include <boost/config.hpp>
00015 #include <boost/nowide/stackstring.hpp>
00016 #include <vector>
00017 
00018 #ifdef BOOST_WINDOWS
00019 #include <boost/nowide/windows.hpp>
00020 #endif
00021 
00022 namespace boost {
00023 namespace nowide {
00024     #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_DOXYGEN)
00025     using ::getenv;
00026     using ::setenv;
00027     using ::unsetenv;
00028     using ::putenv;
00029     #else
00030 
00031 
00032 
00033 
00034 
00035     inline char *getenv(char const *key)
00036     {
00037         static stackstring value;
00038         
00039         wshort_stackstring name;
00040         if(!name.convert(key))
00041             return 0;
00042 
00043         static const size_t buf_size = 64;
00044         wchar_t buf[buf_size];
00045         std::vector<wchar_t> tmp;
00046         wchar_t *ptr = buf;
00047         size_t n = GetEnvironmentVariableW(name.c_str(),buf,buf_size);
00048         if(n == 0 && GetLastError() == 203) // ERROR_ENVVAR_NOT_FOUND
00049             return 0;
00050         if(n >= buf_size) {
00051             tmp.resize(n+1,L'\0');
00052             n = GetEnvironmentVariableW(name.c_str(),&tmp[0],tmp.size() - 1);
00053             // The size may have changed
00054             if(n >= tmp.size() - 1)
00055                 return 0;
00056             ptr = &tmp[0];
00057         }
00058         if(!value.convert(ptr))
00059             return 0;
00060         return value.c_str();
00061     }
00068     inline int setenv(char const *key,char const *value,int override)
00069     {
00070         wshort_stackstring name;
00071         if(!name.convert(key))
00072             return -1;
00073         if(!override) {
00074             wchar_t unused[2];
00075             if(!(GetEnvironmentVariableW(name.c_str(),unused,2)==0 && GetLastError() == 203)) // ERROR_ENVVAR_NOT_FOUND
00076                 return 0;
00077         }
00078         wstackstring wval;
00079         if(!wval.convert(value))
00080             return -1;
00081         if(SetEnvironmentVariableW(name.c_str(),wval.c_str()))
00082             return 0;
00083         return -1;
00084     }
00088     inline int unsetenv(char const *key)
00089     {
00090         wshort_stackstring name;
00091         if(!name.convert(key))
00092             return -1;
00093         if(SetEnvironmentVariableW(name.c_str(),0))
00094             return 0;
00095         return -1;
00096     }
00100     inline int putenv(char *string)
00101     {
00102         char const *key = string;
00103         char const *key_end = string;
00104         while(*key_end!='=' && key_end!='\0')
00105             key_end++;
00106         if(*key_end == '\0')
00107             return -1;
00108         wshort_stackstring wkey;
00109         if(!wkey.convert(key,key_end))
00110             return -1;
00111         
00112         wstackstring wvalue;
00113         if(!wvalue.convert(key_end+1))
00114             return -1;
00115 
00116         if(SetEnvironmentVariableW(wkey.c_str(),wvalue.c_str()))
00117             return 0;
00118         return -1;
00119     }
00120     #endif
00121 } // nowide
00122 } // namespace boost
00123 
00124 #endif
00125 
00126 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4