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

# TextWriter.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/TextWriter.h"
    00027 #include "Core/System/StringMethod.h"
    00028
    00029 namespace Lamp{
    00030
    00031 //------------------------------------------------------------------------------
    00032 // コンストラクタ
    00033 TextWriter::TextWriter() : Writer(){
    00034     bufferSize_ = defaultBufferSize;
    00035     buffer_ = new char[bufferSize_];
    00036 }
    00037 //------------------------------------------------------------------------------
    00038 // デストラクタ
    00039 TextWriter::~TextWriter(){
    00040     delete[] buffer_;
    00041 }
    00042 //------------------------------------------------------------------------------
    00043 // テキストの書き出し
    00044 void TextWriter::writeText(const String& text){
    00045     writeText(text.getBytes(), text.getSize());
    00046 }
    00047 //------------------------------------------------------------------------------
    00048 // テキストの書き出し
    00049 void TextWriter::writeText(const char* text){
    00050     Assert(text != NULL);
    00051     writeText(text, (int)StdStrlen(text));
    00052 }
    00053 //------------------------------------------------------------------------------
    00054 // テキストの書き出し
    00055 // ファイル以外のストリームに対して改行変換を必要としないのであれば
    00056 // もっとシンプルにできる
    00057 void TextWriter::writeText(const char* text, int size){
    00058     Assert(text != NULL);
    00059     // バッファの拡張
    00060     int maxSize = size * 2;
    00061     if(maxSize > bufferSize_){
    00062         delete[] buffer_;
    00063         while(true){
    00064             bufferSize_ *= 2;
    00065             if(maxSize <= bufferSize_){ break; }
    00066         }
    00067         buffer_ = new char[bufferSize_];
    00068     }
    00069     // 改行フィルタ
    00070     int writeSize = 0;
    00071     char preChar = ' ';
    00072     for(int i = 0;i < size;i++){
    00073         if((text[i] == '\n') && (preChar != '\r')){
    00074             buffer_[writeSize] = '\r';
    00075             writeSize++;
    00076         }
    00077         buffer_[writeSize] = text[i];
    00078         preChar = text[i];
    00079         writeSize++;
    00080     }
    00081     writeBytes(buffer_, writeSize);
    00082 }
    00083 //------------------------------------------------------------------------------
    00084 // フォーマットテキストの書き出し
    00085 void TextWriter::writeFormat(const char* format, ...){
    00086     Assert(format != NULL);
    00087     int formatBufferSize = formatDefaultBufferSize;
    00088     char* formatBuffer;
    00089     int writeSize;
    00090     va_list args;
    00091     va_start(args, format);
    00092     while(true){
    00093         formatBuffer = new char[formatBufferSize];
    00094         writeSize = StdVsnprintf(formatBuffer, formatBufferSize, format, args);
    00095         if(writeSize != -1){ break; }
    00096         formatBufferSize *= 2;
    00097         delete[] formatBuffer;
    00098     }
    00099     va_end(args);
    00100     // NULL終端されていない可能性があるが問題無し
    00101     writeText(formatBuffer, writeSize);
    00102     delete[] formatBuffer;
    00103 }
    00104 //------------------------------------------------------------------------------
    00105 } // End of namespace Lamp
    00106 //------------------------------------------------------------------------------