1 /*--------------------------------------------------------------------*//*:Ignore this sentence.
2 Copyright (C) 1999, 2001, 2003 SIL International. All rights reserved.
3 
4 Distributable under the terms of either the Common Public License or the
5 GNU Lesser General Public License, as specified in the LICENSING.txt file.
6 
7 File: GrCommon.h
8 Responsibility: Sharon Correll
9 Last reviewed:
10 
11 	Common generic header file.
12 
13 	This header file checks for the following compile time switches:
14 
15 		USING_MFC
16 ----------------------------------------------------------------------------------------------*/
17 
18 #ifndef GRCOMMON_INCLUDED
19 #define GRCOMMON_INCLUDED
20 
21 #define _SECURE_SCL 0	// to allow GlyphSetIterator to work, which points off the end of a vector
22 #define _HAS_ITERATOR_DEBUGGING 0
23 
24 // Standard Headers.
25 
26 #include <cstdlib>
27 #include <cstdio>
28 #include <cstdarg>
29 #include <climits>
30 //#include <exception>
31 #include <new>
32 
33 #include <fstream>
34 #include <iostream>
35 #include <vector>
36 #include <algorithm>
37 #include <string>
38 ///#include <stdexcept> -- possibly needed for std::string Xran and Xlen functions??
39 
40 // Uncomment this to allow multiple versions of gr to coexist
41 // in the same program e.g. pangographite with gtk uses namespace gr
42 // mozilla graphite defines this to gr2moz incase the 2 libraries are
43 // incompatible.
44 // Provided the client includes GrClient.h first this #define is
45 // picked up by all files.
46 
47 #define gr gr3ooo
48 
49 // Project headers
50 #include "GrPlatform.h"
51 
52 #ifndef GrAssert
53 #include <cassert>
54 #define GrAssert(exp) assert(exp)
55 #endif
56 
57 using std::max;
58 using std::min;
59 
60 namespace gr
61 {
62 
63 
64 /***********************************************************************************************
65 	Simple types.
66 ***********************************************************************************************/
67 typedef char	schar;
68 
69 // TODO ShonK: Make generic use these where appropriate.
70 #ifdef UNICODE
71 typedef utf16 	achar;
72 #else // !UNICODE
73 typedef schar	achar;
74 #endif // UNICODE
75 
76 typedef achar 	*Psz;
77 //typedef const achar * Pcsz;
78 
79 
80 
81 // This is to make a signed isizeof operator, otherwise we get tons of warnings about
82 // signed / unsigned mismatches.
83 #define isizeof(T) (sizeof(T))
84 
85 #define SizeOfArray(rgt) (isizeof(rgt) / isizeof(rgt[0]))
86 
87 
88 /***********************************************************************************************
89 	Tests for valid strings and pointers.
90 ***********************************************************************************************/
ValidPsz(const data16 * pszw)91 inline bool ValidPsz(const data16 *pszw)
92 {
93 	// TODO ShonK: Determine if IsBadStringPtrW is implemented on Win9x.
94 	return pszw != 0 && !GrIsBadStringPtrW(pszw, 0x10000000);
95 }
96 
ValidPsz(const schar * pszs)97 inline bool ValidPsz(const schar *pszs)
98 {
99 	return pszs != 0 && !GrIsBadStringPtrA(pszs, 0x10000000);
100 }
101 
ValidReadPtr(T * pt)102 template<typename T> inline bool ValidReadPtr(T *pt)
103 {
104 	return pt != 0 && !GrIsBadReadPtr(pt, isizeof(T));
105 }
106 
ValidWritePtr(T * pt)107 template<typename T> inline bool ValidWritePtr(T *pt)
108 {
109 	return pt != 0 && !GrIsBadWritePtr(pt, isizeof(T));
110 }
111 
ValidReadPtrSize(const void * pv,int cb)112 inline bool ValidReadPtrSize(const void *pv, int cb)
113 {
114 	if (cb < 0)	return false;
115 	if (cb == 0)	return true;
116 
117 	return pv != 0 && !GrIsBadReadPtr(pv, cb);
118 }
119 
ValidWritePtrSize(void * pv,int cb)120 inline bool ValidWritePtrSize(void *pv, int cb)
121 {
122 //	if (!bstr || ::IsBadReadPtr((byte *)bstr - isizeof(int), isizeof(int) + isizeof(OLECHAR)))
123 //		return false;
124 //	int cb = ((int *)bstr)[-1];
125 //	if (::IsBadReadPtr((byte *)bstr - isizeof(int), isizeof(int) + isizeof(OLECHAR) + cb))
126 //		return false;
127 	if (cb < 0)	return false;
128 	if (cb == 0)	return true;
129 
130 	return pv != 0 && !GrIsBadWritePtr(pv, cb);
131 }
132 
133 
134 } // namespace gr
135 
136 
137 // TODO Remove these as soon as they are no longer needed by GrCompiler:
138 //#include "UtilMem.h"
139 //#include "UtilRect.h"
140 //#include "UtilString.h"
141 //#include "UtilVector.h"
142 
143 #if defined(GR_NO_NAMESPACE)
144 using namespace gr;
145 #endif
146 
147 #endif // !GRCOMMON_INCLUDED
148 
149