Browse Subversion Repository
Contents of /common/FindFiles.cpp
Parent Directory
| Revision Log
Revision 330 -
( show annotations)
( download)
( as text)
Thu Apr 3 22:56:30 2008 UTC
(16 years ago)
by satofumi
File MIME type: text/x-c++src
File size: 1541 byte(s)
remove debug printf
| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief ファイル探索 |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
|
| 9 |
\todo 実装は、MSC 用とそうでない用に完全に分ける。読みにくい |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "FindFiles.h" |
| 13 |
#include <sys/types.h> |
| 14 |
#include "DetectOS.h" |
| 15 |
#ifdef MSC |
| 16 |
#include <windows.h> |
| 17 |
#else |
| 18 |
#include <dirent.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
using namespace boost::xpressive; |
| 22 |
|
| 23 |
|
| 24 |
/*! |
| 25 |
\brief recursive は未実装。多分、実装しない... |
| 26 |
*/ |
| 27 |
size_t beego::findFiles(std::vector<std::string>& files, |
| 28 |
const char* findRootPath, |
| 29 |
boost::xpressive::sregex pattern, bool recursive) { |
| 30 |
|
| 31 |
// ディレクトリからファイル一覧を取得する |
| 32 |
#ifdef MSC |
| 33 |
WIN32_FIND_DATAA fd; |
| 34 |
std::string match_pattern = std::string(findRootPath) + "*"; |
| 35 |
HANDLE dir = FindFirstFileA(match_pattern.c_str(), &fd); |
| 36 |
if (dir == INVALID_HANDLE_VALUE) { |
| 37 |
return 0; |
| 38 |
} |
| 39 |
#else |
| 40 |
DIR* dir = opendir(findRootPath); |
| 41 |
if (dir == NULL) { |
| 42 |
// !!! エラー出力 |
| 43 |
return 0; |
| 44 |
} |
| 45 |
#endif |
| 46 |
size_t n = strlen(findRootPath); |
| 47 |
const char* add_str = (findRootPath[n -1] == '/') ? "" : "/"; |
| 48 |
|
| 49 |
// ファイル毎にパターンにマッチするかの判定を行う |
| 50 |
size_t found_num = 0; |
| 51 |
#ifdef MSC |
| 52 |
do { |
| 53 |
std::string line = fd.cFileName; |
| 54 |
#else |
| 55 |
struct dirent* entry; |
| 56 |
while ((entry = readdir(dir)) != NULL) { |
| 57 |
std::string line = entry->d_name; |
| 58 |
#endif |
| 59 |
smatch match; |
| 60 |
if (regex_search(line, match, pattern)) { |
| 61 |
files.push_back(std::string(findRootPath) + add_str + line); |
| 62 |
++found_num; |
| 63 |
} |
| 64 |
#ifdef MSC |
| 65 |
} while (FindNextFileA(dir, &fd)); |
| 66 |
FindClose(dir); |
| 67 |
#else |
| 68 |
} |
| 69 |
closedir(dir); |
| 70 |
#endif |
| 71 |
return found_num; |
| 72 |
} |
|