2010-02-09 05:18:39 +01:00
|
|
|
// Copyright (c) 2008-2010 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones
|
|
|
|
// This file is part of the "cAudio Engine"
|
|
|
|
// For conditions of distribution and use, see copyright notice in cAudio.h
|
|
|
|
|
2010-02-21 02:47:24 +01:00
|
|
|
#include "../Headers/cMemorySource.h"
|
|
|
|
|
2010-03-05 02:52:25 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
2009-06-21 05:24:30 +02:00
|
|
|
namespace cAudio
|
|
|
|
{
|
|
|
|
|
|
|
|
cMemorySource::cMemorySource(const void* data, int size, bool copy) : Data(NULL), Size(0), Valid(false), Pos(0)
|
|
|
|
{
|
|
|
|
if(data && size > 0)
|
|
|
|
{
|
|
|
|
Size = size;
|
|
|
|
if(copy)
|
|
|
|
{
|
2010-03-08 06:27:02 +01:00
|
|
|
Data = (char*)CAUDIO_MALLOC(Size);
|
2009-08-08 07:51:32 +02:00
|
|
|
if(Data)
|
|
|
|
memcpy(Data, data, Size);
|
2009-06-21 05:24:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Data = (char*)data;
|
|
|
|
}
|
2009-08-08 07:51:32 +02:00
|
|
|
if(Data)
|
|
|
|
Valid = true;
|
2009-06-21 05:24:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cMemorySource::~cMemorySource()
|
|
|
|
{
|
2010-03-08 06:27:02 +01:00
|
|
|
CAUDIO_FREE(Data);
|
2009-08-26 01:56:35 +02:00
|
|
|
}
|
2009-06-21 05:24:30 +02:00
|
|
|
|
|
|
|
bool cMemorySource::isValid()
|
|
|
|
{
|
|
|
|
return Valid;
|
|
|
|
}
|
2009-08-26 01:56:35 +02:00
|
|
|
|
2009-06-21 05:24:30 +02:00
|
|
|
int cMemorySource::getCurrentPos()
|
|
|
|
{
|
|
|
|
return Pos;
|
|
|
|
}
|
2009-08-26 01:56:35 +02:00
|
|
|
|
2009-06-21 05:24:30 +02:00
|
|
|
int cMemorySource::getSize()
|
|
|
|
{
|
|
|
|
return Size;
|
|
|
|
}
|
2009-08-26 01:56:35 +02:00
|
|
|
|
2009-06-21 05:24:30 +02:00
|
|
|
int cMemorySource::read(void* output, int size)
|
|
|
|
{
|
2010-02-21 04:42:40 +01:00
|
|
|
|
2009-08-08 07:51:32 +02:00
|
|
|
//memset(output, 0, size);
|
2009-06-21 05:24:30 +02:00
|
|
|
if(Pos+size <= Size)
|
|
|
|
{
|
|
|
|
memcpy(output, Data+Pos, size);
|
|
|
|
Pos += size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int extra = (Pos+size) - Size;
|
|
|
|
int copied = size - extra;
|
|
|
|
memcpy(output, Data+Pos, copied);
|
|
|
|
Pos = Size;
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
}
|
2009-08-26 01:56:35 +02:00
|
|
|
|
2009-06-21 05:24:30 +02:00
|
|
|
bool cMemorySource::seek(int amount, bool relative)
|
|
|
|
{
|
|
|
|
if(relative)
|
|
|
|
{
|
|
|
|
Pos += amount;
|
|
|
|
if(Pos > Size)
|
|
|
|
{
|
|
|
|
Pos = Size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Pos = amount;
|
|
|
|
if(Pos > Size)
|
|
|
|
{
|
|
|
|
Pos = Size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|