1 /* -*- Mode: c++; -*- */
2 /*  --------------------------------------------------------------------
3  *  Filename:
4  *    storage.h
5  *
6  *  Description:
7  *    Declaration of the Binc::Storage class
8  *
9  *  Authors:
10  *    Andreas Aardal Hanssen <andreas-binc@bincimap.org>
11  *
12  *  Bugs:
13  *
14  *  ChangeLog:
15  *
16  *  --------------------------------------------------------------------
17  *  Copyright 2002-2005 Andreas Aardal Hanssen
18  *
19  *  This program is free software; you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation; either version 2 of the License, or
22  *  (at your option) any later version.
23  *
24  *  This program is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with this program; if not, write to the Free Software
31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
32  *  --------------------------------------------------------------------
33  */
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #ifndef STORAGEPARSER_H_INCLUDED
38 #define STORAGEPARSER_H_INCLUDED
39 #include <map>
40 #include <string>
41 
42 #include <stdio.h>
43 
44 namespace Binc {
45   class Storage {
46   public:
47     enum Mode {
48       ReadOnly, WriteOnly
49     };
50 
51     Storage(const std::string &fileName, Mode mode);
52     ~Storage(void);
53 
54     bool ok(void) const;
55 
56     bool eof(void) const;
57 
58     bool get(std::string *section, std::string *key,
59 	     std::string *value);
60 
61     bool put(const std::string &section, const std::string &key,
62 	     const std::string &value);
63 
64     bool commit(void);
65 
66     std::string getLastError(void) const;
67 
68   protected:
69     void setLastError(const std::string &errorString) const;
70 
71     enum State {
72       Searching, Section, Key, Value,
73       AliasKey, AliasColon, AliasValue, AliasEnd
74     };
75 
76   private:
77     FILE *fp;
78     int fd;
79     std::string fileName;
80     State state;
81 
82     Mode mode;
83 
84     std::string lastError;
85     std::string curSection;
86     bool atEndOfFile;
87     bool firstSection;
88     std::map<std::string, std::string> aliases;
89   };
90 
getLastError(void)91   inline std::string Storage::getLastError(void) const
92   {
93     return lastError;
94   }
95 
eof(void)96   inline bool Storage::eof(void) const
97   {
98     return atEndOfFile;
99   }
100 }
101 
102 #endif
103