MyGUI  3.2.1
MyGUI_DataMemoryStream.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_DataMemoryStream.h"
00009 
00010 namespace MyGUI
00011 {
00012 
00013     DataMemoryStream::DataMemoryStream() :
00014         mData(nullptr),
00015         mSize(0),
00016         mStream(0)
00017     {
00018     }
00019 
00020     DataMemoryStream::DataMemoryStream(unsigned char* _data, size_t _size) :
00021         mData(_data),
00022         mSize(_size),
00023         mStream(nullptr)
00024     {
00025     }
00026 
00027     DataMemoryStream::~DataMemoryStream()
00028     {
00029         delete mStream;
00030     }
00031 
00032     size_t DataMemoryStream::size()
00033     {
00034         return mSize;
00035     }
00036 
00037     bool DataMemoryStream::eof()
00038     {
00039         if (mStream == nullptr)
00040             prepareStream();
00041 
00042         return mStream->eof();
00043     }
00044 
00045     void DataMemoryStream::readline(std::string& _source, Char _delim)
00046     {
00047         if (mStream == nullptr)
00048             prepareStream();
00049 
00050         std::getline(*mStream, _source, (char)_delim);
00051     }
00052 
00053     size_t DataMemoryStream::read(void* _buf, size_t _count)
00054     {
00055         if (mData == nullptr)
00056             return 0;
00057 
00058         size_t count = (std::min)(size(), _count);
00059         ::memcpy(_buf, mData, count);
00060         return count;
00061     }
00062 
00063     void DataMemoryStream::prepareStream()
00064     {
00065         if (mData == nullptr)
00066             return;
00067 
00068         mStream = new std::stringstream((const char*)mData);
00069     }
00070 
00071 } // namespace MyGUI