00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
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 }
00119