unit pcFileIO; { This unit provides a subclass of the fileIO class that supports generation of } { DOS-compatible files (line feeds with carriage returns and proper numeric } { byte ordering). } { } { This unit requires the fileIO unit (see fileIO.p for descriptions of the functions } { provided by this unit). } interface uses objIntf, Types, Files, Devices, fileIO; type cPCFileObj = object(cFileObj) function readLine (var theLine: str255): OSErr; override; function writeLine (theLine: str255): OSErr; override; function readInteger (var theInteger: integer): OSErr; override; function writeInteger (theInteger: integer): OSErr; override; function readLongint (var theLongint: longint): OSErr; override; function writeLongint (theLongint: longint): OSErr; override; function readSingle (var theSingle: real): OSErr; override; function writeSingle (theSingle: real): OSErr; override; function readDouble (var theDouble: double): OSErr; override; function writeDouble (theDouble: double): OSErr; override; end; { Utilities } { These routines are provided for the convenience of the programmer } { generating peecee-compatible applications. Do not call them when using } { the read/write methods of a cPCFileObj object. } function swapByteOrderInt (theInt: integer): integer; function swapByteOrderLong (theLong: longint): longint; function swapByteOrderReal (theReal: real): real; function swapByteOrderDouble (theDouble: double): double; implementation var param: paramBlockRec; function swapByteOrderInt (theInt: integer): integer; type swapIntType = packed record case boolean of true: (i: integer); false: (b: packed array [0..1] of char); end; var temp: swapIntType; dummy: char; begin temp.i := theInt; dummy := temp.b[0]; temp.b[0] := temp.b[1]; temp.b[1] := dummy; swapByteOrderInt := temp.i; end; function swapByteOrderLong (theLong: longint): longint; type swapIntType = packed record case boolean of true: (i: longint); false: (b: packed array [0..3] of char); end; var temp: swapIntType; dummy: char; begin temp.i := theLong; dummy := temp.b[0]; temp.b[0] := temp.b[3]; temp.b[3] := dummy; dummy := temp.b[1]; temp.b[1] := temp.b[2]; temp.b[2] := dummy; swapByteOrderLong := temp.i; end; function swapByteOrderReal (theReal: real): real; type swapRealType = packed record case boolean of true: (r: real); false: (i: longint); end; var temp: swapRealType; begin temp.r := theReal; temp.i := swapByteOrderLong(temp.i); swapByteOrderReal := temp.r; end; function swapByteOrderDouble (theDouble: double): double; type swapDoubleType = packed record case boolean of true: (d: double); false: (b: packed array [0..7] of char); end; var temp: swapDoubleType; dummy: char; begin temp.d := theDouble; dummy := temp.b[0]; temp.b[0] := temp.b[7]; temp.b[7] := dummy; dummy := temp.b[1]; temp.b[1] := temp.b[6]; temp.b[6] := dummy; dummy := temp.b[2]; temp.b[1] := temp.b[5]; temp.b[5] := dummy; dummy := temp.b[3]; temp.b[1] := temp.b[4]; temp.b[4] := dummy; swapByteOrderDouble := temp.d; end; function cPCFileObj.readLine (var theLine: str255): OSErr; var LF: string[1]; begin hLock(handle(SELF)); with param do begin ioCompletion := nil; ioPermssn := fsCurPerm; ioRefNum := refNum; ioBuffer := @theLine[1]; { read into theLine } ioReqCount := 255; { ask for a whole string worth } ioPosMode := $0D80; { stop @ newline char (#13) } ioPosOffset := 0; end; lastErr := PBReadSync(@param); if param.ioActCount > 0 then begin theLine[0] := chr(param.ioActCount); if theLine[length(theLine)] = chr(13) then begin theLine[0] := chr(length(theLine) - 1); if lastErr = noErr then begin with param do begin ioCompletion := nil; ioPermssn := fsCurPerm; ioRefNum := refNum; ioBuffer := @LF[1]; ioReqCount := 1; ioPosMode := fsAtMark; ioPosOffset := 0; end; lastErr := PBReadSync(@param); end; end; end else theLine := ''; readLine := lastErr; hUnlock(handle(SELF)); end; function cPCFileObj.writeLine (theLine: str255): OSErr; var CR: string[1]; begin hLock(handle(SELF)); with param do begin ioCompletion := nil; ioPermssn := fsCurPerm; ioRefNum := refNum; ioBuffer := @theLine[1]; { write from theLine } ioReqCount := length(theLine); ioPosMode := fsAtMark; ioPosOffset := 0; end; lastErr := PBWriteSync(@param); if lastErr = noErr then { append carriage return } begin CR := ' '; CR[1] := chr(13); param.ioBuffer := @CR[1]; param.ioReqCount := 1; param.ioPosMode := fsAtMark; param.ioPosOffset := 0; lastErr := PBWriteSync(@param); end; if lastErr = noErr then { append line feed } begin CR := ' '; CR[1] := chr(10); param.ioBuffer := @CR[1]; param.ioReqCount := 1; param.ioPosMode := fsAtMark; param.ioPosOffset := 0; lastErr := PBWriteSync(@param); end; writeLine := lastErr; hUnlock(handle(SELF)); end; function cPCFileObj.readInteger (var theInteger: integer): OSErr; var numBytes: longint; temp: integer; begin numBytes := sizeof(integer); lastErr := FSRead(refNum, numBytes, @temp); theInteger := swapByteOrderInt(temp); readInteger := lastErr; end; function cPCFileObj.writeInteger (theInteger: integer): OSErr; var numBytes: longint; temp: integer; begin numBytes := sizeof(integer); temp := swapByteOrderInt(theInteger); lastErr := FSWrite(refNum, numBytes, @temp); writeInteger := lastErr; end; function cPCFileObj.readLongint (var theLongint: longint): OSErr; var numBytes: longint; temp: longint; begin numBytes := sizeof(longint); lastErr := FSRead(refNum, numBytes, @temp); theLongint := swapByteOrderLong(temp); readLongint := lastErr; end; function cPCFileObj.writeLongint (theLongint: longint): OSErr; var numBytes: longint; temp: longint; begin numBytes := sizeof(longint); temp := swapByteOrderLong(theLongint); lastErr := FSWrite(refNum, numBytes, @temp); writeLongint := lastErr; end; function cPCFileObj.readSingle (var theSingle: real): OSErr; var numBytes: longint; temp: real; begin numBytes := sizeof(real); lastErr := FSRead(refNum, numBytes, @temp); theSingle := swapByteOrderReal(temp); readSingle := lastErr; end; function cPCFileObj.writeSingle (theSingle: real): OSErr; var numBytes: longint; temp: real; begin numBytes := sizeof(real); temp := swapByteOrderReal(theSingle); lastErr := FSWrite(refNum, numBytes, @temp); writeSingle := lastErr; end; function cPCFileObj.readDouble (var theDouble: double): OSErr; var numBytes: longint; temp: double; begin numBytes := sizeof(double); lastErr := FSRead(refNum, numBytes, @temp); theDouble := swapByteOrderDouble(temp); readDouble := lastErr; end; function cPCFileObj.writeDouble (theDouble: double): OSErr; var numBytes: longint; temp: double; begin numBytes := sizeof(double); temp := swapByteOrderDouble(theDouble); lastErr := FSWrite(refNum, numBytes, @temp); writeDouble := lastErr; end; end.