4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include "Iffy.hpp" |
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 |
+ |
#include <menes-ext/stack.hpp> |
14 |
|
|
15 |
|
struct IffyCommand : public app::Application |
16 |
|
{ |
17 |
|
virtual int Run(const app::ArgumentList& args) |
18 |
|
{ |
19 |
+ |
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 |
|
return 0; |
50 |
|
} |
51 |
|
} iffy; |
52 |
+ |
|
53 |
+ |
Iffy::Iffy(const ext::String& in, const ext::String& out) |
54 |
+ |
{ |
55 |
+ |
if (in == "-" && out == "-") |
56 |
+ |
{ |
57 |
+ |
iffy(api::Cin, api::Cout); |
58 |
+ |
} |
59 |
+ |
else if (in == "-") |
60 |
+ |
{ |
61 |
+ |
api::FileWriter fout(out); |
62 |
+ |
|
63 |
+ |
iffy(api::Cin, fout); |
64 |
+ |
} |
65 |
+ |
else if (out == "-") |
66 |
+ |
{ |
67 |
+ |
api::FileReader fin(in); |
68 |
+ |
|
69 |
+ |
iffy(fin, api::Cout); |
70 |
+ |
} |
71 |
+ |
else |
72 |
+ |
{ |
73 |
+ |
api::FileReader fin(in); |
74 |
+ |
api::FileWriter fout(out); |
75 |
+ |
|
76 |
+ |
iffy(fin, fout); |
77 |
+ |
} |
78 |
+ |
} |
79 |
+ |
|
80 |
+ |
ext::String Iffy::program; |
81 |
+ |
bool Iffy::debug(false); |
82 |
+ |
|
83 |
+ |
void Iffy::iffy(ios::Reader& in, ios::Writer& out) |
84 |
+ |
{ |
85 |
+ |
ext::String segment; |
86 |
+ |
bool text(true); |
87 |
+ |
ios::FormatWriter fout(out); |
88 |
+ |
ext::Stack<ext::String> opens; |
89 |
+ |
|
90 |
+ |
while (read(in, segment, text)) if (text) |
91 |
+ |
{ |
92 |
+ |
fout << segment; |
93 |
+ |
|
94 |
+ |
text = false; |
95 |
+ |
} |
96 |
+ |
else |
97 |
+ |
{ |
98 |
+ |
Tag tag(segment); |
99 |
+ |
|
100 |
+ |
if (tag == "br") tag = STANDALONE; |
101 |
+ |
|
102 |
+ |
switch (tag) |
103 |
+ |
{ |
104 |
+ |
case OPEN: |
105 |
+ |
opens.Push(tag); |
106 |
+ |
|
107 |
+ |
break; |
108 |
+ |
case CLOSE: |
109 |
+ |
if (tag != opens.Top()) tag = opens.Top(); |
110 |
+ |
|
111 |
+ |
opens.Pop(); |
112 |
+ |
|
113 |
+ |
break; |
114 |
+ |
default: |
115 |
+ |
break; |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
fout << tag; |
119 |
+ |
|
120 |
+ |
text = true; |
121 |
+ |
} |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
bool Iffy::read(ios::Reader& in, ext::String& segment, bool text) |
125 |
+ |
{ |
126 |
+ |
segment.Clear(); |
127 |
+ |
|
128 |
+ |
byte_t atom; |
129 |
+ |
|
130 |
+ |
while (in.Get(atom)) |
131 |
+ |
{ |
132 |
+ |
if (atom == (text ? '<' : '>')) return true; |
133 |
+ |
|
134 |
+ |
segment.InsertLast(atom); |
135 |
+ |
} |
136 |
+ |
|
137 |
+ |
return !segment.IsEmpty(); |
138 |
+ |
} |