1 /******************************************************************************
2  * Copyright (c) 1998, Frank Warmerdam
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
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  ******************************************************************************
22  *
23  * cpl_serv.h
24  *
25  * This include file derived and simplified from the GDAL Common Portability
26  * Library.
27  */
28 
29 #ifndef CPL_SERV_H_INCLUDED
30 #define CPL_SERV_H_INCLUDED
31 
32 /* ==================================================================== */
33 /*        Standard include files.                                                */
34 /* ==================================================================== */
35 
36 #include "geo_config.h"
37 #include <stdio.h>
38 
39 #include <math.h>
40 
41 #ifdef HAVE_STRING_H
42 #  include <string.h>
43 #endif
44 #if defined(HAVE_STRINGS_H) && !defined(_WIN32)
45 #  include <strings.h>
46 #endif
47 #ifdef HAVE_STDLIB_H
48 #  include <stdlib.h>
49 #endif
50 
51 /**********************************************************************
52  * Do we want to build as a DLL on windows?
53  **********************************************************************/
54 #if !defined(CPL_DLL)
55 #  if defined(_WIN32) && defined(BUILD_AS_DLL)
56 #    define CPL_DLL     __declspec(dllexport)
57 #  else
58 #    define CPL_DLL
59 #  endif
60 #endif
61 
62 /* ==================================================================== */
63 /*      Other standard services.                                        */
64 /* ==================================================================== */
65 #ifdef __cplusplus
66 #  define CPL_C_START                extern "C" {
67 #  define CPL_C_END                }
68 #else
69 #  define CPL_C_START
70 #  define CPL_C_END
71 #endif
72 
73 #ifndef NULL
74 #  define NULL        0
75 #endif
76 
77 #ifndef FALSE
78 #  define FALSE        0
79 #endif
80 
81 #ifndef TRUE
82 #  define TRUE        1
83 #endif
84 
85 #ifndef MAX
86 #  define MIN(a,b)      ((a<b) ? a : b)
87 #  define MAX(a,b)      ((a>b) ? a : b)
88 #endif
89 
90 #ifndef NULL
91 #define NULL 0
92 #endif
93 
94 #ifndef ABS
95 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
96 #endif
97 
98 #ifndef EQUAL
99 #if defined(_WIN32) && !defined(__CYGWIN__)
100 #  define EQUALN(a,b,n)           (strnicmp(a,b,n)==0)
101 #  define EQUAL(a,b)              (stricmp(a,b)==0)
102 #else
103 /*  https://stackoverflow.com/questions/3694723/error-c3861-strcasecmp-identifier-not-found-in-visual-studio-2008/26359433 */
104 /* not #if defined(_WIN32) || defined(_WIN64) because we have strncasecmp in mingw */
105 #  ifdef _MSC_VER
106 #    define EQUALN(a,b,n)           (_strnicmp(a,b,n)==0)
107 #    define EQUAL(a,b)              (_stricmp(a,b)==0)
108 #  else
109 #    define EQUALN(a,b,n)           (strncasecmp(a,b,n)==0)
110 #    define EQUAL(a,b)              (strcasecmp(a,b)==0)
111 #  endif
112 #endif
113 #endif
114 
115 /* ==================================================================== */
116 /*      VSI Services (just map directly onto Standard C services.       */
117 /* ==================================================================== */
118 
119 #define VSIFOpen        fopen
120 #define VSIFClose        fclose
121 #define VSIFEof                feof
122 #define VSIFPrintf        fprintf
123 #define VSIFPuts        fputs
124 #define VSIFPutc        fputc
125 #define VSIFGets        fgets
126 #define VSIRewind        rewind
127 #define VSIFSeek        fseek
128 #define VSIFTell        ftell
129 #define VSIFRead        fread
130 
131 #ifndef notdef
132 #define VSICalloc(x,y)        _GTIFcalloc(x*y)
133 #define VSIMalloc        _GTIFcalloc
134 #define VSIFree                _GTIFFree
135 #define VSIRealloc      _GTIFrealloc
136 #else
137 #define VSICalloc(x,y)        (((char *) _GTIFcalloc(x*y+4)) + 4)
138 #define VSIMalloc(x)        (((char *) _GTIFcalloc((x)+4)) + 4)
139 #define VSIFree(x)      _GTIFFree(((char *) (x)) - 4)
140 #define VSIRealloc(p,n) (((char *) _GTIFrealloc(((char *)p)-4,(n)+4)) + 4)
141 #endif
142 
143 /* -------------------------------------------------------------------- */
144 /*      Safe malloc() API.  Thin cover over VSI functions with fatal    */
145 /*      error reporting if memory allocation fails.                     */
146 /* -------------------------------------------------------------------- */
147 CPL_C_START
148 
149 #define CPLMalloc  gtCPLMalloc
150 #define CPLCalloc  gtCPLCalloc
151 #define CPLRealloc gtCPLRealloc
152 #define CPLStrdup  gtCPLStrdup
153 
154 void CPL_DLL *CPLMalloc( int );
155 void CPL_DLL *CPLCalloc( int, int );
156 void CPL_DLL *CPLRealloc( void *, int );
157 char CPL_DLL *CPLStrdup( const char * );
158 
159 #define CPLFree(x)        { if( x != NULL ) VSIFree(x); }
160 
161 /* -------------------------------------------------------------------- */
162 /*      Read a line from a text file, and strip of CR/LF.               */
163 /* -------------------------------------------------------------------- */
164 
165 #define CPLReadLine gtCPLReadLine
166 
167 const char CPL_DLL *CPLReadLine( FILE * );
168 
169 /*=====================================================================
170                    Error handling functions (cpl_error.c)
171  =====================================================================*/
172 
173 typedef enum
174 {
175     CE_None = 0,
176     CE_Log = 1,
177     CE_Warning = 2,
178     CE_Failure = 3,
179     CE_Fatal = 4
180 } CPLErr;
181 
182 #define CPLError      gtCPLError
183 #define CPLErrorReset gtCPLErrorReset
184 #define CPLGetLastErrorNo gtCPLGetLastErrorNo
185 #define CPLGetLastErrorMsg gtCPLGetLastErrorMsg
186 #define CPLSetErrorHandler gtCPLSetErrorHandler
187 #define _CPLAssert    gt_CPLAssert
188 
189 void CPL_DLL CPLError(CPLErr eErrClass, int err_no, const char *fmt, ...);
190 void CPL_DLL CPLErrorReset();
191 int  CPL_DLL CPLGetLastErrorNo();
192 const char CPL_DLL * CPLGetLastErrorMsg();
193 void CPL_DLL CPLSetErrorHandler(void(*pfnErrorHandler)(CPLErr,int,
194                                                        const char *));
195 void CPL_DLL _CPLAssert( const char *, const char *, int );
196 
197 #ifdef DEBUG
198 #  define CPLAssert(expr)  ((expr) ? (void)(0) : _CPLAssert(#expr,__FILE__,__LINE__))
199 #else
200 #  define CPLAssert(expr)
201 #endif
202 
203 CPL_C_END
204 
205 /* ==================================================================== */
206 /*      Well known error codes.                                         */
207 /* ==================================================================== */
208 
209 #define CPLE_AppDefined                        1
210 #define CPLE_OutOfMemory                2
211 #define CPLE_FileIO                        3
212 #define CPLE_OpenFailed                        4
213 #define CPLE_IllegalArg                        5
214 #define CPLE_NotSupported                6
215 #define CPLE_AssertionFailed                7
216 #define CPLE_NoWriteAccess                8
217 
218 /*=====================================================================
219                    Stringlist functions (strlist.c)
220  =====================================================================*/
221 CPL_C_START
222 
223 #define CSLAddString gtCSLAddString
224 #define CSLCount     gtCSLCount
225 #define CSLGetField  gtCSLGetField
226 #define CSLDestroy   gtCSLDestroy
227 #define CSLDuplicate gtCSLDuplicate
228 #define CSLTokenizeString gtCSLTokenizeString
229 #define CSLTokenizeStringComplex gtCSLTokenizeStringComplex
230 
231 char CPL_DLL   **CSLAddString(char **papszStrList, const char *pszNewString);
232 int  CPL_DLL   CSLCount(char **papszStrList);
233 const char CPL_DLL *CSLGetField( char **, int );
234 void CPL_DLL   CSLDestroy(char **papszStrList);
235 char CPL_DLL   **CSLDuplicate(char **papszStrList);
236 
237 char CPL_DLL   **CSLTokenizeString(const char *pszString );
238 char CPL_DLL   **CSLTokenizeStringComplex(const char *pszString,
239                                    const char *pszDelimiter,
240                                    int bHonourStrings, int bAllowEmptyTokens );
241 
242 /* ==================================================================== */
243 /*      .csv file related functions (from cpl_csv.c)                    */
244 /* ==================================================================== */
245 
246 typedef enum {
247     CC_ExactString,
248     CC_ApproxString,
249     CC_Integer
250 } CSVCompareCriteria;
251 
252 #define CSVFilename gtCSVFilename
253 #define CSVReadParseLine gtCSVReadParseLine
254 #define CSVScanLines gtCSVScanLines
255 #define CSVScanFile gtCSVScanFile
256 #define CSVScanFileByName gtCSVScanFileByName
257 #define CSVGetFieldId gtCSVGetFieldId
258 #define CSVDeaccess gtCSVDeaccess
259 #define CSVGetField gtCSVGetField
260 #define SetCSVFilenameHook gtSetCSVFilenameHook
261 #define CSVGetFileFieldId gtCSVGetFileFieldId
262 
263 const char CPL_DLL *CSVFilename( const char * );
264 
265 char CPL_DLL **CSVReadParseLine( FILE * );
266 char CPL_DLL **CSVScanLines( FILE *, int, const char *, CSVCompareCriteria );
267 char CPL_DLL **CSVScanFile( const char *, int, const char *,
268                             CSVCompareCriteria );
269 char CPL_DLL **CSVScanFileByName( const char *, const char *, const char *,
270                                   CSVCompareCriteria );
271 int CPL_DLL CSVGetFieldId( FILE *, const char * );
272 int CPL_DLL CSVGetFileFieldId( const char *, const char * );
273 
274 void CPL_DLL CSVDeaccess( const char * );
275 
276 const char CPL_DLL *CSVGetField( const char *, const char *, const char *,
277                                  CSVCompareCriteria, const char * );
278 
279 void CPL_DLL SetCSVFilenameHook( const char *(*)(const char *) );
280 
281 CPL_C_END
282 
283 #endif /* ndef CPL_SERV_H_INCLUDED */
284