1 /*
2 	Copyright (C) 2007-2010 Christopher E. Miller
3 	
4 	This software is provided 'as-is', without any express or implied
5 	warranty.  In no event will the authors be held liable for any damages
6 	arising from the use of this software.
7 	
8 	Permission is granted to anyone to use this software for any purpose,
9 	including commercial applications, and to alter it and redistribute it
10 	freely, subject to the following restrictions:
11 	
12 	1. The origin of this software must not be misrepresented; you must not
13 	   claim that you wrote the original software. If you use this software
14 	   in a product, an acknowledgment in the product documentation would be
15 	   appreciated but is not required.
16 	2. Altered source versions must be plainly marked as such, and must not be
17 	   misrepresented as being the original software.
18 	3. This notice may not be removed or altered from any source distribution.
19 */
20 
21 
22 module dfl.internal.dlib;
23 
24 
25 alias typeof(""c[]) Dstring;
26 alias typeof(""c.ptr) Dstringz;
27 alias typeof(" "c[0]) Dchar;
28 alias typeof(""w[]) Dwstring;
29 alias typeof(""w.ptr) Dwstringz;
30 alias typeof(" "w[0]) Dwchar;
31 alias typeof(""d[]) Ddstring;
32 alias typeof(""d.ptr) Ddstringz;
33 alias typeof(" "d[0]) Ddchar;
34 
35 
36 version(D_Version2)
37 {
38 	version = DFL_D2;
39 	version = DFL_D2_AND_ABOVE;
40 }
41 else version(D_Version3)
42 {
43 	version = DFL_D3;
44 	version = DFL_D3_AND_ABOVE;
45 	version = DFL_D2_AND_ABOVE;
46 }
47 else version(D_Version4)
48 {
49 	version = DFL_D4;
50 	version = DFL_D4_AND_ABOVE;
51 	version = DFL_D3_AND_ABOVE;
52 	version = DFL_D2_AND_ABOVE;
53 }
54 else
55 {
56 	version = DFL_D1;
57 }
58 //version = DFL_D1_AND_ABOVE;
59 
60 
61 version(DFL_D1)
62 {
63 	public import dfl.internal.d1;
64 }
65 else
66 {
67 	public import dfl.internal.d2;
68 }
69 
70 
71 version(DFL_D1)
72 {
73 	version(DFL_USE_CORE_MEMORY)
74 	{
75 	}
76 	else
77 	{
78 		version = DFL_NO_USE_CORE_MEMORY;
79 		version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION;
80 	}
81 	
82 	version(DFL_CONV_TO_TEMPLATE)
83 	{
84 	}
85 	else
86 	{
87 		version = DFL_NO_CONV_TO_TEMPLATE;
88 	}
89 }
90 
91 
92 version(DFL_D2_AND_ABOVE)
93 {
94 	version(DFL_beforeDMD2020)
95 	{
96 		version = DFL_NO_USE_CORE_MEMORY;
97 		version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION;
98 		version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR;
99 		
100 		version = DFL_beforeDMD2021;
101 		version = DFL_beforeDMD2029;
102 	}
103 	
104 	version(DFL_beforeDMD2021)
105 	{
106 		version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR;
107 		
108 		version = DFL_beforeDMD2029;
109 	}
110 	
111 	version(DFL_beforeDMD2029)
112 	{
113 		version(DFL_CONV_TO_TEMPLATE)
114 		{
115 		}
116 		else
117 		{
118 			version = DFL_NO_CONV_TO_TEMPLATE;
119 		}
120 	}
121 }
122 
123 
124 version(DFL_NO_USE_CORE_MEMORY)
125 {
126 	version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION;
127 }
128 
129 
130 public import std.traits;
131 
132 
133 alias ReturnType!(Object.opEquals) Dequ; // Since D2 changes mid-stream.
134 
135 
136 Dstring getObjectString(Object o)
137 {
138 	return o.toString();
139 }
140 
141 
142 version(DFL_NO_USE_CORE_MEMORY)
143 {
144 	private import std.gc; // If you get "module gc cannot read file 'core\memory.d'" then use -version=DFL_NO_USE_CORE_MEMORY <http://wiki.dprogramming.com/Dfl/CompileVersions>
145 	
146 	void gcPin(void* p) { }
147 	void gcUnpin(void* p) { }
148 	
149 	deprecated alias std.gc.genCollect gcGenCollect;
150 	
151 	alias std.gc.fullCollect gcFullCollect;
152 }
153 else
154 {
155 	private import core.memory; // If you get "module gc cannot read file 'std\gc.d'" then use -version=DFL_USE_CORE_MEMORY <http://wiki.dprogramming.com/Dfl/CompileVersions>
156 	
157 	void gcPin(void* p) { }
158 	void gcUnpin(void* p) { }
159 	
160 	deprecated void gcGenCollect()
161 	{
162 		core.memory.GC.collect();
163 	}
164 	
165 		void gcFullCollect() nothrow
166 	{
167 			try
168 			{
169 				core.memory.GC.collect();
170 			}
171 			catch (Throwable e)
172 			{
173 			}
174 		}
175 	}
176 
177 
178 private import std..string;
179 
180 alias std..string.icmp stringICmp;
181 
182 version(DFL_NO_CONV_TO_TEMPLATE)
183 {
184 	alias std..string.toString stringFromStringz;
185 }
186 else
187 {
188 	version(DFL_DMD2029)
189 	{
190 		Dstring stringFromStringz(Dstringz sz)
191 		{
192 			return std.conv.to!(Dstring, Dstringz)(sz); // D 2.029
193 		}
194 	}
195 	else
196 	{
197 		Dstring stringFromStringz(Dstringz sz)
198 		{
199 			return std.conv.to!(Dstring)(sz);
200 		}
201 	}
202 	
203 	version(DFL_D2_AND_ABOVE)
204 	{
205 		Dstring stringFromStringz(char* sz)
206 		{
207 			return stringFromStringz(cast(Dstringz)sz);
208 		}
209 	}
210 }
211 
212 alias std..string.split stringSplit;
213 
214 version(DFL_NO_CONV_TO_TEMPLATE)
215 {
216 	alias std..string.toString intToString;
217 }
218 else
219 {
220 	Dstring intToString(int i) 
221 	{ 
222 		return to!(Dstring)(i); // D 2.029
223 	}
224 }
225 
226 alias std.algorithm.find charFindInString;
227 
228 alias std..string.toStringz stringToStringz;
229 
230 Dstring uintToHexString(uint num)
231 {
232 	return std..string.format("%X", num);
233 }
234 
235 alias std..string.splitLines stringSplitLines;
236 
237 
238 private import std.path;
239 
240 alias std.path.dirName pathGetDirName;
241 
242 version(D_Version2)
243 	alias std.ascii.newline nativeLineSeparatorString;
244 else
245 	alias std.path.linesep nativeLineSeparatorString;
246 
247 alias std.path.buildPath pathJoin;
248 
249 alias std.path.pathSeparator nativePathSeparatorString;
250 
251 
252 version(_DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION)
253 {
254 	private import std.outofmemory;
255 	
256 	alias std.outofmemory.OutOfMemoryException OomException;
257 }
258 else
259 {
260 	private import core.exception;
261 	
262 	version(_DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR)
263 	{
264 		class OomException: core.exception.OutOfMemoryException
265 		{
266 			this()
267 			{
268 				super(null, 0);
269 			}
270 		}
271 	}
272 	else
273 	{
274 		class OomException: core.exception.OutOfMemoryError
275 		{
276 			this()
277 			{
278 				super(null, 0);
279 			}
280 		}
281 	}
282 }
283 
284 
285 private import std.utf;
286 
287 alias std.utf.decode utf8stringGetUtf32char;
288 
289 alias std.utf.toUTF8 utf16stringtoUtf8string;
290 
291 alias std.utf.toUTF16 utf8stringtoUtf16string;
292 
293 alias std.utf.toUTFz!(typeof(Dwstring.init.ptr)) utf8stringToUtf16stringz;
294 
295 alias std.utf.toUTF8 utf32stringtoUtf8string;
296 
297 alias std.utf.toUTF32 utf8stringtoUtf32string;
298 
299 
300 private import std.uni;
301 
302 alias std.uni.toLower utf32charToLower;
303 
304 
305 private import std.conv;
306 
307 version(DFL_NO_CONV_TO_TEMPLATE)
308 {
309 	alias std.conv.toInt stringToInt;
310 }
311 else
312 {
313 	version(DFL_DMD2029)
314 	{
315 		alias std.conv.to!(int, Dstring) stringToInt; // D 2.029
316 	}
317 	else
318 	{
319 		int stringToInt(Dstring s)
320 		{
321 			return std.conv.to!(int)(s);
322 		}
323 	}
324 }
325 
326 
327 private import std.ascii;
328 
329 alias std.ascii.isHexDigit charIsHexDigit;
330 
331 
332 private import std.stream;
333 
334 alias std.stream.Stream DStream;
335 
336 alias std.stream.OutputStream DOutputStream;
337 
338 alias std.stream.StreamException DStreamException;
339 
340 
341 alias Object DObject;
342 version(DFL_D2_AND_ABOVE)
343 {
344 	version(DFL_CanThrowObject)
345 	{
346 		alias Object DThrowable;
347 	}
348 	else
349 	{
350 		alias Throwable DThrowable;
351 	}
352 }
353 else
354 {
355 	alias Object DThrowable;
356 }
357 
358 
359 char* unsafeToStringz(Dstring s)
360 {
361 	// This is intentionally unsafe, hence the name.
362 	if(!s.ptr[s.length])
363 		//return s.ptr;
364 		return cast(char*)s.ptr; // Needed in D2.
365 	//return stringToStringz(s);
366 	return cast(char*)stringToStringz(s); // Needed in D2.
367 }
368