Skip to content

Commit

Permalink
Add ability to provide filter when searching directory for files
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Burman <yanburman@users.noreply.github.com>
  • Loading branch information
yanburman committed Jul 7, 2016
1 parent f31e454 commit 6f41022
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/FileFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void FileFinder::add_work_item(const std::string &szRawFile, const std::string &
int FileFinder::find_files(std::string &dir)
{
std::list<std::string> files;
int ret = list_dir(dir, files);
int ret = list_dir(dir, files, m_Filter);
if (ret)
return ret;

Expand Down Expand Up @@ -121,7 +121,7 @@ int FileFinder::find_file(std::string &fname)
}

std::list<std::string> files;
int ret = list_dir(dir_name, files);
int ret = list_dir(dir_name, files, m_Filter);
if (ret)
return ret;

Expand Down
9 changes: 9 additions & 0 deletions src/FileFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <string>
#include <vector>
#include "utils.h"

struct RawWorkItem {
RawWorkItem(const std::string &szRawFile, const std::string &szMetadataFile)
Expand All @@ -19,6 +20,12 @@ struct RawWorkItem {
class FileFinder
{
public:
FileFinder()
{
m_Filter.push_back(jpeg_suffix);
m_Filter.push_back(raw_suffix);
}

~FileFinder()
{
cleanup_work_items();
Expand All @@ -38,6 +45,8 @@ class FileFinder
void cleanup_work_items(void);

std::vector<RawWorkItem *> m_WorkItems;

std::list<std::string> m_Filter;
};

#endif // __FILE_FIDER_H__
25 changes: 17 additions & 8 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include <pthread.h>
#endif

const static std::string jpeg_suffix(".JPG");

const std::string jpeg_suffix(".JPG");
const std::string raw_suffix(".RAW");
const std::string dng_suffix(".dng");

int list_dir(const std::string &dir, std::list<std::string> &files)
int list_dir(const std::string &dir, std::list<std::string> &files, const std::list<std::string> &filter)
#if defined(_WIN32) || defined(_WIN64)
{
WIN32_FIND_DATA ffd;
Expand All @@ -41,8 +41,13 @@ int list_dir(const std::string &dir, std::list<std::string> &files)
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
std::string fname(ffd.cFileName);

if (has_suffix(fname, raw_suffix) || has_suffix(fname, jpeg_suffix))
files.push_back(fname);
std::list<std::string>::const_iterator it;
for (it = filter.begin(); it != filter.end(); ++it) {
if (has_suffix(fname, *it)) {
files.push_back(fname);
break;
}
}
}
} while (FindNextFile(hFind, &ffd) != 0);

Expand Down Expand Up @@ -70,8 +75,13 @@ int list_dir(const std::string &dir, std::list<std::string> &files)
if (dirp->d_type == DT_REG) {
std::string fname(dirp->d_name);

if (has_suffix(fname, raw_suffix) || has_suffix(fname, jpeg_suffix))
files.push_back(fname);
std::list<std::string>::const_iterator it;
for (it = filter.begin(); it != filter.end(); ++it) {
if (has_suffix(fname, *it)) {
files.push_back(fname);
break;
}
}
}
}
closedir(dp);
Expand Down Expand Up @@ -108,4 +118,3 @@ void set_thread_prio_low(void)
pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
}
#endif

4 changes: 3 additions & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ static inline bool has_suffix(const std::string &str, const std::string &suffix)
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}

int list_dir(const std::string &dir, std::list<std::string> &files);
int list_dir(const std::string &dir, std::list<std::string> &files, const std::list<std::string> &filter);

extern const std::string jpeg_suffix;
extern const std::string raw_suffix;
extern const std::string dng_suffix;

size_t get_num_cpus(void);

Expand Down

0 comments on commit 6f41022

Please sign in to comment.