1 |
// Iffy |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Cleaner.hpp" |
8 |
#include "Matcher/Matcher.hpp" |
9 |
|
10 |
#include <menes-api/exename.hpp> |
11 |
#include <menes-api/files.hpp> |
12 |
#include <menes-app/application.hpp> |
13 |
|
14 |
struct IffyCommand : public app::Application |
15 |
{ |
16 |
virtual int Run(const app::ArgumentList& args) |
17 |
{ |
18 |
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 |
return 0; |
49 |
} |
50 |
} iffy; |
51 |
|
52 |
Iffy::Iffy(const ext::String& in, const ext::String& out) |
53 |
{ |
54 |
_H<ios::Reader> rin; |
55 |
_H<ios::Writer> rout; |
56 |
|
57 |
if (in == "-") rin = &api::Cin; else rin = new api::FileReader(in); |
58 |
if (out == "-") rout = &api::Cout; else rout = new api::FileWriter(out); |
59 |
|
60 |
iffy(*rin, *rout); |
61 |
} |
62 |
|
63 |
ext::String Iffy::program; |
64 |
bool Iffy::debug(false); |
65 |
|
66 |
void Iffy::iffy(ios::Reader& in, ios::Writer& out) |
67 |
{ |
68 |
ext::Buffer segment; |
69 |
bool text(false); |
70 |
Cleaner cleaner; |
71 |
|
72 |
while (read(in, segment, (text = !text))) if (text) |
73 |
{ |
74 |
if (!segment.IsEmpty()) cleaner.insert(segment); |
75 |
} |
76 |
else |
77 |
{ |
78 |
Matcher matcher; |
79 |
|
80 |
if (segment == matcher("^!--( \\(\\d{1,2}:\\d{2}:\\d{2} [AP]M\\))--$")) |
81 |
{ |
82 |
cleaner.insert(matcher[1]); |
83 |
|
84 |
continue; |
85 |
} |
86 |
|
87 |
Tag tag(segment); |
88 |
|
89 |
if (tag == "br") tag = STANDALONE; |
90 |
|
91 |
cleaner.insert(tag); |
92 |
} |
93 |
|
94 |
cleaner.clean(); |
95 |
|
96 |
ios::FormatWriter fout(out); |
97 |
|
98 |
fout << cleaner << ios::NewLine; |
99 |
} |
100 |
|
101 |
bool Iffy::read(ios::Reader& in, ext::Buffer& segment, bool text) |
102 |
{ |
103 |
segment.Clear(); |
104 |
|
105 |
byte_t atom; |
106 |
|
107 |
while (in.Get(atom)) |
108 |
{ |
109 |
if (atom == (text ? '<' : '>')) return true; |
110 |
|
111 |
segment.InsertLast(atom); |
112 |
} |
113 |
|
114 |
return !segment.IsEmpty(); |
115 |
} |