mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
environment.hpp
Go to the documentation of this file.
1/** \file environment.hpp
2 * \author Jared R. Males
3 * \brief Utilities for working with the environment
4 * \ingroup utils_files
5 */
6
7//***********************************************************************//
8// Copyright 2015-2020 Jared R. Males (jaredmales@gmail.com)
9//
10// This file is part of mxlib.
11//
12// mxlib is free software: you can redistribute it and/or modify
13// it under the terms of the GNU General Public License as published by
14// the Free Software Foundation, either version 3 of the License, or
15// (at your option) any later version.
16//
17// mxlib is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
24//***********************************************************************//
25
26#ifndef environment_hpp
27#define environment_hpp
28
29#include <locale>
30#include <string>
31#include <string.h>
32
33namespace mx
34{
35namespace sys
36{
37
38/// Return the value of an environment variable
39/** Call the standard getenv function, but handles the null pointer as an empty string.
40 *
41 * \returns the value of the environment varialbe, or empty string if it doesn't exist
42 *
43 * \tparam maxsz is the maximum allowable size of the environment variable. Prevents buffer overflow.
44 *
45 * \ingroup system
46 */
47template<size_t maxsz=4096>
48std::string getEnv( const std::string &estr /**< [in] is the name of the environment variable to query */ )
49{
50 char *e = getenv( estr.c_str() );
51
52 if( e )
53 {
54 size_t sz = strnlen(e, maxsz);
55 if(sz < maxsz)
56 {
57 return std::string( e );
58 }
59 else
60 {
61 return "";
62 }
63 }
64 else
65 {
66 return "";
67 }
68}
69
70} // namespace sys
71} // namespace mx
72
73#endif // environment_hpp
std::string getEnv(const std::string &estr)
Return the value of an environment variable.
The mxlib c++ namespace.
Definition mxlib.hpp:37