Added yp2::PlainFile class which reads Unix-style plain text files.
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 25 Jan 2006 11:27:19 +0000 (11:27 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 25 Jan 2006 11:27:19 +0000 (11:27 +0000)
src/Makefile.am
src/plainfile.cpp [new file with mode: 0644]
src/util.hpp

index 05bd100..f9d5e14 100644 (file)
@@ -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 (file)
index 0000000..07361aa
--- /dev/null
@@ -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 <cstdlib>
+#include <string>
+#include <iostream>
+#include <fstream>
+
+#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<std::string> &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
+ */
index 45add31..5b81d04 100644 (file)
@@ -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%
 #include <yaz/z-core.h>
 #include <string>
 #include <list>
+#include <vector>
 
 #include <boost/utility.hpp>
-
+#include <boost/scoped_ptr.hpp>
 #include "package.hpp"
 
 namespace yp2 {
@@ -74,7 +75,16 @@ namespace yp2 {
     private:
         ODR m_odr;
     };
-        
+
+    class PlainFile {
+        class Rep;
+        boost::scoped_ptr<Rep> m_p;
+    public:
+        PlainFile();
+        ~PlainFile();
+        bool open(const std::string &fname);
+        bool getline(std::vector<std::string> &args);
+    };
 }
 #endif
 /*