// Iffy // // Douglas Thrift // // $Id$ #include "Cleaner.hpp" #include "Matcher.hpp" #include #include #include #include struct IffyCommand : public app::Application { virtual int Run(const app::ArgumentList& args) { Iffy::program = api::GetExecutablePath().GetName(); ext::String in("-"), out("-"); for (size_t index(0); index < args.GetSize(); ++index) { Matcher matcher; if (args[index] == "-D") { if (!Iffy::debug) Iffy::debug = true; } else if (args[index] == matcher("^-in=(.+)$")) { in = matcher[1]; } else if (args[index] == matcher("^-out=(.+)$")) { out = matcher[1]; } else { api::Cout << "Usage: " << Iffy::program << " [-in=in] [-out=out] [-D]\n"; return 1; } } Iffy(in, out); return 0; } } iffy; Iffy::Iffy(const ext::String& in, const ext::String& out) { _H rin; _H rout; if (in == "-") rin = &api::Cin; else rin = new api::FileReader(in); if (out == "-") rout = &api::Cout; else rout = new api::FileWriter(out); iffy(*rin, *rout); } ext::String Iffy::program; bool Iffy::debug(false); void Iffy::iffy(ios::Reader& in, ios::Writer& out) { ext::Buffer segment; bool text(false); Cleaner cleaner; while (read(in, segment, (text = !text))) if (text) { if (!segment.IsEmpty()) cleaner.insert(segment); } else { Matcher matcher; if (segment == matcher("^!--( \\(\\d{1,2}:\\d{2}:\\d{2} [AP]M\\))--$")) { cleaner.insert(matcher[1]); continue; } Tag tag(segment); if (tag == "br") tag = STANDALONE; cleaner.insert(tag); } cleaner.clean(); ios::FormatWriter fout(out); fout << cleaner << ios::NewLine; } bool Iffy::read(ios::Reader& in, ext::Buffer& segment, bool text) { segment.Clear(); byte_t atom; while (in.Get(atom)) { if (atom == (text ? '<' : '>')) return true; segment.InsertLast(atom); } return !segment.IsEmpty(); }