IStream.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 //----------------------------------------------------------------------------------
00015 // Includes
00016 //----------------------------------------------------------------------------------
00017 #include "IStream.h"
00018 
00019 namespace nrEngine {
00020 
00021         //----------------------------------------------------------------------------------
00022         IStream::IStream(uint32 streamBufSize) : mSize(0), mStreamBufSize(streamBufSize)
00023         {
00024                 // check for the size. If size is smaller than 1 we want to have unbuffered data
00025                 if (streamBufSize < 1)
00026                         mStreamBufSize = 1;
00027 
00028                 // allocate memory for the buffer
00029                 mBuffer = new byte[mStreamBufSize];
00030         }
00031 
00032 
00033         //----------------------------------------------------------------------------------
00034         IStream::IStream(const std::string& name, uint32 streamBufSize) : mName(name), mSize(0), mStreamBufSize(streamBufSize)
00035         {
00036                 // check for the size. If size is smaller than 1 we want to have unbuffered data
00037                 if (streamBufSize < 1)
00038                         mStreamBufSize = 1;
00039 
00040                 // allocate memory for the buffer
00041                 mBuffer = new byte[mStreamBufSize];
00042         }
00043 
00044         //----------------------------------------------------------------------------------
00045         IStream::~IStream()
00046         {
00047                 // release the buffer
00048                 NR_SAFE_DELETE_ARRAY(mBuffer);
00049         }
00050 
00051 
00052         //----------------------------------------------------------------------------------
00053   template <typename T> IStream& IStream::operator >>(T& val)
00054   {
00055                 read(static_cast<void*>(&val), sizeof(T));
00056                 return *this;
00057         }
00058 
00059 
00060         //----------------------------------------------------------------------------------
00061         std::string IStream::getLine()
00062         {
00063 
00064                 // some variables to store the data
00065                 size_t c = mStreamBufSize;
00066                 std::string str;
00067 
00068                 // read until we reached end of file or got
00069                 // on got the new line character
00070                 while (c == mStreamBufSize - 1){
00071 
00072                         c = readDelim(mBuffer, mStreamBufSize - 1);
00073 
00074                         // copy the content of the bufer to the string
00075                         str += (char*)mBuffer;
00076                 }
00077 
00078                 // return the string
00079                 return str;
00080         }
00081 
00082         //----------------------------------------------------------------------------------
00083         std::string IStream::getAsString(){
00084 
00085                 // create buffer to hold the whole data
00086                 char* pBuf = new char[mSize+1];
00087 
00088                 // read the whole data
00089                 size_t c = read(pBuf, mSize);
00090 
00091                 // setup the end characeter and create the string
00092                 pBuf[c] = '\0';
00093                 std::string str(pBuf);
00094 
00095                 // free data and return
00096                 delete [] pBuf;
00097 
00098                 return str;
00099         }
00100 
00101         //----------------------------------------------------------------------------------
00102         size_t IStream::read(void *buf, size_t count)
00103         {
00104                 return read(buf, count, 1);
00105         }
00106 
00107 
00108 };
00109 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1