MODULE WinFiles64;
(**
	project	= "BlackBox"
	organization	= "www.oberon.ch"
	contributors	= "Oberon microsystems"
	version	= "System/Rsrc/About"
	copyright	= "System/Rsrc/About"
	license	= "Docu/BB-License"
	changes	= "
	- 20161022, center #138, Support for files larger than 2GB
	- 20190630, center #200, network file delete under Win10 sometimes fails
	- 20210116, bbcb, #045, restore Files interface to 1.6 
"
	issues	= "
	- ...
	"

	Based on HostFiles. Changes for 64-bit file length support are marked with (*64-bit*)
**)

	IMPORT SYSTEM, WinApi, Files64, Files, HostFiles := WinFiles, Kernel, WinEnv;

	CONST
		tempName = "odcxxxxx.tmp";
		docType = "odc";

		pathLen* = HostFiles.pathLen;

		nofbufs = 4;	(* max number of buffers per file *)
		bufsize = 2 * 1024;	(* size of each buffer *)

		invalid = WinApi.INVALID_HANDLE_VALUE;

		temp = 0; new = 1; shared = 2; hidden = 3; exclusive = 4; closed = 5;	(* file states *)
		create = -1;

		ok = 0;
		invalidName = 1;	invalidNameErr = 123; paramErr = 87;
		notFound = 2;	fileNotFoundErr = 2; pathNotFoundErr = 3;
		existsAlready = 3;	fileExistsErr = 80; alreadyExistsErr = 183;
		writeProtected = 4;	writeProtectedErr = 19;
		ioError = 5;
		accessDenied = 6;	accessDeniedErr = 5; sharingErr = 32; netAccessDeniedErr = 65;
		notEnoughMem = 80;	notEnoughMemoryErr = 8;
		notEnoughDisk = 81;	diskFullErr = 39; tooManyOpenFilesErr = 4; noSystemResourcesErr = 1450;

		noMoreFilesErr = 18;

		cancel = -8; retry = -9;

	TYPE
		FullName* = HostFiles.FullName;

		Locator* = HostFiles.Locator;

		Buffer = POINTER TO RECORD
			dirty: BOOLEAN;
			org: LONGINT; (*64-bit*)
			len: INTEGER;
			data: ARRAY bufsize OF BYTE
		END;


		File = POINTER TO RECORD (Files64.File)
			state: INTEGER;
			name: FullName;
			ref: WinApi.HANDLE;
			loc: Locator;
			swapper: INTEGER;	(* index into file table / next buffer to swap *)
			len: LONGINT; (*64-bit*)
			bufs: ARRAY nofbufs OF Buffer;
			t: LONGINT	(* time stamp of last file operation *)
		END;

		Reader = POINTER TO RECORD (Files64.Reader)
			base: File;
			org: LONGINT; (*64-bit*)
			offset: INTEGER;
			buf: Buffer
		END;

		Writer = POINTER TO RECORD (Files64.Writer)
			base: File;
			org: LONGINT; (*64-bit*)
			offset: INTEGER;
			buf: Buffer
		END;

		Directory = POINTER TO RECORD (Files64.Directory)
			temp: Locator
		END;

		Identifier = RECORD (Kernel.Identifier)
			name: FullName
		END;

		Searcher = RECORD (Kernel.Identifier)
			t0: INTEGER;
			f: File
		END;

		Counter = RECORD (Kernel.Identifier)
			count: INTEGER
		END;


	VAR
		dir: Directory;
		wildcard: Files.Type;
		startupDir, customDir: FullName;
		startupLen, customLen: INTEGER;
		isCustomDir, isUseDir: BOOLEAN;
		res: INTEGER;
		
		errString: PROCEDURE (IN string: ARRAY OF CHAR);
		errLn: PROCEDURE;
		stderr: WinApi.HANDLE;

	(* debugging functions *)

	PROCEDURE SetErr* (string: PROCEDURE(IN msg: ARRAY OF CHAR); ln: PROCEDURE);
	BEGIN errString := string; errLn := ln
	END SetErr;
	
	PROCEDURE ErrString (IN msg: ARRAY OF CHAR);
	BEGIN IF errString # NIL THEN errString(msg) END;
	END ErrString;

	PROCEDURE ErrLn;
	BEGIN IF errLn # NIL THEN errLn END;
	END ErrLn;

	PROCEDURE Msg (IN msg: ARRAY OF CHAR);
	BEGIN ErrString(msg); ErrLn
	END Msg;

	PROCEDURE Print (IN s: ARRAY OF CHAR; len: INTEGER);
		VAR res, written: INTEGER; ss: ARRAY 2048 (* max * 2 *) OF SHORTCHAR;
	BEGIN
		IF len > 0 THEN
			res := WinApi.WideCharToMultiByte(WinApi.CP_OEMCP,{},s,len,ss,LEN(ss),NIL,NIL);
			IF (res > 0) & (res <= LEN(ss)) THEN
				res := WinApi.WriteFile(stderr, SYSTEM.ADR(ss[0]), res, written, NIL)
			END
		END
	END Print;

	PROCEDURE DefErrLn;
	BEGIN
		Print(0DX + 0AX, 2)
	END DefErrLn;

	PROCEDURE DefErrString (IN str: ARRAY OF CHAR);
		CONST max = 1024;
		VAR s: ARRAY max + 2 OF CHAR; i, j, len: INTEGER;
	BEGIN
		IF LEN(str$) < max THEN
			Print(str, LEN(str$))
		ELSE
			len := LEN(str); s[max+1] := 0X;
			i := 0; j := 0;
			WHILE (i < len) & (str[i] # 0X) DO
				s[j] := SHORT(str[i]); INC(i);
				IF j = max THEN
					Print(s, j);
					j := 0
				ELSE INC(j) END
			END;
			IF j > 0 THEN
				s[j] := 0X;
				Print(s, j)
			END
		END
	END DefErrString;
	
	PROCEDURE IntToString (x: LONGINT; OUT a: ARRAY OF CHAR);
		CONST max = 25;
		VAR b: ARRAY max OF CHAR; i, j: INTEGER; y: LONGINT;
	BEGIN
		IF x < 0 THEN x := -x; a[0] := '-'; j := 1 ELSIF x > 0 THEN j := 0 ELSE j := 1; a[0] := '0' END;
		i := 0;
		WHILE x # 0 DO y := x MOD 10; b[i] := SHORT(CHR(y + ORD('0'))); x := x DIV 10; INC(i) END;
		DEC(i); WHILE (i >= 0) & (j < max) DO a[j] := b[i]; INC(j); DEC(i) END;
		ASSERT(j < max, 20);
		a[j] := 0X; 
	END IntToString;
	
	PROCEDURE Int (x: LONGINT);
		VAR buf: ARRAY 16 OF CHAR;
	BEGIN
		IntToString(x, buf); ErrString(buf)
	END Int;

	(* end of debugging functions *)
	
	
	PROCEDURE Error (n: INTEGER): INTEGER;
		VAR res: INTEGER;
	BEGIN
		IF n = ok THEN res := ok
		ELSIF n = invalidNameErr THEN res := invalidName
		ELSIF (n = fileNotFoundErr) OR (n = pathNotFoundErr) THEN res := notFound
		ELSIF (n = fileExistsErr) OR (n = alreadyExistsErr) THEN res := existsAlready
		ELSIF n = writeProtectedErr THEN res := writeProtected
		ELSIF (n = sharingErr) OR (n = accessDeniedErr) OR (n = netAccessDeniedErr) THEN res := accessDenied
		ELSIF n = notEnoughMemoryErr THEN res := notEnoughMem
		ELSIF (n = diskFullErr) OR (n = tooManyOpenFilesErr) THEN res := notEnoughDisk
		ELSE res := -n
		END;
		RETURN res
	END Error;

	PROCEDURE Upper (ch: CHAR): CHAR;
	BEGIN
		IF ("a" <= ch) & (ch <= "z") THEN RETURN CAP(ch) (* common case optimized *)
		ELSIF ch > 7FX THEN RETURN SYSTEM.VAL(CHAR, WinApi.CharUpperW( SYSTEM.VAL(WinApi.PtrWSTR, ORD(ch))))
		ELSE RETURN ch
		END
	END Upper;

	PROCEDURE Diff (IN a, b: ARRAY OF CHAR; caseSens: BOOLEAN): INTEGER;
		VAR i: INTEGER; cha, chb: CHAR;
	BEGIN
		i := 0;
		REPEAT
			cha := a[i]; chb := b[i]; INC(i);
			IF cha # chb THEN
				IF ~caseSens THEN
					cha := Upper(cha);
					chb := Upper(chb);
				END;
				IF cha = "\" THEN cha := "/" END;
				IF chb = "\" THEN chb := "/" END;
				IF cha # chb THEN RETURN ORD(cha) - ORD(chb) END
			END
		UNTIL cha = 0X;
		RETURN 0
	END Diff;
	
	PROCEDURE Lower (ch: CHAR): CHAR;
	BEGIN
		IF ("A" <= ch) & (ch <= "Z") THEN RETURN CHR(ORD(ch) + 32)
		ELSIF ch > 7FX THEN
			RETURN SYSTEM.VAL(CHAR, WinApi.CharLowerW(SYSTEM.VAL(WinApi.PtrWSTR, ORD(ch))))
		ELSE RETURN ch
		END
	END Lower;

	PROCEDURE GetType (IN name: ARRAY OF CHAR; VAR type: Files.Type);
		VAR i, j: INTEGER; ch: CHAR;
	BEGIN
		j := 0; i := LEN(name$);
		WHILE (i > 0) & (name[i] # ".") DO DEC(i) END;
		IF i > 0 THEN
			INC(i); ch := name[i];
			WHILE (j < LEN(type) - 1) & (ch # 0X) DO
				ch := Lower(ch);
				type[j] := ch; INC(j);
				INC(i); ch := name[i]
			END
		END;
		type[j] := 0X
	END GetType;

	PROCEDURE Append (IN path, name: ARRAY OF CHAR; type: Files.Type; max: INTEGER;
		VAR res: ARRAY OF CHAR
	);
		VAR i, j, n, m, dot: INTEGER; ch: CHAR;
	BEGIN
		i := 0;
		WHILE path[i] # 0X DO res[i] := path[i]; INC(i) END;
		IF path # "" THEN
			ASSERT((res[i-1] # "/") & (res[i-1] # "\"), 100);
			res[i] := "\"; INC(i)
		END;
		j := 0; ch := name[0]; n := 0; m := max; dot := -1;
		IF max = 12 THEN m := 8 END;
		WHILE (i < LEN(res) - 1) & (ch # 0X) DO
			IF (ch = "/") OR (ch = "\") THEN
				res[i] := ch; INC(i); n := 0; m := max; dot := -1;
				IF max = 12 THEN m := 8 END
			ELSIF (n < m) OR (ch = ".") & (n = 8) THEN
				res[i] := ch; INC(i); INC(n);
				IF ch = "." THEN dot := n;
					IF max = 12 THEN m := n + 3 END
				END
			END;
			INC(j); ch := name[j]
		END;
		IF (dot = -1) & (type # "") THEN
			IF max = 12 THEN m := n + 4 END;
			IF (n < m) & (i < LEN(res) - 1) THEN res[i] := "."; INC(i); INC(n); dot := n END
		END;
		IF n = dot THEN j := 0;
			WHILE (n < m) & (i < LEN(res) - 1) & (type[j] # 0X) DO res[i] := type[j]; INC(i); INC(j) END
		END;
		res[i] := 0X
	END Append;

	PROCEDURE CloseFileHandle (f: File; VAR res: INTEGER);
	BEGIN
		IF (f.ref = invalid) OR (WinApi.CloseHandle(f.ref) # 0) THEN res := ok	(* !!! *)
		ELSE res := WinApi.GetLastError()
		END;
		f.ref := invalid
	END CloseFileHandle;

	PROCEDURE CloseFile (f: File; VAR res: INTEGER);
		VAR s: INTEGER;
	BEGIN
		IF f.state = exclusive THEN
			f.Flush;
			res := WinApi.FlushFileBuffers(f.ref)
		 END;
		s := f.state; f.state := closed;
		CloseFileHandle (f, res);
		IF (s IN {temp, new, hidden}) & (f.name # "") THEN
			res := WinApi.DeleteFileW(f.name)
		END
	END CloseFile;

	PROCEDURE (f: File) FINALIZE;
		VAR res: INTEGER;
	BEGIN
		IF f.state # closed THEN CloseFile(f, res) END
	END FINALIZE;

	PROCEDURE (VAR id: Identifier) Identified (): BOOLEAN;
		VAR f: File;
	BEGIN
		f := id.obj(File);
		RETURN (f.state IN {shared, exclusive}) & (Diff(f.name, id.name, f.loc.caseSens) = 0)
	END Identified;

	PROCEDURE ThisFile (IN name: FullName): File;
		VAR id: Identifier; p: ANYPTR;
	BEGIN
		id.typ := SYSTEM.TYP(File); id.name := name$;
		p := Kernel.ThisFinObj(id);
		IF p # NIL THEN RETURN p(File)
		ELSE RETURN NIL
		END
	END ThisFile;

	PROCEDURE (VAR s: Searcher) Identified (): BOOLEAN;
		VAR f: File;
	BEGIN
		f := s.obj(File);
		IF (f.ref # invalid) & ((s.f = NIL) OR (f.t < s.f.t)) THEN s.f := f END;
		RETURN FALSE
	END Identified;

	PROCEDURE SearchFileToClose;
		VAR s: Searcher; p: ANYPTR; (* res: LONGINT; *)
	BEGIN
		s.typ := SYSTEM.TYP(File); s.f := NIL;
		p := Kernel.ThisFinObj(s);
		IF s.f # NIL THEN
			res := WinApi.CloseHandle(s.f.ref); s.f.ref := invalid;
			IF res = 0 THEN res := WinApi.GetLastError(); HALT(100) END
		END
	END SearchFileToClose;

	PROCEDURE NewFileRef (state: INTEGER; VAR name: FullName): WinApi.HANDLE;
	BEGIN
		IF state = create THEN
			RETURN WinApi.CreateFileW(name, WinApi.GENERIC_READ + WinApi.GENERIC_WRITE, {},
										NIL, WinApi.CREATE_NEW, WinApi.FILE_ATTRIBUTE_TEMPORARY, 0)
		ELSIF state = shared THEN
			RETURN WinApi.CreateFileW(name, WinApi.GENERIC_READ, WinApi.FILE_SHARE_READ,
										NIL, WinApi.OPEN_EXISTING, {}, 0)
		ELSE
			RETURN WinApi.CreateFileW(name, WinApi.GENERIC_READ + WinApi.GENERIC_WRITE, {},
										NIL, WinApi.OPEN_EXISTING, {}, 0)
		END
	END NewFileRef;

	PROCEDURE OpenFile (state: INTEGER; VAR name: FullName; VAR ref, res: INTEGER);
	BEGIN
		ref := NewFileRef(state, name);
		IF ref = invalid THEN
			res := WinApi.GetLastError();
			IF (res = tooManyOpenFilesErr) OR (res = noSystemResourcesErr) THEN
				Kernel.Collect;
				ref := NewFileRef(state, name);
				IF ref = invalid THEN
					res := WinApi.GetLastError();
					IF (res = tooManyOpenFilesErr) OR (res = noSystemResourcesErr) THEN
						SearchFileToClose;
						ref := NewFileRef(state, name);
						IF ref = invalid THEN
							res := WinApi.GetLastError()
						ELSE res := ok
						END
					END
				ELSE res := ok
				END
			END
		ELSE res := ok
		END
	END OpenFile;

	PROCEDURE GetTempFileName (IN path: FullName; OUT name: FullName; num: INTEGER);
		VAR i: INTEGER; str: ARRAY 16 OF CHAR;
	BEGIN
		str := tempName; i := 7;
		WHILE i > 2 DO
			str[i] := CHR(num MOD 10 + ORD("0")); DEC(i); num := num DIV 10
		END;
		Append(path, str, "", 8, name)
	END GetTempFileName;

	PROCEDURE CreateFile (f: File; VAR res: INTEGER);
		VAR num, n: INTEGER;
	BEGIN
		IF f.name = "" THEN
			num := WinApi.GetTickCount(); n := 200;
			REPEAT
				GetTempFileName(f.loc.path, f.name, num); INC(num); DEC(n);
				OpenFile(create, f.name, f.ref, res)
			UNTIL (res # fileExistsErr) & (res # alreadyExistsErr) & (res # paramErr) OR (n = 0)
		ELSE
			OpenFile(f.state, f.name, f.ref, res)
		END
	END CreateFile;

	PROCEDURE Delete (IN fname, path: FullName; VAR res: INTEGER);
		VAR num, n, s: INTEGER; f: File; new: FullName; attr: SET;
	BEGIN
		ASSERT(fname # "", 100);
		f := ThisFile(fname);
		IF f = NIL THEN
			IF WinApi.DeleteFileW(fname) # 0 THEN res := ok
			ELSE res := WinApi.GetLastError()
			END
		ELSE (* still in use => make it anonymous *)
			IF f.ref # invalid THEN res := WinApi.CloseHandle(f.ref); f.ref := invalid END;	(* !!! *)
			attr := BITS(WinApi.GetFileAttributesW(fname));
			ASSERT(attr # {0..MAX(SET)}, 101);
			IF WinApi.FILE_ATTRIBUTE_READONLY * attr = {} THEN
				s := f.state; num := WinApi.GetTickCount(); n := 200;
				REPEAT
					GetTempFileName(path, new, num); INC(num); DEC(n);
					IF WinApi.MoveFileW(fname, new) # 0 THEN res := ok
					ELSE res := WinApi.GetLastError()
					END
				UNTIL (res # fileExistsErr) & (res # alreadyExistsErr)
					& (res # paramErr) & (res # sharingErr) & (res # accessDeniedErr) OR (n = 0);
				IF res = ok THEN
					f.state := hidden; f.name := new$
				END
			ELSE
				res := writeProtectedErr
			END
		END
	END Delete;

	PROCEDURE FlushBuffer (f: File; i: INTEGER);
		VAR buf: Buffer; res, h: INTEGER;
	BEGIN
		buf := f.bufs[i];
		IF (buf # NIL) & buf.dirty THEN
			IF f.ref = invalid THEN CreateFile(f, res) (* ASSERT(res = ok, 100) *) END;
			IF f.ref # invalid THEN
				h := SHORT(buf.org DIV 100000000L); h := WinApi.SetFilePointer(f.ref, SHORT(buf.org), h, 0); (*64-bit*)
				IF (WinApi.WriteFile(f.ref, SYSTEM.ADR(buf.data), buf.len, h, NIL) = 0) OR (h < buf.len) THEN
					res := WinApi.GetLastError(); HALT(101)
				END;
				buf.dirty := FALSE; f.t := Kernel.Time()
			END
		END
	END FlushBuffer;



	(* File *)

	PROCEDURE (f: File) NewReader (old: Files64.Reader): Files64.Reader;
		VAR r: Reader;
	BEGIN	(* portable *)
		ASSERT(f.state # closed, 20);
		IF (old # NIL) & (old IS Reader) THEN r := old(Reader) ELSE NEW(r) END;
		IF r.base # f THEN
			r.base := f; r.buf := NIL; r.SetPos(0)
		END;
		r.eof := FALSE;
		RETURN r
	END NewReader;

	PROCEDURE (f: File) NewWriter (old: Files64.Writer): Files64.Writer;
		VAR w: Writer;
	BEGIN	(* portable *)
		ASSERT(f.state # closed, 20); ASSERT(f.state # shared, 21);
		IF (old # NIL) & (old IS Writer) THEN w := old(Writer) ELSE NEW(w) END;
		IF w.base # f THEN
			w.base := f; w.buf := NIL; w.SetPos(f.len)
		END;
		RETURN w
	END NewWriter;

	PROCEDURE (f: File) Length (): LONGINT; (*64-bit*)
	BEGIN	(* portable *)
		RETURN f.len
	END Length;

	PROCEDURE (f: File) Flush;
		VAR i: INTEGER;
	BEGIN	(* portable *)
		i := 0; WHILE i # nofbufs DO FlushBuffer(f, i); INC(i) END
	END Flush;

	PROCEDURE GetPath (IN fname: FullName; OUT path: FullName);
		VAR i: INTEGER;
	BEGIN
		path := fname$; i := LEN(path$);
		WHILE (i > 0) & (path[i] # "\") & (path[i] # "/") & (path[i-1] # ":") DO DEC(i) END;
		path[i] := 0X
	END GetPath;

	PROCEDURE CreateDir (IN path: FullName; OUT res: INTEGER);
		VAR sec: WinApi.SECURITY_ATTRIBUTES; p: FullName;
	BEGIN
		ASSERT(path # "", 100);
		sec.nLength :=SIZE(WinApi.SECURITY_ATTRIBUTES);
		sec.lpSecurityDescriptor := 0; sec.bInheritHandle := 0;
		res := WinApi.CreateDirectoryW(path, sec);
		IF res = 0 THEN res := WinApi.GetLastError() ELSE res := ok END;
		IF (res = fileNotFoundErr) OR (res = pathNotFoundErr) THEN
			GetPath(path, p);
			IF path # p THEN
				CreateDir(p, res);	(* recursive call *)
				IF res = ok THEN
					res := WinApi.CreateDirectoryW(path, sec);
					IF res = 0 THEN res := WinApi.GetLastError() ELSE res := ok END
				END
			END
		END
	END CreateDir;

	PROCEDURE CheckPath (IN path: FullName; ask: BOOLEAN; VAR res: INTEGER);
		VAR s: ARRAY 300 OF CHAR; t: ARRAY 32 OF CHAR;
	BEGIN
		IF ask & (HostFiles.MessageBox # NIL) THEN
			IF HostFiles.MapParamString # NIL THEN
				HostFiles.MapParamString("#System:CreateDir", path, "", "", s);
				HostFiles.MapParamString("#System:MissingDirectory", "", "", "", t)
			ELSE
				s := path$; t := "Missing Directory"
			END;
			res := HostFiles.MessageBox(t, s, {0, 6})	(* ok cancel, icon information *)
		ELSE
			res := 1
		END;
		IF res = 1 THEN CreateDir(path, res)
		ELSIF res = 2 THEN res := cancel
		END
	END CheckPath;

	PROCEDURE CheckDelete (IN fname, path: FullName; ask: BOOLEAN; VAR res: INTEGER);
		VAR s: ARRAY 300 OF CHAR; t: ARRAY 16 OF CHAR;
	BEGIN
		REPEAT
			Delete(fname, path, res);
			IF (res = writeProtectedErr) OR (res = sharingErr) OR (res = accessDeniedErr)
				OR (res = netAccessDeniedErr)
			THEN
				IF ask & (HostFiles.MessageBox # NIL) THEN
					IF HostFiles.MapParamString # NIL THEN
						IF res = writeProtectedErr THEN
							HostFiles.MapParamString("#System:ReplaceWriteProtected", fname, 0DX, "", s)
						ELSIF (res = accessDeniedErr) OR (res = netAccessDeniedErr) THEN
							HostFiles.MapParamString("#System:ReplaceAccessDenied", fname, 0DX, "", s)
						ELSE
							HostFiles.MapParamString("#System:ReplaceInUse", fname, 0DX, "", s)
						END;
						HostFiles.MapParamString("#System:FileError", "", "", "", t)
					ELSE
						s := fname$; t := "File Error"
					END;
					res := HostFiles.MessageBox(t, s, {0, 2, 4, 5});	(* retry cancel, icon warning *)
					IF res = 2 THEN res := cancel
					ELSIF res = 4 THEN res := retry
					END
				ELSE
					res := cancel
				END
			ELSE
				res := ok
			END
		UNTIL res # retry
	END CheckDelete;

	PROCEDURE (f: File) Register (name: Files.Name; type: Files.Type; ask: BOOLEAN; OUT res: INTEGER);
		VAR b: INTEGER; fname: FullName;
	BEGIN
		ASSERT(f.state = new, 20); ASSERT(name # "", 21);
		Append(f.loc.path, name, type, f.loc.maxLen, fname);
		CheckDelete(fname, f.loc.path, ask, res);
		ASSERT(res # paramErr, 100);
		IF res = ok THEN
			IF f.name = "" THEN
				f.name := fname$;
				OpenFile(create, f.name, f.ref, res);
				IF res = ok THEN
					f.state := exclusive; CloseFile(f, res);
					b := WinApi.SetFileAttributesW(f.name, WinApi.FILE_ATTRIBUTE_ARCHIVE)
				END
			ELSE
				f.state := exclusive; CloseFile(f, res);
				IF WinApi.MoveFileW(f.name, fname) # 0 THEN
					res := ok; f.name := fname$;
					b := WinApi.SetFileAttributesW(f.name, WinApi.FILE_ATTRIBUTE_ARCHIVE)
				ELSE
					res := WinApi.GetLastError();
					ASSERT(res # paramErr, 101);
					b := WinApi.DeleteFileW(f.name)
				END
			END
		END;
		res := Error(res)
	END Register;

	PROCEDURE (f: File) Close;
		VAR res: INTEGER;
	BEGIN	(* portable *)
		IF f.state # closed THEN
(*
			IF f.state = exclusive THEN
				CloseFile(f, res)
			ELSE
				CloseFileHandle(f, res)
			END
*)
			CloseFile(f, res)
		END
	END Close;


	(* Reader *)

	PROCEDURE (r: Reader) Base (): Files64.File;
	BEGIN	(* portable *)
		RETURN r.base
	END Base;

(*
	PROCEDURE (r: Reader) Available (): INTEGER;
	BEGIN	(* portable *)
		ASSERT(r.base # NIL, 20);
		RETURN r.base.len - r.org - r.offset
	END Available;
*)
	PROCEDURE (r: Reader) SetPos (pos: LONGINT); (*64-bit*)
		VAR f: File; org: LONGINT(*64-bit*); offset, i, count, res: INTEGER; buf: Buffer;
	BEGIN
		f := r.base; ASSERT(f # NIL, 20); ASSERT(f.state # closed, 25);
		ASSERT(pos >= 0, 22); ASSERT(pos <= f.len, 21);
		offset := SHORT(pos MOD bufsize); (*64-bit*) org := pos - offset;
		i := 0; WHILE (i # nofbufs) & (f.bufs[i] # NIL) & (org # f.bufs[i].org) DO INC(i) END;
		IF i # nofbufs THEN
			buf := f.bufs[i];
			IF buf = NIL THEN	(* create new buffer *)
				NEW(buf); f.bufs[i] := buf; buf.org := -1
			END
		ELSE	(* choose an existing buffer *)
			f.swapper := (f.swapper + 1) MOD nofbufs;
			FlushBuffer(f, f.swapper); buf := f.bufs[f.swapper]; buf.org := -1
		END;
		IF buf.org # org THEN
			IF org + bufsize > f.len THEN buf.len := SHORT(f.len - org) (*64-bit*) ELSE buf.len := bufsize END;
			count := buf.len;
			IF count > 0 THEN
				IF f.ref = invalid THEN CreateFile(f, res) (* ASSERT(res = ok, 100) *) END;
				IF f.ref # invalid THEN
					i := SHORT(org DIV 100000000L); i := WinApi.SetFilePointer(f.ref, SHORT(org), i, 0); (*64-bit*)
					IF (WinApi.ReadFile(f.ref, SYSTEM.ADR(buf.data), count, i, NIL) = 0) OR (i < count) THEN
						res := WinApi.GetLastError(); res := Error(res); HALT(101)
					END;
					f.t := Kernel.Time()
				END
			END;
			buf.org := org; buf.dirty := FALSE
		END;
		r.buf := buf; r.org := org; r.offset := offset; r.eof := FALSE
		(* 0<= r.org <= r.base.len *)
		(* 0 <= r.offset < bufsize *)
		(* 0 <= r.buf.len <= bufsize *)
		(* r.offset <= r.base.len *)
		(* r.offset <= r.buf.len *)
	END SetPos;

	PROCEDURE (r: Reader) Pos (): LONGINT; (*64-bit*)
	BEGIN	(* portable *)
		ASSERT(r.base # NIL, 20);
		RETURN r.org + r.offset
	END Pos;

	PROCEDURE (r: Reader) ReadByte (OUT x: BYTE);
	BEGIN	(* portable *)
		IF (r.org # r.buf.org) OR (r.offset >= bufsize) THEN r.SetPos(r.org + r.offset) END;
		IF r.offset < r.buf.len THEN
			x := r.buf.data[r.offset]; INC(r.offset)
		ELSE
			x := 0; r.eof := TRUE
		END
	END ReadByte;

	PROCEDURE (r: Reader) ReadBytes (VAR x: ARRAY OF BYTE; beg, len: INTEGER);
		VAR from, to, count, restInBuf: INTEGER;
	BEGIN	(* portable *)
		ASSERT(beg >= 0, 21);
		IF len > 0 THEN
			ASSERT(beg + len <= LEN(x), 23);
			WHILE len # 0 DO
				IF (r.org # r.buf.org) OR (r.offset >= bufsize) THEN r.SetPos(r.org + r.offset) END;
				restInBuf := r.buf.len - r.offset;
				IF restInBuf = 0 THEN r.eof := TRUE; RETURN
				ELSIF restInBuf <= len THEN count := restInBuf
				ELSE count := len
				END;
				from := SYSTEM.ADR(r.buf.data[r.offset]); to := SYSTEM.ADR(x) + beg;
				SYSTEM.MOVE(from, to, count);
				INC(r.offset, count); INC(beg, count); DEC(len, count)
			END;
			r.eof := FALSE
		ELSE ASSERT(len = 0, 22)
		END
	END ReadBytes;



	(* Writer *)

	PROCEDURE (w: Writer) Base (): Files64.File;
	BEGIN	(* portable *)
		RETURN w.base
	END Base;

	PROCEDURE (w: Writer) SetPos (pos: LONGINT); (*64-bit*)
		VAR f: File; org: LONGINT(*64-bit*); offset, i, count, res: INTEGER; buf: Buffer;
	BEGIN
		f := w.base; ASSERT(f # NIL, 20); ASSERT(f.state # closed, 25);
		ASSERT(pos >= 0, 22); ASSERT(pos <= f.len, 21);
		offset := SHORT(pos MOD bufsize); (*64-bit*) org := pos - offset;
		i := 0; WHILE (i # nofbufs) & (f.bufs[i] # NIL) & (org # f.bufs[i].org) DO INC(i) END;
		IF i # nofbufs THEN
			buf := f.bufs[i];
			IF buf = NIL THEN	(* create new buffer *)
				NEW(buf); f.bufs[i] := buf; buf.org := -1
			END
		ELSE	(* choose an existing buffer *)
			f.swapper := (f.swapper + 1) MOD nofbufs;
			FlushBuffer(f, f.swapper); buf := f.bufs[f.swapper]; buf.org := -1
		END;
		IF buf.org # org THEN
			IF org + bufsize > f.len THEN buf.len := SHORT(f.len - org) (*64-bit*) ELSE buf.len := bufsize END;
			count := buf.len;
			IF count > 0 THEN
				IF f.ref = invalid THEN CreateFile(f, res) (* ASSERT(res = ok, 100) *) END;
				IF f.ref # invalid THEN
					i := SHORT(org DIV 100000000L); i := WinApi.SetFilePointer(f.ref, SHORT(org), i, 0); (*64-bit*)
					IF (WinApi.ReadFile(f.ref, SYSTEM.ADR(buf.data), count, i, NIL) = 0) OR (i < count) THEN
						res := WinApi.GetLastError(); res := Error(res); HALT(101)
					END;
					f.t := Kernel.Time()
				END
			END;
			buf.org := org; buf.dirty := FALSE
		END;
		w.buf := buf; w.org := org; w.offset := offset
		(* 0<= w.org <= w.base.len *)
		(* 0 <= w.offset < bufsize *)
		(* 0 <= w.buf.len <= bufsize *)
		(* w.offset <= w.base.len *)
		(* w.offset <= w.buf.len *)
	END SetPos;

	PROCEDURE (w: Writer) Pos (): LONGINT; (*64-bit*)
	BEGIN	(* portable *)
		ASSERT(w.base # NIL, 20);
		RETURN w.org + w.offset
	END Pos;

	PROCEDURE (w: Writer) WriteByte (x: BYTE);
	BEGIN	(* portable *)
		ASSERT(w.base.state # closed, 25);
		IF (w.org # w.buf.org) OR (w.offset >= bufsize) THEN w.SetPos(w.org + w.offset) END;
		w.buf.data[w.offset] := x; w.buf.dirty := TRUE;
		IF w.offset = w.buf.len THEN INC(w.buf.len); INC(w.base.len) END;
		INC(w.offset)
	END WriteByte;

	PROCEDURE (w: Writer) WriteBytes (IN x: ARRAY OF BYTE; beg, len: INTEGER);
		VAR from, to, count, restInBuf: INTEGER;
	BEGIN	(* portable *)
		ASSERT(beg >= 0, 21); ASSERT(w.base.state # closed, 25);
		IF len > 0 THEN
			ASSERT(beg + len <= LEN(x), 23);
			WHILE len # 0 DO
				IF (w.org # w.buf.org) OR (w.offset >= bufsize) THEN w.SetPos(w.org + w.offset) END;
				restInBuf := bufsize - w.offset;
				IF restInBuf <= len THEN count := restInBuf ELSE count := len END;
				from := SYSTEM.ADR(x) + beg; to := SYSTEM.ADR(w.buf.data[w.offset]);
				SYSTEM.MOVE(from, to, count);
				INC(w.offset, count); INC(beg, count); DEC(len, count);
				IF w.offset > w.buf.len THEN INC(w.base.len, w.offset - w.buf.len); w.buf.len := w.offset END;
				w.buf.dirty := TRUE
			END
		ELSE ASSERT(len = 0, 22)
		END
	END WriteBytes;



	(* Directory *)

	PROCEDURE (d: Directory) This (IN path: ARRAY OF CHAR): Files.Locator;
	BEGIN
		RETURN Files.dir.This(path)
	END This;

	PROCEDURE (d: Directory) New (loc: Files.Locator; ask: BOOLEAN): Files64.File;
		VAR f: File; res: INTEGER; attr: SET;
	BEGIN
		ASSERT(loc # NIL, 20); f := NIL; res := ok;
		WITH loc: Locator DO
			IF loc.path # "" THEN
				attr := BITS(WinApi.GetFileAttributesW(loc.path));
				IF attr = {0..MAX(SET)} THEN	(* error *)
					res := WinApi.GetLastError();
					IF (res = fileNotFoundErr) OR (res = pathNotFoundErr) THEN
						IF loc.res = 76 THEN CreateDir(loc.path, res)
						ELSE CheckPath(loc.path, ask, res)
						END
					ELSE res := pathNotFoundErr
					END
				ELSIF WinApi.FILE_ATTRIBUTE_DIRECTORY * attr = {} THEN res := fileExistsErr
				END
			END;
			IF res = ok THEN
				NEW(f); f.loc := loc; f.name := "";
				f.state := new; f.swapper := -1; f.len := 0; f.ref := invalid
			END
		ELSE res := invalidNameErr
		END;
		loc.res := Error(res);
		RETURN f
	END New;

	PROCEDURE (d: Directory) Temp (): Files64.File;
		VAR f: File;
	BEGIN
		NEW(f); f.loc := d.temp; f.name := "";
		f.state := temp; f.swapper := -1; f.len := 0; f.ref := invalid;
		RETURN f
	END Temp;

	PROCEDURE GetStartupDir (loc: Locator; OUT dir: FullName);
		VAR i, j: INTEGER;
	BEGIN
		dir := startupDir$; i := startupLen; j := loc.rootLen;
		WHILE loc.path[j] # 0X DO dir[i] := loc.path[j]; INC(i); INC(j) END;
		dir[i] := 0X
	END GetStartupDir;
	
	PROCEDURE GetCustomDir (loc: Locator; OUT dir: FullName);
		VAR i, j: INTEGER;
	BEGIN
		dir := customDir$; i := customLen; j := loc.rootLen;
		WHILE loc.path[j] # 0X DO dir[i] := loc.path[j]; INC(i); INC(j) END;
		dir[i] := 0X
	END GetCustomDir;

	PROCEDURE (d: Directory) Old (loc: Files.Locator; name: Files.Name; shrd: BOOLEAN): Files64.File;
		VAR res, i, j: INTEGER; f: File; ref: WinApi.HANDLE; fname: FullName; type: Files.Type; s: BYTE;
	BEGIN
		ASSERT(loc # NIL, 20); ASSERT(name # "", 21);
		res := ok; f := NIL;
		WITH loc: Locator DO
			Append(loc.path, name, "", loc.maxLen, fname);
			f := ThisFile(fname);
			IF f # NIL THEN
				IF ~shrd OR (f.state = exclusive) THEN loc.res := Error(sharingErr); RETURN NIL
				ELSE loc.res := ok; RETURN f
				END
			END;
			IF shrd THEN s := shared ELSE s := exclusive END;
			OpenFile(s, fname, ref, res);
			
			IF ((res = fileNotFoundErr) OR (res = pathNotFoundErr))
				& (isUseDir & isCustomDir) THEN
				GetCustomDir(loc, fname);
				Append(fname, name, "", loc.maxLen, fname);
				f := ThisFile(fname);
				IF f # NIL THEN
					IF ~shrd OR (f.state = exclusive) THEN loc.res := Error(sharingErr); RETURN NIL
					ELSE loc.res := ok; RETURN f
					END
				END;
				OpenFile(s, fname, ref, res)
			END;
			
			IF ((res = fileNotFoundErr) OR (res = pathNotFoundErr))
				& (isUseDir OR isCustomDir) THEN
				GetStartupDir(loc, fname);
				Append(fname, name, "", loc.maxLen, fname);
				f := ThisFile(fname);
				IF f # NIL THEN
					IF ~shrd OR (f.state = exclusive) THEN loc.res := Error(sharingErr); RETURN NIL
					ELSE loc.res := ok; RETURN f
					END
				END;
				OpenFile(s, fname, ref, res)
			END;
			
			IF res = ok THEN
				NEW(f); f.loc := loc;
				f.swapper := -1;
				GetType(name, type);
				f.InitType(type);
				ASSERT(ref # invalid, 107);
				f.ref := ref; f.name := fname$; f.state := s; f.t := Kernel.Time();
				f.len := WinApi.GetFileSize(ref, j);
				f.len := f.len MOD 100000000L + j * 100000000L (*64-bit*)
			END
		END;
		loc.res := Error(res);
		RETURN f
	END Old;

	PROCEDURE (d: Directory) Delete* (loc: Files.Locator; name: Files.Name);
		VAR res: INTEGER; fname: FullName;
	BEGIN
		ASSERT(loc # NIL, 20);
		WITH loc: Locator DO
			Append(loc.path, name, "", loc.maxLen, fname);
			Delete(fname, loc.path, res)
		ELSE res := invalidNameErr
		END;
		loc.res := Error(res)
	END Delete;

	PROCEDURE (d: Directory) Rename* (loc: Files.Locator; old, new: Files.Name; ask: BOOLEAN);
		VAR res, i: INTEGER; oldname, newname, tn: FullName; f: File; attr: SET;
	BEGIN
		ASSERT(loc # NIL, 20);
		WITH loc: Locator DO
			Append(loc.path, old, "", loc.maxLen, oldname); Append(loc.path, new, "", loc.maxLen, newname);
			attr :=BITS( WinApi.GetFileAttributesW(oldname));
			IF ORD(attr) # -1 THEN
				f := ThisFile(oldname);
				IF (f # NIL) & (f.ref # invalid) THEN res := WinApi.CloseHandle(f.ref); f.ref := invalid END;
				IF Diff(oldname, newname, loc.caseSens) # 0 THEN
					CheckDelete(newname, loc.path, ask, res);
					IF res = ok THEN
						IF WinApi.MoveFileW(oldname, newname) # 0 THEN
							IF f # NIL THEN	(* still in use => update file table *)
								f.name := newname$
							END
						ELSE res := WinApi.GetLastError()
						END
					END
				ELSE	(* destination is same file as source *)
					tn := oldname$; i := LEN(tn$) - 1;
					REPEAT
						tn[i] := CHR(ORD(tn[i]) + 1);
						IF WinApi.MoveFileW(oldname, tn) # 0 THEN res := ok
						ELSE res := WinApi.GetLastError()
						END
					UNTIL (res # fileExistsErr) & (res # alreadyExistsErr) & (res # paramErr);
					IF res = ok THEN
						IF WinApi.MoveFileW(tn, newname) = 0 THEN res := WinApi.GetLastError() END
					END
				END
			ELSE res := fileNotFoundErr
			END
		ELSE res := invalidNameErr
		END;
		loc.res := Error(res)
	END Rename;

	PROCEDURE (d: Directory) SameFile* (loc0: Files.Locator; name0: Files.Name;
															loc1: Files.Locator; name1: Files.Name): BOOLEAN;
		VAR p0, p1: FullName;
	BEGIN
		ASSERT(loc0 # NIL, 20); ASSERT(loc1 # NIL, 21);
		WITH loc0: Locator DO Append(loc0.path, name0, "", loc0.maxLen, p0) END;
		WITH loc1: Locator DO Append(loc1.path, name1, "", loc1.maxLen, p1) END;
		RETURN Diff(p0, p1, loc0(Locator).caseSens) = 0
	END SameFile;

	PROCEDURE (d: Directory) FileList* (loc: Files.Locator): Files64.FileInfo;
		VAR diff, res, firstRes: INTEGER; first, last, info: Files64.FileInfo; s: FullName;
			
		PROCEDURE Do (s: FullName): INTEGER;
		VAR i: INTEGER; find: WinApi.HANDLE;
			fd: WinApi.WIN32_FIND_DATAW; st: WinApi.SYSTEMTIME;
		BEGIN
			Append(s, wildcard, wildcard, loc(Locator).maxLen, s);
			find := WinApi.FindFirstFileW(s, fd);
			IF find # invalid THEN
				REPEAT
					IF ~(WinApi.FILE_ATTRIBUTE_DIRECTORY * fd.dwFileAttributes # {})
							& (LEN(fd.cFileName$) < LEN(info.name)) THEN
						info := first; last := NIL; s := fd.cFileName$;
						IF info # NIL THEN
							diff := Diff(info.name, s, loc(Locator).caseSens)
						END;
						WHILE (info # NIL) & (diff < 0) DO
							last := info; info := info.next;
							IF info # NIL THEN diff := Diff(info.name, s, loc(Locator).caseSens) END
						END;
						IF (info = NIL) OR (diff # 0) THEN
							NEW(info);
							info.name := fd.cFileName$;
							info.length := fd.nFileSizeLow MOD 100000000L + fd.nFileSizeHigh * 100000000L; (*64-bit*)
							IF (info.length < 0) OR (fd.nFileSizeHigh # 0) THEN info.length := -1 END;
							res := WinApi.FileTimeToSystemTime(fd.ftLastWriteTime, st);
							info.modified.year := st.wYear;
							info.modified.month := st.wMonth;
							info.modified.day := st.wDay;
							info.modified.hour := st.wHour;
							info.modified.minute := st.wMinute;
							info.modified.second := st.wSecond;
							info.attr := {};
							IF WinApi.FILE_ATTRIBUTE_HIDDEN * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.hidden)
							END;
							IF WinApi.FILE_ATTRIBUTE_READONLY * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.readOnly)
							END;
							IF WinApi.FILE_ATTRIBUTE_SYSTEM * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.system)
							END;
							IF WinApi.FILE_ATTRIBUTE_ARCHIVE * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.archive)
							END;
							GetType(fd.cFileName, info.type);
							IF last = NIL THEN info.next := first; first := info ELSE info.next := last.next; last.next := info END
						END
					END;
					i := WinApi.FindNextFileW(find, fd)
				UNTIL i = 0;
				res := WinApi.GetLastError(); i := WinApi.FindClose(find)
			ELSE res := WinApi.GetLastError()
			END;
			IF res = noMoreFilesErr THEN res := ok END;
			RETURN res
		END Do;
	
		
	BEGIN
		ASSERT(loc # NIL, 20);
		first := NIL; last :=NIL;
		WITH loc: Locator DO
			res := Do(loc.path);
			firstRes := res;
			(* check custom directory *)
			IF (isUseDir & isCustomDir) & (res IN {ok, fileNotFoundErr, pathNotFoundErr}) THEN
				GetCustomDir(loc, s);
				res := Do(s);
			END;
			(* check startup directory *)
			IF (isUseDir OR isCustomDir) & (res IN {ok, fileNotFoundErr, pathNotFoundErr}) THEN
				GetStartupDir(loc, s);
				res := Do(s);
			END;
			IF (res = fileNotFoundErr) OR (res = pathNotFoundErr) THEN res := firstRes END;
			loc.res := Error(res)
		ELSE loc.res := invalidName
		END;
		RETURN first
	END FileList;

	PROCEDURE (d: Directory) LocList* (loc: Files.Locator): Files.LocInfo;
		VAR diff, res, firstRes: INTEGER; first, last, info: Files.LocInfo; s: FullName;
			
		PROCEDURE Do (s: FullName): INTEGER;
		VAR i: INTEGER; find: WinApi.HANDLE;
			fd: WinApi.WIN32_FIND_DATAW;
		BEGIN
			Append(s, wildcard, wildcard, loc(Locator).maxLen, s);
			find := WinApi.FindFirstFileW(s, fd);
			IF find # invalid THEN
				REPEAT
					IF (WinApi.FILE_ATTRIBUTE_DIRECTORY * fd.dwFileAttributes # {})
					& (fd.cFileName # ".") & (fd.cFileName # "..") & (LEN(fd.cFileName$) < LEN(info.name)) THEN
						info := first; last := NIL; s := fd.cFileName$;
						IF info # NIL THEN diff := Diff(info.name, s, loc(Locator).caseSens) END;
						WHILE (info # NIL) & (diff < 0) DO
							last := info; info := info.next;
							IF info # NIL THEN diff := Diff(info.name, s, loc(Locator).caseSens) END
						END;
						IF (info = NIL) OR (diff # 0) THEN
							NEW(info);
							info.name := fd.cFileName$;
							info.attr := {};
							IF WinApi.FILE_ATTRIBUTE_HIDDEN * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.hidden)
							END;
							IF WinApi.FILE_ATTRIBUTE_READONLY * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.readOnly)
							END;
							IF WinApi.FILE_ATTRIBUTE_SYSTEM * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.system)
							END;
							IF WinApi.FILE_ATTRIBUTE_ARCHIVE * fd.dwFileAttributes # {} THEN
								INCL(info.attr, Files.archive)
							END;
							IF last = NIL THEN
								info.next := first; first := info
							ELSE
								info.next := last.next; last.next := info
							END
						END
					END;
					i := WinApi.FindNextFileW(find, fd)
				UNTIL i = 0;
				res := WinApi.GetLastError(); i := WinApi.FindClose(find)
			ELSE res := WinApi.GetLastError()
			END;
			IF res = noMoreFilesErr THEN res := ok END;
			RETURN res
		END Do;
	
	BEGIN
		ASSERT(loc # NIL, 20);
		first := NIL; last :=NIL;
		WITH loc: Locator DO
			res := Do(loc.path);
			firstRes := res;
			(* check custom directory *)
			IF (isUseDir & isCustomDir) & (res IN {ok, fileNotFoundErr, pathNotFoundErr}) THEN
				GetCustomDir(loc, s);
				res := Do(s);
			END;
			(* check startup directory *)
			IF (isUseDir OR isCustomDir) & (res IN {ok, fileNotFoundErr, pathNotFoundErr}) THEN
				GetStartupDir(loc, s);
				res := Do(s);
			END;
			IF (res = fileNotFoundErr) OR (res = pathNotFoundErr) THEN res := firstRes END;
			loc.res := Error(res)
		ELSE loc.res := invalidName
		END;
		RETURN first
	END LocList;

	PROCEDURE (d: Directory) GetFileName (name: Files.Name; type: Files.Type; OUT filename: Files.Name);
	BEGIN
		Append("", name, type, LEN(filename), filename)
	END GetFileName;

	(** Miscellaneous **)

	PROCEDURE (VAR id: Counter) Identified (): BOOLEAN;
		VAR f: File;
	BEGIN
		f := id.obj(File);
		IF f.state # closed THEN INC(id.count) END;
		RETURN FALSE
	END Identified;

	PROCEDURE NofFiles* (): INTEGER;
		VAR p: ANYPTR; cnt: Counter;
	BEGIN
		cnt.typ := SYSTEM.TYP(File);
		cnt.count := 0; p := Kernel.ThisFinObj(cnt);
		RETURN cnt.count
	END NofFiles;

	PROCEDURE GetModDate* (f: Files64.File; VAR year, month, day, hour, minute, second: INTEGER);
		VAR res: INTEGER; ft: WinApi.FILETIME; st: WinApi.SYSTEMTIME;
	BEGIN
		ASSERT(f IS File, 20);
		res := WinApi.GetFileTime(f(File).ref, NIL, NIL, ft);
		res := WinApi.FileTimeToSystemTime(ft, st);
		year := st.wYear; month := st.wMonth; day := st.wDay;
		hour := st.wHour; minute := st.wMinute; second := st.wSecond
	END GetModDate;

	PROCEDURE Init;
		CONST
			bbCustomDir = "CUSTOM";
			bbUseDir = "USE";
		VAR res, res1, i, j: INTEGER; path, opt, s: FullName; attr: SET; p: WinApi.PtrWSTR;
	BEGIN
		wildcard := "*"; NEW(dir);
		res := WinApi.GetModuleFileNameW(0, path, LEN(path));
		GetPath(path, startupDir);
		i := LEN(startupDir$); startupLen := i;
		
		isCustomDir := FALSE;
		WinEnv.GetPar(bbCustomDir, path, res);
		IF res = 0 THEN
			res := 3;	(* error *)
			res1 := WinApi.ExpandEnvironmentStringsW(path, s, LEN(s) - 2);
			IF (res1 = 0) OR (res1 > LEN(s) - 2) THEN s := path ELSE path := s$ END;
			res1 := WinApi.GetFullPathNameW(s, LEN(path), path, NIL);
			attr := BITS(WinApi.GetFileAttributesW(path));
			IF (attr # {0..MAX(SET)}) & (WinApi.FILE_ATTRIBUTE_DIRECTORY * attr # {}) THEN
				res := 0
			ELSIF (path[1] = ":") & ((path[2] = 0X) OR (path[2] = "\") & (path[3] = 0X))
				& (WinApi.GetDriveTypeW(s) >= 2) THEN res := 0
			END
		END;
		IF res = 0 THEN
			i := LEN(path$);
			IF (path[i-1] = "/") OR (path[i-1] = "\") THEN DEC(i); path[i] := 0X END;
			customDir := path; customLen := i;
			isCustomDir := TRUE;
		END;
		
		isUseDir := FALSE;
		WinEnv.GetPar(bbUseDir, path, res);
		IF res = 0 THEN
			res := 3;	(* error *)
			res1 := WinApi.ExpandEnvironmentStringsW(path, s, LEN(s) - 2);
			IF (res1 = 0) OR (res1 > LEN(s) - 2) THEN s := path ELSE path := s$ END;
			res1 := WinApi.GetFullPathNameW(s, LEN(path), path, NIL);
			attr := BITS(WinApi.GetFileAttributesW(path));
			IF (attr # {0..MAX(SET)}) & (WinApi.FILE_ATTRIBUTE_DIRECTORY * attr # {}) THEN
				res := 0
			ELSIF (path[1] = ":") & ((path[2] = 0X) OR (path[2] = "\") & (path[3] = 0X))
				& (WinApi.GetDriveTypeW(s) >= 2) THEN res := 0
			END
		END;
		IF res = 0 THEN
			i := LEN(path$);
			IF (path[i-1] = "/") OR (path[i-1] = "\") THEN DEC(i); path[i] := 0X END;
			isUseDir := TRUE;
		END;
		
		res := WinApi.GetTempPathW(LEN(path), path);
		dir.temp := HostFiles.NewLocator(path);
		Files64.SetDir(dir)
	END Init;
	
BEGIN
	Init
END WinFiles64.
