1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
4 // This file is part of Scorched3D.
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;
33 void ResourceBundle::addEntry(ResourceBundleEntry
*entry
)
35 ResourceBundleSet::iterator findItor
= entries_
.find(entry
);
36 if (findItor
!= entries_
.end())
38 entries_
.erase(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;
50 while (fgets(buffer
, 2048, in
) != 0)
52 std::string key
, value
;
55 for (char *start
=buffer
; *start
; start
++)
59 if (*start
== '=') inKey
= false;
60 else key
.push_back(*start
);
62 else value
.push_back(*start
);
66 if (key
.size() == 0) continue;
67 if (key
[0] == '#' || key
[0] == '\\' || key
[0] == ';') continue;
70 LangStringUtil::appendToLang(langValue
, value
);
72 ResourceBundleEntry
*entry
= new ResourceBundleEntryImpl(key
, langValue
);
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();
90 ResourceBundleEntry
*entry
= *itor
;
92 std::string value
= LangStringUtil::convertFromLang(entry
->getValue());
93 fprintf(out
, "%s = %s\n", entry
->getKey(), value
.c_str());