1 #ifndef cppcomp_h
2 #define cppcomp_h
3 //{
4 /*
5    cppcomp.h : This file is part of pstoedit
6    header declaring compiler dependent stuff
7 
8    Copyright (C) 1998 - 2014 Wolfgang Glunz, wglunz35_AT_pstoedit.net
9 
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14 
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19 
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 
24 */
25 #ifdef HAVE_CONFIG_H
26 	#include "pstoedit_config.h"
27 #endif
28 
29 #ifdef _MSC_VER
30 	#ifndef DLLEXPORT
31 	#define DLLEXPORT __declspec( dllexport )
32 	#endif
33 #else
34 	#define DLLEXPORT
35 #endif
36 
37 
38 
39 
40 #ifdef _AIX
41 	#define _unix
42 #endif
43 
44 #if defined (_MSC_VER) && (_MSC_VER >= 1100)
45 	// MSVC 5 and 6 have ANSI C++ header files, but the compilation
46 	// is much slower and object files get bigger.
47 	// add other compiler that support STL and the ANSI C++ standard here
48 	// 1100 means : compiler v 11 which is MSVC 5
49 	// 1200 - VS 6
50 	// 1300 - VS 7 (2002)
51 	// 1400 - VS 8 (2005)
52 
53 	// NOTE: If your compiler or installation does not come with
54 	// an installation of the STL, just comment out the next line
55 
56 	#define HAVE_STL
57 	// old - now using STL dynamic vectors #define USE_FIXED_ARRAY 1
58 	#define HAVE_TYPENAME
59 
60 	#define USE_NEWSTRSTREAM
61 
62 	#define HAVE_AUTOPTR
63 
64 #endif
65 
66 #if defined (FORCESTLUSAGE)
67 //
68 	#define HAVE_STL
69 #endif
70 
71 #if defined (__GNUG__)  && (__GNUC__>=3)
72 
73 	#define HAVE_STL
74 	#define HAVE_TYPENAME
75 	#define HAVE_AUTOPTR
76 	#define USE_NEWSTRSTREAM
77 
78 #endif
79 
80 #ifdef __SUNPRO_CC
81 	#if __SUNPRO_CC >= 0x500
82 		#define HAVE_TYPENAME
83 		#if __SUNPRO_CC_COMPAT >= 5
84 // SUN's new compiler seems to work with STL (only)
85 			#define HAVE_STL
86 			#define HAVE_AUTOPTR
87 			#ifndef INTERNALBOOL
88 				#define INTERNALBOOL
89 			#endif
90 		#else
91 // we compile with -compat=4
92 			#ifdef INTERNALBOOL
93 				#undef INTERNALBOOL
94 			#endif
95 		#endif
96 	#else
97 // for the old version (CC 4.x)
98 	#endif
99 #endif
100 
101 #ifndef HAVE_TYPENAME
102 	#define typename
103 #endif
104 
105 //
106 // Note: for MSVC the usage of precompiled headers has to be turned
107 // off, otherwise compilation fails within the stdio. (the first time
108 // it works, but not a second one) (at least with MSVC 5)
109 //
110 
111 //{
112 #ifdef HAVE_STL
113 
114 #define I_fstream		<fstream>
115 #define I_iomanip		<iomanip>
116 #define I_ios			<ios>
117 #define I_iostream		<iostream>
118 
119 #if (defined (_MSC_VER) && _MSC_VER >= 1100)
120 // 1100 is version 5.0
121 #pragma warning(disable: 4786)
122 #define I_istream       <istream>
123 #define I_ostream       <ostream>
124 #define I_streamb       <streambuf>
125 #else
126 #define I_istream       <iostream>
127 #define I_ostream       <iostream>
128 #define I_streamb       <iostream>
129 #endif
130 
131 #ifdef  USE_NEWSTRSTREAM
132 #define I_strstream 	<sstream>
133 #define C_istrstream istringstream
134 #define C_ostrstream ostringstream
135 #else
136 #define I_strstream 	<strstream>
137 #define C_istrstream istrstream
138 #define C_ostrstream ostrstream
139 #endif
140 
141 
142 #define I_stdio			<cstdio>
143 #define I_stdlib		<cstdlib>
144 
145 #define USESTD using namespace std;
146 
147 #else
148 //} {
149 
150 // section for non ANSI C++ compilers
151 #define I_fstream		<fstream.h>
152 #define I_iomanip		<iomanip.h>
153 #define I_ios			<ios.h>
154 #define I_iostream		<iostream.h>
155 
156 #define I_istream		<iostream.h>
157 #define I_ostream		<iostream.h>
158 #define I_streamb		<iostream.h>
159 
160 
161 #if (defined(unix) || defined(__unix__) || defined(_unix) || defined(__unix) || defined(__EMX__) || defined (NetBSD) ) && !defined(DJGPP)
162 	#define I_strstream		<strstream.h>
163 	// next macro is to avoid usage of the above lengthy || list and because in .fl file __unix__ cannot be used sometimes because m4
164 	// expands this sometimes to ""
165 	#define PSTOEDIT_UNIXLIKE
166 #else
167 	#define I_strstream		<strstrea.h>
168 #endif
169 
170 #define C_istrstream istrstream
171 #define C_ostrstream ostrstream
172 
173 #if defined (__GNUG__) || defined (BOOLDEFINED) || ( defined (_MSC_VER) && _MSC_VER >= 1100) || defined (__BCPLUSPLUS__) && __BCPLUSPLUS__ >= 0x0400 || defined (INTERNALBOOL) || defined (_BOOL_EXISTS)
174 	// no need to define bool
175 	// 1100 is version 5.0
176 	// is for Digital Unix
177 #else
178 	typedef int bool;
179 	const bool false = 0;
180 	const bool true  = 1;
181 	#define BOOLDEFINED 1
182 #endif
183 
184 #define I_stdio		<stdio.h>
185 #define I_stdlib	<stdlib.h>
186 #define USESTD
187 
188 #endif
189 //}
190 
191 // if (defined (_MSC_VER) && _MSC_VER >= 1100) || defined (LINT) || defined (__COVERITY__)
192 #if 1
193 #define NOCOPYANDASSIGN(classname) \
194 	private: \
195 		classname(const classname&); \
196 		const classname & operator=(const classname&);
197 #else
198 /* nothing - GNU has problems with this anyway. But, it does not harm.
199    During compilation with VC++ potential misusages of the
200    forbidden methods will be detected */
201 	#define NOCOPYANDASSIGN(classname)
202 #endif
203 
204 
205 // rcw2: work round case insensitivity in RiscOS
206 #ifdef riscos
207   #define I_string_h "unix:string.h"
208 #else
209   #define I_string_h <string.h>
210 #endif
211 
212 // some code seems to rely on _WIN32 instead of WIN32
213 #if defined (WIN32)
214 	#ifndef _WIN32
215 		#define _WIN32 WIN32
216 	#endif
217 #endif
218 
219 #ifndef NIL
220 // 0 pointers
221 	#define NIL 0
222 #endif
223 
224 
225 
226 
227 //{
228 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
229 // for MS VS 8 (== cl version 14) we can use the new secure *_s string functions
230 // for other systems we have to "emulate" them
231 #define TARGETWITHLEN(str,len) str,len
232 
233 // MSVC moans: 'open': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _open. See online help for details.
234 
235 #define OPEN _open
236 #define CLOSE _close
237 #define READ _read
238 #define STRICMP _stricmp
239 #define FILENO _fileno
240 #define SETMODE _setmode
241 #define TEMPNAM _tempnam
242 #define GETCWD _getcwd
243 
244 #else
245 //} {
246 
247 #include I_iostream
248 #include I_string_h	// for strlen
249 #include I_stdlib	// for exit
250 
251 #define OPEN open
252 #define CLOSE close
253 #define READ read
254 #define STRICMP strcasecmp
255 //#define STRICMP stricmp
256 #define FILENO fileno
257 #define SETMODE setmode
258 #define TEMPNAM tempnam
259 #define GETCWD getcwd
260 
261 USESTD
262 
263 // approach for emulation:
264 // * guarantee that the result string ends with \n (array[size-1] = 0)
265 // * if the above condition cannot be achieved - exit with error message
266 //
267 // the implementation is far from optimal from a performance point of view - but for pstoedit this is not critical
268 //
269 
strncpy_s(char * de,size_t de_size,const char * so,size_t count)270 static inline void strncpy_s(char * de, size_t de_size, const char *  so, size_t count)   {
271 	const size_t sourcelen = so ? strlen(so) : 0;
272 	size_t tobecopied = sourcelen < count ? sourcelen : count;
273 	if ( tobecopied < de_size ) {
274 		while (so && *so && (tobecopied > 0) ) {
275 			*de = *so; ++de; ++so; --tobecopied;
276 		} // does not copy final 0
277 		*de = 0;
278 	} else {
279 		cerr << "buffer overflow in strcpy_s. Input string: '" << (so ? so : "NULL") << "' count: " << count  << " sourcelen " << sourcelen << " buffersize " << de_size << endl;
280 		exit(1);
281 	}
282 }
strcpy_s(char * de,size_t de_size,const char * so)283 static inline void strcpy_s(char * de, size_t de_size, const char * so) {
284 	strncpy_s(de, de_size, so, strlen(so) );
285 }
286 
strcat_s(char * de,size_t de_size,const char * so)287 static inline void strcat_s(char * de, size_t de_size, const char *  so) {
288 	// copy string into de + strlen(de)
289 	const size_t already_in = strlen(de);
290 	strcpy_s(de + already_in, de_size - already_in, so);
291 }
292 
293 // sprintf_s requires a second argument indicating the size of the target string
294 // because sprintf can have any number of arguments, we cannot handle this in the
295 // same manner as the functions above. So we need to hide/unhide this second argument
296 // for older compilers
297 
298 #ifdef HAVE_SNPRINTF
299 	#define sprintf_s snprintf
300 	#define TARGETWITHLEN(str,len) str,len
301 #else
302 	#define sprintf_s sprintf
303 	#define TARGETWITHLEN(str,len) str
304 #endif
305 
306 // sscanf_s requires a size argument for output strings, unless we use "to-string", we can use sscanf
307 // but this has to be assured in each individual case !!!
308 
309 // TARGETWITHLEN is not being used in the context of sscanf
310 #define sscanf_s sscanf
311 
312 #endif
313 //}
314 
315 #endif
316 //}
317