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