/* ImageDirIterator.cpp * * Copyright (c) 2007 Machine Perception Laboratory * University of California San Diego. * Authors: Andrew Salamon * * A simple iterator that returns all image files in a given directory. */ #include "ImageDirIterator.h" #include #include "boost/filesystem/operations.hpp" #include "boost/filesystem/fstream.hpp" ImageDirIterator::ImageDirIterator( const std::string dir ) : directory(dir, bf::native ), dirIterator(directory) { } ImageDirIterator::~ImageDirIterator() { } bool ImageDirIterator::operator++() { currentPath = ""; while( dirIterator != endIterator ) { bf::path path = *dirIterator; std::string leaf( path.leaf() ); #ifdef xxDEBUG std::cout << "Iter: " << leaf << std::endl; #endif if( validExtension( leaf ) && !bf::is_directory( path ) ) { currentPath = path.native_file_string(); ++dirIterator; return true; } ++dirIterator; } return false; } bool ImageDirIterator::operator++(int) { return operator++(); } std::string ImageDirIterator::operator*() const { return currentPath; } std::string ImageDirIterator::getExtension( const std::string &fname ) { std::string ext; int pos = -1; for( int i = fname.length() - 1; i >= 0; i-- ) { if( '.' == fname[i] ) { pos = i; break; } } if( pos != -1 ) { ext = fname.substr( pos + 1, fname.length() - pos - 1 ); } return ext; } bool ImageDirIterator::validExtension( std::string &fname ) { // std::string ext = getExtension( fname ); // For use by the CERT Wrapper, just return all files and let the image // processing part of the code decide whether it's valid or not. // if( ext == "c" // || ext == "cpp" // || ext == "cc" // || ext == "h" // || ext == "hpp" ) return true; }