View previous topic :: View next topic |
Author |
Message |
obijohn
Joined: 07 Feb 2006 Posts: 24
|
Posted: Tue Feb 07, 2006 6:26 am Post subject: mango.io.Stdout/Stdin console issues |
|
|
I'm just trying to do some standard console input/output stuff but I'm running into problems. (dmd v. 0.145 on Windows)
First, when I use any form of Stdout with a string literal:
Code: |
private import mango.io.Stdout;
[...]
Stdout << "hello" << CR;
Stdout ("hello") (CR);
Stdout.put("hello").put(CR);
|
I get some dmd/build errors complaining that IWriter is overloaded with both char[] and dchar[], and evidently it doesn't know which one to use. I'm getting around this right now by using string variables instead of literals, but I don't think it's supposed to be an issue like this, at least when the examples in the source files use literals. Right?
Second, when using Stdin to grab some input, something isn't getting flushed or cleared I think:
Code: |
private import mango.io.Stdout;
private import mango.io.Stdin;
[...]
int x, y, z;
char[] str;
str = "input 2 numbers: ";
Stdout (str) ();
Stdin (x) (y);
str = "input 1 more number: ";
Stdout (str) ();
Stdin (z);
|
produces a runtime error after the second Stdout but before reaching the second Stdin:
Code: |
input 2 numbers: 5 7
input 1 more number: Error: unexpected end of input
|
Am I doing something wrong here? Any help would be appreciated.
-- John |
|
Back to top |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Tue Feb 07, 2006 8:30 am Post subject: Re: mango.io.Stdout/Stdin console issues |
|
|
obijohn wrote: | First, when I use any form of Stdout with a string literal:
Code: |
private import mango.io.Stdout;
[...]
Stdout << "hello" << CR;
Stdout ("hello") (CR);
Stdout.put("hello").put(CR);
|
I get some dmd/build errors complaining that IWriter is overloaded with both char[] and dchar[], and evidently it doesn't know which one to use. I'm getting around this right now by using string variables instead of literals, but I don't think it's supposed to be an issue like this, at least when the examples in the source files use literals. Right?
-- John |
Try this:
Code: |
Stdout << "hello"c << CR;
Stdout ("hello"c) (CR);
Stdout.put("hello"c).put(CR);
|
This is a DMD feature, not a problem with Mango. |
|
Back to top |
|
|
obijohn
Joined: 07 Feb 2006 Posts: 24
|
Posted: Tue Feb 07, 2006 8:39 am Post subject: Re: mango.io.Stdout/Stdin console issues |
|
|
Carlos wrote: |
This is a DMD feature, not a problem with Mango. |
Thanks, that explains the first problem. Any ideas on the second?
--John |
|
Back to top |
|
|
obijohn
Joined: 07 Feb 2006 Posts: 24
|
Posted: Tue Feb 07, 2006 12:13 pm Post subject: |
|
|
The more I test this, the more it seems like a mango.io.Stdin problem. Here's an example.
Code: |
private import mango.io.Stdout;
private import mango.io.Stdin;
void main() {
int x, y, z;
Stdout ("input x: "c) ();
Stdin (x);
Stdout ("you entered: "c) (x) (CR);
Stdout ("input y: "c) ();
Stdin (y);
Stdout ("you entered: "c) (x) (y) (CR);
Stdout ("input z: "c) ();
Stdin (z);
Stdout ("you entered: "c) (x) (y) (z) (CR);
}
|
And here are 3 sample sessions of the above program. Note that it will never prompt for the 2nd and 3rd numbers. If 1 number is input at the 1st prompt, the program exits with an "end of input" error. If 2 numbers are input at the 1st prompt, the program still exits with an "end of input" error, however it assigns the 2nd number to y as if it had been input at the 2nd prompt. And if 3 numbers are input at the 1st prompt, the 2nd and 3rd prompts are skipped and all 3 variables are assigned. It's difficult to describe but it's easy to see for yourself below what I'm talking about.
Code: |
input x: 5
you entered: 5
input y: Error: unexpected end of input
|
Code: |
input x: 5 6
you entered: 5
input y: you entered: 56
input z: Error: unexpected end of input
|
Code: |
input x: 5 6 7
you entered: 5
input y: you entered: 56
input z: you entered: 567
|
I guess I should mention that this is with mango 2.0 and dmd 0.145 on Windows XP SP2. I don't know if this behavior exists on linux. |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Tue Feb 07, 2006 12:15 pm Post subject: |
|
|
Egad!
That's a bug. I'll fix it later today. |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Tue Feb 07, 2006 2:41 pm Post subject: |
|
|
I fixed a problem relating to that "unexpected end of input". The issue there is one of input formatting (although there was a bug also).
Some background: when using a Reader for input (such as Stdin), there's an expectation that the incoming data is in a format suitable for parsing; this makes them a bit more strict than, for example, a text Iterator or string parser. Stdin is a bit of a hybrid, since it has to deal with arbitrary user-input, rather than something akin to a structured data-record. Historically, Stdin would treat an "empty token" as an error -- you can cause this by hitting CR without preceeding it with a number (in your examples above).
What I've changed will now ignore empty tokens ~ if you just hit CR, the example program will now just wait for you to enter 'something'.
~~~~~~~~
The second part of the issue is related to how console input is treated. If you enter more than one token at a time (as you show in the above test run), then subsequent calls to stdin will eat whatever tokens are still available. Your example program will wait for more input only when all available token have been consumed.
Why does it do this? Well, if you were to 'pipe' a file into your example (instead of directly interacting with the console) then you'd perhaps be horrified if only one token was read from the file, and all others were then discarded.
Thus, working with the console treats it as though it were a token stream. If you need to ensure a new value is explicitly provided for each prompt, then you'd need to clear the input buffer before reading the next token. To do this, you could use Stdin.getBuffer.clear();
Does that make sense?
~~~~~~~~~~~~~
Note also that you could drop down a level, and use mango.io.Console instead. This will give you more direct control over the console functionality. It also avoids that issue with the ""c suffix when prompting a user with text (I should note that Stdout also has a print() method, which operates just like printf).
With respect to the console, Mango has a few different layers. From bottom to top:
mango.io.Console -- direct console IO
mango.io.Print -- printf like output attached to the console
mango.io.Stdout -- Writer style console output, with a print() method
mango.io.Stdin -- Reader style console input, with token parsing
When working with raw input from mango.io.Console, you might find the mango.text.QuoteIterator functionality useful for extracting tokens. Also, the various parse() methods in mango.convert might come in handy.
I'll check the changes in soon, and you'll be able to pick them up via SVN on dsource.org (you'll need to synchronize properly, since there's been some related changes recently). |
|
Back to top |
|
|
obijohn
Joined: 07 Feb 2006 Posts: 24
|
Posted: Tue Feb 07, 2006 3:56 pm Post subject: |
|
|
kris,
Thanks for your prompt (pun intended ) attention to this. Just to be sure we're on the same page, there are actually 3 issues I noticed. The first 2 you decribed: the "empty token" situation when hitting CR before any input, and entering more tokens than expected as in the last 2 sample sessions of my example program above. However, the other issue is that even when exactly the correct number of tokens have been entered, Stdin does not get cleared completely and subsequent calls result in empy tokens and an error, as in the first sample session to my example program above. I tried using Stdin.getBuffer.clear(), but that does not help.
Is this last issue also addressed in the changes you're making? |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Tue Feb 07, 2006 4:52 pm Post subject: |
|
|
obijohn wrote: | Is this last issue also addressed in the changes you're making? |
I believe so ~ your example here doesn't exhibit any of the above issues. |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Tue Feb 14, 2006 11:32 am Post subject: |
|
|
Sorry this is taking so long to get a patch applied ... I'm having a little trouble getting it to shape up the way I'd like it to be. Haven't forgotten about it! |
|
Back to top |
|
|
obijohn
Joined: 07 Feb 2006 Posts: 24
|
Posted: Wed Feb 15, 2006 6:25 am Post subject: |
|
|
Thanks for keeping this in mind. Right now I'm having to do this kind of thing: Code: |
char[][] tokens;
char[80] cmd_str;
int x,y,z;
tokens = split(toString(gets(cmd_str)));
sscanf(tokens[0], "?d", &x);
sscanf(tokens[1], "?d", &y);
sscanf(tokens[2], "?d", &z);
|
when I'd really LOVE to simply do:
|
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Sun Apr 30, 2006 1:49 pm Post subject: |
|
|
Well, I finally got this fixed
Through a combination of distractions, work, relocating, and a variety of other excuses, this took months to rectify. My profuse apologies for that. Fixes are in the repository. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|