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 alias typeof(""c[]) Dstring;
25 alias typeof(""c.ptr) Dstringz;
26 alias typeof(" "c[0]) Dchar;
27 alias typeof(""w[]) Dwstring;
28 alias typeof(""w.ptr) Dwstringz;
29 alias typeof(" "w[0]) Dwchar;
30 alias typeof(""d[]) Ddstring;
31 alias typeof(""d.ptr) Ddstringz;
32 alias typeof(" "d[0]) Ddchar;
33 
34 
35 version(D_Version2) {
36    version = DFL_D2;
37    version = DFL_D2_AND_ABOVE;
38 }
39 else version(D_Version3) {
40    version = DFL_D3;
41    version = DFL_D3_AND_ABOVE;
42    version = DFL_D2_AND_ABOVE;
43 }
44 else version(D_Version4) {
45    version = DFL_D4;
46    version = DFL_D4_AND_ABOVE;
47    version = DFL_D3_AND_ABOVE;
48    version = DFL_D2_AND_ABOVE;
49 }
50 else {
51    version = DFL_D1;
52 }
53 //version = DFL_D1_AND_ABOVE;
54 
55 
56 version(DFL_D1) {
57    import dfl.internal.d1;
58 } else {
59  // FIX:  import dfl.internal.d2;
60 }
61 
62 
63 version(DFL_D1) {
64    version(DFL_USE_CORE_MEMORY) {
65    } else {
66       version = DFL_NO_USE_CORE_MEMORY;
67       version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION;
68    }
69 
70    version(DFL_CONV_TO_TEMPLATE) {
71    } else {
72       version = DFL_NO_CONV_TO_TEMPLATE;
73    }
74 }
75 
76 
77 version(DFL_D2_AND_ABOVE) {
78    version(DFL_beforeDMD2020) {
79       version = DFL_NO_USE_CORE_MEMORY;
80       version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION;
81       version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR;
82 
83       version = DFL_beforeDMD2021;
84       version = DFL_beforeDMD2029;
85    }
86 
87    version(DFL_beforeDMD2021) {
88       version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR;
89 
90       version = DFL_beforeDMD2029;
91    }
92 
93    version(DFL_beforeDMD2029) {
94       version(DFL_CONV_TO_TEMPLATE) {
95       } else {
96          version = DFL_NO_CONV_TO_TEMPLATE;
97       }
98    }
99 }
100 
101 
102 version(DFL_NO_USE_CORE_MEMORY) {
103    version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION;
104 }
105 
106 
107 import std.traits;
108 
109 
110 alias ReturnType!(Object.opEquals) Dequ; // Since D2 changes mid-stream.
111 
112 
113 Dstring getObjectString(Object o) {
114    return o.toString();
115 }
116 
117 
118 version(DFL_NO_USE_CORE_MEMORY) {
119    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>
120 
121    void gcPin(void* p) { }
122    void gcUnpin(void* p) { }
123 
124    deprecated alias std.gc.genCollect gcGenCollect;
125 
126    alias std.gc.fullCollect gcFullCollect;
127 } else {
128    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>
129 
130    void gcPin(void* p) { }
131    void gcUnpin(void* p) { }
132 
133    deprecated void gcGenCollect() {
134       core.memory.GC.collect();
135    }
136 
137    void gcFullCollect() nothrow {
138       try
139       {
140          core.memory.GC.collect();
141       } catch (Throwable e) {
142       }
143    }
144 }
145 
146 
147 private import std..string;
148 
149 alias std..string.icmp stringICmp;
150 
151 version(DFL_NO_CONV_TO_TEMPLATE) {
152    alias std..string.toString stringFromStringz;
153 } else {
154    version(DFL_DMD2029) {
155       Dstring stringFromStringz(Dstringz sz) {
156          return std.conv.to!(Dstring, Dstringz)(sz); // D 2.029
157       }
158    }
159    else {
160       Dstring stringFromStringz(Dstringz sz) {
161          return std.conv.to!(Dstring)(sz);
162       }
163    }
164 
165    version(DFL_D2_AND_ABOVE) {
166       Dstring stringFromStringz(char* sz) {
167          return stringFromStringz(cast(Dstringz)sz);
168       }
169    }
170 }
171 
172 alias std..string.split stringSplit;
173 
174 version(DFL_NO_CONV_TO_TEMPLATE) {
175    alias std..string.toString intToString;
176 } else {
177    Dstring intToString(int i) {
178       return to!(Dstring)(i); // D 2.029
179    }
180 }
181 
182 alias std.algorithm.find charFindInString;
183 
184 alias std..string.toStringz stringToStringz;
185 
186 Dstring uintToHexString(uint num) {
187    return std..string.format("%X", num);
188 }
189 
190 alias std..string.splitLines stringSplitLines;
191 
192 import std.path;
193 
194 alias std.path.dirName pathGetDirName;
195 
196 version(D_Version2) {
197    alias std.ascii.newline nativeLineSeparatorString;
198 } else {
199    alias std.path.linesep nativeLineSeparatorString;
200 }
201 
202 alias std.path.buildPath pathJoin;
203 
204 alias std.path.pathSeparator nativePathSeparatorString;
205 
206 
207 version(_DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION) {
208    private import std.outofmemory;
209 
210    alias std.outofmemory.OutOfMemoryException OomException;
211 } else {
212    private import core.exception;
213 
214    version(_DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR) {
215       class OomException: core.exception.OutOfMemoryException {
216          this() {
217             super(null, 0);
218          }
219       }
220    } else {
221       class OomException: core.exception.OutOfMemoryError {
222          this() {
223             super(null, 0);
224          }
225       }
226    }
227 }
228 
229 
230 import std.utf;
231 
232 alias std.utf.decode utf8stringGetUtf32char;
233 
234 alias std.utf.toUTF8 utf16stringtoUtf8string;
235 
236 alias std.utf.toUTF16 utf8stringtoUtf16string;
237 
238 alias std.utf.toUTFz!(typeof(Dwstring.init.ptr)) utf8stringToUtf16stringz;
239 
240 alias std.utf.toUTF8 utf32stringtoUtf8string;
241 
242 alias std.utf.toUTF32 utf8stringtoUtf32string;
243 
244 
245 private import std.uni;
246 
247 alias std.uni.toLower utf32charToLower;
248 
249 
250 import std.conv;
251 
252 version(DFL_NO_CONV_TO_TEMPLATE) {
253    alias std.conv.toInt stringToInt;
254 } else {
255    version(DFL_DMD2029) {
256       alias std.conv.to!(int, Dstring) stringToInt; // D 2.029
257    } else {
258       int stringToInt(Dstring s) {
259          return std.conv.to!(int)(s);
260       }
261    }
262 }
263 
264 private import std.ascii;
265 
266 alias std.ascii.isHexDigit charIsHexDigit;
267 
268 private import dfl.internal.stream;
269 
270 alias dfl.internal.stream.Stream DStream;
271 
272 alias dfl.internal.stream.OutputStream DOutputStream;
273 
274 alias dfl.internal.stream.StreamException DStreamException;
275 
276 
277 alias Object DObject;
278 version(DFL_D2_AND_ABOVE) {
279    version(DFL_CanThrowObject) {
280       alias Object DThrowable;
281    } else {
282       alias Throwable DThrowable;
283    }
284 } else {
285    alias Object DThrowable;
286 }
287 
288 
289 char* unsafeToStringz(Dstring s) {
290    // This is intentionally unsafe, hence the name.
291    if(!s.ptr[s.length]) {
292       return cast(char*)s.ptr;   // Needed in D2.
293    }
294    return cast(char*)stringToStringz(s); // Needed in D2.
295 }