| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief ファイルから引数情報を取得する |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
|
| 9 |
\todo xpressive に置き換える |
| 10 |
*/ |
| 11 |
|
| 12 |
#if (!HAVE_CONFIG_H || defined(HAVE_BOOST_REGEX)) && \ |
| 13 |
!defined(FORCE_NO_BOOST_REGEX) |
| 14 |
#define USE_BOOST_REGEX |
| 15 |
#endif |
| 16 |
|
| 17 |
#include "FileToArgs.h" |
| 18 |
#include <string> |
| 19 |
#include <fstream> |
| 20 |
#include <vector> |
| 21 |
#include <map> |
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
#include <config.h> |
| 24 |
#endif |
| 25 |
#ifdef USE_BOOST_REGEX |
| 26 |
#include <boost/regex.hpp> |
| 27 |
#endif |
| 28 |
|
| 29 |
using namespace beego; |
| 30 |
|
| 31 |
|
| 32 |
struct FileToArgs::pImpl { |
| 33 |
|
| 34 |
std::vector<std::string> args; |
| 35 |
std::vector<char*> argv_ptr; |
| 36 |
|
| 37 |
typedef std::map<std::string,size_t> TagMap; |
| 38 |
TagMap tag_map; |
| 39 |
|
| 40 |
bool parseLine(std::string& line, const char* programName) { |
| 41 |
|
| 42 |
if (line.empty()) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
if (line[0] == '#') { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
// !!! strtok() は置き換えるべきかな? |
| 51 |
// !!! Boost の何かを使う? |
| 52 |
const char* token = strtok(const_cast<char*>(line.c_str()), " \t\r\n"); |
| 53 |
if (token == NULL) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
const char* next_token = strtok(NULL, " \t\r\n"); |
| 57 |
if (next_token == NULL) { |
| 58 |
line = token; |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
// トークンが2つ以上あった場合の処理 |
| 63 |
#ifdef USE_BOOST_REGEX |
| 64 |
boost::regex pattern(token); |
| 65 |
if (! boost::regex_match(programName, pattern)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
#else |
| 69 |
if (strcmp(token, programName)) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
#endif |
| 73 |
line = next_token; |
| 74 |
return true; |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
FileToArgs::FileToArgs(void) : pimpl(new pImpl), argc(0) { |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
FileToArgs::~FileToArgs(void) { |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
bool FileToArgs::load(const char* fname, const char* programName) { |
| 88 |
pimpl->args.clear(); |
| 89 |
argc = 0; |
| 90 |
|
| 91 |
// 最初にプログラム名を格納 |
| 92 |
pimpl->args.push_back(programName); |
| 93 |
|
| 94 |
// ファイルを開く |
| 95 |
std::ifstream fin(fname); |
| 96 |
if (! fin.is_open()) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
while (! fin.eof()) { |
| 101 |
|
| 102 |
// 行毎の情報を引数として扱う |
| 103 |
std::string line; |
| 104 |
getline(fin, line); |
| 105 |
|
| 106 |
if (! pimpl->parseLine(line, programName)) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
// '=' までのタグが重複したら、後からの内容で上書きする |
| 111 |
size_t tag_size = line.find('='); |
| 112 |
if (tag_size != std::string::npos) { |
| 113 |
std::string tag = line.substr(0, tag_size); |
| 114 |
pImpl::TagMap::iterator p = pimpl->tag_map.find(tag); |
| 115 |
if (p != pimpl->tag_map.end()) { |
| 116 |
size_t find_index = p->second; |
| 117 |
pimpl->args[find_index] = line; |
| 118 |
continue; |
| 119 |
} else { |
| 120 |
size_t insert_index = pimpl->args.size(); |
| 121 |
pimpl->tag_map. |
| 122 |
insert(std::pair<std::string,size_t>(tag, insert_index)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
pimpl->args.push_back(line); |
| 127 |
} |
| 128 |
|
| 129 |
// argc, argv の生成 |
| 130 |
for (std::vector<std::string>::iterator it = pimpl->args.begin(); |
| 131 |
it != pimpl->args.end(); ++it) { |
| 132 |
pimpl->argv_ptr.push_back(&(*it)[0]); |
| 133 |
} |
| 134 |
argc = static_cast<int>(pimpl->args.size()); |
| 135 |
argv = &(pimpl->argv_ptr)[0]; |
| 136 |
|
| 137 |
return true; |
| 138 |
} |