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