git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / lang / ResourceBundle.cpp
blob4ce8f9d660b08db908270375cfd399d03c61b54f
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D 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 // Scorched3D 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
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <lang/ResourceBundle.h>
22 #include <lang/ResourceBundleEntryImpl.h>
23 #include <common/DefinesString.h>
25 ResourceBundleEntry *ResourceBundle::getEntry(const std::string &key)
27 ResourceBundleEntryImpl searchEntry(key);
28 ResourceBundleSet::iterator findItor = entries_.find(&searchEntry);
29 if (findItor == entries_.end()) return 0;
30 return *findItor;
33 void ResourceBundle::addEntry(ResourceBundleEntry *entry)
35 ResourceBundleSet::iterator findItor = entries_.find(entry);
36 if (findItor != entries_.end())
38 entries_.erase(findItor);
39 delete *findItor;
41 entries_.insert(entry);
44 bool ResourceBundle::loadFromFile(const std::string &file)
46 FILE *in = fopen(file.c_str(), "r");
47 if (!in) return false;
49 char buffer[2048];
50 while (fgets(buffer, 2048, in) != 0)
52 std::string key, value;
54 bool inKey = true;
55 for (char *start=buffer; *start; start++)
57 if (inKey)
59 if (*start == '=') inKey = false;
60 else key.push_back(*start);
62 else value.push_back(*start);
64 S3D::trim(key);
65 S3D::trim(value);
66 if (key.size() == 0) continue;
67 if (key[0] == '#' || key[0] == '\\' || key[0] == ';') continue;
69 LangString langValue;
70 LangStringUtil::appendToLang(langValue, value);
72 ResourceBundleEntry *entry = new ResourceBundleEntryImpl(key, langValue);
73 addEntry(entry);
75 fclose(in);
77 return true;
80 bool ResourceBundle::writeToFile(const std::string &file)
82 FILE *out = fopen(file.c_str(), "w");
83 if (!out) return false;
85 ResourceBundleSet::iterator itor;
86 for (itor = entries_.begin();
87 itor != entries_.end();
88 ++itor)
90 ResourceBundleEntry *entry = *itor;
92 std::string value = LangStringUtil::convertFromLang(entry->getValue());
93 fprintf(out, "%s = %s\n", entry->getKey(), value.c_str());
96 fclose(out);
97 return true;