srdcpp target and the input pipe

Monkey Forums/Monkey Programming/srdcpp target and the input pipe

ziggy(Posted 2011) [#1]
How could I send data to the standard input pipe of a StdCpp monkey program? I'm in the need of creating a console app using monkey (A modified version of the compiler).
Any ideas how to do this?


skid(Posted 2011) [#2]
You will need some kind of stdio.monkey layer. I'm not sure if this is win32 compatible but here is the start of a stdio layer for monkey:


#if TARGET="stdcpp" Or TARGET="glfw"

Extern

Class FILE
End

Global stdin:FILE
Global stdout:FILE
Global stderr:FILE

Function fputc(c,file:FILE)
Function fflush(file:FILE)

Public

Function Main()
	Print "hello "
	
	fputc(65,stdout)

	fflush(stdout)
End




ziggy(Posted 2011) [#3]
Ok... thanks. Testing...


ziggy(Posted 2011) [#4]
As I'm not very familiar with the C++ implementation of i/o streams, shouldn't the stdout, stderr and stdin be initialized somehow? fopen?

I'm interested basically in a way to make "fgetc" or a similar way to read a line of text from the input stream.

If any C++ expert can provide some light into my complete darkness...


ziggy(Posted 2011) [#5]
My failing attempt:
#if TARGET="stdcpp" Or TARGET="glfw"

Extern

Class FILE
End

Global stdin:FILE
Global stdout:FILE
Global stderr:FILE

Function fputc(c,file:FILE)
Function fflush(file:FILE)
Function fgetc:Int(file:FILE)
Const EOF
Public

Function Main()
	Print "hello "
	
	'We try to get an input from the keyboard but no luck...
	Local c:int, result:String
	repeat
		c = fgetc (stdin);
		result = result + String.FromChar(c)
	Until (c = EOF)
	
End

#End 





skid(Posted 2011) [#6]
On Mac the following works in Monk, as you hit enter in console you get a 13 then a 10:


Function Main()
	Print "hello "
	
	Local c:Int, result:String
	
	While True
		c=fgetc(stdin);
		Print c
	Wend
End



Waiting for EOF doesn't make sense to me, stdin and stdout are typically I think pipes not files that are available for lifetime of process.


ziggy(Posted 2011) [#7]
I've got it working this way:
#if TARGET="stdcpp" Or TARGET="glfw"

Extern

Class FILE
End

Global stdin:FILE
Global stdout:FILE
Global stderr:FILE

Function fputc(c,file:FILE)
Function fflush(file:FILE)
Function fgetc:Int(file:FILE)
Const EOF
Public

Function Main()
	Print "hello "
	local name:String = Input("Tell me your name?")
	Print "Your name is " + name
	Input("Press ENTER to end.")
End


Function Input:String(prompt:String=">")
	
	Local c:Int, result:String
	
	For Local i:Int = 0 until prompt.Length 
		fputc(prompt[i],stdout)
	end
	fflush(stdout)
	
	c=fgetc(stdin)
	
	While c<>10 And c<>13
		result += String.FromChar(c)
		c=fgetc(stdin);
	Wend
	
	fflush(stdin)
	
End

#End 


But I don't know why the program keeps resident when the execution has ended. Anything I'm missing to let the program end its execution properly?

Also, the string "construction" in the Input function sucks, as it creates a copy of itself for each char, wich I'm sure it should not perform very well, don't know if there's any kind of sring builder in Monkey or bank or something like it to make it a bit neater?


ziggy(Posted 2011) [#8]
Ok, got this and it seems to be working ok:
#if TARGET="stdcpp" Or TARGET="glfw"

Extern private

Class FILE
End

Global stdin:FILE
Global stdout:FILE
Global stderr:FILE

Function fputc(c,file:FILE)
Function fflush(file:FILE)
Function fgetc:Int(file:FILE)
'Const EOF
Public

'summary: Prompt for user input in the system console window and resturns a String
Function Input:String(prompt:String=">")
	
	Local c:Int, result:String
	
	For Local i:Int = 0 until prompt.Length 
		fputc(prompt[i],stdout)
	end
	fflush(stdout)
	
	c=fgetc(stdin)
	
	While c<>10 And c<>13
		result += String.FromChar(c)
		c=fgetc(stdin);
	Wend
	
	fflush(stdin)
	
	Return result;
End

'summary: Sends text to the standard output pipe
Function Output(value:String)
	For Local i:Int = 0 to value.Length-1
		fputc(value[i],stdout)
	Next
	fflush(stdout)
End

'summary: Sends text to the standard error pipe
Function ErrOutput(value:String)
	For Local i:Int = 0 to value.Length-1
		fputc(value[i],stderr )
	Next
	fflush(stderr)
End


#End