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