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