1
/******************************************************************************
2
* Copyright (c) 1998, Frank Warmerdam
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice shall be included
12
* in all copies or substantial portions of the Software.
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
* DEALINGS IN THE SOFTWARE.
21
******************************************************************************
25
* Include file providing low level portability services for CPL. This
26
* should be the first include file for any CPL based code. It provides the
29
* o Includes some standard system include files, such as stdio, and stdlib.
31
* o Defines CPL_C_START, CPL_C_END macros.
33
* o Ensures that some other standard macros like NULL are defined.
35
* o Defines some portability stuff like CPL_MSB, or CPL_LSB.
37
* o Ensures that core types such as GBool, GInt32, GInt16, GUInt32,
38
* GUInt16, and GByte are defined.
40
* $Log: cpl_port.h,v $
41
* Revision 1.9 1999/02/17 01:41:17 warmerda
44
* Revision 1.8 1999/02/02 21:32:38 warmerda
45
* Added CPL_{MSB,LSB}WORD{16,32} macros.
47
* Revision 1.7 1999/02/02 19:02:36 warmerda
48
* Removed duplicates of base types, and CPL_LSB
50
* Revision 1.6 1999/01/28 18:36:06 warmerda
51
* Ensure WIN32 is defined on Windows.
53
* Revision 1.5 1999/01/28 05:26:12 danmo
54
* Added byte swapping macros.
56
* Revision 1.4 1998/12/15 19:05:30 warmerda
59
* Revision 1.3 1998/12/14 04:50:07 warmerda
60
* Added DBMALLOC support
62
* Revision 1.2 1998/12/04 21:38:40 danmo
63
* Changed str*casecmp() to str*icmp() for WIN32
65
* Revision 1.1 1998/12/03 18:26:02 warmerda
70
#ifndef CPL_BASE_H_INCLUDED
71
#define CPL_BASE_H_INCLUDED
73
/* ==================================================================== */
74
/* We will use WIN32 as a standard windows define. */
75
/* ==================================================================== */
76
#if defined(_WIN32) && !defined(WIN32)
80
/* ==================================================================== */
81
/* Standard include files. */
82
/* ==================================================================== */
95
/* ==================================================================== */
96
/* Base portability stuff ... this stuff may need to be */
97
/* modified for new platforms. */
98
/* ==================================================================== */
100
/*---------------------------------------------------------------------
101
* types for 16 and 32 bits integers, etc...
102
*--------------------------------------------------------------------*/
103
#if UINT_MAX == 65535
105
typedef unsigned long GUInt32;
108
typedef unsigned int GUInt32;
111
typedef short GInt16;
112
typedef unsigned short GUInt16;
113
typedef unsigned char GByte;
117
/* ==================================================================== */
118
/* Other standard services. */
119
/* ==================================================================== */
121
# define CPL_C_START extern "C" {
128
/* # define CPL_DLL __declspec(dllexport) */
145
# define MIN(a,b) ((a<b) ? a : b)
146
# define MAX(a,b) ((a>b) ? a : b)
154
# define ABS(x) ((x<0) ? (-1*(x)) : x)
159
# define EQUALN(a,b,n) (strnicmp(a,b,n)==0)
160
# define EQUAL(a,b) (stricmp(a,b)==0)
162
# define EQUALN(a,b,n) (strncasecmp(a,b,n)==0)
163
# define EQUAL(a,b) (strcasecmp(a,b)==0)
167
/*---------------------------------------------------------------------
168
* CPL_LSB and CPL_MSB
169
* Only one of these 2 macros should be defined and specifies the byte
170
* ordering for the current platform.
171
* This should be defined in the Makefile, but if it is not then
172
* the default is CPL_LSB (Intel ordering, LSB first).
173
*--------------------------------------------------------------------*/
174
#if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
178
/*---------------------------------------------------------------------
179
* Little endian <==> big endian byte swap macros.
180
*--------------------------------------------------------------------*/
182
#define CPL_SWAP16(x) \
184
(((GUInt16)(x) & 0x00ffU) << 8) | \
185
(((GUInt16)(x) & 0xff00U) >> 8) ))
187
#define CPL_SWAP32(x) \
189
(((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
190
(((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
191
(((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
192
(((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
194
/* Until we have a safe 64 bits integer data type defined, we'll replace
195
m * this version of the CPL_SWAP64() macro with a less efficient one.
198
#define CPL_SWAP64(x) \
200
(uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
201
(uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
202
(uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
203
(uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
204
(uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
205
(uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
206
(uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
207
(uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
210
#define CPL_SWAPDOUBLE(p) { \
211
double _tmp = *(double *)(p); \
212
((GByte *)(p))[0] = ((GByte *)&_tmp)[7]; \
213
((GByte *)(p))[1] = ((GByte *)&_tmp)[6]; \
214
((GByte *)(p))[2] = ((GByte *)&_tmp)[5]; \
215
((GByte *)(p))[3] = ((GByte *)&_tmp)[4]; \
216
((GByte *)(p))[4] = ((GByte *)&_tmp)[3]; \
217
((GByte *)(p))[5] = ((GByte *)&_tmp)[2]; \
218
((GByte *)(p))[6] = ((GByte *)&_tmp)[1]; \
219
((GByte *)(p))[7] = ((GByte *)&_tmp)[0]; \
223
# define CPL_MSBWORD16(x) (x)
224
# define CPL_LSBWORD16(x) CPL_SWAP16(x)
225
# define CPL_MSBWORD32(x) (x)
226
# define CPL_LSBWORD32(x) CPL_SWAP32(x)
228
# define CPL_LSBWORD16(x) (x)
229
# define CPL_MSBWORD16(x) CPL_SWAP16(x)
230
# define CPL_LSBWORD32(x) (x)
231
# define CPL_MSBWORD32(x) CPL_SWAP32(x)
234
#endif /* ndef CPL_BASE_H_INCLUDED */