About Monkey 2 › Forums › Monkey 2 Development › Temporary Standard Input – UPDATE
This topic contains 4 replies, has 3 voices, and was last updated by
Danilo
2 years, 1 month ago.
-
AuthorPosts
-
November 26, 2016 at 4:26 am #5439
Hello, I come with a temporary solution on getting standard input from a Console/Terminal because I can’t find a method in Monkey2. xD So far this seems to work in in Linux, not sure about other Targets. Seems a little “hacky” but it works. I might improve it later on. Feel free to use in whatever. Haha.
Monkey1234567891011121314151617181920212223242526Function GetInput:String(prompt:String, maxLength:Int)Local char := New Byte[1]Local input := New Byte[maxLength]Local count:Int = 0Local data:String = ""libc.fputs(prompt, libc.stdout) ' InLine PromptWhile(True)If(String.FromChar(char[0]) <> "~n" And count < maxLength)libc.fread(char.Data, 1, 1, libc.stdin)input[count] = char[0]count += 1Elseinput[count - 1] = 0ExitEndifWendFor Local i := Eachin inputdata += String.FromChar(i)NextReturn dataEnd FunctionEasy and Familiar usage.
Monkey12Local name:String = GetInput("What's your name? ", 32)Print "Hello, " + name + "."Peace be with you.
^^^ Is now obsolete.
——– UPDATE ——-Monkey12345' Add the following to libc.monkey2 after the "fputs" functionFunction fgets:char_t Ptr(chr:char_t Ptr, n:Int, stream:FILE Ptr)Recompile the libc.monkey2 module. (./scripts/rebuildmods.sh) for Linux/Mac (./scripts/rebuildmods.bat) Windows
Basic Usage Example Project:
Monkey1234567891011121314151617181920212223#Import "<libc>"Namespace APPUsing libcFunction GetInput:String(prompt:String)Local buffer:char_t ' MEM ADRESS FOR INPUTfputs(prompt, stdout) ' PROMPT THE USERfgets(Varptr buffer, 1024, stdin) ' PIPE "stdin" into "bufer"Return String.FromCString(Varptr buffer) ' Return Monkey String from 'Buffer'EndFunction Main()Print "Hi how are you?"'Get C InputLocal answer:String = GetInput("Enter: ")Print "good boy"EndHopefully this can get commit to the build? *hint* *hint*
February 25, 2017 at 12:16 pm #7295Just tested you snippet but the update gave me a memory access violation..
First example worked though.
EDIT: had to modify a bit because the string was not ending correctly..
[/crayon]Monkey1234567891011121314151617181920212223242526[crayon-5cb9e4c13347a934094065 inline="true" ]Function GetInput:String(prompt:String, maxLength:Int)Local char := New Byte[1]Local input := New Byte[maxLength]Local count:Int = 0Local data:String = ""libc.fputs(prompt, libc.stdout) ' InLine PromptWhile(True)If(String.FromChar(char[0]) <> "~n" And count < maxLength)libc.fread(char.Data, 1, 1, libc.stdin)input[count] = char[0]count += 1Elseinput[count - 1] = 0ExitEndifWendFor Local i := 0 Until count-1data += String.FromChar(input[i])NextReturn dataEnd FunctionFebruary 25, 2017 at 1:12 pm #7296Monkey123456Function GetInput:String(prompt:String)Local buffer:char_t ' MEM ADRESS FOR INPUTfputs(prompt, stdout) ' PROMPT THE USERfgets(Varptr buffer, 1024, stdin) ' PIPE "stdin" into "bufer"Return String.FromCString(Varptr buffer) ' Return Monkey String from 'Buffer'EndYou need to allocate memory for the buffer in the first line.
Just ‘buffer:char_t’ is not a memory buffer of 1024 chars.February 25, 2017 at 2:42 pm #7298do you recommend that I create a new array
buffer = new int[1024] ?
in C it will be[/crayon]Monkey123[crayon-5cb9e4c139cf4789646981 inline="true" ]char char_t[1024]but in monkey how would I reserve the memory without the use of the Cast operator?
or should i use malloc!?
February 26, 2017 at 12:15 am #7302Like you did with ‘New Byte’ in the other code:
Monkey1buffer := New char_t[1024]You access the address of the buffer using ‘VarPtr buffer[0]’
Monkey123456789101112131415161718192021222324252627282930#Import "<libc>"Namespace APPUsing libcExternFunction fgets:char_t Ptr(chr:char_t Ptr, n:Int, stream:FILE Ptr)PublicFunction GetInput:String(prompt:String)Local buffer:char_t[] = New char_t[1024] ' MEM ADRESS FOR INPUTfputs(prompt, stdout) ' PROMPT THE USERfgets(Varptr buffer[0], 1024, stdin) ' PIPE "stdin" into "bufer"Return String.FromCString(Varptr buffer[0]) ' Return Monkey String from 'Buffer'EndFunction Main()Print "Hi how are you?"'Get C InputLocal answer:String = GetInput("Enter: ")Print "good boy"End -
AuthorPosts
You must be logged in to reply to this topic.