]> icculus.org git repositories - divverent/netradiant.git/blob - libs/profile/file.h
initial
[divverent/netradiant.git] / libs / profile / file.h
1 /*
2 Copyright (c) 2001, Loki software, inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, 
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list 
9 of conditions and the following disclaimer.
10
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
15 Neither the name of Loki software nor the names of its contributors may be used 
16 to endorse or promote products derived from this software without specific prior 
17 written permission. 
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
29 */
30
31 //
32 //  file.h
33 ////////////////////////////////////////////////////
34
35 #if !defined(INCLUDED_PROFILE_FILE_H)
36 #define INCLUDED_PROFILE_FILE_H
37
38 #include "idatastream.h"
39
40 /*!
41 API for data streams
42
43 Based on an initial implementation by Loki software
44 modified to be abstracted and shared across modules
45
46 NOTE: why IDataStream and not IStream? because IStream is defined in windows IDL headers
47 */
48
49 class IDataStream : public InputStream, public OutputStream
50 {
51 public:  
52   typedef int offset_type;
53   typedef std::size_t position_type;
54
55   virtual void IncRef() = 0;  ///< Increment the number of references to this object
56   virtual void DecRef() = 0; ///< Decrement the reference count
57
58   virtual position_type GetPosition() const = 0;
59   virtual int Seek(offset_type lOff, int nFrom) = 0;
60
61   virtual void SetLength(size_type nNewLen) = 0;
62   virtual size_type GetLength() const = 0;
63
64   virtual char* ReadString(char* pBuf, size_type nMax) = 0;
65   virtual int GetChar()=0;
66
67   virtual int PutChar(int c)=0;
68   virtual void printf(const char*, ...) = 0; ///< completely matches the usual printf behaviour
69
70   virtual void Abort() = 0;
71   virtual void Flush() = 0;
72   virtual void Close() = 0;
73 };
74
75 #include <stdio.h>
76
77 class MemStream : public IDataStream
78 {
79 public:
80   MemStream();
81   MemStream(size_type nLen);
82   virtual ~MemStream();
83
84   int refCount;
85   void IncRef() { refCount++; }
86   void DecRef() { refCount--; if (refCount <= 0) delete this; }
87
88 protected:
89   // MemFile specific:
90   size_type m_nGrowBytes;
91   size_type m_nPosition;
92   size_type m_nBufferSize;
93   size_type m_nFileSize;
94   unsigned char* m_pBuffer;
95   bool m_bAutoDelete;
96   void GrowFile(size_type nNewLen);
97
98 public:
99   position_type GetPosition() const;
100   int Seek(offset_type lOff, int nFrom);
101   void SetLength(size_type nNewLen);
102   size_type GetLength() const;
103
104   unsigned char* GetBuffer() const
105     { return m_pBuffer; }
106
107   size_type read(byte_type* buffer, size_type length);
108   size_type write(const byte_type* buffer, size_type length);
109
110   char* ReadString(char* pBuf, size_type nMax);
111   int GetChar();
112
113   int PutChar(int c);
114   void printf(const char*, ...); ///< \todo implement on MemStream
115
116   void Abort();
117   void Flush();
118   void Close();
119   bool Open(const char *filename, const char *mode);
120 };
121
122 class FileStream : public IDataStream
123 {
124 public:
125   FileStream();
126   virtual ~FileStream();
127
128   int refCount;
129   void IncRef() { refCount++; }
130   void DecRef() { refCount--; if (refCount <= 0) delete this; }
131
132 protected:
133   // DiscFile specific:
134   FILE* m_hFile;
135   bool m_bCloseOnDelete;
136
137 public:
138   position_type GetPosition() const;
139   int Seek(offset_type lOff, int nFrom);
140   void SetLength(size_type nNewLen);
141   size_type GetLength() const;
142
143   size_type read(byte_type* buffer, size_type length);
144   size_type write(const byte_type* buffer, size_type length);
145
146   char* ReadString(char* pBuf, size_type nMax);
147   int GetChar();
148
149   int PutChar(int c);
150   void printf(const char*, ...); ///< completely matches the usual printf behaviour
151
152   void Abort();
153   void Flush();
154   void Close();
155   bool Open(const char *filename, const char *mode);
156 };
157
158 #endif