Mercurial > projects > ldc
annotate dmd/root.c @ 429:6bd99cc5eb08
Allow / as a path separator on Windows.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Tue, 29 Jul 2008 17:35:33 +0200 |
parents | aaade6ded589 |
children | 120cb5ea9eea |
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 case '/': | |
523 break; | |
524 #endif | |
525 default: | |
526 if (e == str) | |
527 break; | |
528 e--; | |
529 continue; | |
530 } | |
531 return NULL; | |
532 } | |
533 } | |
534 | |
535 char *FileName::ext() | |
536 { | |
537 return ext(str); | |
538 } | |
539 | |
540 /******************************** | |
541 * Return mem.malloc'd filename with extension removed. | |
542 */ | |
543 | |
544 char *FileName::removeExt(const char *str) | |
545 { | |
546 const char *e = ext(str); | |
547 if (e) | |
548 { size_t len = (e - str) - 1; | |
549 char *n = (char *)mem.malloc(len + 1); | |
550 memcpy(n, str, len); | |
551 n[len] = 0; | |
552 return n; | |
553 } | |
554 return mem.strdup(str); | |
555 } | |
556 | |
557 /******************************** | |
558 * Return filename name excluding path (read-only). | |
559 */ | |
560 | |
561 char *FileName::name(const char *str) | |
562 { | |
563 char *e; | |
564 size_t len = strlen(str); | |
565 | |
566 e = (char *)str + len; | |
567 for (;;) | |
568 { | |
569 switch (*e) | |
570 { | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
571 |
336 | 572 case '/': |
573 return e + 1; | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
574 |
336 | 575 #if _WIN32 |
576 case '\\': | |
577 case ':': | |
578 return e + 1; | |
579 #endif | |
580 default: | |
581 if (e == str) | |
582 break; | |
583 e--; | |
584 continue; | |
585 } | |
586 return e; | |
587 } | |
588 } | |
589 | |
590 char *FileName::name() | |
591 { | |
592 return name(str); | |
593 } | |
594 | |
595 /************************************** | |
596 * Return path portion of str. | |
597 * Path will does not include trailing path separator. | |
598 */ | |
599 | |
600 char *FileName::path(const char *str) | |
601 { | |
602 char *n = name(str); | |
603 char *path; | |
604 size_t pathlen; | |
605 | |
606 if (n > str) | |
607 { | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
608 |
336 | 609 if (n[-1] == '/') |
610 n--; | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
611 |
336 | 612 #if _WIN32 |
613 if (n[-1] == '\\') | |
614 n--; | |
615 #endif | |
616 } | |
617 pathlen = n - str; | |
618 path = (char *)mem.malloc(pathlen + 1); | |
619 memcpy(path, str, pathlen); | |
620 path[pathlen] = 0; | |
621 return path; | |
622 } | |
623 | |
624 /************************************** | |
625 * Replace filename portion of path. | |
626 */ | |
627 | |
628 char *FileName::replaceName(char *path, char *name) | |
629 { char *f; | |
630 char *n; | |
631 size_t pathlen; | |
632 size_t namelen; | |
633 | |
634 if (absolute(name)) | |
635 return name; | |
636 | |
637 n = FileName::name(path); | |
638 if (n == path) | |
639 return name; | |
640 pathlen = n - path; | |
641 namelen = strlen(name); | |
642 f = (char *)mem.malloc(pathlen + 1 + namelen + 1); | |
643 memcpy(f, path, pathlen); | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
644 |
336 | 645 if (path[pathlen - 1] != '/') |
646 { f[pathlen] = '/'; | |
647 pathlen++; | |
648 } | |
429
6bd99cc5eb08
Allow / as a path separator on Windows.
Christian Kamm <kamm incasoftware de>
parents:
336
diff
changeset
|
649 |
336 | 650 #if _WIN32 |
651 if (path[pathlen - 1] != '\\' && path[pathlen - 1] != ':') | |
652 { f[pathlen] = '\\'; | |
653 pathlen++; | |
654 } | |
655 #endif | |
656 memcpy(f + pathlen, name, namelen + 1); | |
657 return f; | |
658 } | |
659 | |
660 /*************************** | |
661 */ | |
662 | |
663 FileName *FileName::defaultExt(const char *name, const char *ext) | |
664 { | |
665 char *e; | |
666 char *s; | |
667 size_t len; | |
668 size_t extlen; | |
669 | |
670 e = FileName::ext(name); | |
671 if (e) // if already has an extension | |
672 return new FileName((char *)name, 0); | |
673 | |
674 len = strlen(name); | |
675 extlen = strlen(ext); | |
676 s = (char *)alloca(len + 1 + extlen + 1); | |
677 memcpy(s,name,len); | |
678 s[len] = '.'; | |
679 memcpy(s + len + 1, ext, extlen + 1); | |
680 return new FileName(s, 0); | |
681 } | |
682 | |
683 /*************************** | |
684 */ | |
685 | |
686 FileName *FileName::forceExt(const char *name, const char *ext) | |
687 { | |
688 char *e; | |
689 char *s; | |
690 size_t len; | |
691 size_t extlen; | |
692 | |
693 e = FileName::ext(name); | |
694 if (e) // if already has an extension | |
695 { | |
696 len = e - name; | |
697 extlen = strlen(ext); | |
698 | |
699 s = (char *)alloca(len + extlen + 1); | |
700 memcpy(s,name,len); | |
701 memcpy(s + len, ext, extlen + 1); | |
702 return new FileName(s, 0); | |
703 } | |
704 else | |
705 return defaultExt(name, ext); // doesn't have one | |
706 } | |
707 | |
708 /****************************** | |
709 * Return !=0 if extensions match. | |
710 */ | |
711 | |
712 int FileName::equalsExt(const char *ext) | |
713 { const char *e; | |
714 | |
715 e = FileName::ext(); | |
716 if (!e && !ext) | |
717 return 1; | |
718 if (!e || !ext) | |
719 return 0; | |
720 #if linux | |
721 return strcmp(e,ext) == 0; | |
722 #endif | |
723 #if _WIN32 | |
724 return stricmp(e,ext) == 0; | |
725 #endif | |
726 } | |
727 | |
728 /************************************* | |
729 * Copy file from this to to. | |
730 */ | |
731 | |
732 void FileName::CopyTo(FileName *to) | |
733 { | |
734 File file(this); | |
735 | |
736 #if _WIN32 | |
737 file.touchtime = mem.malloc(sizeof(WIN32_FIND_DATAA)); // keep same file time | |
738 #endif | |
739 #if linux | |
740 file.touchtime = mem.malloc(sizeof(struct stat)); // keep same file time | |
741 #endif | |
742 file.readv(); | |
743 file.name = to; | |
744 file.writev(); | |
745 } | |
746 | |
747 /************************************* | |
748 * Search Path for file. | |
749 * Input: | |
750 * cwd if !=0, search current directory before searching path | |
751 */ | |
752 | |
753 char *FileName::searchPath(Array *path, char *name, int cwd) | |
754 { | |
755 if (absolute(name)) | |
756 { | |
757 return exists(name) ? name : NULL; | |
758 } | |
759 if (cwd) | |
760 { | |
761 if (exists(name)) | |
762 return name; | |
763 } | |
764 if (path) | |
765 { unsigned i; | |
766 | |
767 for (i = 0; i < path->dim; i++) | |
768 { | |
769 char *p = (char *)path->data[i]; | |
770 char *n = combine(p, name); | |
771 | |
772 if (exists(n)) | |
773 return n; | |
774 } | |
775 } | |
776 return NULL; | |
777 } | |
778 | |
779 int FileName::exists(const char *name) | |
780 { | |
781 #if linux | |
782 struct stat st; | |
783 | |
784 if (stat(name, &st) < 0) | |
785 return 0; | |
786 if (S_ISDIR(st.st_mode)) | |
787 return 2; | |
788 return 1; | |
789 #endif | |
790 #if _WIN32 | |
791 DWORD dw; | |
792 int result; | |
793 | |
794 dw = GetFileAttributesA(name); | |
795 if (dw == -1L) | |
796 result = 0; | |
797 else if (dw & FILE_ATTRIBUTE_DIRECTORY) | |
798 result = 2; | |
799 else | |
800 result = 1; | |
801 return result; | |
802 #endif | |
803 } | |
804 | |
805 void FileName::ensurePathExists(const char *path) | |
806 { | |
807 //printf("FileName::ensurePathExists(%s)\n", path ? path : ""); | |
808 if (path && *path) | |
809 { | |
810 if (!exists(path)) | |
811 { | |
812 char *p = FileName::path(path); | |
813 if (*p) | |
814 { | |
815 #if _WIN32 | |
816 size_t len = strlen(p); | |
817 if (len > 2 && p[-1] == ':') | |
818 { mem.free(p); | |
819 return; | |
820 } | |
821 #endif | |
822 ensurePathExists(p); | |
823 mem.free(p); | |
824 } | |
825 #if _WIN32 | |
826 if (path[strlen(path) - 1] != '\\') | |
827 #endif | |
828 #if linux | |
829 if (path[strlen(path) - 1] != '\\') | |
830 #endif | |
831 { | |
832 //printf("mkdir(%s)\n", path); | |
833 #if _WIN32 | |
834 if (mkdir(path)) | |
835 #endif | |
836 #if linux | |
837 if (mkdir(path, 0777)) | |
838 #endif | |
839 error("cannot create directory %s", path); | |
840 } | |
841 } | |
842 } | |
843 } | |
844 | |
845 /****************************** File ********************************/ | |
846 | |
847 File::File(FileName *n) | |
848 { | |
849 ref = 0; | |
850 buffer = NULL; | |
851 len = 0; | |
852 touchtime = NULL; | |
853 name = n; | |
854 } | |
855 | |
856 File::File(char *n) | |
857 { | |
858 ref = 0; | |
859 buffer = NULL; | |
860 len = 0; | |
861 touchtime = NULL; | |
862 name = new FileName(n, 0); | |
863 } | |
864 | |
865 File::~File() | |
866 { | |
867 if (buffer) | |
868 { | |
869 if (ref == 0) | |
870 mem.free(buffer); | |
871 #if _WIN32 | |
872 else if (ref == 2) | |
873 UnmapViewOfFile(buffer); | |
874 #endif | |
875 } | |
876 if (touchtime) | |
877 mem.free(touchtime); | |
878 } | |
879 | |
880 void File::mark() | |
881 { | |
882 mem.mark(buffer); | |
883 mem.mark(touchtime); | |
884 mem.mark(name); | |
885 } | |
886 | |
887 /************************************* | |
888 */ | |
889 | |
890 int File::read() | |
891 { | |
892 #if linux | |
893 off_t size; | |
894 ssize_t numread; | |
895 int fd; | |
896 struct stat buf; | |
897 int result = 0; | |
898 char *name; | |
899 | |
900 name = this->name->toChars(); | |
901 //printf("File::read('%s')\n",name); | |
902 fd = open(name, O_RDONLY); | |
903 if (fd == -1) | |
904 { result = errno; | |
905 //printf("\topen error, errno = %d\n",errno); | |
906 goto err1; | |
907 } | |
908 | |
909 if (!ref) | |
910 mem.free(buffer); | |
911 ref = 0; // we own the buffer now | |
912 | |
913 //printf("\tfile opened\n"); | |
914 if (fstat(fd, &buf)) | |
915 { | |
916 printf("\tfstat error, errno = %d\n",errno); | |
917 goto err2; | |
918 } | |
919 size = buf.st_size; | |
920 buffer = (unsigned char *) mem.malloc(size + 2); | |
921 if (!buffer) | |
922 { | |
923 printf("\tmalloc error, errno = %d\n",errno); | |
924 goto err2; | |
925 } | |
926 | |
927 numread = ::read(fd, buffer, size); | |
928 if (numread != size) | |
929 { | |
930 printf("\tread error, errno = %d\n",errno); | |
931 goto err2; | |
932 } | |
933 | |
934 if (touchtime) | |
935 memcpy(touchtime, &buf, sizeof(buf)); | |
936 | |
937 if (close(fd) == -1) | |
938 { | |
939 printf("\tclose error, errno = %d\n",errno); | |
940 goto err; | |
941 } | |
942 | |
943 len = size; | |
944 | |
945 // Always store a wchar ^Z past end of buffer so scanner has a sentinel | |
946 buffer[size] = 0; // ^Z is obsolete, use 0 | |
947 buffer[size + 1] = 0; | |
948 return 0; | |
949 | |
950 err2: | |
951 close(fd); | |
952 err: | |
953 mem.free(buffer); | |
954 buffer = NULL; | |
955 len = 0; | |
956 | |
957 err1: | |
958 result = 1; | |
959 return result; | |
960 #endif | |
961 #if _WIN32 | |
962 DWORD size; | |
963 DWORD numread; | |
964 HANDLE h; | |
965 int result = 0; | |
966 char *name; | |
967 | |
968 name = this->name->toChars(); | |
969 h = CreateFileA(name,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING, | |
970 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,0); | |
971 if (h == INVALID_HANDLE_VALUE) | |
972 goto err1; | |
973 | |
974 if (!ref) | |
975 mem.free(buffer); | |
976 ref = 0; | |
977 | |
978 size = GetFileSize(h,NULL); | |
979 buffer = (unsigned char *) mem.malloc(size + 2); | |
980 if (!buffer) | |
981 goto err2; | |
982 | |
983 if (ReadFile(h,buffer,size,&numread,NULL) != TRUE) | |
984 goto err2; | |
985 | |
986 if (numread != size) | |
987 goto err2; | |
988 | |
989 if (touchtime) | |
990 { | |
991 if (!GetFileTime(h, NULL, NULL, &((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime)) | |
992 goto err2; | |
993 } | |
994 | |
995 if (!CloseHandle(h)) | |
996 goto err; | |
997 | |
998 len = size; | |
999 | |
1000 // Always store a wchar ^Z past end of buffer so scanner has a sentinel | |
1001 buffer[size] = 0; // ^Z is obsolete, use 0 | |
1002 buffer[size + 1] = 0; | |
1003 return 0; | |
1004 | |
1005 err2: | |
1006 CloseHandle(h); | |
1007 err: | |
1008 mem.free(buffer); | |
1009 buffer = NULL; | |
1010 len = 0; | |
1011 | |
1012 err1: | |
1013 result = 1; | |
1014 return result; | |
1015 #endif | |
1016 } | |
1017 | |
1018 /***************************** | |
1019 * Read a file with memory mapped file I/O. | |
1020 */ | |
1021 | |
1022 int File::mmread() | |
1023 { | |
1024 #if linux | |
1025 return read(); | |
1026 #endif | |
1027 #if _WIN32 | |
1028 HANDLE hFile; | |
1029 HANDLE hFileMap; | |
1030 DWORD size; | |
1031 char *name; | |
1032 | |
1033 name = this->name->toChars(); | |
1034 hFile = CreateFile(name, GENERIC_READ, | |
1035 FILE_SHARE_READ, NULL, | |
1036 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
1037 if (hFile == INVALID_HANDLE_VALUE) | |
1038 goto Lerr; | |
1039 size = GetFileSize(hFile, NULL); | |
1040 //printf(" file created, size %d\n", size); | |
1041 | |
1042 hFileMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,size,NULL); | |
1043 if (CloseHandle(hFile) != TRUE) | |
1044 goto Lerr; | |
1045 | |
1046 if (hFileMap == NULL) | |
1047 goto Lerr; | |
1048 | |
1049 //printf(" mapping created\n"); | |
1050 | |
1051 if (!ref) | |
1052 mem.free(buffer); | |
1053 ref = 2; | |
1054 buffer = (unsigned char *)MapViewOfFileEx(hFileMap, FILE_MAP_READ,0,0,size,NULL); | |
1055 if (CloseHandle(hFileMap) != TRUE) | |
1056 goto Lerr; | |
1057 if (buffer == NULL) // mapping view failed | |
1058 goto Lerr; | |
1059 | |
1060 len = size; | |
1061 //printf(" buffer = %p\n", buffer); | |
1062 | |
1063 return 0; | |
1064 | |
1065 Lerr: | |
1066 return GetLastError(); // failure | |
1067 #endif | |
1068 } | |
1069 | |
1070 /********************************************* | |
1071 * Write a file. | |
1072 * Returns: | |
1073 * 0 success | |
1074 */ | |
1075 | |
1076 int File::write() | |
1077 { | |
1078 #if linux | |
1079 int fd; | |
1080 ssize_t numwritten; | |
1081 char *name; | |
1082 | |
1083 name = this->name->toChars(); | |
1084 fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644); | |
1085 if (fd == -1) | |
1086 goto err; | |
1087 | |
1088 numwritten = ::write(fd, buffer, len); | |
1089 if (len != numwritten) | |
1090 goto err2; | |
1091 | |
1092 if (close(fd) == -1) | |
1093 goto err; | |
1094 | |
1095 if (touchtime) | |
1096 { struct utimbuf ubuf; | |
1097 | |
1098 ubuf.actime = ((struct stat *)touchtime)->st_atime; | |
1099 ubuf.modtime = ((struct stat *)touchtime)->st_mtime; | |
1100 if (utime(name, &ubuf)) | |
1101 goto err; | |
1102 } | |
1103 return 0; | |
1104 | |
1105 err2: | |
1106 close(fd); | |
1107 ::remove(name); | |
1108 err: | |
1109 return 1; | |
1110 #endif | |
1111 #if _WIN32 | |
1112 HANDLE h; | |
1113 DWORD numwritten; | |
1114 char *name; | |
1115 | |
1116 name = this->name->toChars(); | |
1117 h = CreateFileA(name,GENERIC_WRITE,0,NULL,CREATE_ALWAYS, | |
1118 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,NULL); | |
1119 if (h == INVALID_HANDLE_VALUE) | |
1120 goto err; | |
1121 | |
1122 if (WriteFile(h,buffer,len,&numwritten,NULL) != TRUE) | |
1123 goto err2; | |
1124 | |
1125 if (len != numwritten) | |
1126 goto err2; | |
1127 | |
1128 if (touchtime) { | |
1129 SetFileTime(h, NULL, NULL, &((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime); | |
1130 } | |
1131 if (!CloseHandle(h)) | |
1132 goto err; | |
1133 return 0; | |
1134 | |
1135 err2: | |
1136 CloseHandle(h); | |
1137 DeleteFileA(name); | |
1138 err: | |
1139 return 1; | |
1140 #endif | |
1141 } | |
1142 | |
1143 /********************************************* | |
1144 * Append to a file. | |
1145 * Returns: | |
1146 * 0 success | |
1147 */ | |
1148 | |
1149 int File::append() | |
1150 { | |
1151 #if linux | |
1152 return 1; | |
1153 #endif | |
1154 #if _WIN32 | |
1155 HANDLE h; | |
1156 DWORD numwritten; | |
1157 char *name; | |
1158 | |
1159 name = this->name->toChars(); | |
1160 h = CreateFileA(name,GENERIC_WRITE,0,NULL,OPEN_ALWAYS, | |
1161 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,NULL); | |
1162 if (h == INVALID_HANDLE_VALUE) | |
1163 goto err; | |
1164 | |
1165 #if 1 | |
1166 SetFilePointer(h, 0, NULL, FILE_END); | |
1167 #else // INVALID_SET_FILE_POINTER doesn't seem to have a definition | |
1168 if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) | |
1169 goto err; | |
1170 #endif | |
1171 | |
1172 if (WriteFile(h,buffer,len,&numwritten,NULL) != TRUE) | |
1173 goto err2; | |
1174 | |
1175 if (len != numwritten) | |
1176 goto err2; | |
1177 | |
1178 if (touchtime) { | |
1179 SetFileTime(h, NULL, NULL, &((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime); | |
1180 } | |
1181 if (!CloseHandle(h)) | |
1182 goto err; | |
1183 return 0; | |
1184 | |
1185 err2: | |
1186 CloseHandle(h); | |
1187 err: | |
1188 return 1; | |
1189 #endif | |
1190 } | |
1191 | |
1192 /************************************** | |
1193 */ | |
1194 | |
1195 void File::readv() | |
1196 { | |
1197 if (read()) | |
1198 error("Error reading file '%s'\n",name->toChars()); | |
1199 } | |
1200 | |
1201 /************************************** | |
1202 */ | |
1203 | |
1204 void File::mmreadv() | |
1205 { | |
1206 if (mmread()) | |
1207 readv(); | |
1208 } | |
1209 | |
1210 void File::writev() | |
1211 { | |
1212 if (write()) | |
1213 error("Error writing file '%s'\n",name->toChars()); | |
1214 } | |
1215 | |
1216 void File::appendv() | |
1217 { | |
1218 if (write()) | |
1219 error("Error appending to file '%s'\n",name->toChars()); | |
1220 } | |
1221 | |
1222 /******************************************* | |
1223 * Return !=0 if file exists. | |
1224 * 0: file doesn't exist | |
1225 * 1: normal file | |
1226 * 2: directory | |
1227 */ | |
1228 | |
1229 int File::exists() | |
1230 { | |
1231 #if linux | |
1232 return 0; | |
1233 #endif | |
1234 #if _WIN32 | |
1235 DWORD dw; | |
1236 int result; | |
1237 char *name; | |
1238 | |
1239 name = this->name->toChars(); | |
1240 if (touchtime) | |
1241 dw = ((WIN32_FIND_DATAA *)touchtime)->dwFileAttributes; | |
1242 else | |
1243 dw = GetFileAttributesA(name); | |
1244 if (dw == -1L) | |
1245 result = 0; | |
1246 else if (dw & FILE_ATTRIBUTE_DIRECTORY) | |
1247 result = 2; | |
1248 else | |
1249 result = 1; | |
1250 return result; | |
1251 #endif | |
1252 } | |
1253 | |
1254 void File::remove() | |
1255 { | |
1256 #if linux | |
1257 ::remove(this->name->toChars()); | |
1258 #endif | |
1259 #if _WIN32 | |
1260 DeleteFileA(this->name->toChars()); | |
1261 #endif | |
1262 } | |
1263 | |
1264 Array *File::match(char *n) | |
1265 { | |
1266 return match(new FileName(n, 0)); | |
1267 } | |
1268 | |
1269 Array *File::match(FileName *n) | |
1270 { | |
1271 #if linux | |
1272 return NULL; | |
1273 #endif | |
1274 #if _WIN32 | |
1275 HANDLE h; | |
1276 WIN32_FIND_DATAA fileinfo; | |
1277 Array *a; | |
1278 char *c; | |
1279 char *name; | |
1280 | |
1281 a = new Array(); | |
1282 c = n->toChars(); | |
1283 name = n->name(); | |
1284 h = FindFirstFileA(c,&fileinfo); | |
1285 if (h != INVALID_HANDLE_VALUE) | |
1286 { | |
1287 do | |
1288 { | |
1289 // Glue path together with name | |
1290 char *fn; | |
1291 File *f; | |
1292 | |
1293 fn = (char *)mem.malloc(name - c + strlen(fileinfo.cFileName) + 1); | |
1294 memcpy(fn, c, name - c); | |
1295 strcpy(fn + (name - c), fileinfo.cFileName); | |
1296 f = new File(fn); | |
1297 f->touchtime = mem.malloc(sizeof(WIN32_FIND_DATAA)); | |
1298 memcpy(f->touchtime, &fileinfo, sizeof(fileinfo)); | |
1299 a->push(f); | |
1300 } while (FindNextFileA(h,&fileinfo) != FALSE); | |
1301 FindClose(h); | |
1302 } | |
1303 return a; | |
1304 #endif | |
1305 } | |
1306 | |
1307 int File::compareTime(File *f) | |
1308 { | |
1309 #if linux | |
1310 return 0; | |
1311 #endif | |
1312 #if _WIN32 | |
1313 if (!touchtime) | |
1314 stat(); | |
1315 if (!f->touchtime) | |
1316 f->stat(); | |
1317 return CompareFileTime(&((WIN32_FIND_DATAA *)touchtime)->ftLastWriteTime, &((WIN32_FIND_DATAA *)f->touchtime)->ftLastWriteTime); | |
1318 #endif | |
1319 } | |
1320 | |
1321 void File::stat() | |
1322 { | |
1323 #if linux | |
1324 if (!touchtime) | |
1325 { | |
1326 touchtime = mem.calloc(1, sizeof(struct stat)); | |
1327 } | |
1328 #endif | |
1329 #if _WIN32 | |
1330 HANDLE h; | |
1331 | |
1332 if (!touchtime) | |
1333 { | |
1334 touchtime = mem.calloc(1, sizeof(WIN32_FIND_DATAA)); | |
1335 } | |
1336 h = FindFirstFileA(name->toChars(),(WIN32_FIND_DATAA *)touchtime); | |
1337 if (h != INVALID_HANDLE_VALUE) | |
1338 { | |
1339 FindClose(h); | |
1340 } | |
1341 #endif | |
1342 } | |
1343 | |
1344 void File::checkoffset(size_t offset, size_t nbytes) | |
1345 { | |
1346 if (offset > len || offset + nbytes > len) | |
1347 error("Corrupt file '%s': offset x%"PRIxSIZE" off end of file",toChars(),offset); | |
1348 } | |
1349 | |
1350 char *File::toChars() | |
1351 { | |
1352 return name->toChars(); | |
1353 } | |
1354 | |
1355 | |
1356 /************************* OutBuffer *************************/ | |
1357 | |
1358 OutBuffer::OutBuffer() | |
1359 { | |
1360 data = NULL; | |
1361 offset = 0; | |
1362 size = 0; | |
1363 } | |
1364 | |
1365 OutBuffer::~OutBuffer() | |
1366 { | |
1367 mem.free(data); | |
1368 } | |
1369 | |
1370 void *OutBuffer::extractData() | |
1371 { | |
1372 void *p; | |
1373 | |
1374 p = (void *)data; | |
1375 data = NULL; | |
1376 offset = 0; | |
1377 size = 0; | |
1378 return p; | |
1379 } | |
1380 | |
1381 void OutBuffer::mark() | |
1382 { | |
1383 mem.mark(data); | |
1384 } | |
1385 | |
1386 void OutBuffer::reserve(unsigned nbytes) | |
1387 { | |
1388 //printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); | |
1389 if (size - offset < nbytes) | |
1390 { | |
1391 size = (offset + nbytes) * 2; | |
1392 data = (unsigned char *)mem.realloc(data, size); | |
1393 } | |
1394 } | |
1395 | |
1396 void OutBuffer::reset() | |
1397 { | |
1398 offset = 0; | |
1399 } | |
1400 | |
1401 void OutBuffer::setsize(unsigned size) | |
1402 { | |
1403 offset = size; | |
1404 } | |
1405 | |
1406 void OutBuffer::write(const void *data, unsigned nbytes) | |
1407 { | |
1408 reserve(nbytes); | |
1409 memcpy(this->data + offset, data, nbytes); | |
1410 offset += nbytes; | |
1411 } | |
1412 | |
1413 void OutBuffer::writebstring(unsigned char *string) | |
1414 { | |
1415 write(string,*string + 1); | |
1416 } | |
1417 | |
1418 void OutBuffer::writestring(const char *string) | |
1419 { | |
1420 write(string,strlen(string)); | |
1421 } | |
1422 | |
1423 void OutBuffer::writedstring(const char *string) | |
1424 { | |
1425 #if M_UNICODE | |
1426 for (; *string; string++) | |
1427 { | |
1428 writedchar(*string); | |
1429 } | |
1430 #else | |
1431 write(string,strlen(string)); | |
1432 #endif | |
1433 } | |
1434 | |
1435 void OutBuffer::writedstring(const wchar_t *string) | |
1436 { | |
1437 #if M_UNICODE | |
1438 write(string,wcslen(string) * sizeof(wchar_t)); | |
1439 #else | |
1440 for (; *string; string++) | |
1441 { | |
1442 writedchar(*string); | |
1443 } | |
1444 #endif | |
1445 } | |
1446 | |
1447 void OutBuffer::prependstring(char *string) | |
1448 { unsigned len; | |
1449 | |
1450 len = strlen(string); | |
1451 reserve(len); | |
1452 memmove(data + len, data, offset); | |
1453 memcpy(data, string, len); | |
1454 offset += len; | |
1455 } | |
1456 | |
1457 void OutBuffer::writenl() | |
1458 { | |
1459 #if _WIN32 | |
1460 #if M_UNICODE | |
1461 write4(0x000A000D); // newline is CR,LF on Microsoft OS's | |
1462 #else | |
1463 writeword(0x0A0D); // newline is CR,LF on Microsoft OS's | |
1464 #endif | |
1465 #else | |
1466 #if M_UNICODE | |
1467 writeword('\n'); | |
1468 #else | |
1469 writeByte('\n'); | |
1470 #endif | |
1471 #endif | |
1472 } | |
1473 | |
1474 void OutBuffer::writeByte(unsigned b) | |
1475 { | |
1476 reserve(1); | |
1477 this->data[offset] = (unsigned char)b; | |
1478 offset++; | |
1479 } | |
1480 | |
1481 void OutBuffer::writeUTF8(unsigned b) | |
1482 { | |
1483 reserve(6); | |
1484 if (b <= 0x7F) | |
1485 { | |
1486 this->data[offset] = (unsigned char)b; | |
1487 offset++; | |
1488 } | |
1489 else if (b <= 0x7FF) | |
1490 { | |
1491 this->data[offset + 0] = (unsigned char)((b >> 6) | 0xC0); | |
1492 this->data[offset + 1] = (unsigned char)((b & 0x3F) | 0x80); | |
1493 offset += 2; | |
1494 } | |
1495 else if (b <= 0xFFFF) | |
1496 { | |
1497 this->data[offset + 0] = (unsigned char)((b >> 12) | 0xE0); | |
1498 this->data[offset + 1] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1499 this->data[offset + 2] = (unsigned char)((b & 0x3F) | 0x80); | |
1500 offset += 3; | |
1501 } | |
1502 else if (b <= 0x1FFFFF) | |
1503 { | |
1504 this->data[offset + 0] = (unsigned char)((b >> 18) | 0xF0); | |
1505 this->data[offset + 1] = (unsigned char)(((b >> 12) & 0x3F) | 0x80); | |
1506 this->data[offset + 2] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1507 this->data[offset + 3] = (unsigned char)((b & 0x3F) | 0x80); | |
1508 offset += 4; | |
1509 } | |
1510 else if (b <= 0x3FFFFFF) | |
1511 { | |
1512 this->data[offset + 0] = (unsigned char)((b >> 24) | 0xF8); | |
1513 this->data[offset + 1] = (unsigned char)(((b >> 18) & 0x3F) | 0x80); | |
1514 this->data[offset + 2] = (unsigned char)(((b >> 12) & 0x3F) | 0x80); | |
1515 this->data[offset + 3] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1516 this->data[offset + 4] = (unsigned char)((b & 0x3F) | 0x80); | |
1517 offset += 5; | |
1518 } | |
1519 else if (b <= 0x7FFFFFFF) | |
1520 { | |
1521 this->data[offset + 0] = (unsigned char)((b >> 30) | 0xFC); | |
1522 this->data[offset + 1] = (unsigned char)(((b >> 24) & 0x3F) | 0x80); | |
1523 this->data[offset + 2] = (unsigned char)(((b >> 18) & 0x3F) | 0x80); | |
1524 this->data[offset + 3] = (unsigned char)(((b >> 12) & 0x3F) | 0x80); | |
1525 this->data[offset + 4] = (unsigned char)(((b >> 6) & 0x3F) | 0x80); | |
1526 this->data[offset + 5] = (unsigned char)((b & 0x3F) | 0x80); | |
1527 offset += 6; | |
1528 } | |
1529 else | |
1530 assert(0); | |
1531 } | |
1532 | |
1533 void OutBuffer::writedchar(unsigned b) | |
1534 { | |
1535 reserve(Dchar_mbmax * sizeof(dchar)); | |
1536 offset = (unsigned char *)Dchar::put((dchar *)(this->data + offset), (dchar)b) - | |
1537 this->data; | |
1538 } | |
1539 | |
1540 void OutBuffer::prependbyte(unsigned b) | |
1541 { | |
1542 reserve(1); | |
1543 memmove(data + 1, data, offset); | |
1544 data[0] = (unsigned char)b; | |
1545 offset++; | |
1546 } | |
1547 | |
1548 void OutBuffer::writeword(unsigned w) | |
1549 { | |
1550 reserve(2); | |
1551 *(unsigned short *)(this->data + offset) = (unsigned short)w; | |
1552 offset += 2; | |
1553 } | |
1554 | |
1555 void OutBuffer::writeUTF16(unsigned w) | |
1556 { | |
1557 reserve(4); | |
1558 if (w <= 0xFFFF) | |
1559 { | |
1560 *(unsigned short *)(this->data + offset) = (unsigned short)w; | |
1561 offset += 2; | |
1562 } | |
1563 else if (w <= 0x10FFFF) | |
1564 { | |
1565 *(unsigned short *)(this->data + offset) = (unsigned short)((w >> 10) + 0xD7C0); | |
1566 *(unsigned short *)(this->data + offset + 2) = (unsigned short)((w & 0x3FF) | 0xDC00); | |
1567 offset += 4; | |
1568 } | |
1569 else | |
1570 assert(0); | |
1571 } | |
1572 | |
1573 void OutBuffer::write4(unsigned w) | |
1574 { | |
1575 reserve(4); | |
1576 *(unsigned long *)(this->data + offset) = w; | |
1577 offset += 4; | |
1578 } | |
1579 | |
1580 void OutBuffer::write(OutBuffer *buf) | |
1581 { | |
1582 if (buf) | |
1583 { reserve(buf->offset); | |
1584 memcpy(data + offset, buf->data, buf->offset); | |
1585 offset += buf->offset; | |
1586 } | |
1587 } | |
1588 | |
1589 void OutBuffer::write(Object *obj) | |
1590 { | |
1591 if (obj) | |
1592 { | |
1593 writestring(obj->toChars()); | |
1594 } | |
1595 } | |
1596 | |
1597 void OutBuffer::fill0(unsigned nbytes) | |
1598 { | |
1599 reserve(nbytes); | |
1600 memset(data + offset,0,nbytes); | |
1601 offset += nbytes; | |
1602 } | |
1603 | |
1604 void OutBuffer::align(unsigned size) | |
1605 { unsigned nbytes; | |
1606 | |
1607 nbytes = ((offset + size - 1) & ~(size - 1)) - offset; | |
1608 fill0(nbytes); | |
1609 } | |
1610 | |
1611 void OutBuffer::vprintf(const char *format, va_list args) | |
1612 { | |
1613 char buffer[128]; | |
1614 char *p; | |
1615 unsigned psize; | |
1616 int count; | |
1617 | |
1618 p = buffer; | |
1619 psize = sizeof(buffer); | |
1620 for (;;) | |
1621 { | |
1622 #if _WIN32 | |
1623 count = _vsnprintf(p,psize,format,args); | |
1624 if (count != -1) | |
1625 break; | |
1626 psize *= 2; | |
1627 #endif | |
1628 #if linux | |
1629 count = vsnprintf(p,psize,format,args); | |
1630 if (count == -1) | |
1631 psize *= 2; | |
1632 else if (count >= psize) | |
1633 psize = count + 1; | |
1634 else | |
1635 break; | |
1636 #endif | |
1637 p = (char *) alloca(psize); // buffer too small, try again with larger size | |
1638 } | |
1639 write(p,count); | |
1640 } | |
1641 | |
1642 #if M_UNICODE | |
1643 void OutBuffer::vprintf(const wchar_t *format, va_list args) | |
1644 { | |
1645 dchar buffer[128]; | |
1646 dchar *p; | |
1647 unsigned psize; | |
1648 int count; | |
1649 | |
1650 p = buffer; | |
1651 psize = sizeof(buffer) / sizeof(buffer[0]); | |
1652 for (;;) | |
1653 { | |
1654 #if _WIN32 | |
1655 count = _vsnwprintf(p,psize,format,args); | |
1656 if (count != -1) | |
1657 break; | |
1658 psize *= 2; | |
1659 #endif | |
1660 #if linux | |
1661 count = vsnwprintf(p,psize,format,args); | |
1662 if (count == -1) | |
1663 psize *= 2; | |
1664 else if (count >= psize) | |
1665 psize = count + 1; | |
1666 else | |
1667 break; | |
1668 #endif | |
1669 p = (dchar *) alloca(psize * 2); // buffer too small, try again with larger size | |
1670 } | |
1671 write(p,count * 2); | |
1672 } | |
1673 #endif | |
1674 | |
1675 void OutBuffer::printf(const char *format, ...) | |
1676 { | |
1677 va_list ap; | |
1678 va_start(ap, format); | |
1679 vprintf(format,ap); | |
1680 va_end(ap); | |
1681 } | |
1682 | |
1683 #if M_UNICODE | |
1684 void OutBuffer::printf(const wchar_t *format, ...) | |
1685 { | |
1686 va_list ap; | |
1687 va_start(ap, format); | |
1688 vprintf(format,ap); | |
1689 va_end(ap); | |
1690 } | |
1691 #endif | |
1692 | |
1693 void OutBuffer::bracket(char left, char right) | |
1694 { | |
1695 reserve(2); | |
1696 memmove(data + 1, data, offset); | |
1697 data[0] = left; | |
1698 data[offset + 1] = right; | |
1699 offset += 2; | |
1700 } | |
1701 | |
1702 /****************** | |
1703 * Insert left at i, and right at j. | |
1704 * Return index just past right. | |
1705 */ | |
1706 | |
1707 unsigned OutBuffer::bracket(unsigned i, char *left, unsigned j, char *right) | |
1708 { | |
1709 size_t leftlen = strlen(left); | |
1710 size_t rightlen = strlen(right); | |
1711 reserve(leftlen + rightlen); | |
1712 insert(i, left, leftlen); | |
1713 insert(j + leftlen, right, rightlen); | |
1714 return j + leftlen + rightlen; | |
1715 } | |
1716 | |
1717 void OutBuffer::spread(unsigned offset, unsigned nbytes) | |
1718 { | |
1719 reserve(nbytes); | |
1720 memmove(data + offset + nbytes, data + offset, | |
1721 this->offset - offset); | |
1722 this->offset += nbytes; | |
1723 } | |
1724 | |
1725 /**************************************** | |
1726 * Returns: offset + nbytes | |
1727 */ | |
1728 | |
1729 unsigned OutBuffer::insert(unsigned offset, const void *p, unsigned nbytes) | |
1730 { | |
1731 spread(offset, nbytes); | |
1732 memmove(data + offset, p, nbytes); | |
1733 return offset + nbytes; | |
1734 } | |
1735 | |
1736 void OutBuffer::remove(unsigned offset, unsigned nbytes) | |
1737 { | |
1738 memmove(data + offset, data + offset + nbytes, this->offset - (offset + nbytes)); | |
1739 this->offset -= nbytes; | |
1740 } | |
1741 | |
1742 char *OutBuffer::toChars() | |
1743 { | |
1744 writeByte(0); | |
1745 return (char *)data; | |
1746 } | |
1747 | |
1748 /********************************* Bits ****************************/ | |
1749 | |
1750 Bits::Bits() | |
1751 { | |
1752 data = NULL; | |
1753 bitdim = 0; | |
1754 allocdim = 0; | |
1755 } | |
1756 | |
1757 Bits::~Bits() | |
1758 { | |
1759 mem.free(data); | |
1760 } | |
1761 | |
1762 void Bits::mark() | |
1763 { | |
1764 mem.mark(data); | |
1765 } | |
1766 | |
1767 void Bits::resize(unsigned bitdim) | |
1768 { | |
1769 unsigned allocdim; | |
1770 unsigned mask; | |
1771 | |
1772 allocdim = (bitdim + 31) / 32; | |
1773 data = (unsigned *)mem.realloc(data, allocdim * sizeof(data[0])); | |
1774 if (this->allocdim < allocdim) | |
1775 memset(data + this->allocdim, 0, (allocdim - this->allocdim) * sizeof(data[0])); | |
1776 | |
1777 // Clear other bits in last word | |
1778 mask = (1 << (bitdim & 31)) - 1; | |
1779 if (mask) | |
1780 data[allocdim - 1] &= ~mask; | |
1781 | |
1782 this->bitdim = bitdim; | |
1783 this->allocdim = allocdim; | |
1784 } | |
1785 | |
1786 void Bits::set(unsigned bitnum) | |
1787 { | |
1788 data[bitnum / 32] |= 1 << (bitnum & 31); | |
1789 } | |
1790 | |
1791 void Bits::clear(unsigned bitnum) | |
1792 { | |
1793 data[bitnum / 32] &= ~(1 << (bitnum & 31)); | |
1794 } | |
1795 | |
1796 int Bits::test(unsigned bitnum) | |
1797 { | |
1798 return data[bitnum / 32] & (1 << (bitnum & 31)); | |
1799 } | |
1800 | |
1801 void Bits::set() | |
1802 { unsigned mask; | |
1803 | |
1804 memset(data, ~0, allocdim * sizeof(data[0])); | |
1805 | |
1806 // Clear other bits in last word | |
1807 mask = (1 << (bitdim & 31)) - 1; | |
1808 if (mask) | |
1809 data[allocdim - 1] &= mask; | |
1810 } | |
1811 | |
1812 void Bits::clear() | |
1813 { | |
1814 memset(data, 0, allocdim * sizeof(data[0])); | |
1815 } | |
1816 | |
1817 void Bits::copy(Bits *from) | |
1818 { | |
1819 assert(bitdim == from->bitdim); | |
1820 memcpy(data, from->data, allocdim * sizeof(data[0])); | |
1821 } | |
1822 | |
1823 Bits *Bits::clone() | |
1824 { | |
1825 Bits *b; | |
1826 | |
1827 b = new Bits(); | |
1828 b->resize(bitdim); | |
1829 b->copy(this); | |
1830 return b; | |
1831 } | |
1832 | |
1833 void Bits::sub(Bits *b) | |
1834 { | |
1835 unsigned u; | |
1836 | |
1837 for (u = 0; u < allocdim; u++) | |
1838 data[u] &= ~b->data[u]; | |
1839 } | |
1840 | |
1841 | |
1842 | |
1843 | |
1844 | |
1845 | |
1846 | |
1847 | |
1848 | |
1849 | |
1850 | |
1851 | |
1852 | |
1853 | |
1854 |