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