Uncommented beaudio code
[pwlib.git] / src / ptclib / memfile.cxx
bloba1e383beabf1b75af3b9c5cb75f09d7935ec33f9
1 /*
2 * memfile.cxx
4 * memory file I/O channel class.
6 * Portable Windows Library
8 * Copyright (c) 2002 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
26 * $Log$
27 * Revision 1.4 2002/12/19 03:35:43 robertj
28 * Fixed missing set of lastWriteCount in Write() function.
30 * Revision 1.3 2002/11/06 22:47:25 robertj
31 * Fixed header comment (copyright etc)
33 * Revision 1.2 2002/06/27 03:53:35 robertj
34 * Cleaned up documentation and added Compare() function.
36 * Revision 1.1 2002/06/26 09:03:16 craigs
37 * Initial version
41 #include <ptlib.h>
43 #ifdef __GNUC__
44 #pragma implementation "memfile.h"
45 #endif
47 #include <ptclib/memfile.h>
51 //////////////////////////////////////////////////////////////////////////////
53 PMemoryFile::PMemoryFile()
55 position = 0;
59 PMemoryFile::PMemoryFile(const PBYTEArray & ndata)
61 data = ndata;
62 position = 0;
66 PObject::Comparison PMemoryFile::Compare(const PObject & obj) const
68 PAssert(obj.IsDescendant(Class()), PInvalidCast);
69 return data.Compare(((const PMemoryFile &)obj).data);
73 BOOL PMemoryFile::Read(void * buf, PINDEX len)
75 if ((position + len) > data.GetSize())
76 len = data.GetSize() - position;
78 lastReadCount = len;
80 if (len != 0) {
81 ::memcpy(buf, position + (const BYTE * )data, len);
82 position += len;
83 lastReadCount = len;
86 return lastReadCount != 0;
90 BOOL PMemoryFile::Write(const void * buf, PINDEX len)
92 memcpy(data.GetPointer(position+len) + position, buf, len);
93 position += len;
94 lastWriteCount = len;
95 return TRUE;
99 off_t PMemoryFile::GetLength() const
101 return data.GetSize();
105 BOOL PMemoryFile::SetLength(off_t len)
107 return data.SetSize(len);
111 BOOL PMemoryFile::SetPosition(off_t pos, FilePositionOrigin origin)
113 switch (origin) {
114 case Start:
115 if (pos > data.GetSize())
116 return FALSE;
117 position = pos;
118 break;
120 case Current:
121 if (pos < -position || pos > (data.GetSize() - position))
122 return FALSE;
123 position += pos;
124 break;
126 case End:
127 if (pos < -data.GetSize())
128 return FALSE;
129 position = data.GetSize() - pos;
130 break;
132 return TRUE;
136 off_t PMemoryFile::GetPosition() const
138 return position;
142 // End of File ///////////////////////////////////////////////////////////////