• Reading strings
    6 replies, posted
I'm pretty new to C++ and I have a question. I'm trying to make it so the user can input commands that have parameters like..."writefile(name, text)" (just testing out the [b]stdio[/b] header). But how would I seperate the parameters from writefile() so I can define the parameters as variables for later usage.
C++ with stdio? no. Look for a book or tutorial teaching you with the iostream-header. I'd give you a solution for your problem, but it would probably just confuse you.
[QUOTE=ZeekyHBomb;18891004]C++ with stdio? no. Look for a book or tutorial teaching you with the iostream-header. I'd give you a solution for your problem, but it would probably just confuse you.[/QUOTE] Way to act like a douche. It's pretty simple to parse commands from a text command interface. This is what shells have to do for everything. For simplicity sake, I would go ahead and do away with the parenthesis. Paren nesting is a complicated problem to do with linear input processing. Just separate commands and arguments by a space or a comma or something. You can separate each individual word out with a string tokenizer. The C standard strings library provides a useful function for this called strtok(). [b]Look at reference material. I don't want to explain in detail how to use strtok() because you can figure that out on your own.[/b] Just remember that it will give you a bunch of char* pointers that point to individual space-separated words in the text input. Use this to build an array of strings, as this will be your argument list. Exactly like the argc and argv arguments in your main() function. Now all you gotta do is match up the first word (argv[0]) with a command you can execute. String-matching, in other words. You have defined a bunch of functions, and you need to have a unique 'name' for each one. This is as simple as having a wrapper struct with a string and a function pointer inside of it. The string just declares what text command will call that function. For example: [code] //This function has the code that the writefile command will execute int writefile(int argc, char** argv) { ... } struct command_wrapper { char* name = "writefile"; int* fxn (int, char**) = writefile; //Function pointer }; [/code] To execute a command, you just have code that looks through all the command wrappers you have in a container somewhere, and compare argv[0] to each of their names. If you find a match, then you call that wrapper's fxn with the argc and argv arguments. Tada!!
He did say C++. There's boost::programs_options for that.
Use a scripting language. ChaiScript is very nice for this.
[QUOTE=nullsquared;18892352]Use a scripting language. ChaiScript is very nice for this.[/QUOTE] The idea very well may be to come to grips with writing C++. I use a very similar interface to this for a command console in my game (except that I first string-match with a hash table)
[QUOTE=Cathbadh;18892015]Way to act like a douche. It's pretty simple to parse commands from a text command interface. This is what shells have to do for everything. For simplicity sake, I would go ahead and do away with the parenthesis. Paren nesting is a complicated problem to do with linear input processing. Just separate commands and arguments by a space or a comma or something. You can separate each individual word out with a string tokenizer. The C standard strings library provides a useful function for this called strtok(). [b]Look at reference material. I don't want to explain in detail how to use strtok() because you can figure that out on your own.[/b] Just remember that it will give you a bunch of char* pointers that point to individual space-separated words in the text input. Use this to build an array of strings, as this will be your argument list. Exactly like the argc and argv arguments in your main() function. Now all you gotta do is match up the first word (argv[0]) with a command you can execute. String-matching, in other words. You have defined a bunch of functions, and you need to have a unique 'name' for each one. This is as simple as having a wrapper struct with a string and a function pointer inside of it. The string just declares what text command will call that function. For example: [code] //This function has the code that the writefile command will execute int writefile(int argc, char** argv) { ... } struct command_wrapper { char* name = "writefile"; int* fxn (int, char**) = writefile; //Function pointer }; [/code] To execute a command, you just have code that looks through all the command wrappers you have in a container somewhere, and compare argv[0] to each of their names. If you find a match, then you call that wrapper's fxn with the argc and argv arguments. Tada!![/QUOTE] Thank you.
Sorry, you need to Log In to post a reply to this thread.