From 45dfa4c1fa086a34ca690299ab3b6c410f27a02a Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Tue, 15 Jul 2014 10:25:42 +0200 Subject: [PATCH 1/2] Profile: refactoring * Function loadProfiles split in 2: * loadProfiles( const std::string& avProfilesPath = "" ), which loads profiles. You can have a specific avProfilesPath, or use the path contains in AVPROFILES (env variable). * loadProfile, which loads a single profile. The previous function calls it for each profile. * Function getFilesInDir: do not take "." and ".." as files. --- src/AvTranscoder/Profile.cpp | 86 ++++++++++++++++++------------------ src/AvTranscoder/Profile.hpp | 5 ++- src/AvTranscoder/common.cpp | 9 ++-- src/AvTranscoder/common.hpp | 2 +- 4 files changed, 55 insertions(+), 47 deletions(-) diff --git a/src/AvTranscoder/Profile.cpp b/src/AvTranscoder/Profile.cpp index 05cf6e56..cc6db961 100644 --- a/src/AvTranscoder/Profile.cpp +++ b/src/AvTranscoder/Profile.cpp @@ -34,54 +34,56 @@ Profile::Profile( bool autoload ) loadProfiles(); } -void Profile::loadProfiles() +void Profile::loadProfile( const std::string& avProfileFile ) +{ + std::ifstream infile; + infile.open( avProfileFile.c_str(), std::ifstream::in ); + + Profile::ProfileDesc customProfile; + + std::string line; + while( std::getline( infile, line ) ) + { + std::vector< std::string > keyValue; + split( keyValue, line, "=" ); + if( keyValue.size() == 2 ) + customProfile[ keyValue.at( 0 ) ] = keyValue.at( 1 ); + } + // check if profile contains required values + if( + customProfile.count( avProfileIdentificator ) && + customProfile.count( avProfileIdentificatorHuman ) && + customProfile.count( avProfileType ) && + ( customProfile.find( avProfileType )->second == avProfileTypeVideo || + customProfile.find( avProfileType )->second == avProfileTypeAudio ) + ) + { + _profiles.push_back( customProfile ); + } +} + +void Profile::loadProfiles( const std::string& avProfilesPath ) { loadXdCamHD422( _profiles ); loadDNxHD( _profiles ); loadWave( _profiles ); - - if( const char* envAvProfiles = std::getenv( "AVPROFILES" ) ) + + std::string realAvProfilesPath = avProfilesPath; + if( realAvProfilesPath.empty() ) + realAvProfilesPath = std::getenv( "AVPROFILES" ); + + std::vector< std::string > paths; + split( paths, realAvProfilesPath, ":" ); + for( std::vector< std::string >::iterator dirIt = paths.begin(); dirIt != paths.end(); ++dirIt ) { - std::vector< std::string > paths; - split( paths, envAvProfiles, ":" ); - for( std::vector< std::string >::iterator dirIt = paths.begin(); dirIt != paths.end(); ++dirIt ) + std::vector< std::string > files; + if( getFilesInDir( *dirIt, files ) != 0 ) + continue; + + for( std::vector< std::string >::iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt ) { - //std::cout << "search profile in path " << *dirIt << std::endl; - std::vector< std::string > files; - if( getFilesInDir( *dirIt, files ) != 0 ) - continue; - - for( std::vector< std::string >::iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt ) - { - if( ( *fileIt == "." ) || ( *fileIt == ".." ) ) - continue; - - std::ifstream infile; - infile.open( ( ( *dirIt ) + "/" + ( *fileIt ) ).c_str(), std::ifstream::in ); - - // std::cout << "file " << *dirIt << *fileIt << std::endl; - Profile::ProfileDesc customProfile; - - std::string line; - while( std::getline( infile, line ) ) - { - std::vector< std::string > keyValue; - split( keyValue, line, "=" ); - if( keyValue.size() == 2 ) - customProfile[ keyValue.at( 0 ) ] = keyValue.at( 1 ); - } - // check if profile contains required values - if( - customProfile.count( avProfileIdentificator ) && - customProfile.count( avProfileIdentificatorHuman ) && - customProfile.count( avProfileType ) && - ( customProfile.find( avProfileType )->second == avProfileTypeVideo || - customProfile.find( avProfileType )->second == avProfileTypeAudio ) - ) - { - _profiles.push_back( customProfile ); - } - } + const std::string absPath = ( *dirIt ) + "/" + ( *fileIt ); + loadProfile( absPath ); } } } diff --git a/src/AvTranscoder/Profile.hpp b/src/AvTranscoder/Profile.hpp index 445f8e0d..eca96a4f 100644 --- a/src/AvTranscoder/Profile.hpp +++ b/src/AvTranscoder/Profile.hpp @@ -29,13 +29,16 @@ class Profile static const std::string avProfileWidth; static const std::string avProfileHeight; +public: // typedef std::pair< std::string, std::string > KeyDesc; typedef std::map< std::string, std::string > ProfileDesc; typedef std::vector< ProfileDesc > ProfilesDesc; +public: Profile( bool autoload = false ); - void loadProfiles(); + void loadProfiles( const std::string& avProfilesPath = "" ); + void loadProfile( const std::string& avProfileFile ); void update( const ProfileDesc& profile ); diff --git a/src/AvTranscoder/common.cpp b/src/AvTranscoder/common.cpp index 80fb53fb..af3e5e97 100644 --- a/src/AvTranscoder/common.cpp +++ b/src/AvTranscoder/common.cpp @@ -16,19 +16,22 @@ void split( std::vector< std::string >& splitedString, const std::string& inputS } } -int getFilesInDir( std::string dir, std::vector< std::string > &files ) +int getFilesInDir( const std::string& dir, std::vector< std::string >& files ) { DIR *dp; struct dirent *dirp; if( ( dp = opendir( dir.c_str() ) ) == NULL ) { - std::cout << "Error(" << errno << ") opening " << dir << std::endl; + std::cerr << "Error(" << errno << ") opening " << dir << std::endl; return errno; } while( ( dirp = readdir( dp ) ) != NULL ) { - files.push_back( std::string( dirp->d_name ) ); + std::string filename( dirp->d_name ); + if( filename == "." || filename == ".." ) + continue; + files.push_back( filename ); } closedir( dp ); return 0; diff --git a/src/AvTranscoder/common.hpp b/src/AvTranscoder/common.hpp index a182698c..075234ff 100644 --- a/src/AvTranscoder/common.hpp +++ b/src/AvTranscoder/common.hpp @@ -61,7 +61,7 @@ struct Ratio void split( std::vector< std::string >& splitedString, const std::string& inputString, const std::string& splitChars = ";" ); -int getFilesInDir( std::string dir, std::vector< std::string > &files ); +int getFilesInDir( const std::string& dir, std::vector< std::string >& files ); } From ec9eda9c77ae811f9248ee2cb8768856c6563944 Mon Sep 17 00:00:00 2001 From: Clement Champetier Date: Tue, 15 Jul 2014 11:28:20 +0200 Subject: [PATCH 2/2] Profile: fix when AVPROFILES si empty --- src/AvTranscoder/Profile.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/AvTranscoder/Profile.cpp b/src/AvTranscoder/Profile.cpp index cc6db961..df8695f3 100644 --- a/src/AvTranscoder/Profile.cpp +++ b/src/AvTranscoder/Profile.cpp @@ -70,7 +70,12 @@ void Profile::loadProfiles( const std::string& avProfilesPath ) std::string realAvProfilesPath = avProfilesPath; if( realAvProfilesPath.empty() ) - realAvProfilesPath = std::getenv( "AVPROFILES" ); + { + if( std::getenv( "AVPROFILES" ) ) + realAvProfilesPath = std::getenv( "AVPROFILES" ); + else + return; + } std::vector< std::string > paths; split( paths, realAvProfilesPath, ":" );