All Classes Namespaces Files Functions Variables Enumerations Enumerator
libs/system/findFiles.cpp
Go to the documentation of this file.
00001 
00010 #include "findFiles.h"
00011 #include <sys/types.h>
00012 #include "DetectOS.h"
00013 #ifdef MSC
00014 #include <windows.h>
00015 #else
00016 #include <dirent.h>
00017 #endif
00018 
00019 using namespace boost::xpressive;
00020 using namespace std;
00021 
00022 
00023 size_t qrk::findFiles(std::vector<std::string>& files, const char* root_path,
00024                       const boost::xpressive::sregex pattern)
00025 {
00026 #ifdef MSC
00027     // !!! Windows 実装
00028     WIN32_FIND_DATAA fd;
00029     string match_pattern = string(root_path) + "*";
00030     HANDLE dir = FindFirstFileA(match_pattern.c_str(), &fd);
00031     if (dir == INVALID_HANDLE_VALUE) {
00032         return 0;
00033     }
00034     size_t n = strlen(root_path);
00035     const char* add_str = (root_path[n -1] == '/') ? "" : "/";
00036 
00037     // ファイル毎にパターンにマッチするかの判定を行う
00038     size_t found_num = 0;
00039 
00040     do {
00041         string line = fd.cFileName;
00042         smatch match;
00043         if (regex_search(line, match, pattern)) {
00044             files.push_back(string(root_path) + add_str + line);
00045             ++found_num;
00046         }
00047     } while (FindNextFileA(dir, &fd));
00048     FindClose(dir);
00049 
00050     return found_num;
00051 
00052 #else
00053     // Linux, MacOS 実装
00054     DIR* dir = opendir(root_path);
00055     if (dir == NULL) {
00056         // !!! エラー出力
00057         return 0;
00058     }
00059     size_t n = strlen(root_path);
00060     const char* add_str = (root_path[n -1] == '/') ? "" : "/";
00061 
00062     // ファイル毎にパターンにマッチするかの判定を行う
00063     size_t found_num = 0;
00064 
00065     struct dirent* entry;
00066     while ((entry = readdir(dir)) != NULL) {
00067         string line = entry->d_name;
00068         smatch match;
00069         if (regex_search(line, match, pattern)) {
00070             files.push_back(string(root_path) + add_str + line);
00071             ++found_num;
00072         }
00073     }
00074     closedir(dir);
00075 #endif
00076 
00077     return found_num;
00078 }