comparison dmd2/root/root.h @ 1452:638d16625da2

LDC 2 compiles again.
author Robert Clipsham <robert@octarineparrot.com>
date Sat, 30 May 2009 17:23:32 +0100
parents
children
comparison
equal deleted inserted replaced
1423:42bd767ec5a4 1452:638d16625da2
1
2
3 // Copyright (c) 1999-2006 by Digital Mars
4 // All Rights Reserved
5 // written by Walter Bright
6 // www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
10
11 #ifndef ROOT_H
12 #define ROOT_H
13
14 #include <stdlib.h>
15 #include <stdarg.h>
16
17 #if __DMC__
18 #pragma once
19 #endif
20
21 typedef size_t hash_t;
22
23 #include "dchar.h"
24
25 char *wchar2ascii(wchar_t *);
26 int wcharIsAscii(wchar_t *);
27 char *wchar2ascii(wchar_t *, unsigned len);
28 int wcharIsAscii(wchar_t *, unsigned len);
29
30 int bstrcmp(unsigned char *s1, unsigned char *s2);
31 char *bstr2str(unsigned char *b);
32 void error(const char *format, ...);
33 void error(const wchar_t *format, ...);
34 void warning(const char *format, ...);
35
36 #ifndef TYPEDEFS
37 #define TYPEDEFS
38
39 #if _MSC_VER
40 #include <float.h> // for _isnan
41 #include <malloc.h> // for alloca
42 // According to VC 8.0 docs, long double is the same as double
43 #define strtold strtod
44 #define strtof strtod
45 #define isnan _isnan
46
47 typedef __int64 longlong;
48 typedef unsigned __int64 ulonglong;
49 #else
50 typedef long long longlong;
51 typedef unsigned long long ulonglong;
52 #endif
53
54 #endif
55
56 longlong randomx();
57
58 /*
59 * Root of our class library.
60 */
61
62 struct OutBuffer;
63 struct Array;
64
65 struct Object
66 {
67 Object() { }
68 virtual ~Object() { }
69
70 virtual int equals(Object *o);
71
72 /**
73 * Returns a hash code, useful for things like building hash tables of Objects.
74 */
75 virtual hash_t hashCode();
76
77 /**
78 * Return <0, ==0, or >0 if this is less than, equal to, or greater than obj.
79 * Useful for sorting Objects.
80 */
81 virtual int compare(Object *obj);
82
83 /**
84 * Pretty-print an Object. Useful for debugging the old-fashioned way.
85 */
86 virtual void print();
87
88 virtual char *toChars();
89 virtual dchar *toDchars();
90 virtual void toBuffer(OutBuffer *buf);
91
92 /**
93 * Used as a replacement for dynamic_cast. Returns a unique number
94 * defined by the library user. For Object, the return value is 0.
95 */
96 virtual int dyncast();
97
98 /**
99 * Marks pointers for garbage collector by calling mem.mark() for all pointers into heap.
100 */
101 /*virtual*/ // not used, disable for now
102 void mark();
103 };
104
105 struct String : Object
106 {
107 int ref; // != 0 if this is a reference to someone else's string
108 char *str; // the string itself
109
110 String(char *str, int ref = 1);
111
112 ~String();
113
114 static hash_t calcHash(const char *str, size_t len);
115 static hash_t calcHash(const char *str);
116 hash_t hashCode();
117 unsigned len();
118 int equals(Object *obj);
119 int compare(Object *obj);
120 char *toChars();
121 void print();
122 void mark();
123 };
124
125 struct FileName : String
126 {
127 FileName(char *str, int ref);
128 FileName(char *path, char *name);
129 hash_t hashCode();
130 int equals(Object *obj);
131 static int equals(const char *name1, const char *name2);
132 int compare(Object *obj);
133 static int compare(const char *name1, const char *name2);
134 static int absolute(const char *name);
135 static char *ext(const char *);
136 char *ext();
137 static char *removeExt(const char *str);
138 static char *name(const char *);
139 char *name();
140 static char *path(const char *);
141 static const char *replaceName(const char *path, const char *name);
142
143 static char *combine(const char *path, const char *name);
144 static Array *splitPath(const char *path);
145 static FileName *defaultExt(const char *name, const char *ext);
146 static FileName *forceExt(const char *name, const char *ext);
147 int equalsExt(const char *ext);
148
149 void CopyTo(FileName *to);
150 static char *searchPath(Array *path, const char *name, int cwd);
151 static int exists(const char *name);
152 static void ensurePathExists(const char *path);
153 };
154
155 struct File : Object
156 {
157 int ref; // != 0 if this is a reference to someone else's buffer
158 unsigned char *buffer; // data for our file
159 unsigned len; // amount of data in buffer[]
160 void *touchtime; // system time to use for file
161
162 FileName *name; // name of our file
163
164 File(char *);
165 File(FileName *);
166 ~File();
167
168 void mark();
169
170 char *toChars();
171
172 /* Read file, return !=0 if error
173 */
174
175 int read();
176
177 /* Write file, either succeed or fail
178 * with error message & exit.
179 */
180
181 void readv();
182
183 /* Read file, return !=0 if error
184 */
185
186 int mmread();
187
188 /* Write file, either succeed or fail
189 * with error message & exit.
190 */
191
192 void mmreadv();
193
194 /* Write file, return !=0 if error
195 */
196
197 int write();
198
199 /* Write file, either succeed or fail
200 * with error message & exit.
201 */
202
203 void writev();
204
205 /* Return !=0 if file exists.
206 * 0: file doesn't exist
207 * 1: normal file
208 * 2: directory
209 */
210
211 /* Append to file, return !=0 if error
212 */
213
214 int append();
215
216 /* Append to file, either succeed or fail
217 * with error message & exit.
218 */
219
220 void appendv();
221
222 /* Return !=0 if file exists.
223 * 0: file doesn't exist
224 * 1: normal file
225 * 2: directory
226 */
227
228 int exists();
229
230 /* Given wildcard filespec, return an array of
231 * matching File's.
232 */
233
234 static Array *match(char *);
235 static Array *match(FileName *);
236
237 // Compare file times.
238 // Return <0 this < f
239 // =0 this == f
240 // >0 this > f
241 int compareTime(File *f);
242
243 // Read system file statistics
244 void stat();
245
246 /* Set buffer
247 */
248
249 void setbuffer(void *buffer, unsigned len)
250 {
251 this->buffer = (unsigned char *)buffer;
252 this->len = len;
253 }
254
255 void checkoffset(size_t offset, size_t nbytes);
256
257 void remove(); // delete file
258 };
259
260 struct OutBuffer : Object
261 {
262 unsigned char *data;
263 unsigned offset;
264 unsigned size;
265
266 OutBuffer();
267 ~OutBuffer();
268 void *extractData();
269 void mark();
270
271 void reserve(unsigned nbytes);
272 void setsize(unsigned size);
273 void reset();
274 void write(const void *data, unsigned nbytes);
275 void writebstring(unsigned char *string);
276 void writestring(const char *string);
277 void writedstring(const char *string);
278 void writedstring(const wchar_t *string);
279 void prependstring(const char *string);
280 void writenl(); // write newline
281 void writeByte(unsigned b);
282 void writebyte(unsigned b) { writeByte(b); }
283 void writeUTF8(unsigned b);
284 void writedchar(unsigned b);
285 void prependbyte(unsigned b);
286 void writeword(unsigned w);
287 void writeUTF16(unsigned w);
288 void write4(unsigned w);
289 void write(OutBuffer *buf);
290 void write(Object *obj);
291 void fill0(unsigned nbytes);
292 void align(unsigned size);
293 void vprintf(const char *format, va_list args);
294 void printf(const char *format, ...);
295 #if M_UNICODE
296 void vprintf(const unsigned short *format, va_list args);
297 void printf(const unsigned short *format, ...);
298 #endif
299 void bracket(char left, char right);
300 unsigned bracket(unsigned i, const char *left, unsigned j, const char *right);
301 void spread(unsigned offset, unsigned nbytes);
302 unsigned insert(unsigned offset, const void *data, unsigned nbytes);
303 void remove(unsigned offset, unsigned nbytes);
304 char *toChars();
305 char *extractString();
306 };
307
308 struct Array : Object
309 {
310 unsigned dim;
311 unsigned allocdim;
312 void **data;
313
314 Array();
315 ~Array();
316 void mark();
317 char *toChars();
318
319 void reserve(unsigned nentries);
320 void setDim(unsigned newdim);
321 void fixDim();
322 void push(void *ptr);
323 void *pop();
324 void shift(void *ptr);
325 void insert(unsigned index, void *ptr);
326 void insert(unsigned index, Array *a);
327 void append(Array *a);
328 void remove(unsigned i);
329 void zero();
330 void *tos();
331 void sort();
332 Array *copy();
333 };
334
335 struct Bits : Object
336 {
337 unsigned bitdim;
338 unsigned allocdim;
339 unsigned *data;
340
341 Bits();
342 ~Bits();
343 void mark();
344
345 void resize(unsigned bitdim);
346
347 void set(unsigned bitnum);
348 void clear(unsigned bitnum);
349 int test(unsigned bitnum);
350
351 void set();
352 void clear();
353 void copy(Bits *from);
354 Bits *clone();
355
356 void sub(Bits *b);
357 };
358
359 #endif