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

TextReader.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/InputOutput/TextReader.h"
00027 #include "Core/System/StringMethod.h"
00028 
00029 namespace Lamp{
00030 
00031 //------------------------------------------------------------------------------
00032 // コンストラクタ
00033 TextReader::TextReader(int bufferSize) : Reader(){
00034     bufferSize_ = bufferSize;
00035     buffer_ = new char[bufferSize_];
00036     output_ = new char[bufferSize_];
00037     position_ = bufferSize_;
00038 }
00039 //------------------------------------------------------------------------------
00040 // デストラクタ
00041 TextReader::~TextReader(){
00042     SafeArrayDelete(output_);
00043     SafeArrayDelete(buffer_);
00044 }
00045 //------------------------------------------------------------------------------
00046 // 文字列の一行読み込み
00047 // ここでストリームでなくなっている
00048 String TextReader::readLine(){
00049     // 初期バッファの読み込み
00050     if(position_ == bufferSize_){
00051         readSize_ = Math::minimum(bufferSize_, getSize());
00052         readBytes(buffer_, readSize_);
00053         // EOF対策
00054         filePosition_ = getPosition();
00055         setPosition(0);
00056         position_ = 0;
00057     }
00058     String returnString;
00059     int outputPosition = 0;
00060     while(true){
00061         char character = buffer_[position_];
00062         position_++;
00063         output_[outputPosition] = character;
00064         outputPosition++;
00065         // 改行処理
00066         bool breakFlag = false;
00067         if(character == '\n'){
00068             outputPosition--;
00069             output_[outputPosition] = '\0';
00070             breakFlag = true;
00071         }
00072         if(position_ == readSize_){
00073             // ファイル終端
00074             if(readSize_ != bufferSize_){
00075                 output_[outputPosition] = '\0';
00076                 setPosition(filePosition_);
00077                 break;
00078             }
00079             // バッファの読み込み
00080             setPosition(filePosition_);
00081             readSize_ = Math::minimum(bufferSize_, getSize() - getPosition());
00082             // 丁度読み終わる
00083             if(readSize_ == 0){
00084                 output_[outputPosition] = '\0';
00085                 setPosition(filePosition_);
00086                 break;
00087             }
00088             readBytes(buffer_, readSize_);
00089             // EOF対策
00090             filePosition_ = getPosition();
00091             setPosition(0);
00092             position_ = 0;
00093             // 出力文字列に継ぎ足して出力位置をクリア
00094             output_[outputPosition] = '\0';
00095             returnString += output_;
00096             outputPosition = 0;
00097             output_[outputPosition] = '\0';
00098         }
00099         if(breakFlag){ break; }
00100     }
00101     // 出力
00102     if(output_[outputPosition - 1] == '\r'){
00103         output_[outputPosition - 1] = '\0';
00104     }
00105     returnString += output_;
00106     return returnString;
00107 }
00108 //------------------------------------------------------------------------------
00109 // テキストリーダデータのコピー
00110 void TextReader::copyTextReaderData(TextReader* destination){
00111     ::memcpy(destination->buffer_, buffer_, bufferSize_);
00112     ::memcpy(destination->output_, output_, bufferSize_);
00113     destination->readSize_ = readSize_;
00114     destination->position_ = position_;
00115     destination->filePosition_ = filePosition_;
00116 }
00117 //------------------------------------------------------------------------------
00118 } // End of namespace Lamp
00119 //------------------------------------------------------------------------------

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