00001 //----------------------------------------------------------------------------------- 00002 // 00003 // Pro-Vocation Light Engine (PVLE) 00004 // Copyright (C) 2007-2009 Sukender, KinoX & Buzib 00005 // For more information, contact us : sukender@free.fr 00006 // 00007 // This program is free software; you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation; either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // For any use that is not compatible with the terms of the GNU 00013 // General Public License, please contact the authors for alternative 00014 // licensing options. 00015 // 00016 // This program is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 // GNU General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU General Public License along 00022 // with this program; if not, write to the Free Software Foundation, Inc., 00023 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00024 // 00025 //----------------------------------------------------------------------------------- 00026 00027 00028 #include <PVLE/Util/AppOptions.h> 00029 #include <fstream> 00030 //#include <boost/lexical_cast.hpp> 00031 00032 00033 namespace AppOptions { 00034 00035 #define ANY_STREAM(TYPE) if (v.type() == typeid(TYPE)) { stream << boost::any_cast<TYPE>(v); return stream; } 00037 /*PVLE_EXPORT*/ std::ostream& operator<<(std::ostream& stream, const boost::any& v) { 00038 //if (v.type() == typeid(UINT)) { stream << boost::any_cast<UINT>(v); return stream; } 00039 ANY_STREAM(UINT); 00040 ANY_STREAM(int); 00041 ANY_STREAM(std::string); 00042 ANY_STREAM(bool); 00043 ANY_STREAM(float); 00044 ASSERT(false && "Type of boost::any not supported for output"); 00045 return stream; 00046 } 00047 #undef ANY_STREAM 00048 00049 00050 void write(const boost::program_options::variables_map & vm, const boost::program_options::options_description & desc, const boost::filesystem::path & file, const char * const strHeader) { 00051 namespace po = boost::program_options; 00052 00053 std::ofstream ofs(file.native_file_string().c_str()); 00054 if (strHeader) ofs << strHeader << std::endl; 00055 00056 for(std::vector< boost::shared_ptr<po::option_description> >::const_iterator it=desc.options().begin(), itEnd=desc.options().end(); it!=itEnd; ++it) { 00057 // TODO Groups 00058 const std::string & key = (*it)->key(""); 00059 //const std::string & name = (*it)->long_name(); 00060 const std::string & description = (*it)->description(); 00061 if (!description.empty()) { 00062 if (ofs.tellp()>0) ofs << "\n"; 00063 ofs << "# " << description << std::endl; 00064 } 00065 if (vm[key].empty()) { 00066 // TODO Write default value if it exists 00067 //const po::typed_value * tv = dynamic_cast<const po::typed_value *>( (*it)->semantic() ); 00068 ofs << "#" << key << " = " << std::endl; 00069 } else { 00070 const boost::any & anyVal = vm[key].value(); 00071 // Special case for empty strings: comment the line 00072 if (anyVal.type() == typeid(std::string) && boost::any_cast<std::string>(anyVal).empty()) ofs << "#"; 00073 ofs << key << " = " << anyVal << std::endl; 00074 } 00075 } 00076 00077 ofs.close(); 00078 } 00079 00080 00081 //const rule<> boolean_true = longest_d[as_lower_d["true"] | "1" | "+" | as_lower_d["y"] | as_lower_d["yes"]]; 00082 //const rule<> boolean_false = longest_d[as_lower_d["false"] | "0" | "-" | as_lower_d["n"] | as_lower_d["no"]]; 00083 00084 00085 //#include <boost/regex.hpp> 00086 00087 //void AppOptions::write(const boost::filesystem::path & file) { 00088 // std::ofstream ofs(file.native_file_string().c_str()); 00089 // 00090 // boost::regex regexp("(\n+)"); 00091 // for(Entries::const_iterator it=entries.begin(), itEnd=entries.end(); it!=itEnd; ++it) { 00092 // if (!it->description.empty()) { 00093 // //ofs << "// " << boost::algorithm::replace_all_copy(boost::algorithm::replace_all_copy(it->description, "\n", "\n// "), "\n// \n", "\n\n") << std::endl; 00094 // ofs << (it->description[0]=='\n' ? "" : "// ") << boost::regex_replace(it->description, regexp, "\\1// ") << std::endl; 00095 // } 00096 // 00097 // ofs << it->name << " = " << it->value << std::endl; 00098 // } 00099 // 00100 // ofs.close(); 00101 //} 00102 00103 } // namespace AppOptions