Mercurial > projects > ldc
annotate dmd/root.c @ 430:120cb5ea9eea
Missed a line in the dmdfe path code.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Tue, 29 Jul 2008 17:45:28 +0200 |
parents | 6bd99cc5eb08 |
children | 3dd9ae9ea708 |
rev | line source |
---|---|
336 | 1 |
2 // Copyright (c) 1999-2006 by Digital Mars | |
3 // All Rights Reserved | |
4 // written by Walter Bright | |
5 // www.digitalmars.com | |
6 // License for redistribution is by either the Artistic License | |
7 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
8 // See the included readme.txt for details. | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <stdarg.h> | |
13 #include <string.h> | |
14 #include <stdint.h> | |
15 #include <assert.h> | |
16 | |
17 #if _MSC_VER ||__MINGW32__ | |
18 #include <malloc.h> | |
19 #endif | |
20 | |
21 #if _WIN32 | |
22 #include <windows.h> | |
23 #include <direct.h> | |
24 #endif | |
25 | |
26 #if linux | |
27 #include <sys/types.h> | |
28 #include <sys/stat.h> | |
29 #include <fcntl.h> | |
30 #include <errno.h> | |
31 #include <unistd.h> | |
32 #include <utime.h> | |
33 #endif | |
34 | |
35 #include "port.h" | |
36 #include "root.h" | |
37 #include "dchar.h" | |
38 #include "mem.h" | |
39 #include "mars.h" | |
40 | |
41 #if 0 //__SC__ //def DEBUG | |
42 extern "C" void __cdecl _assert(void *e, void *f, unsigned line) | |
43 { | |
44 printf("Assert('%s','%s',%d)\n",e,f,line); | |
45 fflush(stdout); | |
46 *(char *)0 = 0; | |
47 } | |
48 #endif | |
49 | |
50 /************************************* | |
51 * Convert wchar string to ascii string. | |
52 */ | |
53 | |
54 char *wchar2ascii(wchar_t *us) | |
55 { | |
56 return wchar2ascii(us, wcslen(us)); | |
57 } | |
58 | |
59 char *wchar2ascii(wchar_t *us, unsigned len) | |
60 { | |
61 unsigned i; | |
62 char *p; | |
63 | |
64 p = (char *)mem.malloc(len + 1); | |
65 for (i = 0; i <= len; i++) | |
66 p[i] = (char) us[i]; | |
67 return p; | |
68 } | |
69 | |
70 int wcharIsAscii(wchar_t *us) | |
71 { | |
72 return wcharIsAscii(us, wcslen(us)); | |
73 } | |
74 | |
75 int wcharIsAscii(wchar_t *us, unsigned len) | |
76 { | |
77 unsigned i; | |
78 | |
79 for (i = 0; i <= len; i++) | |
80 { | |
81 if (us[i] & ~0xFF) // if high bits set | |
82 return 0; // it's not ascii | |
83 } | |
84 return 1; | |
85 } | |
86 | |
87 | |
88 /*********************************** | |
89 * Compare length-prefixed strings (bstr). | |
90 */ | |
91 | |
92 int bstrcmp(unsigned char *b1, unsigned char *b2) | |
93 { | |
94 return (*b1 == *b2 && memcmp(b1 + 1, b2 + 1, *b2) == 0) ? 0 : 1; | |
95 } | |
96 | |
97 /*************************************** | |
98 * Convert bstr into a malloc'd string. | |
99 */ | |
100 | |
101 char *bstr2str(unsigned char *b) | |
102 { | |
103 char *s; | |
104 unsigned len; | |
105 | |
106 len = *b; | |
107 s = (char *) mem.malloc(len + 1); | |
108 s[len] = 0; | |
109 return (char *)memcpy(s,b + 1,len); | |
110 } | |
111 | |
112 /************************************** | |
113 * Print error message and exit. | |
114 */ | |
115 | |
116 void error(const char *format, ...) | |
117 { | |
118 va_list ap; | |
119 | |
120 va_start(ap, format); | |
121 printf("Error: "); | |
122 vprintf(format, ap); | |
123 va_end( ap ); | |
124 printf("\n"); | |
125 fflush(stdout); | |
126 | |
127 exit(EXIT_FAILURE); | |
128 } | |
129 | |
130 #if M_UNICODE | |
131 void error(const dchar *format, ...) | |
132 { | |
133 va_list ap; | |
134 | |
135 va_start(ap, format); | |
136 printf("Error: "); | |
137 vwprintf(format, ap); | |
138 va_end( ap ); | |
139 printf("\n"); | |
140 fflush(stdout); | |
141 | |
142 exit(EXIT_FAILURE); | |
143 } | |
144 #endif | |
145 | |
146 void error_mem() | |
147 { | |
148 error("out of memory"); | |
149 } | |
150 | |
151 /************************************** | |
152 * Print warning message. | |
153 */ | |
154 | |
155 void warning(const char *format, ...) | |
156 { | |
157 va_list ap; | |
158 | |
159 va_start(ap, format); | |
160 printf("Warning: "); | |
161 vprintf(format, ap); | |
162 va_end( ap ); | |
163 printf("\n"); | |
164 fflush(stdout); | |
165 } | |
166 | |
167 /****************************** Object ********************************/ | |
168 | |
169 int Object::equals(Object *o) | |
170 { | |
171 return o == this; | |
172 } | |
173 | |
174 hash_t Object::hashCode() | |
175 { | |
176 return (hash_t) this; | |
177 } | |
178 | |
179 int Object::compare(Object *obj) | |
180 { | |
181 return this - obj; | |
182 } | |
183 | |
184 void Object::print() | |
185 { | |
186 printf("%s %p\n", toChars(), this); | |
187 } | |
188 | |
189 char *Object::toChars() | |
190 { | |
191 return "Object"; | |
192 } | |
193 | |
194 dchar *Object::toDchars() | |
195 { | |
196 #if M_UNICODE | |
197 return L"Object"; | |
198 #else | |
199 return toChars(); | |
200 #endif | |
201 } | |
202 | |
203 int Object::dyncast() | |
204 { | |
205 return 0; | |
206 } | |
207 | |
208 void Object::toBuffer(OutBuffer *b) | |
209 { | |
210 b->writestring("Object"); | |
211 } | |
212 | |
213 void Object::mark() | |
214 { | |
215 } | |
216 | |
217 /****************************** String ********************************/ | |
218 | |
219 String::String(char *str, int ref) | |
220 { | |
221 this->str = ref ? str : mem.strdup(str); | |
222 this->ref = ref; | |
223 } | |
224 | |
225 String::~String() | |
226 { | |
227 mem.free(str); | |
228 } | |
229 | |
230 void String::mark() | |
231 { | |
232 mem.mark(str); | |
233 } | |
234 | |
235 hash_t String::calcHash(const char *str, size_t len) | |
236 { | |
237 hash_t hash = 0; | |
238 | |
239 for (;;) | |
240 { | |
241 switch (len) | |
242 { | |
243 case 0: | |
244 return hash; | |
245 | |
246 case 1: | |
247 hash *= 37; | |
248 hash += *(uint8_t *)str; | |
249 return hash; | |
250 | |
251 case 2: | |
252 hash *= 37; | |
253 hash += *(uint16_t *)str; | |
254 return hash; | |
255 | |
256 case 3: | |
257 hash *= 37; | |
258 hash += (*(uint16_t *)str << 8) + | |
259 ((uint8_t *)str)[2]; | |
260 return hash; | |
261 | |
262 default: | |
263 hash *= 37; | |
264 hash += *(uint32_t *)str; | |
265 str += 4; | |
266 len -= 4; | |
267 break; | |
268 } | |
269 } | |
270 } | |
271 | |
272 hash_t String::calcHash(const char *str) | |
273 { | |
274 return calcHash(str, strlen(str)); | |
275 } | |
276 | |
277 hash_t String::hashCode() | |
278 { | |
279 return calcHash(str, strlen(str)); | |
280 } | |
281 | |
282 unsigned String::len() | |
283 { | |
284 return strlen(str); | |
285 } | |
286 | |
287 int String::equals(Object *obj) | |
288 { | |
289 return strcmp(str,((String *)obj)->str) == 0; | |
290 } | |
291 | |
292 int String::compare(Object *obj) | |
293 { | |
294 return strcmp(str,((String *)obj)->str); | |
295 } | |
296 | |
297 char *String::toChars() | |
298 { | |
299 return str; | |
300 } | |
301 | |
302 void String::print() | |
303 { | |
304 printf("String '%s'\n",str); | |
305 } | |
306 | |
307 | |
308 /****************************** FileName ********************************/ | |
309 | |
310 FileName::FileName(char *str, int ref) | |
311 : String(str,ref) | |
312 { | |
313 } | |
314 | |
315 char *FileName::combine(char *path, char *name) | |
316 { char *f; | |
317 size_t pathlen; | |
318 size_t namelen; | |
319 | |
320 if (!path || !*path) | |
321 return name; | |
322 pathlen = strlen(path); | |
323 namelen = strlen(name); | |
324 f = (char *)mem.malloc(pathlen + 1 + namelen + 1); | |
325 memcpy(f, path, pathlen); | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
326 |
336 | 327 if (path[pathlen - 1] != '/') |
328 { f[pathlen] = '/'; | |
329 pathlen++; | |
330 } | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
331 |
336 | 332 #if _WIN32 |
333 if (path[pathlen - 1] != '\\' && path[pathlen - 1] != ':') | |
334 { f[pathlen] = '\\'; | |
335 pathlen++; | |
336 } | |
337 #endif | |
338 memcpy(f + pathlen, name, namelen + 1); | |
339 return f; | |
340 } | |
341 | |
342 FileName::FileName(char *path, char *name) | |
343 : String(combine(path,name),1) | |
344 { | |
345 } | |
346 | |
347 // Split a path into an Array of paths | |
348 Array *FileName::splitPath(const char *path) | |
349 { | |
350 char c = 0; // unnecessary initializer is for VC /W4 | |
351 const char *p; | |
352 OutBuffer buf; | |
353 Array *array; | |
354 | |
355 array = new Array(); | |
356 if (path) | |
357 { | |
358 p = path; | |
359 do | |
360 { char instring = 0; | |
361 | |
362 while (isspace(*p)) // skip leading whitespace | |
363 p++; | |
364 buf.reserve(strlen(p) + 1); // guess size of path | |
365 for (; ; p++) | |
366 { | |
367 c = *p; | |
368 switch (c) | |
369 { | |
370 case '"': | |
371 instring ^= 1; // toggle inside/outside of string | |
372 continue; | |
373 | |
374 #if MACINTOSH | |
375 case ',': | |
376 #endif | |
377 #if _WIN32 | |
378 case ';': | |
379 #endif | |
380 #if linux | |
381 case ':': | |
382 #endif | |
383 p++; | |
384 break; // note that ; cannot appear as part | |
385 // of a path, quotes won't protect it | |
386 | |
387 case 0x1A: // ^Z means end of file | |
388 case 0: | |
389 break; | |
390 | |
391 case '\r': | |
392 continue; // ignore carriage returns | |
393 | |
394 #if linux | |
395 case '~': | |
396 buf.writestring(getenv("HOME")); | |
397 continue; | |
398 #endif | |
399 | |
400 case ' ': | |
401 case '\t': // tabs in filenames? | |
402 if (!instring) // if not in string | |
403 break; // treat as end of path | |
404 default: | |
405 buf.writeByte(c); | |
406 continue; | |
407 } | |
408 break; | |
409 } | |
410 if (buf.offset) // if path is not empty | |
411 { | |
412 buf.writeByte(0); // to asciiz | |
413 array->push(buf.extractData()); | |
414 } | |
415 } while (c); | |
416 } | |
417 return array; | |
418 } | |
419 | |
420 hash_t FileName::hashCode() | |
421 { | |
422 #if _WIN32 | |
423 // We need a different hashCode because it must be case-insensitive | |
424 size_t len = strlen(str); | |
425 hash_t hash = 0; | |
426 unsigned char *s = (unsigned char *)str; | |
427 | |
428 for (;;) | |
429 { | |
430 switch (len) | |
431 { | |
432 case 0: | |
433 return hash; | |
434 | |
435 case 1: | |
436 hash *= 37; | |
437 hash += *(uint8_t *)s | 0x20; | |
438 return hash; | |
439 | |
440 case 2: | |
441 hash *= 37; | |
442 hash += *(uint16_t *)s | 0x2020; | |
443 return hash; | |
444 | |
445 case 3: | |
446 hash *= 37; | |
447 hash += ((*(uint16_t *)s << 8) + | |
448 ((uint8_t *)s)[2]) | 0x202020; | |
449 break; | |
450 | |
451 default: | |
452 hash *= 37; | |
453 hash += *(uint32_t *)s | 0x20202020; | |
454 s += 4; | |
455 len -= 4; | |
456 break; | |
457 } | |
458 } | |
459 #else | |
460 // darwin HFS is case insensitive, though... | |
461 return String::hashCode(); | |
462 #endif | |
463 } | |
464 | |
465 int FileName::compare(Object *obj) | |
466 { | |
467 #if _WIN32 | |
468 return stricmp(str,((FileName *)obj)->str); | |
469 #else | |
470 return String::compare(obj); | |
471 #endif | |
472 } | |
473 | |
474 int FileName::equals(Object *obj) | |
475 { | |
476 #if _WIN32 | |
477 return stricmp(str,((FileName *)obj)->str) == 0; | |
478 #else | |
479 return String::equals(obj); | |
480 #endif | |
481 } | |
482 | |
483 /************************************ | |
484 * Return !=0 if absolute path name. | |
485 */ | |
486 | |
487 int FileName::absolute(const char *name) | |
488 { | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
489 return |
336 | 490 #if _WIN32 |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
491 (*name == '\\') || |
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
492 (*name == '/') || |
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
493 (*name && name[1] == ':') || |
336 | 494 #endif |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
495 (*name == '/'); |
336 | 496 } |
497 | |
498 /******************************** | |
499 * Return filename extension (read-only). | |
500 * Points past '.' of extension. | |
501 * If there isn't one, return NULL. | |
502 */ | |
503 | |
504 char *FileName::ext(const char *str) | |
505 { | |
506 char *e; | |
507 size_t len = strlen(str); | |
508 | |
509 e = (char *)str + len; | |
510 for (;;) | |
511 { | |
512 switch (*e) | |
513 { case '.': | |
514 return e + 1; | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
515 |
336 | 516 case '/': |
517 break; | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
518 |
336 | 519 #if _WIN32 |
520 case '\\': | |
521 case ':': | |
522 break; | |
523 #endif | |
524 default: | |
525 if (e == str) | |
526 break; | |
527 e--; | |
528 continue; | |
529 } | |
530 return NULL; | |
531 } | |
532 } | |
533 | |
534 char *FileName::ext() | |
535 { | |
536 return ext(str); | |
537 } | |
538 | |
539 /******************************** | |
540 * Return mem.malloc'd filename with extension removed. | |
541 */ | |
542 | |
543 char *FileName::removeExt(const char *str) | |
544 { | |
545 const char *e = ext(str); | |
546 if (e) | |
547 { size_t len = (e - str) - 1; | |
548 char *n = (char *)mem.malloc(len + 1); | |
549 memcpy(n, str, len); | |
550 n[len] = 0; | |
551 return n; | |
552 } | |
553 return mem.strdup(str); | |
554 } | |
555 | |
556 /******************************** | |
557 * Return filename name excluding path (read-only). | |
558 */ | |
559 | |
560 char *FileName::name(const char *str) | |
561 { | |
562 char *e; | |
563 size_t len = strlen(str); | |
564 | |
565 e = (char *)str + len; | |
566 for (;;) | |
567 { | |
568 switch (*e) | |
569 { | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
570 |
336 | 571 case '/': |
572 return e + 1; | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
573 |
336 | 574 #if _WIN32 |
575 case '\\': | |
576 case ':': | |
577 return e + 1; | |
578 #endif | |
579 default: | |
580 if (e == str) | |
581 break; | |
582 e--; | |
583 continue; | |
584 } | |
585 return e; | |
586 } | |
587 } | |
588 | |
589 char *FileName::name() | |
590 { | |
591 return name(str); | |
592 } | |
593 | |
594 /************************************** | |
595 * Return path portion of str. | |
596 * Path will does not include trailing path separator. | |
597 */ | |
598 | |
599 char *FileName::path(const char *str) | |
600 { | |
601 char *n = name(str); | |
602 char *path; | |
603 size_t pathlen; | |
604 | |
605 if (n > str) | |
606 { | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
607 |
336 | 608 if (n[-1] == '/') |
609 n--; | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
610 |
336 | 611 #if _WIN32 |
612 if (n[-1] == '\\') | |
613 n--; | |
614 #endif | |
615 } | |
616 pathlen = n - str; | |
617 path = (char *)mem.malloc(pathlen + 1); | |
618 memcpy(path, str, pathlen); | |
619 path[pathlen] = 0; | |
620 return path; | |
621 } | |
622 | |
623 /************************************** | |
624 * Replace filename portion of path. | |
625 */ | |
626 | |
627 char *FileName::replaceName(char *path, char *name) | |
628 { char *f; | |
629 char *n; | |
630 size_t pathlen; | |
631 size_t namelen; | |
632 | |
633 if (absolute(name)) | |
634 return name; | |
635 | |
636 n = FileName::name(path); | |
637 if (n == path) | |
638 return name; | |
639 pathlen = n - path; | |
640 namelen = strlen(name); | |
641 f = (char *)mem.malloc(pathlen + 1 + namelen + 1); | |
642 memcpy(f, path, pathlen); | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
643 |
336 | 644 if (path[pathlen - 1] != '/') |
645 { f[pathlen] = '/'; | |
646 pathlen++; | |
647 } | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
648 |
336 | 649 #if _WIN32 |
650 if (path[pathlen - 1] != '\\' && path[pathlen - 1] != ':') | |
651 { f[pathlen] = '\\'; | |
652 pathlen++; | |
653 } | |
654 #endif | |
655 memcpy(f + pathlen, name, namelen + 1); | |
656 return f; | |
657 } | |
658 | |
659 /*************************** | |
660 */ | |
661 | |
662 FileName *FileName::defaultExt(const char *name, const char *ext) | |
663 { | |
664 char *e; | |
665 char *s; | |
666 size_t len; | |
667 size_t extlen; | |
668 | |
669 e = FileName::ext(name); | |
670 if (e) // if already has an extension | |
671 return new FileName((char *)name, 0); | |
672 | |
673 len = strlen(name); | |
674 extlen = strlen(ext); | |
675 s = (char *)alloca(len + 1 + extlen + 1); | |
676 memcpy(s,name,len); | |
677 s[len] = '.'; | |
678 memcpy(s + len + 1, ext, extlen + 1); | |
679 return new FileName(s, 0); | |
680 } | |
681 | |
682 /*************************** | |
683 */ | |
684 | |
685 FileName *FileName::forceExt(const char *name, const char *ext) | |
686 { | |
687 char *e; | |
688 char *s; | |
689 size_t len; | |
690 size_t extlen; | |
691 | |
692 e = FileName::ext(name); | |
693 if (e) // if already has an extension | |
694 { | |
695 len = e - name; | |
696 extlen = strlen(ext); | |
697 | |
698 s = (char *)alloca(len + extlen + 1); | |
699 memcpy(s,name,len); | |
700 memcpy(s + len, ext, extlen + 1); | |
701 return new FileName(s, 0); | |
702 } | |
703 else | |
704 return defaultExt(name, ext); // doesn't have one | |
705 } | |
706 | |
707 /****************************** | |
708 * Return !=0 if extensions match. | |
709 */ | |
710 | |
711 int FileName::equalsExt(const char *ext) | |
712 { const char *e; | |
713 | |
714 e = FileName::ext(); | |
715 if (!e && !ext) | |
716 return 1; | |
717 if (!e || !ext) | |
718 return 0; | |
719 #if linux | |
720 return strcmp(e,ext) == 0; | |
721 #endif | |
722 #if _WIN32 | |
723 return stricmp(e,ext) == 0; | |
724 #endif | |
725 } | |
726 | |
727 /************************************* | |
728 * Copy file from this to to. | |
729 */ | |
730 | |
731 void FileName::CopyTo(FileName *to) | |
732 { | |
733 File file(this); | |
734 | |
735 #if _WIN32 | |
736 file.touchtime = mem.malloc(sizeof(WIN32_FIND_DATAA)); // keep same file time | |
737 #endif | |
738 #if linux | |
739 file.touchtime = mem.malloc(sizeof(struct stat)); // keep same file time | |
740 #endif | |
741 file.readv(); | |
742 file.name = to; | |
743 file.writev(); | |
744 } | |
745 | |
746 /************************************* | |
747 * Search Path for file. | |
748 * Input: | |
749 * cwd if !=0, search current directory before searching path | |
750 */ | |
751 | |
752 char *FileName::searchPath(Array *path, char *name, int cwd) | |
753 { | |
754 if (absolute(name)) | |
755 { | |
756 return exists(name) ? name : NULL; | |
757 } | |
758 if (cwd) | |
759 { | |
760 if (exists(name)) | |
761 return name; | |
762 } | |
763 if (path) | |
764 { unsigned i; | |
765 | |
766 for (i = 0; i < path->dim; i++) | |
767 { | |
768 char *p = (char *)path->data[i]; | |
769 char *n = combine(p, name); | |
770 | |
771 if (exists(n)) | |
772 return n; | |
773 } | |
774 } | |
775 return NULL; | |
776 } | |
777 | |
778 int FileName::exists(const char *name) | |
779 { | |
780 #if linux | |
781 struct stat st; | |
782 | |
783 if (stat(name, &st) < 0) | |
784 return 0; | |
785 if (S_ISDIR(st.st_mode)) | |
786 return 2; | |
787 return 1; | |
788 #endif | |
789 #if _WIN32 | |
790 DWORD dw; | |
791 int result; | |
792 | |
793 dw = GetFileAttributesA(name); | |
794 if (dw == -1L) | |
795 result = 0; | |
796 else if (dw & FILE_ATTRIBUTE_DIRECTORY) | |
797 result = 2; | |
798 else | |
799 result = 1; | |
800 return result; | |
801 #endif | |
802 } | |
803 | |
804 void FileName::ensurePathExists(const char *path) | |
805 { | |
806 //printf("FileName::ensurePathExists(%s)\n", path ? path : ""); | |
807 if (path && *path) | |
808 { | |
809 if (!exists(path)) | |
810 { | |
811 char *p = FileName::path(path); | |
812 if (*p) | |
813 { | |
814 #if _WIN32 | |
815 size_t len = strlen(p); | |
816 if (len > 2 && p[-1] == ':') | |
817 { mem.free(p); | |
818 return; | |
819 } | |
820 #endif | |
821 ensurePathExists(p); | |
822 mem.free(p); | |
823 } | |
824 #if _WIN32 | |
825 if (path[strlen(path) - 1] != '\\') | |
826 #endif | |
827 #if linux | |
828 if (path[strlen(path) - 1] != '\\') | |
829 #endif | |
830 { | |
831 //printf("mkdir(%s)\n", path); | |
832 #if _WIN32 | |
833 if (mkdir(path)) | |
834 #endif | |
835 #if linux | |
836 if (mkdir(path, 0777)) | |
837 #endif | |
838 error("cannot create directory %s", path); | |
839 } | |
840 } | |
841 } | |
842 } | |
843 | |
844 /****************************** File ********************************/ | |
845 | |
846 File::File(FileName *n) | |
847 { | |
848 ref = 0; | |
849 buffer = NULL; | |
850 len = 0; | |
851 touchtime = NULL; | |
852 name = n; | |
853 } | |
854 | |
855 File::File(char *n) | |
856 { | |
857 ref = 0; | |
858 buffer = NULL; | |
859 len = 0; | |
860 touchtime = NULL; | |
861 name = new FileName(n, 0); | |
862 } | |
863 | |
864 File::~File() | |
865 { | |
866 if (buffer) | |
867 { | |
868 if (ref == 0) | |
869 mem.free(buffer); | |
870 #if _WIN32 | |
871 else if (ref == 2) | |
872 UnmapViewOfFile(buffer); | |
873 #endif | |
874 } | |
875 if (touchtime) | |
876 mem.free(touchtime); | |
877 } | |
878 | |
879 void File::mark() | |
880 { | |
881 mem.mark(buffer); | |
882 mem.mark(touchtime); | |
883 mem.mark(name); | |
884 } | |
885 | |
886 /************************************* | |
887 */ | |
888 | |
889 int File::read() | |
890 { | |
891 #if linux | |
892 off_t size; | |
893 ssize_t numread; | |
894 int fd; | |
895 struct stat buf; | |
896 int result = 0; | |
897 char *name; | |
898 | |
899 name = this->name->toChars(); | |
900 //printf("File::read('%s')\n",name); | |
901 fd = open(name, O_RDONLY); | |
902 if (fd == -1) | |
903 { result = errno; | |
904 //printf("\topen error, errno = %d\n",errno); | |
905 goto err1; | |
906 } | |
907 | |
908 if (!ref) | |
909 mem.free(buffer); | |
910 ref = 0; // we own the buffer now | |
911 | |
912 //printf("\tfile opened\n"); | |
913 if (fstat(fd, &buf)) | |
914 { | |
915 printf("\tfstat error, errno = %d\n",errno); | |
916 goto err2; | |
917 } | |
918 size = buf.st_size; | |
919 buffer = (unsigned char *) mem.malloc(size + 2); | |
920 if (!buffer) | |
921 { | |
922 printf("\tmalloc error, errno = %d\n",errno); | |
923 goto err2; | |
924 } | |
925 | |
926 numread = ::read(fd, buffer, size); | |
927 if (numread != size) | |
928 { | |
929 printf("\tread error, errno = %d\n",errno); | |
930 goto err2; | |
931 } | |
932 | |
933 if (touchtime) | |
934 memcpy(touchtime, &buf, sizeof(buf)); | |
935 | |
936 if (close(fd) == -1) | |
937 { | |
938 printf("\tclose error, errno = %d\n",errno); | |
939 goto err; | |
940 } | |
941 | |
942 len = size; | |
943 | |
944 // Always store a wchar ^Z past end of buffer so scanner has a sentinel | |
945 buffer[size] = 0; // ^Z is obsolete, use 0 | |
946 buffer[size + 1] = 0; | |
947 return 0; | |
948 | |
949 err2: | |
950 close(fd); | |
951 err: | |
952 mem.free(buffer); | |
953 buffer = NULL; | |
954 len = 0; | |
955 | |
956 err1: | |
957 result = 1; | |
958 return result; | |
959 #endif | |
960 #if _WIN32 | |
961 DWORD size; | |
962 DWORD numread; | |
963 HANDLE h; | |
964 int result = 0; | |
965 char *name; | |
966 | |
967 name = this->name->toChars(); | |
968 h = CreateFileA(name,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING, | |
969 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,0); | |
970 if (h == INVALID_HANDLE_VALUE) | |
971 goto err1; | |
972 | |
973 if (!ref) | |
974 mem.free(buffer); | |
975 ref = 0; | |
976 | |
977 size = GetFileSize(h,NULL); | |
978 buffer = (unsigned char *) mem.malloc(size + 2); | |
979 if (!buffer) | |
980 goto err2; | |
981 | |
982 if (ReadFile(h,buffer,size,&numread,NULL) != TRUE) | |
983 goto err2; | |
984 | |
985 if (numread != size) | |
986 goto err2; | |
987 | |
988 if (touchtime) | |
989 { | |
990 if (!GetFileTime(h, NULL, NULL, &((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime)) | |
991 goto err2; | |
992 } | |
993 | |
994 if (!CloseHandle(h)) | |
995 goto err; | |
996 | |
997 len = size; | |
998 | |
999 // Always store a wchar ^Z past end of buffer so scanner has a sentinel | |
1000 buffer[size] = 0; // ^Z is obsolete, use 0 | |
1001 buffer[size + 1] = 0; | |
1002 return 0; | |
1003 | |
1004 err2: | |
1005 CloseHandle(h); | |
1006 err: | |
1007 mem.free(buffer); | |
1008 buffer = NULL; | |
1009 len = 0; | |
1010 | |
1011 err1: | |
1012 result = 1; | |
1013 return result; | |
1014 #endif | |
1015 } | |
1016 | |
1017 /***************************** | |
1018 * Read a file with memory mapped file I/O. | |
1019 */ | |
1020 | |
1021 int File::mmread() | |
1022 { | |
1023 #if linux | |
1024 return read(); | |
1025 #endif | |
1026 #if _WIN32 | |
1027 HANDLE hFile; | |
1028 HANDLE hFileMap; | |
1029 DWORD size; | |
1030 char *name; | |
1031 | |
1032 name = this->name->toChars(); | |
1033 hFile = CreateFile(name, GENERIC_READ, | |
1034 FILE_SHARE_READ, NULL, | |
1035 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
1036 if (hFile == INVALID_HANDLE_VALUE) | |
1037 goto Lerr; | |
1038 size = GetFileSize(hFile, NULL); | |
1039 //printf(" file created, size %d\n", size); | |
1040 | |
1041 hFileMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,size,NULL); | |
1042 if (CloseHandle(hFile) != TRUE) | |
1043 goto Lerr; | |
1044 | |
1045 if (hFileMap == NULL) | |
1046 goto Lerr; | |
1047 | |
1048 //printf(" mapping created\n"); | |
1049 | |
1050 if (!ref) | |
1051 mem.free(buffer); | |
1052 ref = 2; | |
1053 buffer = (unsigned char *)MapViewOfFileEx(hFileMap, FILE_MAP_READ,0,0,size,NULL); | |
1054 if (CloseHandle(hFileMap) != TRUE) | |
1055 goto Lerr; | |
1056 if (buffer == NULL) // mapping view failed | |
1057 goto Lerr; | |
1058 | |
1059 len = size; | |
1060 //printf(" buffer = %p\n", buffer); | |
1061 | |
1062 return 0; | |
1063 | |
1064 Lerr: | |
1065 return GetLastError(); // failure | |
1066 #endif | |
1067 } | |
1068 | |
1069 /********************************************* | |
1070 * Write a file. | |
1071 * Returns: | |
1072 * 0 success | |
1073 */ | |
1074 | |
1075 int File::write() | |
1076 { | |
1077 #if linux | |
1078 int fd; | |
1079 ssize_t numwritten; | |
1080 char *name; | |
1081 | |
1082 name = this->name->toChars(); | |
1083 fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644); | |
1084 if (fd == -1) | |
1085 goto err; | |
1086 | |
1087 numwritten = ::write(fd, buffer, len); | |
1088 if (len != numwritten) | |
1089 goto err2; | |
1090 | |
1091 if (close(fd) == -1) | |
1092 goto err; | |
1093 | |
1094 if (touchtime) | |
1095 { struct utimbuf ubuf; | |
1096 | |
1097 ubuf.actime = ((struct stat *)touchtime)->st_atime; | |
1098 ubuf.modtime = ((struct stat *)touchtime)->st_mtime; | |
1099 if (utime(name, &ubuf)) | |
1100 goto err; | |
1101 } | |
1102 return 0; | |
1103 | |
1104 err2: | |
1105 close(fd); | |
1106 ::remove(name); | |
1107 err: | |
1108 return 1; | |
1109 #endif | |
1110 #if _WIN32 | |
1111 HANDLE h; | |
1112 DWORD numwritten; | |
1113 char *name; | |
1114 | |
1115 name = this->name->toChars(); | |
1116 h = CreateFileA(name,GENERIC_WRITE,0,NULL,CREATE_ALWAYS, | |
1117 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,NULL); | |
1118 if (h == INVALID_HANDLE_VALUE) | |
1119 goto err; | |
1120 | |
1121 if (WriteFile(h,buffer,len,&numwritten,NULL) != TRUE) | |
1122 goto err2; | |
1123 | |
1124 if (len != numwritten) | |
1125 goto err2; | |
1126 | |
1127 if (touchtime) { | |
1128 SetFileTime(h, NULL, NULL, &((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime); | |
1129 } | |
1130 if (!CloseHandle(h)) | |
1131 goto err; | |
1132 return 0; | |
1133 | |
1134 err2: | |
1135 CloseHandle(h); | |
1136 DeleteFileA(name); | |
1137 err: | |
1138 return 1; | |
1139 #endif | |
1140 } | |
1141 | |
1142 /********************************************* | |
1143 * Append to a file. | |
1144 * Returns: | |
1145 * 0 success | |
1146 */ | |
1147 | |
1148 int File::append() | |
1149 { | |
1150 #if linux | |
1151 return 1; | |
1152 #endif | |
1153 #if _WIN32 | |
1154 HANDLE h; | |
1155 DWORD numwritten; | |
1156 char *name; | |
1157 | |
1158 name = this->name->toChars(); | |
1159 h = CreateFileA(name,GENERIC_WRITE,0,NULL,OPEN_ALWAYS, | |
1160 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,NULL); | |
1161 if (h == INVALID_HANDLE_VALUE) | |
1162 goto err; | |
1163 | |
1164 #if 1 | |
1165 SetFilePointer(h, 0, NULL, FILE_END); | |
1166 #else // INVALID_SET_FILE_POINTER doesn't seem to have a definition | |
1167 if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) | |
1168 goto err; | |
1169 #endif | |
1170 | |
1171 if (WriteFile(h,buffer,len,&numwritten,NULL) != TRUE) | |
1172 goto err2; | |
1173 | |
1174 if (len != numwritten) | |
1175 goto err2; | |
1176 | |
1177 if (touchtime) { | |
1178 SetFileTime(h, NULL, NULL, &((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime); | |
1179 } | |
1180 if (!CloseHandle(h)) | |
1181 goto err; | |
1182 return 0; | |
1183 | |
1184 err2: | |
1185 CloseHandle(h); | |
1186 err: | |
1187 return 1; | |
1188 #endif | |
1189 } | |
1190 | |
1191 /************************************** | |
1192 */ | |
1193 | |
1194 void File::readv() | |
1195 { | |
1196 if (read()) | |
1197 error("Error reading file '%s'\n",name->toChars()); | |
1198 } | |
1199 | |
1200 /************************************** | |
1201 */ | |
1202 | |
1203 void File::mmreadv() | |
1204 { | |
1205 if (mmread()) | |
1206 readv(); | |
1207 } | |
1208 | |
1209 void File::writev() | |
1210 { | |
1211 if (write()) | |
1212 error("Error writing file '%s'\n",name->toChars()); | |
1213 } | |
1214 | |
1215 void File::appendv() | |
1216 { | |
1217 if (write()) | |
1218 error("Error appending to file '%s'\n",name->toChars()); | |
1219 } | |
1220 | |
1221 /******************************************* | |
1222 * Return !=0 if file exists. | |
1223 * 0: file doesn't exist | |
1224 * 1: normal file | |
1225 * 2: directory | |
1226 */ | |
1227 | |
1228 int File::exists() | |
1229 { | |
1230 #if linux | |
1231 return 0; | |
1232 #endif | |
1233 #if _WIN32 | |
1234 DWORD dw; | |
1235 int result; | |
1236 char *name; | |
1237 | |
1238 name = this->name->toChars(); | |
1239 if (touchtime) | |
1240 dw = ((WIN32_FIND_DATAA *)touchtime)->dwFileAttributes; | |
1241 else | |
1242 dw = GetFileAttributesA(name); | |
1243 if (dw == -1L) | |
1244 result = 0; | |
1245 else if (dw & FILE_ATTRIBUTE_DIRECTORY) | |
1246 result = 2; | |
1247 else | |
1248 result = 1; | |
1249 return result; | |
1250 #endif | |
1251 } | |
1252 | |
1253 void File::remove() | |
1254 { | |
1255 #if linux | |
1256 ::remove(this->name->toChars()); | |
1257 #endif | |
1258 #if _WIN32 | |
1259 DeleteFileA(this->name->toChars()); | |
1260 #endif | |
1261 } | |
1262 | |
1263 Array *File::match(char *n) | |
1264 { | |
1265 return match(new FileName(n, 0)); | |
1266 } | |
1267 | |
1268 Array *File::match(FileName *n) | |
1269 { | |
1270 #if linux | |
1271 return NULL; | |
1272 #endif | |
1273 #if _WIN32 | |
1274 HANDLE h; | |
1275 WIN32_FIND_DATAA fileinfo; | |
1276 Array *a; | |
1277 char *c; | |
1278 char *name; | |
1279 | |
1280 a = new Array(); | |
1281 c = n->toChars(); | |
1282 name = n->name(); | |
1283 h = FindFirstFileA(c,&fileinfo); | |
1284 if (h != INVALID_HANDLE_VALUE) | |
1285 { | |
1286 do | |
1287 { | |
1288 // Glue path together with name | |
1289 char *fn; | |
1290 File *f; | |
1291 | |
1292 fn = (char *)mem.malloc(name - c + strlen(fileinfo.cFileName) + 1); | |
1293 memcpy(fn, c, name - c); | |
1294 strcpy(fn + (name - c), fileinfo.cFileName); | |
1295 f = new File(fn); | |
1296 f->touchtime = mem.malloc(sizeof(WIN32_FIND_DATAA)); | |
1297 memcpy(f->touchtime, &fileinfo, sizeof(fileinfo)); | |
1298 a->push(f); | |
1299 } while (FindNextFileA(h,&fileinfo) != FALSE); | |
1300 FindClose(h); | |
1301 } | |
1302 return a; | |
1303 #endif | |
1304 } | |
1305 | |
1306 int File::compareTime(File *f) | |
1307 { | |
1308 #if linux | |
1309 return 0; | |
1310 #endif | |
1311 #if _WIN32 | |
1312 if (!touchtime) | |
1313 stat(); | |
1314 if (!f->touchtime) | |
1315 f->stat(); | |
1316 return CompareFileTime(&((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime, &((WIN32_FIND_DATAA *)f->touchtime)->ftLastWriteTime); | |
1317 #endif | |
1318 } | |
1319 | |
1320 void File::stat() | |
1321 { | |
1322 #if linux | |
1323 if (!touchtime) | |
1324 { | |
1325 touchtime = mem.calloc(1, sizeof(struct stat)); | |
1326 } | |
1327 #endif | |
1328 #if _WIN32 | |
1329 HANDLE h; | |
1330 | |
1331 if (!touchtime) | |
1332 { | |
1333 touchtime = mem.calloc(1, sizeof(WIN32_FIND_DATAA)); | |
1334 } | |
1335 h = FindFirstFileA(name->toChars(),(WIN32_FIND_DATAA *)touchtime); | |
1336 if (h != INVALID_HANDLE_VALUE) | |
1337 { | |
1338 FindClose(h); | |
1339 } | |
1340 #endif | |
1341 } | |
1342 | |
1343 void File::checkoffset(size_t offset, size_t nbytes) | |
1344 { | |
1345 if (offset > len || offset + nbytes > len) | |
1346 error("Corrupt file '%s': offset x%"PRIxSIZE" off end of file",toChars(),offset); | |
1347 } | |
1348 | |
1349 char *File::toChars() | |
1350 { | |
1351 return name->toChars(); | |
1352 } | |
1353 | |
1354 | |
1355 /************************* OutBuffer *************************/ | |
1356 | |
1357 OutBuffer::OutBuffer() | |
1358 { | |
1359 data = NULL; | |
1360 offset = 0; | |
1361 size = 0; | |
1362 } | |
1363 | |
1364 OutBuffer::~OutBuffer() | |
1365 { | |
1366 mem.free(data); | |
1367 } | |
1368 | |
1369 void *OutBuffer::extractData() | |
1370 { | |
1371 void *p; | |
1372 | |
1373 p = (void *)data; | |
1374 data = NULL; | |
1375 offset = 0; | |
1376 size = 0; | |
1377 return p; | |
1378 } | |
1379 | |
1380 void OutBuffer::mark() | |
1381 { | |
1382 mem.mark(data); | |
1383 } | |
1384 | |
1385 void OutBuffer::reserve(unsigned nbytes) | |
1386 { | |
1387 //printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); | |
1388 if (size - offset < nbytes) | |
1389 { | |
1390 size = (offset + nbytes) * 2; | |
1391 data = (unsigned char *)mem.realloc(data, size); | |
1392 } | |
1393 } | |
1394 | |
1395 void OutBuffer::reset() | |
1396 { | |
1397 offset = 0; | |
1398 } | |
1399 | |
1400 void OutBuffer::setsize(unsigned size) | |
1401 { | |
1402 offset = size; | |
1403 } | |
1404 | |
1405 void OutBuffer::write(const void *data, unsigned nbytes) | |
1406 { | |
1407 reserve(nbytes); | |
1408 memcpy(this->data + offset, data, nbytes); | |
1409 offset += nbytes; | |
1410 } | |
1411 | |
1412 void OutBuffer::writebstring(unsigned char *string) | |
1413 { | |
1414 write(string,*string + 1); | |
1415 } | |
1416 | |
1417 void OutBuffer::writestring(const char *string) | |
1418 { | |
1419 write(string,strlen(string)); | |
1420 } | |
1421 | |
1422 void OutBuffer::writedstring(const char *string) | |
1423 { | |
1424 #if M_UNICODE | |
1425 for (; *string; string++) | |
1426 { | |
1427 writedchar(*string); | |
1428 } | |
1429 #else | |
1430 write(string,strlen(string)); | |
1431 #endif | |
1432 } | |
1433 | |
1434 void OutBuffer::writedstring(const wchar_t *string) | |
1435 { | |
1436 #if M_UNICODE | |
1437 write(string,wcslen(string) * sizeof(wchar_t)); | |
1438 #else | |
1439 for (; *string; string++) | |
1440 { | |
1441 writedchar(*string); | |
1442 } | |
1443 #endif | |
1444 } | |
1445 | |
1446 void OutBuffer::prependstring(char *string) | |
1447 { unsigned len; | |
1448 | |
1449 len = strlen(string); | |
1450 reserve(len); | |
1451 memmove(data + len, data, offset); | |
1452 memcpy(data, string, len); | |
1453 offset += len; | |
1454 } | |
1455 | |
1456 void OutBuffer::writenl() | |
1457 { | |
1458 #if _WIN32 | |
1459 #if M_UNICODE | |
1460 write4(0x000A000D); // newline is CR,LF on Microsoft OS's | |
1461 #else | |
1462 writeword(0x0A0D); // newline is CR,LF on Microsoft OS's | |
1463 #endif | |
1464 #else | |
1465 #if M_UNICODE | |
1466 writeword('\n'); | |
1467 #else | |
1468 writeByte('\n'); | |
1469 #endif | |
1470 #endif | |
1471 } | |
1472 | |
1473 void OutBuffer::writeByte(unsigned b) | |
1474 { | |
1475 reserve(1); | |
1476 this->data[offset] = (unsigned char)b; | |
1477 offset++; | |
1478 } | |
1479 | |
1480 void OutBuffer::writeUTF8(unsigned b) | |
1481 { | |
1482 reserve(6); | |
1483 if (b <= 0x7F) | |
1484 { | |
1485 this->data[offset] = (unsigned char)b; | |
1486 offset++; | |
1487 } | |
1488 else if (b <= 0x7FF) | |
1489 { | |
1490 this->data[offset + 0] = (unsigned char)((b >> 6) | 0xC0); | |
1491 this->data[offset + 1] = (unsigned char)((b & 0x3F) | 0x80); | |
1492 offset += 2; | |
1493 } | |
1494 else if (b <= 0xFFFF) | |
1495 { | |
1496 this->data[offset + 0] = (unsigned char)((b >> 12) | 0xE0); | |
1497 this->data[offset + 1] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1498 this->data[offset + 2] = (unsigned char)((b & 0x3F) | 0x80); | |
1499 offset += 3; | |
1500 } | |
1501 else if (b <= 0x1FFFFF) | |
1502 { | |
1503 this->data[offset + 0] = (unsigned char)((b >> 18) | 0xF0); | |
1504 this->data[offset + 1] = (unsigned char)(((b >> 12) & 0x3F) | 0x80); | |
1505 this->data[offset + 2] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1506 this->data[offset + 3] = (unsigned char)((b & 0x3F) | 0x80); | |
1507 offset += 4; | |
1508 } | |
1509 else if (b <= 0x3FFFFFF) | |
1510 { | |
1511 this->data[offset + 0] = (unsigned char)((b >> 24) | 0xF8); | |
1512 this->data[offset + 1] = (unsigned char)(((b >> 18) & 0x3F) | 0x80); | |
1513 this->data[offset + 2] = (unsigned char)(((b >> 12) & 0x3F) | 0x80); | |
1514 this->data[offset + 3] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1515 this->data[offset + 4] = (unsigned char)((b & 0x3F) | 0x80); | |
1516 offset += 5; | |
1517 } | |
1518 else if (b <= 0x7FFFFFFF) | |
1519 { | |
1520 this->data[offset + 0] = (unsigned char)((b >> 30) | 0xFC); | |
1521 this->data[offset + 1] = (unsigned char)(((b >> 24) & 0x3F) | 0x80); | |
1522 this->data[offset + 2] = (unsigned char)(((b >> 18) & 0x3F) | 0x80); | |
1523 this->data[offset + 3] = (unsigned char)(((b >> 12) & 0x3F) | 0x80); | |
1524 this->data[offset + 4] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1525 this->data[offset + 5] = (unsigned char)((b & 0x3F) | 0x80); | |
1526 offset += 6; | |
1527 } | |
1528 else | |
1529 assert(0); | |
1530 } | |
1531 | |
1532 void OutBuffer::writedchar(unsigned b) | |
1533 { | |
1534 reserve(Dchar_mbmax * sizeof(dchar)); | |
1535 offset = (unsigned char *)Dchar::put((dchar *)(this->data + offset), (dchar)b) - | |
1536 this->data; | |
1537 } | |
1538 | |
1539 void OutBuffer::prependbyte(unsigned b) | |
1540 { | |
1541 reserve(1); | |
1542 memmove(data + 1, data, offset); | |
1543 data[0] = (unsigned char)b; | |
1544 offset++; | |
1545 } | |
1546 | |
1547 void OutBuffer::writeword(unsigned w) | |
1548 { | |
1549 reserve(2); | |
1550 *(unsigned short *)(this->data + offset) = (unsigned short)w; | |
1551 offset += 2; | |
1552 } | |
1553 | |
1554 void OutBuffer::writeUTF16(unsigned w) | |
1555 { | |
1556 reserve(4); | |
1557 if (w <= 0xFFFF) | |
1558 { | |
1559 *(unsigned short *)(this->data + offset) = (unsigned short)w; | |
1560 offset += 2; | |
1561 } | |
1562 else if (w <= 0x10FFFF) | |
1563 { | |
1564 *(unsigned short *)(this->data + offset) = (unsigned short)((w >> 10) + 0xD7C0); | |
1565 *(unsigned short *)(this->data + offset + 2) = (unsigned short)((w & 0x3FF) | 0xDC00); | |
1566 offset += 4; | |
1567 } | |
1568 else | |
1569 assert(0); | |
1570 } | |
1571 | |
1572 void OutBuffer::write4(unsigned w) | |
1573 { | |
1574 reserve(4); | |
1575 *(unsigned long *)(this->data + offset) = w; | |
1576 offset += 4; | |
1577 } | |
1578 | |
1579 void OutBuffer::write(OutBuffer *buf) | |
1580 { | |
1581 if (buf) | |
1582 { reserve(buf->offset); | |
1583 memcpy(data + offset, buf->data, buf->offset); | |
1584 offset += buf->offset; | |
1585 } | |
1586 } | |
1587 | |
1588 void OutBuffer::write(Object *obj) | |
1589 { | |
1590 if (obj) | |
1591 { | |
1592 writestring(obj->toChars()); | |
1593 } | |
1594 } | |
1595 | |
1596 void OutBuffer::fill0(unsigned nbytes) | |
1597 { | |
1598 reserve(nbytes); | |
1599 memset(data + offset,0,nbytes); | |
1600 offset += nbytes; | |
1601 } | |
1602 | |
1603 void OutBuffer::align(unsigned size) | |
1604 { unsigned nbytes; | |
1605 | |
1606 nbytes = ((offset + size - 1) & ~(size - 1)) - offset; | |
1607 fill0(nbytes); | |
1608 } | |
1609 | |
1610 void OutBuffer::vprintf(const char *format, va_list args) | |
1611 { | |
1612 char buffer[128]; | |
1613 char *p; | |
1614 unsigned psize; | |
1615 int count; | |
1616 | |
1617 p = buffer; | |
1618 psize = sizeof(buffer); | |
1619 for (;;) | |
1620 { | |
1621 #if _WIN32 | |
1622 count = _vsnprintf(p,psize,format,args); | |
1623 if (count != -1) | |
1624 break; | |
1625 psize *= 2; | |
1626 #endif | |
1627 #if linux | |
1628 count = vsnprintf(p,psize,format,args); | |
1629 if (count == -1) | |
1630 psize *= 2; | |
1631 else if (count >= psize) | |
1632 psize = count + 1; | |
1633 else | |
1634 break; | |
1635 #endif | |
1636 p = (char *) alloca(psize); // buffer too small, try again with larger size | |
1637 } | |
1638 write(p,count); | |
1639 } | |
1640 | |
1641 #if M_UNICODE | |
1642 void OutBuffer::vprintf(const wchar_t *format, va_list args) | |
1643 { | |
1644 dchar buffer[128]; | |
1645 dchar *p; | |
1646 unsigned psize; | |
1647 int count; | |
1648 | |
1649 p = buffer; | |
1650 psize = sizeof(buffer) / sizeof(buffer[0]); | |
1651 for (;;) | |
1652 { | |
1653 #if _WIN32 | |
1654 count = _vsnwprintf(p,psize,format,args); | |
1655 if (count != -1) | |
1656 break; | |
1657 psize *= 2; | |
1658 #endif | |
1659 #if linux | |
1660 count = vsnwprintf(p,psize,format,args); | |
1661 if (count == -1) | |
1662 psize *= 2; | |
1663 else if (count >= psize) | |
1664 psize = count + 1; | |
1665 else | |
1666 break; | |
1667 #endif | |
1668 p = (dchar *) alloca(psize * 2); // buffer too small, try again with larger size | |
1669 } | |
1670 write(p,count * 2); | |
1671 } | |
1672 #endif | |
1673 | |
1674 void OutBuffer::printf(const char *format, ...) | |
1675 { | |
1676 va_list ap; | |
1677 va_start(ap, format); | |
1678 vprintf(format,ap); | |
1679 va_end(ap); | |
1680 } | |
1681 | |
1682 #if M_UNICODE | |
1683 void OutBuffer::printf(const wchar_t *format, ...) | |
1684 { | |
1685 va_list ap; | |
1686 va_start(ap, format); | |
1687 vprintf(format,ap); | |
1688 va_end(ap); | |
1689 } | |
1690 #endif | |
1691 | |
1692 void OutBuffer::bracket(char left, char right) | |
1693 { | |
1694 reserve(2); | |
1695 memmove(data + 1, data, offset); | |
1696 data[0] = left; | |
1697 data[offset + 1] = right; | |
1698 offset += 2; | |
1699 } | |
1700 | |
1701 /****************** | |
1702 * Insert left at i, and right at j. | |
1703 * Return index just past right. | |
1704 */ | |
1705 | |
1706 unsigned OutBuffer::bracket(unsigned i, char *left, unsigned j, char *right) | |
1707 { | |
1708 size_t leftlen = strlen(left); | |
1709 size_t rightlen = strlen(right); | |
1710 reserve(leftlen + rightlen); | |
1711 insert(i, left, leftlen); | |
1712 insert(j + leftlen, right, rightlen); | |
1713 return j + leftlen + rightlen; | |
1714 } | |
1715 | |
1716 void OutBuffer::spread(unsigned offset, unsigned nbytes) | |
1717 { | |
1718 reserve(nbytes); | |
1719 memmove(data + offset + nbytes, data + offset, | |
1720 this->offset - offset); | |
1721 this->offset += nbytes; | |
1722 } | |
1723 | |
1724 /**************************************** | |
1725 * Returns: offset + nbytes | |
1726 */ | |
1727 | |
1728 unsigned OutBuffer::insert(unsigned offset, const void *p, unsigned nbytes) | |
1729 { | |
1730 spread(offset, nbytes); | |
1731 memmove(data + offset, p, nbytes); | |
1732 return offset + nbytes; | |
1733 } | |
1734 | |
1735 void OutBuffer::remove(unsigned offset, unsigned nbytes) | |
1736 { | |
1737 memmove(data + offset, data + offset + nbytes, this->offset - (offset + nbytes)); | |
1738 this->offset -= nbytes; | |
1739 } | |
1740 | |
1741 char *OutBuffer::toChars() | |
1742 { | |
1743 writeByte(0); | |
1744 return (char *)data; | |
1745 } | |
1746 | |
1747 /********************************* Bits ****************************/ | |
1748 | |
1749 Bits::Bits() | |
1750 { | |
1751 data = NULL; | |
1752 bitdim = 0; | |
1753 allocdim = 0; | |
1754 } | |
1755 | |
1756 Bits::~Bits() | |
1757 { | |
1758 mem.free(data); | |
1759 } | |
1760 | |
1761 void Bits::mark() | |
1762 { | |
1763 mem.mark(data); | |
1764 } | |
1765 | |
1766 void Bits::resize(unsigned bitdim) | |
1767 { | |
1768 unsigned allocdim; | |
1769 unsigned mask; | |
1770 | |
1771 allocdim = (bitdim + 31) / 32; | |
1772 data = (unsigned *)mem.realloc(data, allocdim * sizeof(data[0])); | |
1773 if (this->allocdim < allocdim) | |
1774 memset(data + this->allocdim, 0, (allocdim - this->allocdim) * sizeof(data[0])); | |
1775 | |
1776 // Clear other bits in last word | |
1777 mask = (1 << (bitdim & 31)) - 1; | |
1778 if (mask) | |
1779 data[allocdim - 1] &= ~mask; | |
1780 | |
1781 this->bitdim = bitdim; | |
1782 this->allocdim = allocdim; | |
1783 } | |
1784 | |
1785 void Bits::set(unsigned bitnum) | |
1786 { | |
1787 data[bitnum / 32] |= 1 << (bitnum & 31); | |
1788 } | |
1789 | |
1790 void Bits::clear(unsigned bitnum) | |
1791 { | |
1792 data[bitnum / 32] &= ~(1 << (bitnum & 31)); | |
1793 } | |
1794 | |
1795 int Bits::test(unsigned bitnum) | |
1796 { | |
1797 return data[bitnum / 32] & (1 << (bitnum & 31)); | |
1798 } | |
1799 | |
1800 void Bits::set() | |
1801 { unsigned mask; | |
1802 | |
1803 memset(data, ~0, allocdim * sizeof(data[0])); | |
1804 | |
1805 // Clear other bits in last word | |
1806 mask = (1 << (bitdim & 31)) - 1; | |
1807 if (mask) | |
1808 data[allocdim - 1] &= mask; | |
1809 } | |
1810 | |
1811 void Bits::clear() | |
1812 { | |
1813 memset(data, 0, allocdim * sizeof(data[0])); | |
1814 } | |
1815 | |
1816 void Bits::copy(Bits *from) | |
1817 { | |
1818 assert(bitdim == from->bitdim); | |
1819 memcpy(data, from->data, allocdim * sizeof(data[0])); | |
1820 } | |
1821 | |
1822 Bits *Bits::clone() | |
1823 { | |
1824 Bits *b; | |
1825 | |
1826 b = new Bits(); | |
1827 b->resize(bitdim); | |
1828 b->copy(this); | |
1829 return b; | |
1830 } | |
1831 | |
1832 void Bits::sub(Bits *b) | |
1833 { | |
1834 unsigned u; | |
1835 | |
1836 for (u = 0; u < allocdim; u++) | |
1837 data[u] &= ~b->data[u]; | |
1838 } | |
1839 | |
1840 | |
1841 | |
1842 | |
1843 | |
1844 | |
1845 | |
1846 | |
1847 | |
1848 | |
1849 | |
1850 | |
1851 | |
1852 | |
1853 |