1 |
// Iffy |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Tag.hpp" |
8 |
#include "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 |
if (in == "-" && out == "-") |
55 |
{ |
56 |
iffy(api::Cin, api::Cout); |
57 |
} |
58 |
else if (in == "-") |
59 |
{ |
60 |
api::FileWriter fout(out); |
61 |
|
62 |
iffy(api::Cin, fout); |
63 |
} |
64 |
else if (out == "-") |
65 |
{ |
66 |
api::FileReader fin(in); |
67 |
|
68 |
iffy(fin, api::Cout); |
69 |
} |
70 |
else |
71 |
{ |
72 |
api::FileReader fin(in); |
73 |
api::FileWriter fout(out); |
74 |
|
75 |
iffy(fin, fout); |
76 |
} |
77 |
} |
78 |
|
79 |
ext::String Iffy::program; |
80 |
bool Iffy::debug(false); |
81 |
|
82 |
void Iffy::iffy(ios::Reader& in, ios::Writer& out) |
83 |
{ |
84 |
ext::String segment; |
85 |
bool text(true); |
86 |
ios::FormatWriter fout(out); |
87 |
|
88 |
while (read(in, segment, text)) if (text) |
89 |
{ |
90 |
fout << segment << ios::Flush; |
91 |
|
92 |
text = false; |
93 |
} |
94 |
else |
95 |
{ |
96 |
Tag tag(segment); |
97 |
|
98 |
if (tag == "br") tag = STANDALONE; |
99 |
|
100 |
// XXX: automagically fix |
101 |
|
102 |
fout << tag << ios::Flush; |
103 |
|
104 |
text = true; |
105 |
} |
106 |
} |
107 |
|
108 |
bool Iffy::read(ios::Reader& in, ext::String& segment, bool text) |
109 |
{ |
110 |
segment.Clear(); |
111 |
|
112 |
byte_t atom; |
113 |
|
114 |
while (in.Get(atom)) |
115 |
{ |
116 |
if (atom == (text ? '<' : '>')) return true; |
117 |
|
118 |
segment.InsertLast(atom); |
119 |
} |
120 |
|
121 |
return !segment.IsEmpty(); |
122 |
} |