Import upstream 0.7.1
[manu/suphp.git] / src / IniSection.cpp
1 /*
2     suPHP - (c)2002-2008 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
23 #include "KeyNotFoundException.hpp"
24
25 #include "IniSection.hpp"
26
27 using namespace suPHP;
28
29 void suPHP::IniSection::putValue(const std::string key, const std::string value) {
30     std::pair<std::string, std::string> p;
31     p.first = key;
32     p.second = value;
33     this->entries.insert(p);
34 }
35
36 const std::vector<std::string> suPHP::IniSection::getValues(const std::string& key) const
37     throw (KeyNotFoundException) {
38     std::vector<std::string> values;
39     std::pair<std::multimap<const std::string, const std::string>::const_iterator, std::multimap<const std::string, const std::string>::const_iterator> range = this->entries.equal_range(key);
40     for (std::multimap<const std::string, const std::string>::const_iterator pos = 
41         range.first; pos != range.second; pos++) {
42         values.push_back(pos->second);
43     }
44     if (values.size() == 0) {
45         throw KeyNotFoundException("No value for key " + key + " found", 
46                                    __FILE__, __LINE__);
47     }
48     return values;
49 }
50
51 std::string suPHP::IniSection::getValue(const std::string& key) const
52     throw (KeyNotFoundException) {
53     std::vector<std::string> values;
54     if (this->entries.find(key) != this->entries.end()) {
55         return this->entries.find(key)->second;
56     } else {
57         throw KeyNotFoundException("No value for key " + key + " found", 
58                                    __FILE__, __LINE__);
59     }
60     
61 }
62
63 const std::vector<std::string> suPHP::IniSection::getKeys() const {
64     std::vector<std::string> keys;
65     for (std::multimap<const std::string, const std::string>::const_iterator pos =
66              this->entries.begin();
67          pos != this->entries.end(); pos++) {
68         keys.push_back(pos->first);
69     }
70     return keys;
71 }
72
73 bool suPHP::IniSection::hasKey(const std::string& key) const {
74     if (this->entries.find(key) != this->entries.end()) {
75         return true;
76     } else {
77         return false;
78     }
79     
80 }
81
82 const std::vector<std::string> suPHP::IniSection::operator[](const std::string& key) const
83     throw (KeyNotFoundException) {
84     return this->getValues(key);
85 }
86
87 void suPHP::IniSection::removeValues(const std::string& key) {
88     this->entries.erase(key);
89 }