Skip to main content
gamedev.net
🔒 Locked

Feigning Keyboard Input

Started by Zipster 3 replies 950+ views
Original Post
Zipster
Zipster
In my C++ console application, I need to be able to fake input by programmatically inserting data into cin. The purpose of this is to create an alias system. For example, if the user enters "gravity," I want the program to extract that and then insert "-9.8" into the stream, as if that''s what the user really entered. This all goes on with unformatted input functions. What I do at the moment is call cin.peek to fill the stream buffer, use cin.rdbuf()->in_avail() to allocate a buffer of the right size, and then call cin.getline to retrieve the string. What I do after that I have not been able to figure out, and that''s my question to you all. Once all that is accomplished however, the alias system is finished and the next extraction should only see the "-9.8" in the stream buffer. Mission Accomplished. The only way that even remotely comes close to working is by using cin.putback to "back through" the string and insert its characters into the input stream. However, the major problem is that I can only put back at most as many characters as were originally extracted with getline, so unless I am putting back the exact amount of characters are were originally extracted, it doesn''t work. I also tryed using sputc and sputn, however they had no effect on the buffer. It didn''t change. I then tried something kinky like this:
ostream outStream( cin.rdbuf() );

outStream << myAlias;
 
In a desperate attempt to use the input stream as an output stream... but still no luck. I have a feeling I''m on the right track, but missing something trivial like an intermediary flush.
ironfroggy
ironfroggy
why do you have to fake the input like that? why not just use an abstraction layer that reads input and modifies it as needed adn then the rest of your code can read from that.
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Zipster
Zipster
The entire code base already uses cin as the source of input. Also, I''m really not up to emulating all the functionality that cin has that my program needs through the abstraction, and I''m really not anticipating all the debugging pains that would follow.

Maybe I''m asking for too much, writing data to the cin stream buffer... I tried doing all this in my own custom stream buffer, but that turned out to be a bad idea.

I could write my own stream buffer again and use it in conjunction with the system I have setup now, but I was hoping I would already be able to do something like that with the existing framework.
MrPoopypants
MrPoopypants
I think your making it wayyy more complicated than it needs to be.


Just cin the string and if the string reads gravity then set some other variable = 9.8

for example:

    
float option;

cin.getline(sString, maxlength);
// do stuff to check string contents

// so if it is "gravity" then


option = 9.8;




hope that helps


[edited by - MrPoopypants on February 16, 2003 5:57:55 PM]
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
Zipster
Zipster
Well, it''s something like that. The alias system isn''t just an alias system though, it''s a generic trigger system that executes code based on input. For example, I''ve set up one trigger class that simply outputs text whenever certain input is entered. My second trigger type is the alias, which extracts the old input and inserts the new.

The manager class doesn''t have knowledge of how the triggers operate, if user input matches a trigger it handles then it just executes an overloaded function and the trigger does what it''s supposed to do, which can really be anything as far as the manager is concerned. Basically, the trigger has to do everything, it can''t set variables or return related information because it doesn''t mean anything outside the overloaded function anyway.

All I want to know is whether or not this is possible. If it isn''t, then I''ll have to write my own stream buffer, but I''m trying not to go back down that road.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.