TuttleOFX  1
LibAVPresetParser.cpp
Go to the documentation of this file.
00001 #include "LibAVPresetParser.hpp"
00002 #include "LibAVPresetFileParser.hpp"
00003 
00004 #include <tuttle/plugin/global.hpp>
00005 
00006 #include <boost/foreach.hpp>
00007 #include <boost/filesystem.hpp>
00008 #include <boost/algorithm/string.hpp>
00009 
00010 namespace tuttle {
00011 namespace plugin {
00012 namespace av {
00013 
00014 namespace bfs = boost::filesystem;
00015 
00016 LibAVPresetParser::LibAVPresetParser()
00017 {
00018 }
00019 
00020 LibAVPresetParser::~LibAVPresetParser()
00021 {
00022 }
00023 
00024 PresetsList LibAVPresetParser::researchPresetsFiles()
00025 {
00026         PresetsList presetList;
00027         
00028         PresetsList files = getPresetListForPathList( "LIBAV_DATADIR" );
00029         BOOST_FOREACH( const std::string& preset, files )
00030         {
00031                 presetList.push_back( preset );
00032         }
00033         
00034         files = getPresetListForPathList( "TUTTLE_HOME" );
00035         BOOST_FOREACH( const std::string& preset, files )
00036         {
00037                 presetList.push_back( preset );
00038         }
00039         
00040 #ifndef _WIN32
00041         files = getPresetListForPathList( "HOME" );
00042         BOOST_FOREACH( const std::string& preset, files )
00043         {
00044                 presetList.push_back( preset );
00045         }
00046 #endif
00047         return presetList;
00048 }
00049 
00050 PresetsList LibAVPresetParser::getPresetListForPathList( const std::string& envVar )
00051 {
00052         PresetsList presetPaths;
00053  
00054         if( const char* const dataDir = std::getenv( envVar.c_str() ) )
00055         {
00056                 const std::string s( dataDir );
00057                 boost::split( presetPaths, s, boost::is_any_of( DIRLIST_SEP_CHARS ) );
00058         }
00059         
00060         PresetsList configs;
00061         BOOST_FOREACH( const bfs::path presetPath, presetPaths )
00062         {
00063                 if( bfs::exists( presetPath ) && bfs::is_directory( presetPath ) )
00064                 {
00065                         bfs::directory_iterator itEnd;
00066                         for( bfs::directory_iterator iter( presetPath ); iter != itEnd; ++iter )
00067                         {
00068                                 if( isValidExtension( iter->path().string() ) )
00069                                 {
00070                                         configs.push_back( iter->path().string() );
00071                                 }
00072                         }
00073                         // research into the subdir (if exist)
00074                         bfs::path p = presetPath / kPresetSubDirPath;
00075                         if( bfs::exists( p ) && bfs::is_directory( p ) )
00076                         {
00077                                 for( bfs::directory_iterator iter( p ); iter != itEnd; ++iter )
00078                                 {
00079                                         if( isValidExtension( iter->path().string() ) )
00080                                         {
00081                                                 configs.push_back( iter->path().string() );
00082                                         }
00083                                 }
00084                         }
00085                 }
00086         }
00087         return configs;
00088 }
00089 
00090 bool LibAVPresetParser::isValidExtension( const std::string& filename )
00091 {
00092         bfs::path f = filename;
00093         
00094         return  ( f.extension() == kPresetExtension ) ||
00095                         ( f.extension() == kPresetFormatExtension ) ||
00096                         ( f.extension() == kPresetVideoExtension ) ||
00097                         ( f.extension() == kPresetAudioExtension );
00098 }
00099 
00100 void LibAVPresetParser::getPresetList( std::vector<std::string>& presetID, const std::string& extension )
00101 {
00102         BOOST_FOREACH( const std::string& presetPath, researchPresetsFiles() )
00103         {
00104                 bfs::path f = presetPath;
00105                 if( f.extension() == extension )
00106                 {
00107                         presetID.push_back( LibAVPresetFileParser::getId( presetPath ) );
00108                 }
00109         }
00110 }
00111 
00112 void LibAVPresetParser::getPresetList( std::vector<std::string>& presetID, std::vector<std::string>& presetLabelID , const std::string& extension )
00113 {
00114         BOOST_FOREACH( const std::string& presetPath, researchPresetsFiles() )
00115         {
00116                 bfs::path f = presetPath;
00117                 if( f.extension() == extension )
00118                 {
00119                         LibAVPresetFileParser fp( presetPath );
00120                         presetID.push_back( fp.getId() );
00121                         presetLabelID.push_back( fp.getIdLabel() );
00122                 }
00123         }
00124 }
00125 
00126 PresetsList getPresetsPathList( const std::string& extension = "" )
00127 {
00128         PresetsList list;
00129         BOOST_FOREACH( const std::string& presetPath, LibAVPresetParser::researchPresetsFiles() )
00130         {
00131                 bfs::path f = presetPath;
00132                 if( f.extension() == extension )
00133                         list.push_back( presetPath );
00134         }
00135         return list;
00136 }
00137 
00138 PresetsList getPresetsFiles( const std::string& extension = "" )
00139 {
00140         PresetsList list;
00141         BOOST_FOREACH( const std::string& presetPath, LibAVPresetParser::researchPresetsFiles() )
00142         {
00143                 bfs::path f = presetPath;
00144                 if( f.extension() == extension )
00145                         LibAVPresetFileParser preset( presetPath );
00146         }
00147         return list;
00148 }
00149 
00150 
00151 }
00152 }
00153 }