Applied a slightly modified version of WANG Cong patch: fix ipcmsg bugs and cleanups.
[fmail.git] / src / configuration.cpp
blob29000dc9fef130c374f668e2256fd6e2d990a42c
1 /*
2 libfmail: Generic Search Tree implementation
4 Copyright (C) 2007 Carlos Daniel Ruvalcaba Valenzuela <clsdaniel@gmail.com>
6 This program 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.
11 This program 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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <string>
22 #include <map>
23 #include <fstream>
24 #include <libfmail/configuration.h>
26 /* TODO: Do checking of file inputs */
27 Configuration::Configuration(char *filename){
28 if (filename)
29 this->Load(filename);
32 Configuration::~Configuration(){
35 int Configuration::Load(char *filename){
36 std::fstream conf;
37 std::string buffer, vname, eqop, vvalue;
39 /* Open Stream to file */
40 conf.open(filename);
42 if (conf.is_open()){
43 while (!conf.eof()){
44 conf >> vname;
45 conf >> eqop;
46 conf >> std::ws >> vvalue;
47 if (eqop == "=")
48 conf_map[vname] = vvalue;
50 return 0;
53 return -1;
56 /* TODO: Implement saving */
57 int Configuration::Save(char *filename){
58 if (filename)
59 return 0;
60 return 1;
63 std::string Configuration::getString(char *key, char *default_value){
64 std::map<std::string, std::string>::iterator it;
65 std::string ret;
67 if (conf_map.find(key) == conf_map.end()){
68 return std::string(default_value);
71 return conf_map[key];
74 const char* Configuration::getCString(char *key, char *default_value){
75 if (conf_map.find(key) == conf_map.end()){
76 return default_value;
79 return conf_map[key].c_str();
82 int Configuration::getInt(char *key, int default_value){
83 if (conf_map.find(key) == conf_map.end()){
84 return default_value;
87 return atoi(conf_map[key].c_str());
90 float Configuration::getFloat(char *key, float default_value){
91 if (conf_map.find(key) == conf_map.end()){
92 return default_value;
95 return atof(conf_map[key].c_str());
98 /* TODO: Fill setString */
99 void Configuration::setString(char *key, char *value){
100 conf_map[key] = std::string(value);
103 /* TODO: Fill setInt */
104 void Configuration::setInt(char *key, int value){
105 char buffer[32];
107 sprintf(buffer, "%i", value);
108 conf_map[key] = std::string(buffer);
111 /* TODO: Fill setFloat */
112 void Configuration::setFloat(char *key, float value){
113 char buffer[32];
115 sprintf(buffer, "%f", value);
116 conf_map[key] = std::string(buffer);