Debian Codegraph @

Contrib/VPatch/Source/GenPat/GlobalTypes.h

raw
1 //---------------------------------------------------------------------------
2 // GlobalTypes.h
3 //---------------------------------------------------------------------------
4 // -=* VPatch *=-
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions
7 //---------------------------------------------------------------------------
8 // Website: http://www.tibed.net/vpatch
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would be
21 // appreciated but is not required.
22 // 2. Altered source versions must be plainly marked as such, and must not be
23 // misrepresented as being the original software.
24 // 3. This notice may not be removed or altered from any source distribution.
25 //
26 // Reviewed for Unicode support by Jim Park -- 08/29/2007
27
28 #if !defined(GlobalTypes_H)
29 #define GlobalTypes_H
30
31 #ifndef _MSC_VER
32 #include <stdint.h>
33 #endif
34 #include <iostream>
35 #include <fstream>
36 #include <ios>
37 #include <string>
38
39 using namespace std;
40
41 #ifdef _MSC_VER
42 typedef unsigned char uint8_t;
43 typedef unsigned __int32 uint32_t;
44 typedef unsigned __int64 uint64_t;
45 #define CHECKSUM_BLOCK unsigned __int64
46 #define __WIN32__
47 #else
48 #define CHECKSUM_BLOCK unsigned long long
49 #endif
50
51 typedef uint32_t TFileOffset;
52
53 // This is a hacky partial replacement for <i|o>[f]stream so we can open wchar_t*
54 #include "tchar.h"
55 #include <stdio.h>
56 #include <assert.h>
57
58 class simplebfstream {
59 FILE*m_File;
60 ios_base::iostate m_state;
61 streamsize m_LastReadCount;
62 public:
63 simplebfstream() : m_File(0), m_state(ios_base::badbit|ios_base::failbit) {}
64 ~simplebfstream()
65 {
66 if (m_File) fclose(m_File);
67 }
68
69 bool open(const TCHAR*filename, ios_base::openmode mode)
70 {
71 TCHAR mAcc, mFmt = _T('b');
72 if (ios::in&mode) mAcc = _T('r');
73 if (ios::out&mode) mAcc = _T('w');
74 assert(0==(mode&~(ios::in|ios::binary|ios::out)));
75
76 TCHAR modestr[3] = {mAcc, mFmt, _T('\0')};
77 m_File = FOPEN(filename, modestr);
78 m_state = m_File ? ios_base::goodbit : ios_base::badbit|ios_base::failbit;
79
80 return good();
81 }
82
83 void close()
84 {
85 if (!m_File || fclose(m_File))
86 {
87 m_state |= ios_base::failbit;
88 }
89 m_File = 0;
90 }
91
92 bool is_open() const {return !!m_File;}
93 bool eof() const {return !!(ios_base::eofbit & m_state);}
94 bool bad() const {return !!(ios_base::badbit & m_state);}
95 bool fail() const {return !!((ios_base::failbit|ios_base::badbit) & m_state);}
96 bool good() const {return ios_base::goodbit==m_state;}
97
98 streamsize gcount() const {return m_LastReadCount;}
99 long tellg() const {return ftell(m_File);}
100
101 simplebfstream& read(char*s,streamsize n)
102 {
103 streamsize cbio = fread(s, 1, n);
104 m_LastReadCount = cbio;
105 if (cbio != n)
106 {
107 m_state |= ferror(m_File) ? ios_base::badbit : (ios_base::eofbit|ios_base::failbit);
108 }
109 return *this;
110 }
111
112 simplebfstream& seekg(streamoff off, ios_base::seekdir dir)
113 {
114 int origin = ios_base::beg==dir ? SEEK_SET : ios_base::cur==dir ? SEEK_CUR : SEEK_END;
115 if (fseek(off, origin))
116 {
117 // BUGBUG: Does not follow standard
118 m_state |= ios_base::badbit|ios_base::failbit;
119 }
120 return *this;
121 }
122
123 simplebfstream& seekp(streamoff off, ios_base::seekdir dir) {return seekg(off, dir);}
124 long tellp() const {return tellg();}
125
126 simplebfstream& write(const char* s, streamsize n)
127 {
128 streamsize cbio = fwrite(s, 1, n);
129 if (cbio != n) m_state |= ios_base::badbit;
130 return *this;
131 }
132
133 bool operator ! () const {return fail();}
134 protected:
135 // streamsize and streamoff can be INT64 on x86 in VS2015
136 template<class F> streamsize readwritehelper(void*buf, size_t itemsize, streamsize count, F func)
137 {
138 if (sizeof(streamsize) <= sizeof(size_t))
139 return func(buf, itemsize, (size_t) count, m_File);
140 for (streamsize totc = 0;;)
141 {
142 size_t small = count > 0x7fffffff ? 0x7fffffff : (size_t) count;
143 size_t rv = func(((char*)buf) + totc, itemsize, small, m_File);
144 count -= (streamsize) rv, totc += rv;
145 if (rv != small)
146 return totc;
147 }
148 }
149 streamsize fread(void*buf, size_t itemsize, streamsize count)
150 {
151 return readwritehelper(buf, itemsize, count, ::fread);
152 }
153 streamsize fwrite(const void*buf, size_t itemsize, streamsize count)
154 {
155 return readwritehelper((void*) buf, itemsize, count, ::fwrite);
156 }
157 int fseek(streamoff off, int origin)
158 {
159 if (sizeof(streamoff) <= sizeof(long))
160 return ::fseek(m_File, (long) off, origin);
161 for (;;)
162 {
163 long small = off > 0x7fffffff ? 0x7fffffff : off < -2147483647 ? -2147483647 : (long) off;
164 int retval = ::fseek(m_File, small, origin);
165 off -= small, origin = SEEK_CUR;
166 if (!off || retval)
167 return retval;
168 }
169 }
170 };
171
172 typedef simplebfstream bistream;
173 typedef bistream bifstream;
174 typedef simplebfstream bostream;
175 typedef bostream bofstream;
176
177 #endif // GlobalTypes_H