-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirclog2json.cpp
288 lines (233 loc) · 7.6 KB
/
irclog2json.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "options.h"
#include "file_name_manager.h"
#include "message/line_parser.h"
#include "message/iso_2022_jp_line_parser.h"
#include "message/utf8_line_parser.h"
#include "message/tiarra_iso_2022_jp_line_parser.h"
#include "message/tiarra_line_parser.h"
#include "message/madoka_iso_2022_jp_line_parser.h"
#include "message/madoka_line_parser.h"
#include "message/message_base.h"
#include "irc_log_parser.h"
#include "basic_irc_log_parser.h"
#include "madoka_log_parser.h"
#include <memory>
#include <clocale>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <libgen.h>
#include <optional>
#include <regex>
#include <string>
#include <vector>
#include <picojson.h>
/**
* @brief コマンドライン引数からオプションを解析する。
* @param [in] argc コマンドライン引数の個数。
* @param [in] argv コマンドライン引数の配列。
* @param [out] options オプションの設定先。
* @param [out] print_usage 使用法を出力するか。
* @retval <true> 有効なオプションのみ含まれたいた場合。
* @retval <false> 無効なオプションが含まれていた場合。
*/
static bool ParseOptions(int argc, char* argv[], irclog2json::Options& options,
bool& print_usage);
/**
* @brief ログをJSONオブジェクトの配列に変換する。
* @param ifs 入力ファイルストリーム。
* @param channel チャンネル名。
* @param tm_date 日付を表すtm構造体。
* @param options オプション設定。
* @return 変換結果。JSONオブジェクトの配列。
*/
static picojson::value
ConvertLogToJsonObjects(std::ifstream* ifs, const std::string& channel,
const struct tm& tm_date,
const irclog2json::Options& options);
/**
* @brief 使用法を表示する。
* @param argv0 コマンドライン引数の最初に設定されるプログラム名。
* @param os 出力ストリーム。
*/
static void PrintUsage(char* argv0, std::ostream& os);
int main(int argc, char* argv[]) {
char* argv0 = argv[0];
std::setlocale(LC_ALL, "");
irclog2json::Options options;
bool print_usage;
bool no_invalid_option = ParseOptions(argc, argv, options, print_usage);
argc -= optind;
argv += optind;
if (print_usage) {
PrintUsage(argv0, no_invalid_option ? std::cout : std::cerr);
return no_invalid_option ? 0 : 1;
}
if (argc != 2) {
PrintUsage(argv0, std::cerr);
return 1;
}
if (options.log_format == irclog2json::Options::LogFormat::NotSpecified) {
std::cerr << "Specify the log format." << std::endl;
return 1;
}
std::string input_path{argv[0]};
irclog2json::FileNameManager file_name_manager{input_path};
// ログの日付
std::optional<struct tm> tm_date = file_name_manager.GetDate();
if (!tm_date) {
std::cerr << input_path << ": Invalid date" << std::endl;
return 1;
}
// 出力ファイルのパス
std::string output_path = file_name_manager.GetOutputPath();
// チャンネル名
std::string channel{argv[1]};
std::optional<picojson::value> result;
{
std::ifstream ifs;
ifs.open(input_path);
if (ifs.fail()) {
std::perror(input_path.c_str());
return 1;
}
result = ConvertLogToJsonObjects(&ifs, channel, *tm_date, options);
}
{
std::ofstream ofs;
ofs.open(output_path);
if (ofs.fail()) {
std::perror(output_path.c_str());
return 1;
}
ofs << result->serialize(options.pretty);
}
std::cout << "Output " << output_path << '.' << std::endl;
return 0;
}
static bool ParseOptions(int argc, char* argv[], irclog2json::Options& options,
bool& print_usage) {
constexpr struct option long_options[] = {
{"tiarra", no_argument, nullptr, 't'},
{"madoka", no_argument, nullptr, 'm'},
{"iso-2022-jp", no_argument, nullptr, 'j'},
{"pretty", no_argument, nullptr, 'p'},
{"help", no_argument, nullptr, 'h'},
{0, 0, 0, 0}};
const char* optstring = "tmjph";
print_usage = false;
bool no_invalid_option = true;
int c;
int option_index = 0;
while (true) {
c = getopt_long(argc, argv, optstring, long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 't':
// -t, --tiarra
options.log_format = irclog2json::Options::LogFormat::Tiarra;
break;
case 'm':
// -m, --madoka
options.log_format = irclog2json::Options::LogFormat::Madoka;
break;
case 'j':
// -j, --iso-2022-jp
options.iso_2022_jp = true;
break;
case 'p':
// -p, --pretty
options.pretty = true;
break;
case 'h':
// -h, --help
print_usage = true;
break;
case '?':
// 無効なオプション
no_invalid_option = false;
print_usage = true;
break;
default:
break;
}
}
return no_invalid_option;
}
static picojson::value
ConvertLogToJsonObjects(std::ifstream* ifs, std::string const& channel,
struct tm const& tm_date,
irclog2json::Options const& options) {
using irclog2json::BasicIrcLogParser;
using irclog2json::IrcLogParser;
using irclog2json::MadokaLogParser;
using irclog2json::message::LineParser;
using irclog2json::message::MadokaIso2022JpLineParser;
using irclog2json::message::MadokaLineParser;
using irclog2json::message::MessageBase;
using irclog2json::message::TiarraIso2022JpLineParser;
using irclog2json::message::TiarraLineParser;
using irclog2json::message::UTF8LineParser;
using LogFormat = irclog2json::Options::LogFormat;
std::unique_ptr<IrcLogParser> parser;
{
std::unique_ptr<LineParser> line_parser;
switch (options.log_format) {
case LogFormat::Tiarra: {
{
auto utf8_line_parser =
std::make_unique<TiarraLineParser>(channel, tm_date);
if (options.iso_2022_jp) {
line_parser = std::make_unique<TiarraIso2022JpLineParser>(
std::move(utf8_line_parser));
} else {
line_parser = std::move(utf8_line_parser);
}
}
parser = std::make_unique<BasicIrcLogParser>(ifs, std::move(line_parser));
break;
}
case LogFormat::Madoka: {
{
auto utf8_line_parser =
std::make_unique<MadokaLineParser>(channel, tm_date);
if (options.iso_2022_jp) {
line_parser = std::make_unique<MadokaIso2022JpLineParser>(
std::move(utf8_line_parser));
} else {
line_parser = std::move(utf8_line_parser);
}
}
parser = std::make_unique<MadokaLogParser>(ifs, std::move(line_parser));
break;
}
default:
return picojson::value{picojson::array{}};
}
}
std::vector<std::unique_ptr<MessageBase>> messages =
parser->ExtractMessages();
picojson::array json_messages;
json_messages.reserve(messages.size());
for (const auto& m : messages) {
json_messages.emplace_back(m->ToJsonObject());
}
return picojson::value{json_messages};
}
static void PrintUsage(char* argv0, std::ostream& os) {
os << "Usage: " << argv0 << " (-t | -m) [-j] [-p] LOG_FILE CHANNEL"
<< std::endl;
os << " -t, --tiarra specify that the log file is Tiarra format"
<< std::endl;
os << " -m, --madoka specify that the log file is madoka format"
<< std::endl;
os << " -j, --iso-2022-jp specify that the charset is ISO-2022-JP"
<< std::endl;
os << " -p, --pretty output pretty-printed JSON" << std::endl;
os << " -h, --help display this help and exit" << std::endl;
}