Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

ConfigurationFile.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * コンフィグレーションファイル実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Core/Utility/ConfigurationFile.h"
00027 #include "Core/InputOutput/TextFileReader.h"
00028 #include "Core/InputOutput/TextFileWriter.h"
00029 #include "Core/InputOutput/TextConverter.h"
00030 #include "Core/Utility/StringTokenizer.h"
00031 
00032 namespace Lamp{
00033 
00034 //------------------------------------------------------------------------------
00035 // コンストラクタ
00036 ConfigurationFile::ConfigurationFile(const String& fileName){
00037     load(fileName);
00038 }
00039 //------------------------------------------------------------------------------
00040 // デストラクタ
00041 ConfigurationFile::~ConfigurationFile(){
00042     database_.clear();
00043 }
00044 //------------------------------------------------------------------------------
00045 // ロード
00046 void ConfigurationFile::load(const String& fileName){
00047     fileName_ = fileName;
00048     TextFileReader reader(fileName_);
00049     String line;
00050     while(!reader.isEnd()){
00051         line = reader.readLine();
00052         StringTokenizer tokenizer(line, "\t");
00053         // 空行読み飛ばし
00054         if(!tokenizer.hasMoreTokens()){ continue; }
00055         String key = tokenizer.getNextToken();
00056         if(key.startsWith("//")){ continue; }
00057         Assert(tokenizer.hasMoreTokens());
00058         if(!tokenizer.hasMoreTokens()){ continue; }
00059         String value = tokenizer.getNextToken();
00060         add(key, value);
00061     }
00062 }
00063 //------------------------------------------------------------------------------
00064 // データの追加
00065 void ConfigurationFile::add(const String& key, const String& value){
00066     Assert(!key.equals(""));
00067     Assert(key.getIndexOf('\n') == -1);
00068     Assert(key.getIndexOf('\t') == -1);
00069     Assert(!value.equals(""));
00070     Assert(value.getIndexOf('\n') == -1);
00071     Assert(value.getIndexOf('\t') == -1);
00072     database_.put(key, value);
00073 }
00074 //------------------------------------------------------------------------------
00075 // charデータの取得
00076 bool ConfigurationFile::getChar(const String& key, char* value) const{
00077     String valueString = database_.get(key);
00078     if(valueString.getSize() == 0){ return false; }
00079     return valueString.parseChar(value);
00080 }
00081 //------------------------------------------------------------------------------
00082 // u_charデータの取得
00083 bool ConfigurationFile::getUChar(const String& key, u_char* value) const{
00084     String valueString = database_.get(key);
00085     if(valueString.getSize() == 0){ return false; }
00086     return valueString.parseUChar(value);
00087 }
00088 //------------------------------------------------------------------------------
00089 // shortデータの取得
00090 bool ConfigurationFile::getShort(const String& key, short* value) const{
00091     String valueString = database_.get(key);
00092     if(valueString.getSize() == 0){ return false; }
00093     return valueString.parseShort(value);
00094 }
00095 //------------------------------------------------------------------------------
00096 // u_shortデータの取得
00097 bool ConfigurationFile::getUShort(const String& key, u_short* value) const{
00098     String valueString = database_.get(key);
00099     if(valueString.getSize() == 0){ return false; }
00100     return valueString.parseUShort(value);
00101 }
00102 //------------------------------------------------------------------------------
00103 // intデータの取得
00104 bool ConfigurationFile::getInt(const String& key, int* value) const{
00105     String valueString = database_.get(key);
00106     if(valueString.getSize() == 0){ return false; }
00107     return valueString.parseInt(value);
00108 }
00109 //------------------------------------------------------------------------------
00110 // u_intデータの取得
00111 bool ConfigurationFile::getUInt(const String& key, u_int* value) const{
00112     String valueString = database_.get(key);
00113     if(valueString.getSize() == 0){ return false; }
00114     return valueString.parseUInt(value);
00115 }
00116 //------------------------------------------------------------------------------
00117 // floatデータの取得
00118 bool ConfigurationFile::getFloat(const String& key, float* value) const{
00119     String valueString = database_.get(key);
00120     if(valueString.getSize() == 0){ return false; }
00121     return valueString.parseFloat(value);
00122 }
00123 //------------------------------------------------------------------------------
00124 // doubleデータの取得
00125 bool ConfigurationFile::getDouble(const String& key, double* value) const{
00126     String valueString = database_.get(key);
00127     if(valueString.getSize() == 0){ return false; }
00128     return valueString.parseDouble(value);
00129 }
00130 //------------------------------------------------------------------------------
00131 // Stringデータの取得
00132 bool ConfigurationFile::getString(const String& key, String* value) const{
00133     String valueString = database_.get(key);
00134     if(valueString.getSize() == 0){ return false; }
00135     if(valueString.charAt(0) != '"'){ return false; }
00136     if(valueString.charAt(valueString.getSize() - 1) != '"'){ return false; }
00137     *value = TextConverter::textToString(valueString);
00138     return true;
00139 }
00140 //------------------------------------------------------------------------------
00141 } // End of namespace Lamp
00142 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:29 2005 for Lamp by doxygen 1.3.2