0.6.0-1 release
[manu/suphp.git] / src / IniFile.cpp
1 /*
2     suPHP - (c)2002-2005 Sebastian Marsching <sebastian@marsching.com>
3
4     This file is part of suPHP.
5
6     suPHP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     suPHP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with suPHP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include <string>
22 #include <fstream>
23 #include <streambuf>
24 #include <sstream>
25
26 #include "KeyNotFoundException.hpp"
27
28 #include "IniFile.hpp"
29
30 using namespace suPHP;
31
32 IniSection& suPHP::IniFile::getSection(std::string name)
33     throw (KeyNotFoundException) {
34     if (this->sections.find(name) != this->sections.end()) {
35         return this->sections.find(name)->second;
36     } else {
37         throw KeyNotFoundException("Section " + name + " not found", 
38                                    __FILE__, __LINE__);
39     }
40 }
41
42 bool suPHP::IniFile::hasSection(std::string name) {
43     if (this->sections.find(name) != this->sections.end()) {
44         return true;
45     } else {
46         return false;
47     }
48     
49 }
50
51 IniSection& suPHP::IniFile::operator[](std::string name) 
52     throw (KeyNotFoundException) {
53     return this->getSection(name);
54 }
55
56
57 void suPHP::IniFile::parse(File file) throw (IOException, ParsingException) {
58     SmartPtr<std::ifstream> is = file.getInputStream();
59     IniSection *current_section = NULL;
60     while (!is->eof() && !is->bad() && !is->fail()) {
61         std::string line;
62         std::string tstr;
63         char dummy;
64         int startpos = 0;
65         int endpos = 0;
66         
67         // Read line from file
68         getline(*is, line);
69         
70         tstr = line;
71         
72         // Find first char not being space or tab
73         startpos = tstr.find_first_not_of(" \t");
74         // Find last char not being space or tab
75         endpos = tstr.find_last_not_of(" \t");
76
77         // Skip empty line, only containing whitespace
78         if (startpos == std::string::npos)
79             continue;
80         
81         // Get trimmed string
82         tstr = tstr.substr(startpos, endpos - startpos + 1);
83
84         // Is line a comment (starting with ";")?
85         if (tstr[0] == ';') {
86             // Comments are not interessting => skip
87             continue;
88        
89         // Is line a section mark ("[section]")?
90         } else if (tstr[0] == '[' && tstr[tstr.size()-1] == ']') {
91             // Extract name of section
92             std::string name = tstr.substr(1, tstr.size() - 2);
93             // If section is not yet existing, create it
94             if (!this->hasSection(name)) {
95                 std::pair<std::string, IniSection> p;
96                 IniSection sect;
97                 p.first = name;
98                 p.second = sect;
99                 this->sections.insert(p);
100             }
101             // Set current section
102             current_section = &(this->getSection(name));
103             
104         // Is the line a key-value pair?
105         } else if (tstr.find_first_of('=') != std::string::npos) {
106             std::string name;
107             std::string value;
108             int eqpos = 0;
109             
110             // Check wheter we already have a section
111             if (current_section == NULL) {
112                 throw ParsingException("Option line \"" + tstr +
113                                        "\" before first section", 
114                                        __FILE__, __LINE__);
115             }
116             
117             // Extract name
118             eqpos = tstr.find_first_of('=');
119             name = tstr.substr(0, eqpos);
120             value = tstr.substr(eqpos + 1, tstr.size() - eqpos + 1);
121             name = name.substr(0, name.find_last_not_of(" \t") + 1);
122             value = value.substr(value.find_first_not_of(" \t"));
123             if (value[0] == '"')
124                 value = value.substr(1);
125             if (value[value.size()-1] == '"')
126                 value = value.substr(0, value.size()-1);
127             current_section->putValue(name, value);
128         // Line is something we do not know
129         } else {
130             throw ParsingException("Illegal line \"" + tstr + "\"", 
131                                    __FILE__, __LINE__);
132         }
133     }
134     is->close();
135 }