1 //  Copyright (C) 2010, Vaclav Haisman. All rights reserved.
2 //
3 //  Redistribution and use in source and binary forms, with or without modifica-
4 //  tion, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of  source code must  retain the above copyright  notice,
7 //     this list of conditions and the following disclaimer.
8 //
9 //  2. Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //
13 //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
14 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15 //  FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
16 //  APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
17 //  INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
18 //  DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
19 //  OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
20 //  ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
21 //  (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
22 //  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 #include "dcmtk/oflog/internal/env.h"
25 #include "dcmtk/oflog/helpers/strhelp.h"
26 #include "dcmtk/oflog/streams.h"
27 
28 #ifdef DCMTK_LOG4CPLUS_HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 
32 #ifdef DCMTK_LOG4CPLUS_HAVE_WCHAR_H
33 #include <wchar.h>
34 #endif
35 
36 
37 #include <cassert>
38 #include <cstdlib>
39 #include <sstream>
40 
41 
42 namespace dcmtk {
43 namespace log4cplus { namespace internal {
44 
45 
46 bool
get_env_var(tstring & value,tstring const & name)47 get_env_var (tstring & value, tstring const & name)
48 {
49 #if defined (_WIN32) && defined (DCMTK_OFLOG_UNICODE)
50     tchar const * val = _wgetenv (name.c_str ());
51     if (val)
52         value = val;
53 
54     return !! val;
55 
56 #else
57     char const * val
58         = getenv (DCMTK_LOG4CPLUS_TSTRING_TO_STRING (name).c_str ());
59     if (val)
60         value = DCMTK_LOG4CPLUS_STRING_TO_TSTRING (val);
61 
62     return !! val;
63 
64 #endif
65 }
66 
67 
68 bool
parse_bool(bool & val,tstring const & str)69 parse_bool (bool & val, tstring const & str)
70 {
71     log4cplus::tistringstream iss (STD_NAMESPACE string(str.c_str(), str.length()));
72     STD_NAMESPACE string word;
73     if (! (iss >> word))
74         return false;
75     tchar ch;
76     if (iss >> ch)
77         return false;
78     word = STD_NAMESPACE string (helpers::toLower (word.c_str()).c_str());
79 
80     bool result = true;
81     if (word == DCMTK_LOG4CPLUS_TEXT ("true"))
82         val = true;
83     else if (word == DCMTK_LOG4CPLUS_TEXT ("false"))
84         val = false;
85     else
86     {
87         iss.clear ();
88         iss.seekg (0);
89         assert (iss);
90 
91         long lval;
92         iss >> lval;
93         result = !! iss && ! (iss >> ch);
94         if (result)
95             val = !! lval;
96     }
97 
98     return result;
99 }
100 
101 
102 } } // namespace log4cplus { namespace internal {
103 } // end namespace dcmtk
104