comparison dmd/root.c @ 1:c53b6e3fe49a trunk

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