1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
35 #include <synfig/general.h>
39 /* === U S I N G =========================================================== */
43 using namespace synfig;
44 using namespace synfigapp;
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 /* === P R O C E D U R E S ================================================= */
52 /* === M E T H O D S ======================================================= */
63 Settings::get_value(const synfig::String& key)const
66 if(!get_value(key,value))
67 return synfig::String();
72 Settings::add_domain(Settings* domain, const synfig::String& name)
74 domain_map[name]=domain;
78 Settings::remove_domain(const synfig::String& name)
80 domain_map.erase(name);
84 Settings::get_value(const synfig::String& key, synfig::String& value)const
86 // Search for the value in any children domains
87 DomainMap::const_iterator iter;
88 for(iter=domain_map.begin();iter!=domain_map.end();++iter)
90 // if we have a domain hit
91 if(key.size()>iter->first.size() && String(key.begin(),key.begin()+iter->first.size())==iter->first)
93 synfig::String key_(key.begin()+iter->first.size()+1,key.end());
95 // If the domain has it, then we have got a hit
96 if(iter->second->get_value(key_,value))
101 // Search for the value in our simple map
102 if(simple_value_map.count(key))
104 value=simple_value_map.find(key)->second;
113 Settings::set_value(const synfig::String& key,const synfig::String& value)
115 // Search for the key in any children domains
116 DomainMap::iterator iter;
117 for(iter=domain_map.begin();iter!=domain_map.end();++iter)
119 // if we have a domain hit
120 if(key.size()>iter->first.size() && String(key.begin(),key.begin()+iter->first.size())==iter->first)
122 synfig::String key_(key.begin()+iter->first.size()+1,key.end());
124 return iter->second->set_value(key_,value);
128 simple_value_map[key]=value;
133 Settings::get_key_list()const
137 // Get keys from the domains
139 DomainMap::const_iterator iter;
140 for(iter=domain_map.begin();iter!=domain_map.end();++iter)
142 KeyList sub_key_list(iter->second->get_key_list());
143 KeyList::iterator key_iter;
144 for(key_iter=sub_key_list.begin();key_iter!=sub_key_list.end();++key_iter)
145 key_list.push_back(iter->first+'.'+*key_iter);
149 // Get keys from the simple variables
151 ValueBaseMap::const_iterator iter;
152 for(iter=simple_value_map.begin();iter!=simple_value_map.end();++iter)
153 key_list.push_back(iter->first);
163 Settings::save_to_file(const synfig::String& filename)const
165 synfig::String tmp_filename(filename+".TMP");
169 std::ofstream file(tmp_filename.c_str());
171 if(!file)return false;
173 KeyList key_list(get_key_list());
177 KeyList::const_iterator iter;
178 for(iter=key_list.begin();iter!=key_list.end();++iter)
180 if(!file)return false;
181 file<<*iter<<'='<<get_value(*iter)<<endl;
187 }catch(...) { return false; }
190 char old_file[80]="sif.XXXXXXXX";
192 rename(filename.c_str(),old_file);
193 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
195 rename(old_file,tmp_filename.c_str());
200 if(rename(tmp_filename.c_str(),filename.c_str())!=0)
208 Settings::load_from_file(const synfig::String& filename)
210 std::ifstream file(filename.c_str());
217 if(!line.empty() && ((line[0]>='a' && line[0]<='z')||(line[0]>='A' && line[0]<='Z')))
219 std::string::iterator equal(find(line.begin(),line.end(),'='));
220 if(equal==line.end())
222 std::string key(line.begin(),equal);
223 std::string value(equal+1,line.end());
225 //synfig::info("Settings::load_from_file(): Trying Key \"%s\" with a value of \"%s\".",key.c_str(),value.c_str());
227 if(!set_value(key,value))
228 synfig::warning("Settings::load_from_file(): Key \"%s\" with a value of \"%s\" was rejected.",key.c_str(),value.c_str());
232 synfig::error("Settings::load_from_file(): Attept to set key \"%s\" with a value of \"%s\" has thrown an exception.",key.c_str(),value.c_str());