home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-05 | 9.3 KB | 381 lines | [TEXT/PJMM] |
- unit MyFileSystemUtils;
-
- interface
-
- {$IFC undefined THINK_Pascal}
- uses
- Files, Types;
- {$ENDC}
-
- procedure MyResolveAliasFile (var fs: FSSpec);
- function MyGetCatInfo (vrn: integer; dirID: longInt; var name: string; index: integer; var pb: CInfoPBRec): OSErr;
- function FSpGetCatInfo (var fs: FSSpec; index: integer; var pb: CInfoPBRec): OSErr;
- function FSpSetCatInfo (var fs: FSSpec; var pb: CInfoPBRec): OSErr;
- function MyFSMakeFSSpec (vrn: integer; dirID: longInt; name: str255; var fs: FSSpec): OSErr;
- procedure MyGetModDate (var fs: FSSpec; var moddate: longInt);
- function DuplicateFile (var org, new: FSSpec): OSErr;
- function CopyData (src, dst: integer; len: longInt): OSErr;
- function TouchDir (fs: FSSpec): OSErr;
- function TouchFolder (vrn: integer; dirID: longInt): OSErr;
- function CreateUniqueFile (var fs: FSSpec; creator, ftype: OSType): OSErr;
- function MyFSReadLineEOL (refnum: integer; ch: char; var s: str255): OSErr;
- function MyFSReadLine (refnum: integer; var s: str255): OSErr;
- function MyFSReadLineAt (refnum: integer; pos: longInt; var s: str255): OSErr;
- function MyFSWrite (refnum: integer; len: longInt; p: ptr): OSErr;
- function MyFSWriteAt (refnum: integer; mode: integer; pos, len: longInt; p: ptr): OSErr;
- function MyFSReadAt (refnum: integer; pos, len: longInt; p: ptr): OSErr;
- function FSSpecToFullPath (fs: FSSpec; var path: str255): OSErr;
-
- implementation
-
- uses
- {$IFC undefined THINK_Pascal}
- Errors, Packages, GestaltEqu,
- {$ENDC}
- Aliases, MyTypes, MyStrings;
-
- function FSSPecToFullPath (fs: FSSpec; var path: str255): OSErr;
- var
- err: OSErr;
- pb: CInfoPBRec;
- begin
- err := FSMakeFSSpec(fs.vRefNum, fs.parID, fs.name, fs);
- if err = noErr then begin
- if fs.parID = 1 then begin
- path := concat(fs.name, ':');
- end
- else begin
- path := fs.name;
- while (err = nOErr) & (fs.parID <> 1) do begin
- err := MyGetCatInfo(fs.vRefNum, fs.parID, fs.name, -1, pb);
- path := concat(fs.name, ':', path);
- fs.parID := pb.ioFlParID;
- end;
- end;
- end;
- FSSPecToFullPath := err;
- end;
-
- function TouchDir (fs: FSSpec): OSErr;
- var
- pb: CInfoPBRec;
- err: OSErr;
- begin
- if fs.name = '' then begin
- TouchDir := TouchFolder(fs.vRefNum, fs.parID);
- end
- else begin
- pb.ioVRefNum := fs.vRefNum;
- pb.ioDirID := fs.parID;
- pb.ioNamePtr := @fs.name;
- pb.ioFDirIndex := 0;
- err := PBGetCatInfoSync(@pb);
- if err = noErr then begin
- pb.ioNamePtr := nil;
- GetDateTime(pb.ioDrMdDat);
- err := PBSetCatInfoSync(@pb);
- end;
- TouchDir := err;
- end;
- end;
-
- function TouchFolder (vrn: integer; dirID: longInt): OSErr;
- var
- pb: CInfoPBRec;
- err: OSErr;
- begin
- pb.ioVRefNum := vrn;
- pb.ioDirID := dirID;
- pb.ioNamePtr := nil;
- pb.ioFDirIndex := -1;
- err := PBGetCatInfoSync(@pb);
- if err = noErr then begin
- pb.ioVRefNum := vrn;
- pb.ioDirID := dirID;
- pb.ioNamePtr := nil;
- GetDateTime(pb.ioDrMdDat);
- err := PBSetCatInfoSync(@pb);
- end;
- TouchFolder := err;
- end;
-
- function CreateUniqueFile (var fs: FSSpec; creator, ftype: OSType): OSErr;
- var
- oname: str31;
- n: str255;
- i: integer;
- oe: OSErr;
- begin
- oname := fs.name;
- LimitStringLength(oname, 27, '…');
- oe := HCreate(fs.vRefNum, fs.parID, fs.name, creator, ftype);
- i := 1;
- while oe = dupFNErr do begin
- NumToString(i, n);
- fs.name := concat(oname, '#', n);
- oe := HCreate(fs.vRefNum, fs.parID, fs.name, creator, ftype);
- i := i + 1;
- end;
- CreateUniqueFile := oe;
- end;
-
- function MyFSReadAt (refnum: integer; pos, len: longInt; p: ptr): OSErr;
- var
- pb: ParamBlockRec;
- oe: OSErr;
- begin
- pb.ioRefNum := refnum;
- pb.ioBuffer := p;
- pb.ioReqCount := len;
- pb.ioPosMode := fsFromStart;
- pb.ioPosOffset := pos;
- oe := PBReadSync(@pb);
- if (oe = noErr) & (pb.ioActCount <> len) then begin
- oe := -1;
- end;
- MyFSReadAt := oe;
- end;
-
- function MyFSReadLineEOL (refnum: integer; ch: char; var s: str255): OSErr;
- var
- pb: ParamBlockRec;
- err: OSErr;
- begin
- pb.ioRefNum := refnum;
- {$PUSH}
- {$R-}
- pb.ioBuffer := @s[1];
- pb.ioReqCount := SizeOf(s) - 1;
- pb.ioPosMode := fsFromMark + fsNewLine + BSL(ord(ch), 8);
- pb.ioPosOffset := 0;
- err := PBReadSync(@pb);
- if (err = eofErr) & (pb.ioActCount > 0) then begin
- err := noErr;
- end;
- if err = noErr then begin
- if s[pb.ioActCount] = ch then begin
- pb.ioActCount := pb.ioActCount - 1;
- end;
- s[0] := chr(pb.ioActCount);
- end;
- {$POP}
- MyFSReadLineEOL := err;
- end;
-
- function MyFSReadLine (refnum: integer; var s: str255): OSErr;
- begin
- MyFSReadLine := MyFSReadLineEOL(refnum, cr, s);
- end;
-
- function MyFSReadLineAt (refnum: integer; pos: longInt; var s: str255): OSErr;
- var
- pb: ParamBlockRec;
- err: OSErr;
- begin
- pb.ioRefNum := refnum;
- {$PUSH}
- {$R-}
- pb.ioBuffer := @s[1];
- pb.ioReqCount := SizeOf(s) - 1;
- pb.ioPosMode := fsFromStart + fsNewLine + BSL(ord(cr), 8);
- pb.ioPosOffset := pos;
- err := PBReadSync(@pb);
- if (err = eofErr) & (pb.ioActCount > 0) then begin
- err := noErr;
- end;
- if err = noErr then begin
- s[0] := chr(pb.ioActCount - 1);
- end;
- {$POP}
- MyFSReadLineAt := err;
- end;
-
- function MyFSWrite (refnum: integer; len: longInt; p: ptr): OSErr;
- var
- oe: OSErr;
- count: longInt;
- begin
- oe := noErr;
- if len > 0 then begin
- count := len;
- oe := FSWrite(refnum, count, p);
- if (oe = noErr) & (count <> len) then
- oe := -1;
- end;
- MyFSWrite := oe;
- end;
-
- procedure MyResolveAliasFile (var fs: FSSpec);
- var
- isfolder, wasalias: boolean;
- temp: FSSpec;
- gv: longInt;
- oe: OSErr;
- begin
- if (Gestalt(gestaltAliasMgrAttr, gv) = noErr) & (BTST(gv, gestaltAliasMgrPresent)) then begin
- temp := fs;
- oe := ResolveAliasFile(fs, true, isfolder, wasalias);
- if oe <> noErr then
- fs := temp;
- end;
- end;
-
- function MyGetCatInfo (vrn: integer; dirID: longInt; var name: string; index: integer; var pb: CInfoPBRec): OSErr;
- begin
- pb.ioVRefNum := vrn;
- pb.ioDirID := dirID;
- pb.ioNamePtr := @name;
- pb.ioFDirIndex := index;
- MyGetCatInfo := PBGetCatInfoSync(@pb);
- end;
-
- function FSpGetCatInfo (var fs: FSSpec; index: integer; var pb: CInfoPBRec): OSErr;
- begin
- pb.ioVRefNum := fs.vRefNum;
- pb.ioDirID := fs.parID;
- pb.ioNamePtr := @fs.name;
- pb.ioFDirIndex := index;
- FSpGetCatInfo := PBGetCatInfoSync(@pb);
- end;
-
- function FSpSetCatInfo (var fs: FSSpec; var pb: CInfoPBRec): OSErr;
- begin
- pb.ioVRefNum := fs.vRefNum;
- pb.ioDirID := fs.parID;
- pb.ioNamePtr := @fs.name;
- FSpSetCatInfo := PBSetCatInfoSync(@pb);
- end;
-
- function MyFSMakeFSSpec (vrn: integer; dirID: longInt; name: str255; var fs: FSSpec): OSErr;
- var
- pb: CInfoPBRec;
- oe: OSErr;
- gv: longInt;
- begin
- if (Gestalt(gestaltFSAttr, gv) = noErr) & (BTST(gv, gestaltHasFSSpecCalls)) then begin
- oe := FSMakeFSSpec(vrn, dirID, name, fs);
- end
- else begin
- oe := MyGetCatInfo(vrn, dirID, name, 0, pb);
- if (oe = noErr) then begin
- fs.vRefNum := pb.ioVRefNum;
- fs.parID := pb.ioFlParID;
- fs.name := name;
- end;
- end;
- MyFSMakeFSSpec := oe;
- end;
-
- procedure MyGetModDate (var fs: FSSpec; var moddate: longInt);
- var
- oe: OSErr;
- pb: CInfoPBRec;
- begin
- oe := MyGetCatInfo(fs.vRefNum, fs.parID, fs.name, 0, pb);
- if oe = noErr then begin
- moddate := pb.ioFlMdDat
- end
- else begin
- moddate := $80000000;
- end;
- end;
-
- function CopyData (src, dst: integer; len: longInt): OSErr;
- const
- buffer_len = 4096;
- var
- buffer: array[1..buffer_len] of signedByte;
- l: longInt;
- oe: OSErr;
- begin
- oe := noErr;
- while (len > 0) & (oe = noErr) do begin
- if len > SizeOf(buffer) then
- l := SizeOf(buffer)
- else
- l := len;
- oe := FSRead(src, l, @buffer);
- if (l = 0) & (oe = noErr) then
- oe := -1;
- if oe = noErr then
- oe := MyFSWrite(dst, l, @buffer);
- len := len - l;
- end;
- CopyData := oe;
- end;
-
- function DuplicateFile (var org, new: FSSpec): OSErr;
- var
- oe, ooe: OSErr;
- fi: FInfo;
- pb: CInfoPBRec;
- orn, nrn: integer;
- rlen, dlen: longInt;
- begin
- oe := FSpGetFInfo(org, fi);
- if oe = noErr then
- oe := FSpCreate(new, fi.fdCreator, fi.fdType, 0);
- if oe = noErr then begin
- oe := MyGetCatInfo(org.vRefNum, org.parID, org.name, 0, pb);
- if oe = noErr then begin
- dlen := pb.ioFlLgLen;
- rlen := pb.ioFlRLgLen;
- pb.ioVRefNum := new.vRefNum;
- pb.ioDirID := new.parID;
- pb.ioNamePtr := @new.name;
- pb.ioFDirIndex := 0;
- oe := PBGetCatInfoSync(@pb);
- end;
-
- if oe = noErr then begin
- oe := FSpOpenDF(org, fsRdPerm, orn);
- if oe = noErr then begin
- oe := FSpOpenDF(new, fsWrPerm, nrn);
- if oe = noErr then begin
- oe := CopyData(orn, nrn, dlen);
- ooe := FSClose(nrn);
- if oe = noErr then
- ooe := oe;
- end;
- ooe := FSClose(orn);
- end;
- end;
-
- if oe = noErr then begin
- oe := FSpOpenRF(org, fsRdPerm, orn);
- if oe = noErr then begin
- oe := FSpOpenRF(new, fsWrPerm, nrn);
- if oe = noErr then begin
- oe := CopyData(orn, nrn, rlen);
- ooe := FSClose(nrn);
- if oe = noErr then
- ooe := oe;
- end;
- ooe := FSClose(orn);
- end;
- end;
-
- if oe <> noErr then
- ooe := FSpDelete(new);
- end;
- DuplicateFile := oe;
- end;
-
- function MyFSWriteAt (refnum: integer; mode: integer; pos, len: longInt; p: ptr): OSErr;
- var
- pb: ParamBlockRec;
- oe: OSErr;
- begin
- pb.ioRefNum := refnum;
- pb.ioBuffer := p;
- pb.ioReqCount := len;
- pb.ioPosMode := mode;
- pb.ioPosOffset := pos;
- oe := PBWriteSync(@pb);
- if (oe = noErr) & (pb.ioActCount <> len) then begin
- oe := -1;
- end;
- MyFSWriteAt := oe;
- end;
-
- end.