TuttleOFX
1
|
00001 #include "LibAVPresetFileParser.hpp" 00002 00003 #include <tuttle/plugin/global.hpp> 00004 #include <tuttle/plugin/exceptions.hpp> 00005 00006 #include <boost/filesystem.hpp> 00007 #include <boost/algorithm/string.hpp> 00008 #include <boost/foreach.hpp> 00009 #include <boost/property_tree/ptree.hpp> 00010 #include <boost/property_tree/json_parser.hpp> 00011 00012 #include <cstdio> 00013 #include <cstring> 00014 #include <fstream> 00015 00016 namespace tuttle { 00017 namespace plugin { 00018 namespace av { 00019 00020 namespace bfs = boost::filesystem; 00021 00022 LibAVPresetFileParser::LibAVPresetFileParser( const std::string& filepath ) 00023 : presetType( eUnknownPresetType ) 00024 { 00025 presetsParameters = parseFile( filepath ); 00026 } 00027 00028 00029 LibAVPresetFileParser::~LibAVPresetFileParser() 00030 { 00031 00032 } 00033 00034 std::string LibAVPresetFileParser::getId( const std::string& filepath ) 00035 { 00036 return bfs::path( filepath ).stem().string(); 00037 } 00038 00039 PresetParameters LibAVPresetFileParser::parseFile( const std::string& filepath ) 00040 { 00041 id = getId( filepath ); 00042 std::string ext = bfs::path( filepath ).extension().string(); 00043 00044 if( ext == kPresetExtension ) 00045 { 00046 presetType = eMainPresetType; 00047 } 00048 if( ext == kPresetFormatExtension ) 00049 { 00050 presetType = eFormatPresetType; 00051 } 00052 if( ext == kPresetVideoExtension ) 00053 { 00054 presetType = eVideoPresetType; 00055 } 00056 if( ext == kPresetAudioExtension ) 00057 { 00058 presetType = eAudioPresetType; 00059 } 00060 00061 PresetParameters opts; 00062 std::ifstream inPreset; 00063 00064 inPreset.open( filepath.c_str(), std::ifstream::in ); 00065 if( ! inPreset.is_open() ) 00066 { 00067 using namespace tuttle::plugin; 00068 BOOST_THROW_EXCEPTION( exception::File() 00069 << exception::user() + "AV Preset: Unable to open preset file." 00070 << exception::filename( filepath ) ); 00071 } 00072 00073 boost::property_tree::basic_ptree<std::string,std::string> pt; 00074 try 00075 { 00076 boost::property_tree::json_parser::read_json( inPreset, pt ); 00077 boost::property_tree::basic_ptree<std::string,std::string>::const_iterator iter = pt.begin(), iterEnd = pt.end(); 00078 for( ; iter != iterEnd; ++iter ) 00079 { 00080 //TUTTLE_LOG_INFO( iter->first << " " << iter->second.get_value<std::string>() ); 00081 std::vector<std::string> values; 00082 std::string value = iter->second.get_value<std::string>(); 00083 00084 if( value.length() == 0 ) 00085 { 00086 BOOST_FOREACH( boost::property_tree::ptree::value_type &v, pt.get_child( iter->first ) ) 00087 { 00088 //TUTTLE_LOG_INFO( v.second.get_value<std::string>() ); 00089 values.push_back( v.second.get_value<std::string>() ); 00090 } 00091 opts[ iter->first ] = values; 00092 } 00093 else 00094 { 00095 if( iter->first == kPresetLabel ) 00096 { 00097 idLabel = value; 00098 } 00099 else 00100 { 00101 values.push_back( iter->second.get_value<std::string>() ); 00102 opts[ iter->first ] = values; 00103 } 00104 } 00105 } 00106 } 00107 catch( boost::property_tree::json_parser::json_parser_error &je ) 00108 { 00109 TUTTLE_LOG_WARNING( "Error parsing: " << je.filename() << " on line: " << je.line() ); 00110 TUTTLE_LOG_WARNING( je.message() ); 00111 } 00112 00113 inPreset.close(); 00114 return opts; 00115 00116 } 00117 00118 void LibAVPresetFileParser::print() const 00119 { 00120 std::string name = id; 00121 00122 switch( presetType ) 00123 { 00124 case eMainPresetType: name += "(main)"; break; 00125 case eFormatPresetType: name += "(format)"; break; 00126 case eVideoPresetType: name += "(video)"; break; 00127 case eAudioPresetType: name += "(audio)"; break; 00128 case eUnknownPresetType: name += "(unknown)"; break; 00129 } 00130 00131 name += ": "; 00132 name += idLabel; 00133 00134 TUTTLE_LOG_WARNING( "* " << name << " *" ); 00135 00136 //TUTTLE_LOG_VAR( TUTTLE_INFO, presetsParameters.size() ); 00137 00138 PresetParameters::const_iterator it = presetsParameters.begin(); 00139 for( ; it != presetsParameters.end(); ++it ) 00140 TUTTLE_LOG_WARNING( it->first << ": " << it->second.at(0) ); 00141 } 00142 00143 } 00144 } 00145 }