Added IsSupportingRTP function to simplify detecting when STUN supports RTP
[pwlib.git] / src / ptclib / memfile.cxx
blob9517e89940524e42fa53281f51e717a4c242f527
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.5 2004/04/03 08:22:21 csoutheren
28 * Remove pseudo-RTTI and replaced with real RTTI
30 * Revision 1.4 2002/12/19 03:35:43 robertj
31 * Fixed missing set of lastWriteCount in Write() function.
33 * Revision 1.3 2002/11/06 22:47:25 robertj
34 * Fixed header comment (copyright etc)
36 * Revision 1.2 2002/06/27 03:53:35 robertj
37 * Cleaned up documentation and added Compare() function.
39 * Revision 1.1 2002/06/26 09:03:16 craigs
40 * Initial version
44 #include <ptlib.h>
46 #ifdef __GNUC__
47 #pragma implementation "memfile.h"
48 #endif
50 #include <ptclib/memfile.h>
54 //////////////////////////////////////////////////////////////////////////////
56 PMemoryFile::PMemoryFile()
58 position = 0;
62 PMemoryFile::PMemoryFile(const PBYTEArray & ndata)
64 data = ndata;
65 position = 0;
69 PObject::Comparison PMemoryFile::Compare(const PObject & obj) const
71 PAssert(PIsDescendant(&obj, PMemoryFile), PInvalidCast);
72 return data.Compare(((const PMemoryFile &)obj).data);
76 BOOL PMemoryFile::Read(void * buf, PINDEX len)
78 if ((position + len) > data.GetSize())
79 len = data.GetSize() - position;
81 lastReadCount = len;
83 if (len != 0) {
84 ::memcpy(buf, position + (const BYTE * )data, len);
85 position += len;
86 lastReadCount = len;
89 return lastReadCount != 0;
93 BOOL PMemoryFile::Write(const void * buf, PINDEX len)
95 memcpy(data.GetPointer(position+len) + position, buf, len);
96 position += len;
97 lastWriteCount = len;
98 return TRUE;
102 off_t PMemoryFile::GetLength() const
104 return data.GetSize();
108 BOOL PMemoryFile::SetLength(off_t len)
110 return data.SetSize(len);
114 BOOL PMemoryFile::SetPosition(off_t pos, FilePositionOrigin origin)
116 switch (origin) {
117 case Start:
118 if (pos > data.GetSize())
119 return FALSE;
120 position = pos;
121 break;
123 case Current:
124 if (pos < -position || pos > (data.GetSize() - position))
125 return FALSE;
126 position += pos;
127 break;
129 case End:
130 if (pos < -data.GetSize())
131 return FALSE;
132 position = data.GetSize() - pos;
133 break;
135 return TRUE;
139 off_t PMemoryFile::GetPosition() const
141 return position;
145 // End of File ///////////////////////////////////////////////////////////////