GPL v2.
[metaproxy-moved-to-github.git] / src / plainfile.cpp
1 /* $Id: plainfile.cpp,v 1.5 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23
24 #include <cstdlib>
25 #include <string>
26 #include <iostream>
27 #include <fstream>
28
29 #include "util.hpp"
30
31 #define PLAINFILE_MAX_LINE 256
32
33 namespace mp = metaproxy_1;
34
35 namespace metaproxy_1 {
36     class PlainFile::Rep {
37         friend class PlainFile;
38         Rep();
39         void close();
40         int lineno;
41         std::ifstream *fh;
42     };
43 }
44
45 mp::PlainFile::Rep::Rep() : lineno(1)
46 {
47     fh = 0;
48 }
49
50 mp::PlainFile::PlainFile() : m_p(new Rep)
51 {
52 }
53
54 void mp::PlainFile::Rep::close()
55 {
56     delete fh;
57     fh = 0;
58     lineno = 0;
59 }
60
61 mp::PlainFile::~PlainFile()
62 {
63     m_p->close();
64 }
65
66 bool mp::PlainFile::open(const std::string &fname)
67 {
68     m_p->close();
69
70     std::ifstream *new_file = new std::ifstream(fname.c_str());
71     if (! *new_file)
72     {
73         delete new_file;
74         return false;
75     }
76     m_p->fh = new_file;
77     return true;
78 }
79
80 bool mp::PlainFile::getline(std::vector<std::string> &args)
81 {
82     args.clear();
83
84     if (!m_p->fh)
85         return false;  // no file at all.
86
87     char line_cstr[PLAINFILE_MAX_LINE];
88     while (true)
89     {
90         if (m_p->fh->eof())
91         {
92             m_p->close();   // might as well close it now
93             return false;
94         }
95         
96         m_p->lineno++;
97         m_p->fh->getline(line_cstr, PLAINFILE_MAX_LINE-1);
98         char first = line_cstr[0];
99         if (first && !strchr("# \t", first))
100             break;
101         // comment or blank line.. read next.
102     }
103     const char *cp = line_cstr;
104     while (true)
105     {
106         // skip whitespace
107         while (*cp && strchr(" \t", *cp))
108             cp++;
109         if (*cp == '\0')
110             break;
111         const char *cp0 = cp;
112         while (*cp && !strchr(" \t", *cp))
113             cp++;
114         std::string arg(cp0, cp - cp0);
115         args.push_back(arg);
116     }
117     return true;
118 }
119
120 /*
121  * Local variables:
122  * c-basic-offset: 4
123  * indent-tabs-mode: nil
124  * c-file-style: "stroustrup"
125  * End:
126  * vim: shiftwidth=4 tabstop=8 expandtab
127  */