home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-05 | 2.6 KB | 115 lines | [TEXT/PJMM] |
- unit MyHandleFile;
-
- interface
-
- uses
- MyTypes;
-
- type
- HandleFile = record
- data: handle; { The data }
- pos: longInt; { current position in handle }
- crlf: CRLFTypes; { Only used for output, reading will handle either case }
- error: OSErr; { Cumulative error }
- end;
- { You are free to modify any and all fields, keep 0<=pos<GetHandleSize(data) }
-
- procedure CreateHandleFile (var hf: HandleFile; le: CRLFTypes);
- procedure DestroyHandleFile (var hf: HandleFile);
- procedure WriteToHandleFile (var hf: HandleFile; s: str255); { Write string at pos into data }
- function ReadLongLineFromHandle (var hf: HandleFile; var off, len: longInt): boolean;
- function ReadFromHandleFile (var hf: HandleFile; var s: str255): boolean;
- { Read string from pos in data, update pos, return true unless eohandle }
-
- implementation
-
- {$IFC undefined THINK_Pascal}
- uses
- Memory, ToolUtils;
- {$ENDC}
-
- procedure CreateHandleFile (var hf: HandleFile; le: CRLFTypes);
- begin
- with hf do begin
- data := NewHandle(0);
- pos := 0;
- crlf := le;
- error := MemError;
- end;
- end;
-
- procedure DestroyHandleFile (var hf: HandleFile);
- begin
- DisposeHandle(hf.data);
- hf.data := nil;
- end;
-
- procedure WriteToHandleFile (var hf: HandleFile; s: str255);
- var
- ret: longInt;
- begin
- with hf do begin
- case crlf of
- CL_CR:
- s := concat(s, cr);
- CL_LF:
- s := concat(s, lf);
- CL_CRLF:
- s := concat(s, cr, lf);
- end;
- ret := Munger(data, pos, nil, 0, @s[1], length(s));
- if ret >= 0 then
- pos := ret
- else if error = noErr then
- error := ret;
- end;
- end;
-
- function ReadLongLineFromHandle (var hf: HandleFile; var off, len: longInt): boolean;
- var
- size: longInt;
- p: ptr;
- begin
- with hf do begin
- size := GetHandleSize(data);
- ReadLongLineFromHandle := pos < size;
- off := pos;
- p := ptr(ord(data^) + pos);
- while (pos < size) & (p^ <> 13) & (p^ <> 10) do begin
- pos := pos + 1;
- longInt(p) := longInt(p) + 1;
- end;
- len := pos - off;
- if (pos < size) & (p^ = 13) then begin
- pos := pos + 1;
- longInt(p) := longInt(p) + 1;
- end;
- if (pos < size) & (p^ = 10) then begin
- pos := pos + 1;
- longInt(p) := longInt(p) + 1;
- end;
- end;
- end;
-
- function ReadFromHandleFile (var hf: HandleFile; var s: str255): boolean;
- var
- off, len: longInt;
- p: ptr;
- gotline: boolean;
- begin
- s := '';
- gotline := ReadLongLineFromHandle(hf, off, len);
- if gotline then begin
- p := ptr(ord(hf.data^) + off);
- if len > 255 then
- len := 255;
- {$PUSH}
- {$R-}
- s[0] := chr(len);
- BlockMove(p, @s[1], len);
- {$POP}
- end;
- ReadFromHandleFile := gotline;
- end;
-
- end.