ResourceBundle.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <lang/ResourceBundle.h>
00022 #include <lang/ResourceBundleEntryImpl.h>
00023 #include <common/DefinesString.h>
00024 
00025 ResourceBundleEntry *ResourceBundle::getEntry(const std::string &key)
00026 {
00027         ResourceBundleEntryImpl searchEntry(key);
00028         ResourceBundleSet::iterator findItor = entries_.find(&searchEntry);
00029         if (findItor == entries_.end()) return 0;
00030         return *findItor;
00031 }
00032 
00033 void ResourceBundle::addEntry(ResourceBundleEntry *entry)
00034 {
00035         ResourceBundleSet::iterator findItor = entries_.find(entry);
00036         if (findItor != entries_.end())
00037         {
00038                 entries_.erase(findItor);
00039                 delete *findItor;
00040         }
00041         entries_.insert(entry);
00042 }
00043 
00044 bool ResourceBundle::loadFromFile(const std::string &file)
00045 {
00046         FILE *in = fopen(file.c_str(), "r");
00047         if (!in) return false;
00048 
00049         char buffer[2048];
00050         while (fgets(buffer, 2048, in) != 0)
00051         {
00052                 std::string key, value;
00053 
00054                 bool inKey = true;
00055                 for (char *start=buffer; *start; start++)
00056                 {                       
00057                         if (inKey) 
00058                         {
00059                                 if (*start == '=') inKey = false;
00060                                 else key.push_back(*start);
00061                         }
00062                         else value.push_back(*start);
00063                 }
00064                 S3D::trim(key);
00065                 S3D::trim(value);
00066                 if (key.size() == 0) continue;
00067                 if (key[0] == '#' || key[0] == '\\' || key[0] == ';') continue;
00068 
00069                 LangString langValue;
00070                 LangStringUtil::appendToLang(langValue, value);
00071 
00072                 ResourceBundleEntry *entry = new ResourceBundleEntryImpl(key, langValue);
00073                 addEntry(entry);
00074         }
00075         fclose(in);
00076 
00077         return true;
00078 }
00079 
00080 bool ResourceBundle::writeToFile(const std::string &file)
00081 {
00082         FILE *out = fopen(file.c_str(), "w");
00083         if (!out) return false;
00084 
00085         ResourceBundleSet::iterator itor;
00086         for (itor = entries_.begin();
00087                 itor != entries_.end();
00088                 ++itor)
00089         {
00090                 ResourceBundleEntry *entry = *itor;
00091 
00092                 std::string value = LangStringUtil::convertFromLang(entry->getValue());
00093                 fprintf(out, "%s = %s\n", entry->getKey(), value.c_str());
00094         }
00095 
00096         fclose(out);
00097         return true;
00098 }

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3