mirror of
https://github.com/projectM-visualizer/projectm.git
synced 2026-03-10 09:25:18 +00:00
partial preset factory integration
git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/personal/carm/represet@1137 6778bc44-b910-0410-a7a0-be141de4315d
This commit is contained in:
@ -11,6 +11,7 @@
|
||||
//
|
||||
#include "PresetLoader.hpp"
|
||||
#include "Preset.hpp"
|
||||
#include "PresetFactory.hpp"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
@ -34,122 +35,120 @@ extern "C"
|
||||
|
||||
#include "Common.hpp"
|
||||
|
||||
const std::string PresetLoader::PROJECTM_FILE_EXTENSION(".prjm");
|
||||
const std::string PresetLoader::MILKDROP_FILE_EXTENSION(".milk");
|
||||
|
||||
PresetLoader::PresetLoader(std::string dirname = std::string()) :m_dirname(dirname), m_dir(0), m_ratingsSum(0)
|
||||
PresetLoader::PresetLoader ( std::string dirname = std::string() ) :_dirname ( dirname ), _dir ( 0 ), _ratingsSum ( 0 )
|
||||
{
|
||||
// Do one scan
|
||||
if (m_dirname != std::string())
|
||||
rescan();
|
||||
// Do one scan
|
||||
if ( _dirname != std::string() )
|
||||
rescan();
|
||||
}
|
||||
|
||||
PresetLoader::~PresetLoader()
|
||||
{
|
||||
if (m_dir)
|
||||
closedir(m_dir);
|
||||
if ( _dir )
|
||||
closedir ( _dir );
|
||||
}
|
||||
|
||||
void PresetLoader::setScanDirectory(std::string dirname)
|
||||
void PresetLoader::setScanDirectory ( std::string dirname )
|
||||
{
|
||||
m_dirname = dirname;
|
||||
_dirname = dirname;
|
||||
}
|
||||
|
||||
|
||||
void PresetLoader::rescan()
|
||||
{
|
||||
// std::cerr << "Rescanning..." << std::endl;
|
||||
// std::cerr << "Rescanning..." << std::endl;
|
||||
|
||||
// Clear the directory entry collection
|
||||
m_entries.clear();
|
||||
m_presetNames.clear();
|
||||
m_ratings.clear();
|
||||
m_ratingsSum = 0;
|
||||
// If directory already opened, close it first
|
||||
if (m_dir)
|
||||
{
|
||||
closedir(m_dir);
|
||||
m_dir = 0;
|
||||
}
|
||||
// Clear the directory entry collection
|
||||
_entries.clear();
|
||||
_presetNames.clear();
|
||||
_ratings.clear();
|
||||
_ratingsSum = 0;
|
||||
// If directory already opened, close it first
|
||||
if ( _dir )
|
||||
{
|
||||
closedir ( _dir );
|
||||
_dir = 0;
|
||||
}
|
||||
|
||||
// Allocate a new a stream given the current directory name
|
||||
if ((m_dir = opendir(m_dirname.c_str())) == NULL)
|
||||
{
|
||||
handleDirectoryError();
|
||||
return; // no files loaded. m_entries is empty
|
||||
}
|
||||
// Allocate a new a stream given the current directory name
|
||||
if ( ( _dir = opendir ( _dirname.c_str() ) ) == NULL )
|
||||
{
|
||||
handleDirectoryError();
|
||||
return; // no files loaded. _entries is empty
|
||||
}
|
||||
|
||||
struct dirent * dir_entry;
|
||||
std::set<std::string> alphaSortedFileSet;
|
||||
std::set<std::string> alphaSortedPresetNameSet;
|
||||
|
||||
while ((dir_entry = readdir(m_dir)) != NULL)
|
||||
{
|
||||
struct dirent * dir_entry;
|
||||
std::set<std::string> alphaSortedFileSet;
|
||||
std::set<std::string> alphaSortedPresetNameSet;
|
||||
|
||||
std::ostringstream out;
|
||||
// Convert char * to friendly string
|
||||
std::string filename(dir_entry->d_name);
|
||||
while ( ( dir_entry = readdir ( _dir ) ) != NULL )
|
||||
{
|
||||
|
||||
// Verify extension is projectm or milkdrop
|
||||
if ((filename.rfind(PROJECTM_FILE_EXTENSION) != (filename.length() - PROJECTM_FILE_EXTENSION.length()))
|
||||
&& (filename.rfind(MILKDROP_FILE_EXTENSION) != (filename.length() - MILKDROP_FILE_EXTENSION.length())))
|
||||
continue;
|
||||
std::ostringstream out;
|
||||
// Convert char * to friendly string
|
||||
std::string filename ( dir_entry->d_name );
|
||||
|
||||
if (filename.length() <= MILKDROP_FILE_EXTENSION.length())
|
||||
continue;
|
||||
// Verify extension is projectm or milkdrop
|
||||
if ( ( filename.rfind ( PROJECTM_FILE_EXTENSION ) != ( filename.length() - PROJECTM_FILE_EXTENSION.length() ) )
|
||||
&& ( filename.rfind ( MILKDROP_FILE_EXTENSION ) != ( filename.length() - MILKDROP_FILE_EXTENSION.length() ) ) )
|
||||
continue;
|
||||
|
||||
if (filename.length() > 0 && filename[0] == '.')
|
||||
continue;
|
||||
if ( filename.length() <= MILKDROP_FILE_EXTENSION.length() )
|
||||
continue;
|
||||
|
||||
// Create full path name
|
||||
out << m_dirname << PATH_SEPARATOR << filename;
|
||||
if ( filename.length() > 0 && filename[0] == '.' )
|
||||
continue;
|
||||
|
||||
// Add to our directory entry collection
|
||||
alphaSortedFileSet.insert(out.str());
|
||||
alphaSortedPresetNameSet.insert(filename);
|
||||
// Create full path name
|
||||
out << _dirname << PATH_SEPARATOR << filename;
|
||||
|
||||
// the directory entry struct is freed elsewhere
|
||||
}
|
||||
// Add to our directory entry collection
|
||||
alphaSortedFileSet.insert ( out.str() );
|
||||
alphaSortedPresetNameSet.insert ( filename );
|
||||
|
||||
// Push all entries in order from the file set to the file entries member (which is an indexed vector)
|
||||
for (std::set<std::string>::iterator pos = alphaSortedFileSet.begin();
|
||||
pos != alphaSortedFileSet.end();++pos)
|
||||
m_entries.push_back(*pos);
|
||||
// the directory entry struct is freed elsewhere
|
||||
}
|
||||
|
||||
// Push all entries in order from the file set to the file entries member (which is an indexed vector)
|
||||
for ( std::set<std::string>::iterator pos = alphaSortedFileSet.begin();
|
||||
pos != alphaSortedFileSet.end();++pos )
|
||||
_entries.push_back ( *pos );
|
||||
|
||||
// Push all preset names in similar fashion
|
||||
for ( std::set<std::string>::iterator pos = alphaSortedPresetNameSet.begin();
|
||||
pos != alphaSortedPresetNameSet.end();++pos )
|
||||
_presetNames.push_back ( *pos );
|
||||
|
||||
// Give all presets equal rating of 3 - why 3? I don't know
|
||||
_ratings = std::vector<int> ( _presetNames.size(), 3 );
|
||||
_ratingsSum = 3 * _ratings.size();
|
||||
|
||||
assert ( _entries.size() == _presetNames.size() );
|
||||
assert ( _ratings.size() == _entries.size() );
|
||||
|
||||
// Push all preset names in similar fashion
|
||||
for (std::set<std::string>::iterator pos = alphaSortedPresetNameSet.begin();
|
||||
pos != alphaSortedPresetNameSet.end();++pos)
|
||||
m_presetNames.push_back(*pos);
|
||||
|
||||
// Give all presets equal rating of 3 - why 3? I don't know
|
||||
m_ratings = std::vector<int>(m_presetNames.size(), 3);
|
||||
m_ratingsSum = 3 * m_ratings.size();
|
||||
|
||||
assert(m_entries.size() == m_presetNames.size());
|
||||
assert(m_ratings.size() == m_entries.size());
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// Associates a preset loading handler a file extension.
|
||||
/// Any subsequent call to an extension overrides the previously set handler.
|
||||
/// \param extension the file extension to attach the handler to
|
||||
/// \param handler a preset loading handler that will be associated with the extension
|
||||
void PresetLoader::registerHandler(const std::string & extension, PresetLoadingHandler * handler) {
|
||||
void PresetLoader::registerFactory ( const std::string & extension, PresetFactory * factory ) {
|
||||
_factories[extension] = factory;
|
||||
}
|
||||
|
||||
_handlers[extension'] = handler;
|
||||
// }
|
||||
|
||||
std::auto_ptr<Preset> PresetLoader::loadPreset(unsigned int index, PresetInputs & presetInputs, PresetOutputs & presetOutputs) const
|
||||
std::auto_ptr<Preset> PresetLoader::loadPreset ( unsigned int index ) const
|
||||
{
|
||||
|
||||
// Check that index isn't insane
|
||||
assert(index >= 0);
|
||||
assert(index < m_entries.size());
|
||||
// Check that index isn't insane
|
||||
assert ( index >= 0 );
|
||||
assert ( index < _entries.size() );
|
||||
|
||||
// Return a new autopointer to a preset
|
||||
return std::auto_ptr<Preset>(new Preset(m_entries[index], m_presetNames[index], presetInputs, presetOutputs));
|
||||
// Return a new autopointer to a preset
|
||||
const std::string extension = parseExtension ( _entries[index] );
|
||||
|
||||
if ( !_factories.count ( extension ) )
|
||||
return std::auto_ptr<Preset> ( 0 );
|
||||
else
|
||||
return _factories[extension]->allocate ( _entries[index], _presetNames[index] );
|
||||
}
|
||||
|
||||
void PresetLoader::handleDirectoryError()
|
||||
@ -159,99 +158,107 @@ void PresetLoader::handleDirectoryError()
|
||||
std::cerr << "[PresetLoader] warning: errno unsupported on win32 platforms. fix me" << std::endl;
|
||||
#else
|
||||
|
||||
switch (errno)
|
||||
{
|
||||
case ENOENT:
|
||||
std::cerr << "[PresetLoader] ENOENT error. The path \"" << this->m_dirname << "\" probably does not exist. \"man open\" for more info." << std::endl;
|
||||
break;
|
||||
case ENOMEM:
|
||||
std::cerr << "[PresetLoader] out of memory! Are you running Windows?" << std::endl;
|
||||
abort();
|
||||
case ENOTDIR:
|
||||
std::cerr << "[PresetLoader] directory specified is not a preset directory! Trying to continue..." << std::endl;
|
||||
break;
|
||||
case ENFILE:
|
||||
std::cerr << "[PresetLoader] Your system has reached its open file limit. Trying to continue..." << std::endl;
|
||||
break;
|
||||
case EMFILE:
|
||||
std::cerr << "[PresetLoader] too many files in use by projectM! Bailing!" << std::endl;
|
||||
break;
|
||||
case EACCES:
|
||||
std::cerr << "[PresetLoader] permissions issue reading the specified preset directory." << std::endl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch ( errno )
|
||||
{
|
||||
case ENOENT:
|
||||
std::cerr << "[PresetLoader] ENOENT error. The path \"" << this->_dirname << "\" probably does not exist. \"man open\" for more info." << std::endl;
|
||||
break;
|
||||
case ENOMEM:
|
||||
std::cerr << "[PresetLoader] out of memory! Are you running Windows?" << std::endl;
|
||||
abort();
|
||||
case ENOTDIR:
|
||||
std::cerr << "[PresetLoader] directory specified is not a preset directory! Trying to continue..." << std::endl;
|
||||
break;
|
||||
case ENFILE:
|
||||
std::cerr << "[PresetLoader] Your system has reached its open file limit. Trying to continue..." << std::endl;
|
||||
break;
|
||||
case EMFILE:
|
||||
std::cerr << "[PresetLoader] too many files in use by projectM! Bailing!" << std::endl;
|
||||
break;
|
||||
case EACCES:
|
||||
std::cerr << "[PresetLoader] permissions issue reading the specified preset directory." << std::endl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void PresetLoader::setRating(unsigned int index, int rating) {
|
||||
assert(index >=0);
|
||||
assert(index < m_ratings.size());
|
||||
|
||||
m_ratingsSum -= m_ratings[index];
|
||||
m_ratings[index] = rating;
|
||||
m_ratingsSum += rating;
|
||||
|
||||
assert(m_entries.size() == m_presetNames.size());
|
||||
assert(m_ratings.size() == m_entries.size());
|
||||
|
||||
}
|
||||
|
||||
unsigned int PresetLoader::addPresetURL(const std::string & url, const std::string & presetName, int rating) {
|
||||
m_entries.push_back(url);
|
||||
m_presetNames.push_back(presetName);
|
||||
m_ratings.push_back(rating);
|
||||
m_ratingsSum += rating;
|
||||
|
||||
assert(m_entries.size() == m_presetNames.size());
|
||||
assert(m_ratings.size() == m_entries.size());
|
||||
|
||||
return m_entries.size()-1;
|
||||
}
|
||||
|
||||
void PresetLoader::removePreset(unsigned int index) {
|
||||
|
||||
m_entries.erase(m_entries.begin()+index);
|
||||
m_presetNames.erase(m_presetNames.begin()+index);
|
||||
m_ratingsSum -= m_ratings[index];
|
||||
m_ratings.erase(m_ratings.begin()+index);
|
||||
|
||||
assert(m_entries.size() == m_presetNames.size());
|
||||
assert(m_ratings.size() == m_entries.size());
|
||||
|
||||
}
|
||||
|
||||
const std::string & PresetLoader::getPresetURL ( unsigned int index) const {
|
||||
return m_entries[index];
|
||||
}
|
||||
|
||||
const std::string & PresetLoader::getPresetName ( unsigned int index) const {
|
||||
return m_presetNames[index];
|
||||
}
|
||||
|
||||
int PresetLoader::getPresetRating ( unsigned int index) const {
|
||||
return m_ratings[index];
|
||||
}
|
||||
|
||||
|
||||
const std::vector<int> & PresetLoader::getPresetRatings () const {
|
||||
return m_ratings;
|
||||
}
|
||||
|
||||
|
||||
int PresetLoader::getPresetRatingsSum () const {
|
||||
return m_ratingsSum;
|
||||
}
|
||||
|
||||
void PresetLoader::insertPresetURL(unsigned int index, const std::string & url, const std::string & presetName, int rating)
|
||||
void PresetLoader::setRating ( unsigned int index, int rating )
|
||||
{
|
||||
m_entries.insert(m_entries.begin()+index, url);
|
||||
m_presetNames.insert(m_presetNames.begin() + index, presetName);
|
||||
m_ratings.insert(m_ratings.begin()+index, rating);
|
||||
m_ratingsSum += rating;
|
||||
|
||||
assert(m_entries.size() == m_presetNames.size());
|
||||
assert(m_ratings.size() == m_entries.size());
|
||||
|
||||
assert ( index >=0 );
|
||||
assert ( index < _ratings.size() );
|
||||
|
||||
_ratingsSum -= _ratings[index];
|
||||
_ratings[index] = rating;
|
||||
_ratingsSum += rating;
|
||||
|
||||
assert ( _entries.size() == _presetNames.size() );
|
||||
assert ( _ratings.size() == _entries.size() );
|
||||
|
||||
}
|
||||
|
||||
unsigned int PresetLoader::addPresetURL ( const std::string & url, const std::string & presetName, int rating )
|
||||
{
|
||||
_entries.push_back ( url );
|
||||
_presetNames.push_back ( presetName );
|
||||
_ratings.push_back ( rating );
|
||||
_ratingsSum += rating;
|
||||
|
||||
assert ( _entries.size() == _presetNames.size() );
|
||||
assert ( _ratings.size() == _entries.size() );
|
||||
|
||||
return _entries.size()-1;
|
||||
}
|
||||
|
||||
void PresetLoader::removePreset ( unsigned int index )
|
||||
{
|
||||
|
||||
_entries.erase ( _entries.begin() +index );
|
||||
_presetNames.erase ( _presetNames.begin() +index );
|
||||
_ratingsSum -= _ratings[index];
|
||||
_ratings.erase ( _ratings.begin() +index );
|
||||
|
||||
assert ( _entries.size() == _presetNames.size() );
|
||||
assert ( _ratings.size() == _entries.size() );
|
||||
|
||||
}
|
||||
|
||||
const std::string & PresetLoader::getPresetURL ( unsigned int index ) const
|
||||
{
|
||||
return _entries[index];
|
||||
}
|
||||
|
||||
const std::string & PresetLoader::getPresetName ( unsigned int index ) const
|
||||
{
|
||||
return _presetNames[index];
|
||||
}
|
||||
|
||||
int PresetLoader::getPresetRating ( unsigned int index ) const
|
||||
{
|
||||
return _ratings[index];
|
||||
}
|
||||
|
||||
|
||||
const std::vector<int> & PresetLoader::getPresetRatings () const
|
||||
{
|
||||
return _ratings;
|
||||
}
|
||||
|
||||
|
||||
int PresetLoader::getPresetRatingsSum () const
|
||||
{
|
||||
return _ratingsSum;
|
||||
}
|
||||
|
||||
void PresetLoader::insertPresetURL ( unsigned int index, const std::string & url, const std::string & presetName, int rating )
|
||||
{
|
||||
_entries.insert ( _entries.begin() +index, url );
|
||||
_presetNames.insert ( _presetNames.begin() + index, presetName );
|
||||
_ratings.insert ( _ratings.begin() +index, rating );
|
||||
_ratingsSum += rating;
|
||||
|
||||
assert ( _entries.size() == _presetNames.size() );
|
||||
assert ( _ratings.size() == _entries.size() );
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user