summaryrefslogtreecommitdiff
path: root/Build/source/libs/graphite/src/segment/FileInput.cpp
blob: 52969532cd358d3a8bb6275b7b56e9c7be018f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: FileInput.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.

Description:
    Contains the functions for reading from a TT file.  (These are functions, not methods
	associated with a class.)
----------------------------------------------------------------------------------------------*/

//:>********************************************************************************************
//:>	Include files
//:>********************************************************************************************

#include "GrCommon.h"
#include "GrData.h"
#ifndef _WIN32
#include "GrMstypes.h"
#endif
#include "GrDebug.h"
#include <fstream>
#include <iostream>
#include <string>
#include "FileInput.h"
#include "GrResult.h"

//#ifndef _MSC_VER
//#include "config.h"
//#endif

#ifdef _MSC_VER
#pragma hdrstop
#endif
#undef THIS_FILE
DEFINE_THIS_FILE

//:End Ignore

//:>********************************************************************************************
//:>	Forward declarations
//:>********************************************************************************************

//:>********************************************************************************************
//:>	Local Constants and static variables
//:>********************************************************************************************

namespace gr
{

//:>********************************************************************************************
//:>	Methods of GrBufferIStream
//:>********************************************************************************************

/*----------------------------------------------------------------------------------------------
	Constructor.
----------------------------------------------------------------------------------------------*/
GrBufferIStream::GrBufferIStream()
{
	m_pbStart = NULL;
	m_pbNext = NULL;
	m_pbLim = NULL;
}

/*----------------------------------------------------------------------------------------------
	Destructor.
----------------------------------------------------------------------------------------------*/
GrBufferIStream::~GrBufferIStream()
{
	Close();
}

/*----------------------------------------------------------------------------------------------
	Initialize the stream.
----------------------------------------------------------------------------------------------*/
#ifdef GR_FW
bool GrBufferIStream::Open(std::wstring stuFileName, int kMode)
#else
bool GrBufferIStream::Open(const char * pcFileName, std::ios::openmode kMode)
#endif
{
	Assert(false); // use OpenBuffer
	return false;
}

/*----------------------------------------------------------------------------------------------
	Initialize the stream to a buffer.
----------------------------------------------------------------------------------------------*/
bool GrBufferIStream::OpenBuffer(byte * pbBuffer, int cb)
{
	Assert(m_pbStart == NULL);
	Assert(m_pbNext == NULL);
	Assert(m_pbLim == NULL);

	m_pbStart = pbBuffer;
	m_pbNext = pbBuffer;
	if (cb > 0)
		m_pbLim = m_pbStart + cb;
	// otherwise we don't know the length

	return true;
}

/*----------------------------------------------------------------------------------------------
	Close the stream.
----------------------------------------------------------------------------------------------*/
void GrBufferIStream::Close()
{
	m_pbStart = NULL;
	m_pbNext = NULL;
	m_pbLim = NULL;
}

/*----------------------------------------------------------------------------------------------
	Read a byte from the stream.
----------------------------------------------------------------------------------------------*/
byte GrBufferIStream::ReadByteFromFont()
{
	byte bInput = *m_pbNext;
	m_pbNext += isizeof(byte);
	if (m_pbLim && m_pbNext > m_pbLim)
		THROW(kresReadFault);
	return bInput;
}

/*----------------------------------------------------------------------------------------------
	Read a short (signed 16-bit) word from the stream. Switch the bytes from big-endian 
	to little-endian format.
----------------------------------------------------------------------------------------------*/
short GrBufferIStream::ReadShortFromFont()
{
	short snInput = *(short *)m_pbNext;
	m_pbNext += isizeof(short);
	if (m_pbLim && m_pbNext > m_pbLim)
		THROW(kresReadFault);
	snInput = lsbf(snInput);
	return snInput;
}

/*----------------------------------------------------------------------------------------------
	Read a wide character (unsigned 16-bit word) from the stream.
	Switch the bytes from big-endian to little-endian format.
----------------------------------------------------------------------------------------------*/
utf16 GrBufferIStream::ReadUShortFromFont()
{
	utf16 chwInput = *(utf16 *)m_pbNext;
	m_pbNext += isizeof(utf16);
	if (m_pbLim && m_pbNext > m_pbLim)
		THROW(kresReadFault);
	chwInput = lsbf(chwInput);
	return chwInput;
}

/*----------------------------------------------------------------------------------------------
	Read a standard (32-bit) word from the stream. Switch the bytes from big-endian 
	to little-endian format.
----------------------------------------------------------------------------------------------*/
int GrBufferIStream::ReadIntFromFont()
{
	int nInput = *(int *)m_pbNext;
	m_pbNext += isizeof(int);
	if (m_pbLim && m_pbNext > m_pbLim)
		THROW(kresReadFault);
	nInput = lsbf(nInput);
	return nInput;
}

/*----------------------------------------------------------------------------------------------
	Read a block of data from the stream. DON'T switch the bytes from big-endian 
	to little-endian format.
----------------------------------------------------------------------------------------------*/
void GrBufferIStream::ReadBlockFromFont(void * pvInput, int cb)
{
	std::copy(m_pbNext, m_pbNext + cb, reinterpret_cast<byte*>(pvInput));
	m_pbNext += cb;
	if (m_pbLim && m_pbNext > m_pbLim)
		THROW(kresReadFault);
}

/*----------------------------------------------------------------------------------------------
	Get the absolute position of the font-file stream (relative to the beginning of
	the file). For buffers, we just return the byte position in the buffer.
----------------------------------------------------------------------------------------------*/
void GrBufferIStream::GetPositionInFont(long * plPos)
{
	*plPos = (m_pbNext - m_pbStart);
}

/*----------------------------------------------------------------------------------------------
	Set the position of the font-file stream to the given absolute position (relative
	to the beginning of the file). For buffers, assume the position is relative to the
	beginning of the buffer.
----------------------------------------------------------------------------------------------*/
void GrBufferIStream::SetPositionInFont(long lPos)
{
	m_pbNext = m_pbStart + lPos;
	if (m_pbLim && m_pbNext > m_pbLim)
		THROW(kresReadFault);
}


//:>********************************************************************************************
//:>	Swap byte order.
//:>********************************************************************************************
int swapb(int nArg)
{
#if WORDS_BIGENDIAN
return nArg;
#else
	int b1, b2, b3, b4;
	b1 = ((nArg & 0xFF000000) >> 24)     & 0x000000FF;	// remove sign extension
	b2 = ((nArg & 0x00FF0000) >> 8);  // & 0x0000FF00;
	b3 = ((nArg & 0x0000FF00) << 8);  // & 0x00FF0000;
	b4 = ((nArg & 0x000000FF) << 24); // & 0xFF000000;
	int nRet = b1 | b2 | b3 | b4;
	return nRet;
#endif
}

unsigned int swapb(unsigned int nArg)
{
#if WORDS_BIGENDIAN
return nArg;
#else
	int b1, b2, b3, b4;
	b1 = ((nArg & 0xFF000000) >> 24)     & 0x000000FF;	// remove sign extension
	b2 = ((nArg & 0x00FF0000) >> 8);  // & 0x0000FF00;
	b3 = ((nArg & 0x0000FF00) << 8);  // & 0x00FF0000;
	b4 = ((nArg & 0x000000FF) << 24); // & 0xFF000000;
	int nRet = b1 | b2 | b3 | b4;
	return nRet;
#endif
}

utf16 swapb(utf16 chwArg)
{
#if WORDS_BIGENDIAN
return chwArg;
#else
	utf16 b1, b2;
	b1 = ((chwArg & 0xFF00) >> 8)     & 0x00FF;		// remove sign extension
	b2 = ((chwArg & 0x00FF) << 8); // & 0xFF00;
	utf16 chwRet = b1 | b2;
	return chwRet;
#endif
}

short swapb(short snArg)
{
#if WORDS_BIGENDIAN
return snArg;
#else
	short b1, b2;
	b1 = ((snArg & 0xFF00) >> 8)     & 0x00FF;		// remove sign extension
	b2 = ((snArg & 0x00FF) << 8); // & 0xFF00;
	short snRet = b1 | b2;
	return snRet;
#endif
}

} //namespace gr