inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_mparam 00005 #define inlib_mparam 00006 00007 // parameter for a main. Coming from environment 00008 // or from (argc,argv) command line arguments. 00009 // Used in SoSDL applications. 00010 00011 #include "args" 00012 #include "system" 00013 00014 namespace inlib { 00015 00016 inline bool mparam_bool(bool a_from_env, 00017 const inlib::args& a_args, 00018 const std::string& a_prefix, 00019 const std::string& a_name, 00020 const bool& a_def, 00021 bool& a_value) { 00022 a_value = a_def; 00023 bool found = true; 00024 if(a_from_env) { 00025 std::string NAME(a_name); 00026 inlib::touppercase(NAME); 00027 if(!inlib::get_env_bool(a_prefix+NAME,a_value)) { 00028 found = false; 00029 a_value = a_def; 00030 } 00031 } else { 00032 if(!a_args.find("-"+a_name,a_value)) { 00033 found = false; 00034 a_value = a_def; 00035 } 00036 } 00037 return found; 00038 } 00039 00040 inline bool mparam(bool a_from_env,const inlib::args& a_args, 00041 const std::string& a_prefix, 00042 const std::string& a_name, 00043 const std::string& a_def, 00044 std::string& a_value) { 00045 a_value = a_def; 00046 bool found = true; 00047 if(a_from_env) { 00048 std::string NAME(a_name); 00049 inlib::touppercase(NAME); 00050 if(!inlib::getenv(a_prefix+NAME,a_value)) { 00051 found = false; 00052 a_value = a_def; 00053 } 00054 } else { 00055 if(!a_args.find("-"+a_name,a_value)) { 00056 found = false; 00057 a_value = a_def; 00058 } 00059 } 00060 return found; 00061 } 00062 00063 template <class T> 00064 inline bool mparam(bool a_from_env,const inlib::args& a_args, 00065 const std::string& a_prefix, 00066 const std::string& a_name, 00067 const T& a_def,T& a_value) { 00068 a_value = a_def; 00069 bool found = true; 00070 if(a_from_env) { 00071 std::string NAME(a_name); 00072 inlib::touppercase(NAME); 00073 if(!inlib::get_env<T>(a_prefix+NAME,a_value)) { 00074 found = false; 00075 a_value = a_def; 00076 } 00077 } else { 00078 if(!a_args.find<T>("-"+a_name,a_value)) { 00079 found = false; 00080 a_value = a_def; 00081 } 00082 } 00083 return found; 00084 } 00085 00086 } 00087 00088 #endif