From: Adam Dickmeiss Date: Wed, 25 Jan 2006 11:27:19 +0000 (+0000) Subject: Added yp2::PlainFile class which reads Unix-style plain text files. X-Git-Tag: YP2.0.0.3~9 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=f47e8cf4cde2ba5465106e219c803e6424a4f177;p=metaproxy-moved-to-github.git Added yp2::PlainFile class which reads Unix-style plain text files. --- diff --git a/src/Makefile.am b/src/Makefile.am index 05bd100..f9d5e14 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.46 2006-01-19 21:43:51 adam Exp $ +## $Id: Makefile.am,v 1.47 2006-01-25 11:27:19 adam Exp $ MAINTAINERCLEANFILES = Makefile.in config.in config.hpp @@ -31,7 +31,7 @@ libyp2_la_SOURCES = \ filter_http_file.cpp filter_http_file.hpp \ factory_static.cpp factory_static.hpp \ pipe.cpp pipe.hpp \ - util.cpp util.hpp \ + util.cpp util.hpp plainfile.cpp \ xmlutil.cpp xmlutil.hpp # Rules for programs.. diff --git a/src/plainfile.cpp b/src/plainfile.cpp new file mode 100644 index 0000000..07361aa --- /dev/null +++ b/src/plainfile.cpp @@ -0,0 +1,110 @@ +/* $Id: plainfile.cpp,v 1.1 2006-01-25 11:27:19 adam Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#include "config.hpp" + +#include +#include +#include +#include + +#include "util.hpp" + +#define PLAINFILE_MAX_LINE 256 + +namespace yp2 { + class PlainFile::Rep { + friend class PlainFile; + Rep(); + void close(); + int lineno; + std::ifstream *fh; + }; +} + +yp2::PlainFile::Rep::Rep() : lineno(1) +{ + fh = 0; +} + +yp2::PlainFile::PlainFile() : m_p(new Rep) +{ +} + +void yp2::PlainFile::Rep::close() +{ + delete fh; + fh = 0; + lineno = 0; +} + +yp2::PlainFile::~PlainFile() +{ + m_p->close(); +} + +bool yp2::PlainFile::open(const std::string &fname) +{ + m_p->close(); + + std::ifstream *new_file = new std::ifstream(fname.c_str()); + if (! *new_file) + { + delete new_file; + return false; + } + m_p->fh = new_file; + return true; +} + +bool yp2::PlainFile::getline(std::vector &args) +{ + args.clear(); + + if (!m_p->fh) + return false; // no file at all. + + char line_cstr[PLAINFILE_MAX_LINE]; + while (true) + { + if (m_p->fh->eof()) + { + m_p->close(); // might as well close it now + return false; + } + + m_p->lineno++; + m_p->fh->getline(line_cstr, PLAINFILE_MAX_LINE-1); + char first = line_cstr[0]; + if (first && !strchr("# \t", first)) + break; + // comment or blank line.. read next. + } + const char *cp = line_cstr; + while (true) + { + // skip whitespace + while (*cp && strchr(" \t", *cp)) + cp++; + if (*cp == '\0') + break; + const char *cp0 = cp; + while (*cp && !strchr(" \t", *cp)) + cp++; + std::string arg(cp0, cp - cp0); + args.push_back(arg); + } + return true; +} + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/util.hpp b/src/util.hpp index 45add31..5b81d04 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -1,4 +1,4 @@ -/* $Id: util.hpp,v 1.11 2006-01-20 22:38:12 marc Exp $ +/* $Id: util.hpp,v 1.12 2006-01-25 11:27:19 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -10,9 +10,10 @@ #include #include #include +#include #include - +#include #include "package.hpp" namespace yp2 { @@ -74,7 +75,16 @@ namespace yp2 { private: ODR m_odr; }; - + + class PlainFile { + class Rep; + boost::scoped_ptr m_p; + public: + PlainFile(); + ~PlainFile(); + bool open(const std::string &fname); + bool getline(std::vector &args); + }; } #endif /*