cAudio  2.3.0
3d Audio Engine
cMemorySource.cpp
1 // Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
2 // This file is part of the "cAudio Engine"
3 // For conditions of distribution and use, see copyright notice in cAudio.h
4 
5 #include "cMemorySource.h"
6 
7 #include <cstring>
8 
9 namespace cAudio
10 {
11 
12 cMemorySource::cMemorySource(const void* data, int size, bool copy) : Data(NULL), Size(0), Valid(false), Pos(0)
13 {
14  if(data && size > 0)
15  {
16  Size = size;
17  if(copy)
18  {
19  Data = (char*)CAUDIO_MALLOC(Size);
20  if(Data)
21  memcpy(Data, data, Size);
22  }
23  else
24  {
25  Data = (char*)data;
26  }
27  if(Data)
28  Valid = true;
29  }
30 }
31 
32 cMemorySource::~cMemorySource()
33 {
34  CAUDIO_FREE(Data);
35 }
36 
38 {
39  return Valid;
40 }
41 
43 {
44  return Pos;
45 }
46 
48 {
49  return Size;
50 }
51 
52 int cMemorySource::read(void* output, int size)
53 {
54 
55  //memset(output, 0, size);
56  if(Pos+size <= Size)
57  {
58  memcpy(output, Data+Pos, size);
59  Pos += size;
60  return size;
61  }
62  else
63  {
64  int extra = (Pos+size) - Size;
65  int copied = size - extra;
66  memcpy(output, Data+Pos, copied);
67  Pos = Size;
68  return copied;
69  }
70 }
71 
72 bool cMemorySource::seek(int amount, bool relative)
73 {
74  if(relative)
75  {
76  Pos += amount;
77  if(Pos > Size)
78  {
79  Pos = Size;
80  return false;
81  }
82  }
83  else
84  {
85  Pos = amount;
86  if(Pos > Size)
87  {
88  Pos = Size;
89  return false;
90  }
91  }
92 
93  return true;
94 }
95 
96 };
virtual bool seek(int amount, bool relative)
Seek to a position in the data stream.
cMemorySource(const void *data, int size, bool copy)
virtual int read(void *output, int size)
Reads out a section of the data stream.
virtual bool isValid()
Returns whether the source is valid.
virtual int getSize()
Returns the total size of the data stream.
virtual int getCurrentPos()
Returns the current location in the data stream.
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:15