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

MemoryOutputStream.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/MemoryOutputStream.h"
00027 
00028 namespace Lamp{
00029 
00030 //------------------------------------------------------------------------------
00031 // コンストラクタ
00032 MemoryOutputStream::MemoryOutputStream(int bufferInitialSize){
00033     bufferSize_ = bufferInitialSize;
00034     buffer_ = new char[bufferSize_];
00035     size_ = position_ = 0;
00036 }
00037 //------------------------------------------------------------------------------
00038 // デストラクタ
00039 MemoryOutputStream::~MemoryOutputStream(){
00040     delete[] buffer_;
00041 }
00042 //------------------------------------------------------------------------------
00043 // バッファチェック
00044 void MemoryOutputStream::bufferCheck(int size){
00045     int newPosition = position_ + size;
00046     if(newPosition > bufferSize_){
00047         while(true){
00048             bufferSize_ *= 2;
00049             if(newPosition <= bufferSize_){ break; }
00050         }
00051         char* newBuffer = new char[bufferSize_];
00052         std::memcpy(newBuffer, buffer_, size_);
00053         delete[] buffer_;
00054         buffer_ = newBuffer;
00055     }
00056 }
00057 //------------------------------------------------------------------------------
00058 // バイトデータの書き出し
00059 void MemoryOutputStream::writeBytes(const void* data, int size){
00060     bufferCheck(size);
00061     std::memcpy(buffer_ + position_, data, size);
00062     position_ += size;
00063     if(position_ > size_){ size_ = position_; }
00064 }
00065 //------------------------------------------------------------------------------
00066 // サイズの取得
00067 int MemoryOutputStream::getSize(){
00068     return size_;
00069 }
00070 //------------------------------------------------------------------------------
00071 // スキップ
00072 void MemoryOutputStream::skip(int size){
00073     bufferCheck(size);
00074     std::memset(buffer_ + position_, 0, size);
00075     position_ += size;
00076     if(position_ > size_){ size_ = position_; }
00077 }
00078 //------------------------------------------------------------------------------
00079 // アライメントを取る
00080 int MemoryOutputStream::align(int alignSize){
00081     int size = position_ % alignSize;
00082     if(size== 0){ return size; }
00083     size = alignSize - size;
00084     bufferCheck(size);
00085     std::memset(buffer_ + position_, 0, size);
00086     position_ += size;
00087     if(position_ > size_){ size_ = position_; }
00088     return size;
00089 }
00090 //------------------------------------------------------------------------------
00091 // 書き込み位置の取得
00092 int MemoryOutputStream::getPosition(){
00093     return position_;
00094 }
00095 //------------------------------------------------------------------------------
00096 // 書き込み位置の設定
00097 void MemoryOutputStream::setPosition(int position){
00098     Assert(position >= 0);
00099     Assert(position <= size_);
00100     position_ = position;
00101 }
00102 //------------------------------------------------------------------------------
00103 // フラッシュ
00104 void MemoryOutputStream::flush(){
00105 }
00106 //------------------------------------------------------------------------------
00107 } // End of namespace Lamp
00108 //------------------------------------------------------------------------------

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