Ooops
January 26, 2021, 06:06:46 AM

Author Topic: [bmx] fmc.StdIOStream by Fabian. [ 1+ years ago ]  (Read 600 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] fmc.StdIOStream by Fabian. [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : fmc.StdIOStream
Author : Fabian.
Posted : 1+ years ago

Description : Since 2006-11-06 my modules stored in the code archives are out of synchronization. To have the latest version you need to use the bmk/syncmods utility or the MaxIDE.

You need to add "webspace100.we.funpic.de/root/mod" as a new line to "cfg/modservers.cfg" in your BlitzMax installation directory.

You can't synchronize with the default BlitzMax 1.22 IDE. If you don't have another IDE, you need to download a <a href="http://webspace100.we.funpic.de/root/programs/MaxIDE.exe" target="_blank">tweaked MaxIDE[/url]. You need to specify "fmc" as additional module scope.

Alternative you can synchronize with the bmk tool: bmk syncmods fmc

For a better description see my <a href="../Community/posts37e4.html?topic=69294" target="_blank">thread[/url] in the programming forum introducing these modules.

Needed modules:
<a href="codearcsffc9.html?code=1780" >fmc.IOStream[/url]
<a href="codearcsb7bf.html?code=1786" >fmc.Development[/url]
<a href="codearcs6306.html?code=1876" >fmc.ObjectTool[/url]


Code :
Code: BlitzMax
  1. Strict
  2. Rem
  3. bbdoc:StdIOStream
  4. EndRem
  5. Module fmc.StdIOStream
  6.  
  7. ModuleInfo "Version: 0.05"
  8. ModuleInfo "Modserver: Fabian"
  9.  
  10. Import brl.standardio
  11. Import fmc.IOStream
  12. Import fmc.Development
  13. Import fmc.ObjectTool
  14.  
  15. Rem
  16. bbdoc:Get standard input and output stream
  17. returns:The standard input and output stream
  18. about:
  19. This function returns the application's standard input and output stream.
  20. You can use this stream to exchange information with the calling application or the user.
  21. Note that #brl.standardio.StandardIOStream is set to this stream when the application starts up.
  22. You also can get this stream calling #brl.stream.OpenStream with "object::stdio" as parameter.
  23. Note that #brl.stream.OpenStream with "object::standardio" as parameter will return
  24. #brl.standardio.StandardIOStream.
  25. EndRem
  26. Function StdIOStream:TStream ( )
  27.   Return Stream
  28. EndFunction
  29.  
  30. Rem
  31. bbdoc:Get the application's command line
  32. returns:The application's command line
  33. about:
  34. This function returns the application's command line.
  35. With the optional parameter @RemoveApp you can control whether the function removes the
  36. application's name and trims the line before it returns it.
  37. EndRem
  38. Function CommandLine$ ( RemoveApp = True )
  39.   If RemoveApp
  40.     Return Line1
  41.   EndIf
  42.   Return Line0
  43. EndFunction
  44.  
  45. Private
  46.  
  47. Global Line0$
  48. Global Line1$
  49. MakeCmdLine
  50. Global Stream:TStdIOStream = New TStdIOStream
  51. StandardIOStream = Stream
  52. New TObjectStreamFactory
  53. ?Win32
  54. BufferedInput
  55. Global InHandle = GetStdHandle ( -10 )
  56. Global OutHandle = GetStdHandle ( -11 )
  57. Global DataEof
  58.  
  59. Function BufferedInput ( )
  60.   Global BufferLen = 1024
  61.   Global Buffer:Byte Ptr = MemAlloc ( BufferLen )
  62.   Global ThreadIn = GetStdHandle ( -10 )
  63.   Global ThreadOut
  64.   Local Pipe
  65.   CreatePipe Varptr Pipe , Varptr ThreadOut , Null , 0
  66.   SetStdHandle -10 , Pipe
  67.   DebugSuspend
  68.   UpdateCStream
  69.   Global T:TThread = TThread.Create ( Thread , 0 )
  70.   DebugResume
  71.  
  72.   Function Thread ( Context )
  73.     Local PipeThreadIn = PeekNamedPipe ( ThreadIn , Null , 0 , Null , Null , Null )
  74.     Repeat
  75.       Local All
  76.       Local Cur
  77.       If Not ( ReadFile ( ThreadIn , Buffer , BufferLen , Varptr All , Null ) And ( All Or PipeThreadIn ) )
  78.         DataEof = True
  79.         CloseHandle ThreadOut
  80.         Return
  81.       EndIf
  82.       While All > Cur
  83.         Local N
  84.         WriteFile ThreadOut , Buffer + Cur , All - Cur , Varptr N , Null
  85.         Cur :+ N
  86.       Wend
  87.       If All
  88.         CallbackMain Func , All
  89.       EndIf
  90.     Forever
  91.   EndFunction
  92.  
  93.   Function UpdateCStream ( )
  94.     ( Int Ptr stdin_ ) [ 4 ] = _open_osfhandle ( GetStdHandle ( -10 ) , $8000 )
  95.     ( Int Ptr stdout_ ) [ 4 ] = _open_osfhandle ( GetStdHandle ( -11 ) , $8001 )
  96.     ( Int Ptr stderr_ ) [ 4 ] = _open_osfhandle ( GetStdHandle ( -12 ) , $8001 )
  97.  
  98.     Extern
  99.       Function _open_osfhandle ( osfhandle , access )
  100.     EndExtern
  101.   EndFunction
  102.  
  103.   Function Func ( Context )
  104.     TEvent.Create ( EVENT_READAVAIL , Stream , Context ).Emit
  105.   EndFunction
  106. EndFunction
  107.  
  108. Extern "Win32"
  109.   Function GetStdHandle ( N )
  110.   Function SetStdHandle ( N , Handle )
  111.   Function CreatePipe ( In Ptr , Out Ptr , PA:Byte Ptr , Size )
  112.   Function FlushFileBuffers ( File )
  113.   Function PeekNamedPipe ( Pipe , Buffer:Byte Ptr , Size , Read Ptr , Avail Ptr , BLTM Ptr )
  114.   Function ReadFile ( File , Buffer:Byte Ptr , Size , Read Ptr , O:Byte Ptr )
  115.   Function WriteFile ( File , Buffer:Byte Ptr , Size , Written Ptr , O:Byte Ptr )
  116.   Function GetCommandLineW:Short Ptr ( )
  117.   Function CloseHandle ( Handle )
  118. EndExtern
  119. ?
  120.  
  121. Function MakeCmdLine ( )
  122. ?Win32
  123.   Line0 = String.FromWString ( GetCommandLineW ( ) )
  124. ?Linux | MacOS
  125.   For Local AppArg$ = EachIn AppArgs
  126.     Line0 :+ " ~q" + AppArg + "~q"
  127.   Next
  128.   Line0 = Line0 [ 1 ..]
  129. ?
  130.   Line1 = Line0.Trim ( )
  131.   If Line1 [.. 1 ] = "~q"
  132.     Line1 = Line1 [ Line1.Find ( "~q" , 1 ) + 1 ..]
  133.   Else
  134.     While Line1 [.. 1 ].Trim ( )
  135.       Line1 = Line1 [ 1 ..]
  136.     Wend
  137.   EndIf
  138.   Line1 = Line1.Trim ( )
  139. EndFunction
  140.  
  141. Type TStdIOStream Extends TStream
  142.   Method Eof ( )
  143. ?Win32
  144.     Return DataEof And Not Size ( )
  145. ?Linux | MacOS
  146.     Return feof_ ( stdin_ )
  147. ?
  148.   EndMethod
  149.  
  150.   Method Pos ( )
  151.     Return -1
  152.   EndMethod
  153.  
  154.   Method Size ( )
  155. ?Win32
  156.     Local Avail
  157.     PeekNamedPipe InHandle , Null , 0 , Null , Varptr Avail , Null
  158.     Return Avail
  159. ?
  160.   EndMethod
  161.  
  162.   Method Seek ( pos )
  163. ?Win32
  164.     If pos = -1
  165.       Return -3
  166.     EndIf
  167. ?
  168.     Return -1
  169.   EndMethod
  170.  
  171.   Method Flush ( )
  172. ?Win32
  173.     FlushFileBuffers OutHandle
  174. ?Linux | MacOS
  175.     fflush_ stdout_
  176. ?
  177.   EndMethod
  178.  
  179.   Method Close ( )
  180.   EndMethod
  181.  
  182.   Method Read ( buf:Byte Ptr , count )
  183. ?Win32
  184.     ReadFile InHandle , buf , count , Varptr count , Null
  185.     Return count
  186. ?Linux | MacOS
  187.     Return fread_ ( buf , 1 , count , stdin_ )
  188. ?
  189.   EndMethod
  190.  
  191.   Method Write ( buf:Byte Ptr , count )
  192. ?Win32
  193.     WriteFile OutHandle , buf , count , Varptr count , Null
  194.     Return count
  195. ?Linux | MacOS
  196.     Return fwrite_ ( buf , 1 , count , stdout_ )
  197. ?
  198.   EndMethod
  199. EndType
  200.  
  201. Type TObjectStreamFactory Extends TStreamFactory
  202.   Method CreateStream:TStream ( url:Object , proto$ , path$ , readable , writeable )
  203.     Local Array:Object [] = Object [] ( url )
  204.     If Array
  205.       Local U$
  206.       Local H [ Len Array ]
  207.       For Local I = 0 Until Len Array
  208.         Local T$ = String Array [ I ]
  209.         If T
  210.           U :+ T
  211.         Else
  212.           H [ I ] = OpenHandle ( Array [ I ] )
  213.           U :+ "object::" + H [ I ]
  214.         EndIf
  215.       Next
  216.       Local S:TStream = OpenStream ( U , readable , writeable )
  217.       For Local N = EachIn H
  218.         If N
  219.           CloseHandle N
  220.         EndIf
  221.       Next
  222.       Return S
  223.     EndIf
  224.     If proto = "object"
  225.       Local I = Int path
  226.       If I
  227.         Return OpenStream ( ObjectForHandle ( I ) , readable , writeable )
  228.       EndIf
  229.       If path.ToLower ( ) = "stdio"
  230.         Return Stream
  231.       EndIf
  232.       If path.ToLower ( ) = "standardio"
  233.         Return StandardIOStream
  234.       EndIf
  235.     EndIf
  236.   EndMethod
  237. EndType


Comments :


Perturbatio(Posted 1+ years ago)

 Is this different in some way from BRL.StandardIO ?


Fabian.(Posted 1+ years ago)

 Yes, a bit:* brl.standardio allows you to change the standardio stream object. That means that you can't be sure whether StandardIOStream is really the standard-io-stream.* The brl.standardio stream object is a text stream, so you can't write a single byte to it using WriteByte.* brl.standardio uses a text-type c-stream; that means that incoming bytes with the value 13 are deleted - outcoming bytes with the value 10 are prefixed with bytes with the value 13.* fmc.StdIOStream adds a stdio stream and object stream factory.


Fabian.(Posted 1+ years ago)

 And, since the last update:* fmc.StdIOStream allows you to get the number of bytes available to read from the standard input stream without causing the application to block.* fmc.StdIOStream provides a function to get the programme's command line unchanged - with whitespace syntax intact.* fmc.StdIOStream emits a read avail event if there's new data to read from the standard input stream.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal