1 |
Douglas Thrift |
275 |
// Iffy |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
Douglas Thrift |
284 |
#include "Cleaner.hpp" |
8 |
douglas |
321 |
#include "Matcher/Matcher.hpp" |
9 |
Douglas Thrift |
275 |
|
10 |
Douglas Thrift |
276 |
#include <menes-api/exename.hpp> |
11 |
|
|
#include <menes-api/files.hpp> |
12 |
Douglas Thrift |
275 |
#include <menes-app/application.hpp> |
13 |
|
|
|
14 |
|
|
struct IffyCommand : public app::Application |
15 |
|
|
{ |
16 |
|
|
virtual int Run(const app::ArgumentList& args) |
17 |
|
|
{ |
18 |
Douglas Thrift |
276 |
Iffy::program = api::GetExecutablePath().GetName(); |
19 |
|
|
|
20 |
|
|
ext::String in("-"), out("-"); |
21 |
|
|
|
22 |
|
|
for (size_t index(0); index < args.GetSize(); ++index) |
23 |
|
|
{ |
24 |
|
|
Matcher matcher; |
25 |
|
|
|
26 |
|
|
if (args[index] == "-D") |
27 |
|
|
{ |
28 |
|
|
if (!Iffy::debug) Iffy::debug = true; |
29 |
|
|
} |
30 |
|
|
else if (args[index] == matcher("^-in=(.+)$")) |
31 |
|
|
{ |
32 |
|
|
in = matcher[1]; |
33 |
|
|
} |
34 |
|
|
else if (args[index] == matcher("^-out=(.+)$")) |
35 |
|
|
{ |
36 |
|
|
out = matcher[1]; |
37 |
|
|
} |
38 |
|
|
else |
39 |
|
|
{ |
40 |
|
|
api::Cout << "Usage: " << Iffy::program << " [-in=in] [-out=out] [-D]\n"; |
41 |
|
|
|
42 |
|
|
return 1; |
43 |
|
|
} |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
Iffy(in, out); |
47 |
|
|
|
48 |
Douglas Thrift |
275 |
return 0; |
49 |
|
|
} |
50 |
|
|
} iffy; |
51 |
Douglas Thrift |
276 |
|
52 |
|
|
Iffy::Iffy(const ext::String& in, const ext::String& out) |
53 |
|
|
{ |
54 |
douglas |
293 |
_H<ios::Reader> rin; |
55 |
|
|
_H<ios::Writer> rout; |
56 |
Douglas Thrift |
276 |
|
57 |
douglas |
293 |
if (in == "-") rin = &api::Cin; else rin = new api::FileReader(in); |
58 |
|
|
if (out == "-") rout = &api::Cout; else rout = new api::FileWriter(out); |
59 |
Douglas Thrift |
276 |
|
60 |
douglas |
293 |
iffy(*rin, *rout); |
61 |
Douglas Thrift |
276 |
} |
62 |
|
|
|
63 |
|
|
ext::String Iffy::program; |
64 |
|
|
bool Iffy::debug(false); |
65 |
|
|
|
66 |
|
|
void Iffy::iffy(ios::Reader& in, ios::Writer& out) |
67 |
|
|
{ |
68 |
douglas |
294 |
ext::Buffer segment; |
69 |
Douglas Thrift |
281 |
bool text(false); |
70 |
Douglas Thrift |
284 |
Cleaner cleaner; |
71 |
Douglas Thrift |
276 |
|
72 |
Douglas Thrift |
281 |
while (read(in, segment, (text = !text))) if (text) |
73 |
Douglas Thrift |
276 |
{ |
74 |
douglas |
294 |
if (!segment.IsEmpty()) cleaner.insert(segment); |
75 |
Douglas Thrift |
276 |
} |
76 |
|
|
else |
77 |
|
|
{ |
78 |
Douglas Thrift |
281 |
Matcher matcher; |
79 |
|
|
|
80 |
|
|
if (segment == matcher("^!--( \\(\\d{1,2}:\\d{2}:\\d{2} [AP]M\\))--$")) |
81 |
|
|
{ |
82 |
Douglas Thrift |
284 |
cleaner.insert(matcher[1]); |
83 |
Douglas Thrift |
281 |
|
84 |
|
|
continue; |
85 |
|
|
} |
86 |
|
|
|
87 |
Douglas Thrift |
276 |
Tag tag(segment); |
88 |
|
|
|
89 |
|
|
if (tag == "br") tag = STANDALONE; |
90 |
|
|
|
91 |
Douglas Thrift |
284 |
cleaner.insert(tag); |
92 |
|
|
} |
93 |
Douglas Thrift |
276 |
|
94 |
Douglas Thrift |
284 |
cleaner.clean(); |
95 |
Douglas Thrift |
277 |
|
96 |
Douglas Thrift |
284 |
ios::FormatWriter fout(out); |
97 |
Douglas Thrift |
277 |
|
98 |
douglas |
295 |
fout << cleaner << ios::NewLine; |
99 |
Douglas Thrift |
276 |
} |
100 |
|
|
|
101 |
douglas |
294 |
bool Iffy::read(ios::Reader& in, ext::Buffer& segment, bool text) |
102 |
Douglas Thrift |
276 |
{ |
103 |
|
|
segment.Clear(); |
104 |
|
|
|
105 |
douglas |
294 |
byte_t atom; |
106 |
Douglas Thrift |
276 |
|
107 |
|
|
while (in.Get(atom)) |
108 |
|
|
{ |
109 |
|
|
if (atom == (text ? '<' : '>')) return true; |
110 |
|
|
|
111 |
|
|
segment.InsertLast(atom); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
return !segment.IsEmpty(); |
115 |
|
|
} |