cMemorySource.cpp

00001 // Copyright (c) 2008-2010 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones
00002 // This file is part of the "cAudio Engine"
00003 // For conditions of distribution and use, see copyright notice in cAudio.h
00004 
00005 #include <cstring>
00006 #include <iostream>
00007 
00008 #ifdef CAUDIO_USE_MMGR
00009 #include "../Headers/cMemoryManager.h"
00010 #endif
00011 
00012 
00013 #include "../Headers/cMemorySource.h"
00014 
00015 namespace cAudio
00016 {
00017 
00018 cMemorySource::cMemorySource(const void* data, int size, bool copy) : Data(NULL), Size(0), Valid(false), Pos(0)
00019 {
00020     if(data && size > 0)
00021     {
00022         Size = size;
00023         if(copy)
00024         {
00025             Data = new char[Size];
00026                         if(Data)
00027                                 memcpy(Data, data, Size);
00028         }
00029         else
00030         {
00031             Data = (char*)data;
00032         }
00033                 if(Data)
00034                         Valid = true;
00035     }
00036 }
00037 
00038 cMemorySource::~cMemorySource()
00039 {
00040     delete[] Data;
00041 }
00042 
00043 bool cMemorySource::isValid()
00044 {
00045     return Valid;
00046 }
00047 
00048 int cMemorySource::getCurrentPos()
00049 {
00050     return Pos;
00051 }
00052 
00053 int cMemorySource::getSize()
00054 {
00055     return Size;
00056 }
00057 
00058 int cMemorySource::read(void* output, int size)
00059 {
00060 
00061     //memset(output, 0, size);
00062     if(Pos+size <= Size)
00063     {
00064         memcpy(output, Data+Pos, size);
00065         Pos += size;
00066         return size;
00067     }
00068     else
00069     {
00070         int extra = (Pos+size) - Size;
00071         int copied = size - extra;
00072         memcpy(output, Data+Pos, copied);
00073         Pos = Size;
00074         return copied;
00075     }
00076 }
00077 
00078 bool cMemorySource::seek(int amount, bool relative)
00079 {
00080     if(relative)
00081     {
00082         Pos += amount;
00083         if(Pos > Size)
00084         {
00085             Pos = Size;
00086             return false;
00087         }
00088     }
00089     else
00090     {
00091         Pos = amount;
00092         if(Pos > Size)
00093         {
00094             Pos = Size;
00095             return false;
00096         }
00097     }
00098 
00099     return true;
00100 }
00101 
00102 };
 All Classes Namespaces Functions Variables Enumerations

Generated on Sat Feb 20 22:55:09 2010 for cAudio by  doxygen 1.6.2