Recent

Author Topic: Readstr with multple input  (Read 491 times)

jamie

  • Hero Member
  • *****
  • Posts: 7651
Readstr with multple input
« on: April 04, 2026, 12:39:03 pm »
Does ReadStr support chainned input? I know writestr does but it seems readstr does not?
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: Readstr with multple input
« Reply #1 on: April 04, 2026, 03:49:59 pm »
Ok, I figured it out, but I don't have the expected results as I would have thought.

There are no formatting options like in the WriteStr where you can use the xx:xx etc to indicated trailing 0's and left spaces.

I am trying to write a helper for the TStream system to closely represent The C++ streaming of characters << and >>.

If the Tstream was in a RECORD I could make operator overloads, but this isn't the case, I have come close however with a helper to chain the outputs, it's the inputs that are getting to look like a mess.

Jamie

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7651
Re: Readstr with multple input
« Reply #2 on: April 04, 2026, 06:50:51 pm »
I made a Helper for TStream, its not complete yet but it will server me well to implement the << >> cout and Cin streams in C++
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$modeswitch TypeHelpers}
  5.  
  6. interface
  7. uses
  8.   Classes, SysUtils;
  9. Type
  10.  
  11. { TStreamHelper }
  12.  
  13. TStreamHelper = Class Helper for TStream
  14.  private
  15.  Function ParseToSpace:String;
  16.  Public
  17.  Function Cout(S:String):TStream;
  18.  Function Cout(B:Byte):TStream;
  19.  Function Cin(Var B:Byte):TStream;
  20.  Function Cin(Var B:Integer):TStream;
  21. End;
  22. implementation
  23.  
  24. { TStreamHelper }
  25.  
  26. function TStreamHelper.ParseToSpace: String;
  27. Var
  28.   B:Byte;
  29. begin
  30.   While Position < Size Do
  31.    Begin
  32.     B:=ReadByte;
  33.     If (Char(B)=' ')and (Result<>'')and(Result[Length(Result)]<>' ') then Exit;
  34.     Result := Result+Char(B);
  35.    end;
  36. end;
  37.  
  38. function TStreamHelper.Cout(S: String):TStream;
  39. begin
  40.   Write(S,Length(S));
  41.   result := Self;
  42. end;
  43.  
  44. function TStreamHelper.Cout(B: Byte): TStream;
  45. begin
  46.  Cout(B.Tostring);
  47.  Result := Self;
  48. end;
  49.  
  50. function TStreamHelper.Cin(var B: Byte): TStream;
  51. begin
  52.   B:=ParseToSpace.ToInteger;
  53.   Result := Self;
  54. end;
  55.  
  56. function TStreamHelper.Cin(var B: Integer): TStream;
  57. begin
  58.   B:=ParseToSpace.ToInteger;
  59.   Result := Self;
  60. end;
  61.  
  62. end.
  63.  

And in some sample code I did just for testing.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   S:TMemoryStream;
  4.   A,B:Integer;
  5.   C,D:Byte;
  6. begin
  7.   S := TMemoryStream.Create;
  8.   S.Cout('The holy Crap ').Cout(255);
  9.   //The Stream pointer needs to be moved back for the following ofcourse.
  10.   S.Cin(C).Cin(A).Cin(D);
  11.   S.Free;
  12. end;                                                                
  13.  
  14.  

That is abount as close as I can get to the C++ << >>, actually, I think it looks better than C++  :D
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2480
Re: Readstr with multple input
« Reply #3 on: April 04, 2026, 08:33:40 pm »
C like:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$LONGSTRINGS ON}
  3. {$IFDEF WINDOWS}
  4.   {$APPTYPE CONSOLE}
  5. {$ENDIF}
  6. {$MODESWITCH ADVANCEDRECORDS}
  7.  
  8. uses SysUtils, Classes;
  9.  
  10. type
  11.   TCout = record
  12.   public
  13.     class operator < (const Left: TCout; const S: string): TCout;
  14.     class operator < (const Left: TCout; const N: SizeInt): TCout;
  15.   end;
  16.  
  17.   TCin = record
  18.   public
  19.     class operator > (const Left: TCin; out N: SizeInt): TCin;
  20.     class operator > (const Left: TCin; out S: string): TCin;
  21.   end;
  22.  
  23. var
  24.   cout: TCout = ();
  25.   cin: TCin = ();
  26.  
  27. { TCin }
  28.  
  29. class operator TCin.>(const Left: TCin; out N: SizeInt): TCin;
  30. begin
  31.   Result := Left;
  32.   Read(N);
  33. end;
  34.  
  35. class operator TCin.>(const Left: TCin; out S: string): TCin;
  36. begin
  37.   Result := Left;
  38.   Readln(S);
  39.   S := TrimLeft(S);
  40. end;
  41.  
  42. { TCout }
  43.  
  44. class operator TCout.<(const Left: TCout; const N: SizeInt): TCout;
  45. begin
  46.   Result := Left;
  47.   Write(N, ' ');
  48. end;
  49.  
  50. class operator TCout.<(const Left: TCout; const S: string): TCout;
  51. begin
  52.   Result := Left;
  53.   Writeln(S);
  54. end;
  55.  
  56. var
  57.   S: string;
  58.   N1, N2: SizeInt;
  59. begin
  60.   cout := cout < 'First line' < 'Last line' < 123 < 567 < '';
  61.   cin := cin > N1 > N2 > S;
  62.   cout := cout < N1 < N2 < S < 'Press Enter to exit';
  63.   Readln;
  64. end.

ASerge

  • Hero Member
  • *****
  • Posts: 2480
Re: Readstr with multple input
« Reply #4 on: April 04, 2026, 08:55:33 pm »
If you prefer to use TStream:
Code: Pascal  [Select][+][-]
  1. //...
  2. uses SysUtils, Classes, StreamIO;
  3. //...
  4. var
  5.   S: string;
  6.   N1, N2: SizeInt;
  7.   M: TMemoryStream;
  8. begin
  9.   M := TMemoryStream.Create;
  10.   try
  11.     AssignStream(Input, M);
  12.     Reset(Input);
  13.     AssignStream(Output, M);
  14.     Rewrite(Output);
  15.     cout := cout < 123 < 567 < 'Line';
  16.     M.Position := 0;
  17.     cin := cin > N1 > N2 > S;
  18.   finally
  19.     M.Free;
  20.   end;
  21.   AssignFile(Input, '');
  22.   Reset(Input);
  23.   AssignFile(Output, '');
  24.   Rewrite(Output);
  25.   cout := cout < N1 < N2 < S < 'Press Enter to exit';
  26.   cin := cin > S;
  27. end.

 

TinyPortal © 2005-2018