From 6da9dfae2174594c03a53e703d358d4d7bb1c1b4 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 26 Apr 1999 18:27:26 +0000 Subject: [PATCH] * fixed write array * read array with maxlen --- rtl/inc/text.inc | 186 +++++++++-------------------------------------- 1 file changed, 33 insertions(+), 153 deletions(-) diff --git a/rtl/inc/text.inc b/rtl/inc/text.inc index f3951ebd69..6c24e20345 100644 --- a/rtl/inc/text.inc +++ b/rtl/inc/text.inc @@ -448,11 +448,14 @@ Begin End; -Type - array00 = array[0..0] Of Char; -Procedure Write_Array(Len : Longint;var f : TextRec;const p : array00);[Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_ARRAY']; +{$ifndef NEWWRITEARRAY} +type + array00=array[0..0] of char; +{$endif} +Procedure Write_Array(Len : Longint;var f : TextRec;const s : {$ifdef NEWWRITEARRAY} array of char{$else}array00{$endif});[Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_ARRAY']; var ArrayLen : longint; + p : pchar; Begin If (InOutRes<>0) then exit; @@ -461,10 +464,13 @@ Begin InOutRes:=105; exit; end; + p:=pchar(@s); ArrayLen:=StrLen(p); + if ArrayLen>high(s) then + ArrayLen:=high(s); If Len>ArrayLen Then WriteBlanks(f,Len-ArrayLen); - WriteBuffer(f,p,ArrayLen); + WriteBuffer(f,p^,ArrayLen); End; @@ -733,14 +739,12 @@ Begin End; -Procedure Read_String(var f : TextRec;var s : String);[Public,Alias:'FPC_READ_TEXT_'+{$ifdef NEWREADINT}'SHORTSTR'{$else}'STRING'{$endif}]; +Function ReadPCharLen(var f:TextRec;s:pchar;maxlen:longint):longint; var - maxlen, sPos,len : Longint; p,startp,maxp : pchar; Begin -{ Delete the string } - s:=''; + ReadPCharLen:=0; { Check error and if file is open } If (InOutRes<>0) then exit; @@ -751,7 +755,6 @@ Begin end; { Read maximal until Maxlen is reached } sPos:=0; - MaxLen:=high(s); repeat If f.BufPos>=f.BufEnd Then begin @@ -771,12 +774,12 @@ Begin { calculate read bytes } len:=p-startp; inc(f.BufPos,Len); - Move(startp^,s[sPos+1],Len); + Move(startp^,s[sPos],Len); inc(sPos,Len); { was it a LF? then leave } if (p0) and (s[spos]=#13) then + if (spos>0) and (s[spos-1]=#13) then dec(sPos); break; end; @@ -784,166 +787,39 @@ Begin if spos=MaxLen then break; until false; -{ Set final length } - s[0]:=chr(sPos); + ReadPCharLen:=spos; +End; + + +Procedure Read_String(var f : TextRec;var s : String);[Public,Alias:'FPC_READ_TEXT_'+{$ifdef NEWREADINT}'SHORTSTR'{$else}'STRING'{$endif}]; +Begin + s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s))); End; Procedure Read_PChar(var f : TextRec;var s : PChar);[Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER']; -var - p,maxp,startp,sidx : PChar; - len : longint; Begin -{ Delete the string } - s^:=#0; -{ Check error and if file is open } - If (InOutRes<>0) then - exit; - if (f.mode<>fmInput) Then - begin - InOutRes:=104; - exit; - end; -{ Read until #10 is found } - sidx:=s; - repeat - If f.BufPos>=f.BufEnd Then - begin - FileFunc(f.InOutFunc)(f); - If f.BufPos>=f.BufEnd Then - break; - end; - p:=@f.Bufptr^[f.BufPos]; - maxp:=@f.Bufptr^[f.BufEnd]; - startp:=p; - { search linefeed } - while (p#10) do - inc(p); - { calculate read bytes } - len:=p-startp; - inc(f.BufPos,Len); - { update output string, take MaxLen into count } - Move(startp^,sidx^,Len); - inc(sidx,len); - { was it a LF? then leave } - if (p0) then - exit; - if (f.mode<>fmInput) Then - begin - InOutRes:=104; - exit; - end; -{ Read until #10 is found } - sidx:=pchar(@s); - repeat - If f.BufPos>=f.BufEnd Then - begin - FileFunc(f.InOutFunc)(f); - If f.BufPos>=f.BufEnd Then - break; - end; - p:=@f.Bufptr^[f.BufPos]; - maxp:=@f.Bufptr^[f.BufEnd]; - startp:=p; - { search linefeed } - while (p#10) do - inc(p); - { calculate read bytes } - len:=p-startp; - inc(f.BufPos,Len); - { update output string, take MaxLen into count } - Move(startp^,sidx^,Len); - inc(sidx,len); - { was it a LF? then leave } - if (p0) then - exit; - if (f.mode<>fmInput) Then - begin - InOutRes:=104; - exit; - end; -{ Read until #10 is found } - sidx:=pchar(s); - spos:=0; - repeat - If f.BufPos>=f.BufEnd Then - begin - FileFunc(f.InOutFunc)(f); - If f.BufPos>=f.BufEnd Then - break; - end; - p:=@f.Bufptr^[f.BufPos]; - if SPos+f.BufEnd-f.BufPos>MaxLen then - maxp:=@f.BufPtr^[f.BufPos+MaxLen-SPos] - else - maxp:=@f.Bufptr^[f.BufEnd]; - startp:=p; - { search linefeed } - while (p#10) do - inc(p); - { calculate read bytes } - len:=p-startp; - inc(f.BufPos,Len); - Move(startp^,sidx^,Len); - inc(sidx,len); - inc(spos,len); - { was it a LF? then leave } - if (p