ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Iffy/Iffy.cpp
Revision: 281
Committed: 2004-11-14T14:41:47-08:00 (20 years, 7 months ago) by Douglas Thrift
File size: 2240 byte(s)
Log Message:
Timestamp!

File Contents

# User Rev Content
1 Douglas Thrift 275 // Iffy
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7 Douglas Thrift 276 #include "Tag.hpp"
8     #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     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 Douglas Thrift 279 ios::FormatWriter fout(out);
86    
87     fout << "<html>";
88    
89 Douglas Thrift 276 ext::String segment;
90 Douglas Thrift 281 bool text(false);
91 Douglas Thrift 277 ext::Stack<ext::String> opens;
92 Douglas Thrift 276
93 Douglas Thrift 281 while (read(in, segment, (text = !text))) if (text)
94 Douglas Thrift 276 {
95 Douglas Thrift 278 fout << segment;
96 Douglas Thrift 276 }
97     else
98     {
99 Douglas Thrift 281 Matcher matcher;
100    
101     if (segment == matcher("^!--( \\(\\d{1,2}:\\d{2}:\\d{2} [AP]M\\))--$"))
102     {
103     fout << matcher[1];
104    
105     continue;
106     }
107    
108 Douglas Thrift 276 Tag tag(segment);
109    
110     if (tag == "br") tag = STANDALONE;
111    
112 Douglas Thrift 277 switch (tag)
113     {
114     case OPEN:
115     opens.Push(tag);
116 Douglas Thrift 276
117 Douglas Thrift 277 break;
118     case CLOSE:
119     if (tag != opens.Top()) tag = opens.Top();
120    
121     opens.Pop();
122    
123     break;
124     default:
125     break;
126     }
127    
128 Douglas Thrift 278 fout << tag;
129 Douglas Thrift 276 }
130 Douglas Thrift 279
131     fout << "</html>\n";
132 Douglas Thrift 276 }
133    
134     bool Iffy::read(ios::Reader& in, ext::String& segment, bool text)
135     {
136     segment.Clear();
137    
138     byte_t atom;
139    
140     while (in.Get(atom))
141     {
142     if (atom == (text ? '<' : '>')) return true;
143    
144     segment.InsertLast(atom);
145     }
146    
147     return !segment.IsEmpty();
148     }

Properties

Name Value
svn:eol-style native
svn:keywords Id