Author Topic: Trying to override TStream's ReadLine  (Read 361 times)

Offline LamptonWorm

  • Jr. Member
  • **
  • Posts: 4
Trying to override TStream's ReadLine
« on: July 07, 2018, 05:12:28 PM »
Hi all,

I want to override the ReadLine method that is called inside Input (StandardIOStream.ReadLine).

I've tried setting up my own type that extends TStream and putting my own ReadLine method in there but it isn't being called.

Advice and pointers welcome :)

Quote
Type TMyStream Extends TStream
   Method ReadLine$()
      Print "overridden"
      Local buf:Byte[1024],sz
      Repeat
         Local ch:Byte
         If Read( Varptr ch,1 )<>1 Or ch=0 Or ch=10 Exit
         If ch=13 Continue
         If sz=buf.length buf=buf[..sz*2]
         buf[sz]=ch
         sz:+1
      Forever
      Return String.FromBytes( buf,sz )
   End Method
End Type

Global StandardIOStream2:TStream=TTextStream.Create( New TCStandardIO,TTextStream.UTF8 )

a$=Input("Stuff?")

Function Input$( prompt$=">" )
   Print "LW Input.."
   StandardIOStream2.WriteString prompt
   StandardIOStream2.Flush
    Return StandardIOStream2.ReadLine()
End Function

Cheers,
LW.

Offline Henri

  • Full Member
  • ***
  • Posts: 166
Re: Trying to override TStream's ReadLine
« Reply #1 on: July 07, 2018, 06:16:36 PM »
Hi,

I didn't look the code closely, but something to try:

Code: BlitzMax
  1.  
  2. Type TMyStream Extends TTextStream
  3.  
  4.         Function Create:TMyStream( stream:TStream,encoding )
  5.                 Local t:TMyStream=New TMyStream
  6.                 t._encoding=encoding
  7.                 t.SetStream stream
  8.                 Return t
  9.         End Function
  10.        
  11.    Method ReadLine$()
  12.       Print "overridden"
  13.       Local buf:Byte[1024],sz
  14.           Repeat
  15.          Local ch:Byte
  16.          If Read( Varptr ch,1 )<>1 Or ch=0 Or ch=10 Exit
  17.          If ch=13 Continue
  18.          If sz=buf.length buf=buf[..sz*2]
  19.          buf[sz]=ch
  20.          sz:+1
  21.       Forever
  22.       Return String.FromBytes( buf,sz )
  23.    End Method
  24. End Type
  25.  
  26. Global StandardIOStream2:TMyStream = TMyStream.Create( New TCStandardIO,TMyStream.UTF8 )
  27. a$=Input("Stuff?")
  28.  
  29. Function Input$( prompt$=">" )
  30.  
  31.     Print "LW Input.."
  32.     StandardIOStream2.WriteString prompt
  33.     StandardIOStream2.Flush
  34.        
  35.         Local s:String = StandardIOStream2.ReadLine()
  36.         Print s
  37.        
  38.         Return
  39. End Function
  40.  

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline LamptonWorm

  • Jr. Member
  • **
  • Posts: 4
Re: Trying to override TStream's ReadLine
« Reply #2 on: July 07, 2018, 10:31:06 PM »
Thanks!