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();
|
336
|
128 static char *removeExt(const char *str);
|
1
|
129 static char *name(const char *);
|
|
130 char *name();
|
|
131 static char *path(const char *);
|
|
132 static char *replaceName(char *path, char *name);
|
|
133
|
|
134 static char *combine(char *path, char *name);
|
|
135 static Array *splitPath(const char *path);
|
|
136 static FileName *defaultExt(const char *name, const char *ext);
|
|
137 static FileName *forceExt(const char *name, const char *ext);
|
|
138 int equalsExt(const char *ext);
|
|
139
|
|
140 void CopyTo(FileName *to);
|
|
141 static char *searchPath(Array *path, char *name, int cwd);
|
|
142 static int exists(const char *name);
|
|
143 static void ensurePathExists(const char *path);
|
|
144 };
|
|
145
|
|
146 struct File : Object
|
|
147 {
|
|
148 int ref; // != 0 if this is a reference to someone else's buffer
|
|
149 unsigned char *buffer; // data for our file
|
|
150 unsigned len; // amount of data in buffer[]
|
|
151 void *touchtime; // system time to use for file
|
|
152
|
|
153 FileName *name; // name of our file
|
|
154
|
|
155 File(char *);
|
|
156 File(FileName *);
|
|
157 ~File();
|
|
158
|
|
159 void mark();
|
|
160
|
|
161 char *toChars();
|
|
162
|
|
163 /* Read file, return !=0 if error
|
|
164 */
|
|
165
|
|
166 int read();
|
|
167
|
|
168 /* Write file, either succeed or fail
|
|
169 * with error message & exit.
|
|
170 */
|
|
171
|
|
172 void readv();
|
|
173
|
|
174 /* Read file, return !=0 if error
|
|
175 */
|
|
176
|
|
177 int mmread();
|
|
178
|
|
179 /* Write file, either succeed or fail
|
|
180 * with error message & exit.
|
|
181 */
|
|
182
|
|
183 void mmreadv();
|
|
184
|
|
185 /* Write file, return !=0 if error
|
|
186 */
|
|
187
|
|
188 int write();
|
|
189
|
|
190 /* Write file, either succeed or fail
|
|
191 * with error message & exit.
|
|
192 */
|
|
193
|
|
194 void writev();
|
|
195
|
|
196 /* Return !=0 if file exists.
|
|
197 * 0: file doesn't exist
|
|
198 * 1: normal file
|
|
199 * 2: directory
|
|
200 */
|
|
201
|
|
202 /* Append to file, return !=0 if error
|
|
203 */
|
|
204
|
|
205 int append();
|
|
206
|
|
207 /* Append to file, either succeed or fail
|
|
208 * with error message & exit.
|
|
209 */
|
|
210
|
|
211 void appendv();
|
|
212
|
|
213 /* Return !=0 if file exists.
|
|
214 * 0: file doesn't exist
|
|
215 * 1: normal file
|
|
216 * 2: directory
|
|
217 */
|
|
218
|
|
219 int exists();
|
|
220
|
|
221 /* Given wildcard filespec, return an array of
|
|
222 * matching File's.
|
|
223 */
|
|
224
|
|
225 static Array *match(char *);
|
|
226 static Array *match(FileName *);
|
|
227
|
|
228 // Compare file times.
|
|
229 // Return <0 this < f
|
|
230 // =0 this == f
|
|
231 // >0 this > f
|
|
232 int compareTime(File *f);
|
|
233
|
|
234 // Read system file statistics
|
|
235 void stat();
|
|
236
|
|
237 /* Set buffer
|
|
238 */
|
|
239
|
|
240 void setbuffer(void *buffer, unsigned len)
|
|
241 {
|
|
242 this->buffer = (unsigned char *)buffer;
|
|
243 this->len = len;
|
|
244 }
|
|
245
|
|
246 void checkoffset(size_t offset, size_t nbytes);
|
|
247
|
|
248 void remove(); // delete file
|
|
249 };
|
|
250
|
|
251 struct OutBuffer : Object
|
|
252 {
|
|
253 unsigned char *data;
|
|
254 unsigned offset;
|
|
255 unsigned size;
|
|
256
|
|
257 OutBuffer();
|
|
258 ~OutBuffer();
|
|
259 void *extractData();
|
|
260 void mark();
|
|
261
|
|
262 void reserve(unsigned nbytes);
|
|
263 void setsize(unsigned size);
|
|
264 void reset();
|
|
265 void write(const void *data, unsigned nbytes);
|
|
266 void writebstring(unsigned char *string);
|
336
|
267 void writestring(const char *string);
|
1
|
268 void writedstring(const char *string);
|
|
269 void writedstring(const wchar_t *string);
|
|
270 void prependstring(char *string);
|
|
271 void writenl(); // write newline
|
|
272 void writeByte(unsigned b);
|
|
273 void writebyte(unsigned b) { writeByte(b); }
|
|
274 void writeUTF8(unsigned b);
|
|
275 void writedchar(unsigned b);
|
|
276 void prependbyte(unsigned b);
|
|
277 void writeword(unsigned w);
|
|
278 void writeUTF16(unsigned w);
|
|
279 void write4(unsigned w);
|
|
280 void write(OutBuffer *buf);
|
|
281 void write(Object *obj);
|
|
282 void fill0(unsigned nbytes);
|
|
283 void align(unsigned size);
|
|
284 void vprintf(const char *format, va_list args);
|
|
285 void printf(const char *format, ...);
|
|
286 #if M_UNICODE
|
|
287 void vprintf(const unsigned short *format, va_list args);
|
|
288 void printf(const unsigned short *format, ...);
|
|
289 #endif
|
|
290 void bracket(char left, char right);
|
|
291 unsigned bracket(unsigned i, char *left, unsigned j, char *right);
|
|
292 void spread(unsigned offset, unsigned nbytes);
|
|
293 unsigned insert(unsigned offset, const void *data, unsigned nbytes);
|
|
294 void remove(unsigned offset, unsigned nbytes);
|
|
295 char *toChars();
|
|
296 char *extractString();
|
|
297 };
|
|
298
|
|
299 struct Array : Object
|
|
300 {
|
|
301 unsigned dim;
|
|
302 unsigned allocdim;
|
|
303 void **data;
|
|
304
|
|
305 Array();
|
|
306 ~Array();
|
|
307 void mark();
|
|
308 char *toChars();
|
|
309
|
|
310 void reserve(unsigned nentries);
|
|
311 void setDim(unsigned newdim);
|
|
312 void fixDim();
|
|
313 void push(void *ptr);
|
|
314 void *pop();
|
|
315 void shift(void *ptr);
|
|
316 void insert(unsigned index, void *ptr);
|
|
317 void insert(unsigned index, Array *a);
|
|
318 void append(Array *a);
|
|
319 void remove(unsigned i);
|
|
320 void zero();
|
|
321 void *tos();
|
|
322 void sort();
|
|
323 Array *copy();
|
|
324 };
|
|
325
|
|
326 struct Bits : Object
|
|
327 {
|
|
328 unsigned bitdim;
|
|
329 unsigned allocdim;
|
|
330 unsigned *data;
|
|
331
|
|
332 Bits();
|
|
333 ~Bits();
|
|
334 void mark();
|
|
335
|
|
336 void resize(unsigned bitdim);
|
|
337
|
|
338 void set(unsigned bitnum);
|
|
339 void clear(unsigned bitnum);
|
|
340 int test(unsigned bitnum);
|
|
341
|
|
342 void set();
|
|
343 void clear();
|
|
344 void copy(Bits *from);
|
|
345 Bits *clone();
|
|
346
|
|
347 void sub(Bits *b);
|
|
348 };
|
|
349
|
|
350 #endif
|