]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/profile/file.h
Remove -Wno-switch
[xonotic/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() {
87         refCount--; if ( refCount <= 0 ) {
88                 delete this;
89         }
90 }
91
92 protected:
93 // MemFile specific:
94 size_type m_nGrowBytes;
95 size_type m_nPosition;
96 size_type m_nBufferSize;
97 size_type m_nFileSize;
98 unsigned char* m_pBuffer;
99 bool m_bAutoDelete;
100 void GrowFile( size_type nNewLen );
101
102 public:
103 position_type GetPosition() const;
104 int Seek( offset_type lOff, int nFrom );
105 void SetLength( size_type nNewLen );
106 size_type GetLength() const;
107
108 unsigned char* GetBuffer() const
109 { return m_pBuffer; }
110
111 size_type read( byte_type* buffer, size_type length );
112 size_type write( const byte_type* buffer, size_type length );
113
114 char* ReadString( char* pBuf, size_type nMax );
115 int GetChar();
116
117 int PutChar( int c );
118 void printf( const char*, ... ); ///< \todo implement on MemStream
119
120 void Abort();
121 void Flush();
122 void Close();
123 bool Open( const char *filename, const char *mode );
124 };
125
126 class FileStream : public IDataStream
127 {
128 public:
129 FileStream();
130 virtual ~FileStream();
131
132 int refCount;
133 void IncRef() { refCount++; }
134 void DecRef() {
135         refCount--; if ( refCount <= 0 ) {
136                 delete this;
137         }
138 }
139
140 protected:
141 // DiscFile specific:
142 FILE* m_hFile;
143 bool m_bCloseOnDelete;
144
145 public:
146 position_type GetPosition() const;
147 int Seek( offset_type lOff, int nFrom );
148 void SetLength( size_type nNewLen );
149 size_type GetLength() const;
150
151 size_type read( byte_type* buffer, size_type length );
152 size_type write( const byte_type* buffer, size_type length );
153
154 char* ReadString( char* pBuf, size_type nMax );
155 int GetChar();
156
157 int PutChar( int c );
158 void printf( const char*, ... ); ///< completely matches the usual printf behaviour
159
160 void Abort();
161 void Flush();
162 void Close();
163 bool Open( const char *filename, const char *mode );
164 };
165
166 #endif