implementing portable directory scanner in preset loader

git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/personal/carm/dev-1.0@228 6778bc44-b910-0410-a7a0-be141de4315d
This commit is contained in:
w1z7ard
2007-07-02 15:50:35 +00:00
parent 7a94b20259
commit 3ea0067caf
4 changed files with 320 additions and 251 deletions

View File

@ -11,19 +11,75 @@
//
#include "PresetLoader.hpp"
#include <iostream>
extern "C" {
#include <errno.h>
#include <linux/types.h>
#include <linux/dirent.h>
}
PresetLoader::PresetLoader(std::string pathame) {
// Initialize a directory watcher
#ifdef LINUX
if ((m_notifyFD = inotify_init()) == -1) {
std::cerr << "[PresetLoader] failed to initialize inotify instance (error " << errno << std::endl;
}
#endif
}
void PresetLoader::rescan() {
// If directory already opened, close it first
if (m_dir) {
closedir(dir);
}
// Allocate a new a stream given the current diectory name
if ((m_dir = opendir(m_dirname)) == NULL) {
handleDirectoryError();
}
std::string fullPath;
std::ostringstream out(fullPath);
struct dirent * dir_entry;
while ((dir_entry = readdir(m_dir)) != NULL) {
// Convert char * to friendly string
std::string filename(dir_entry->d_name);
// Verify extension is projectm or milkdrop
if (filename.rfind(PROJECTM_FILE_EXTENSION) <= 0
|| filename.rfind(MILKDROP_FILE_EXTENSION) <= 0)
continue;
// Create full path name
out << dirname << PATH_SEPARATOR << filename;
// Save to our directory entry collcetion
m_entries.add(out.str());
// We can now toss out the directory entry struct
free(dir_entry);
}
}
void PresetLoader::handleDirectoryError() {
switch (errno) {
case ENOENT:
break;
case ENOMEM:
abort();
case ENOTDIR:
break;
case ENFILE:
std::cerr << "[PresetLoader] Your system has reached its open file limit. Giving up..." << std::endl;
abort();
case EMFILE:
std::cerr << "[PresetLoader] too many files in use by projectM! Bailing!" << std::endl;
abort();
case EACCES:
break;
default:
break;
}
}