I am writing a drop-down console for my app. Suppose I want to output the value of variable myvar by using the following command:
]/get myvar
Is there a better way than to create a map so that the output is
return mymap[argv[0]]; ?
In other words, can I associate the input char array "myvar" to the variable named myvar without doing it manually for all the variables in the program.
Short answer:
No.
Long answer:
Pfff, no way! After your piece of code is build as a binary, there is no such thing as a variable name. Just some pointers, values on the stack, and so on...
If you want to implement something like this, I would recommend you to go for a scripting library (Lua, for example), and manually map some variables so you can read/change those variables via scripts. In this case the console input is basically what you are feeding to the script engine.
This might be a good reference.
UPDATE:
In fact, just found the project called Lua Console.
Seems like it's not maintained anymore, but it doesn't mean it will not work.
Related
I've been using the luaL_loadbuffer for many years to load Lua code from within a C++ program. Suddenly I find I need the script to know its own name. Sure, the script in an anonymous function as far as the Lua context is concerned but the C++ framework around it keeps it in a hashmap with a name, the name of the file from which it was loaded to be precise.
I passed that file name into luaL_loadbuffer when I originally wrote the code but I never actually used it. I now need that name so I can have the script compute metrics about its own execution.
luaL_loadbuffer(LuaContext, code, strlen(code), name)
I now need to use that name from with the Lua context. What's the easiest way to do that?
I'm going to tap the Lua debug function documentation in the meantime while waiting for an answer.
When that code is running, debug.getinfo(1).src will give you name.
I'm trying to write a GDB script (legacy, not Python) that will print information on members of a local variable (a C or C++ struct), but only if that local variable exists. Something like:
# 'magic' should be evaluate to "if 'info locals' has a variable named foo, then
# evaluate to true, otherwise evaluate to false.
if (magic)
print foo->member
end
I know this is somewhat contrived, because the locals are dependent on the stack frame (so I'm probably better off making it conditional on the frame), but I'd still like to know if something along these lines is possible.
First -- Python is just far superior for this kind of thing. That's why we added it to gdb!
However, this can still be done with an older gdb. However, it's awful, and after doing it I think you'll appreciate the Python approach even more. What you do is: first, use the various "set logging" commands to redirect the output to a temporary file . Then use gdb commands to print the information you need, in this case something like "info local". Then, use the "shell" command to shell out to rewrite the temporary file into a file that is itself a gdb script. For example, use "sed" to detect that the variable exists in your output, and then emit "set $var_exists=1". Finally, "source" the result of this scripting and test the convenience variable that was set.
Eww. But it works.
I'm working on a project simulating a stock market. People buy and sell a stock and I would like to call each turn a script to try a strategy against the market.
What I want is a function in C++ which send an vector of integer as argument to a vba or python script which return an array of 3 integers.
I've searched for a solution but all i could find is a way to execute a script in python, but I don't know how I can send and get an argument from this script.
I think my problem is common but i don't know where to head to find a solution.
Thank you!
(I'm not a native english speaker so sorry if I made grammar error)
On windows you use the function CreateProcess() to start another program. Use the full path of the python interpreter as the first argument. Start the second argument with the path to the python script.
If you can fit a string representing your vector in 32768 characters, you can supply the vector in the second argument to CreateProcess.
A more flexible option is create a child process with redirected in- and output, as shown here. You can then write the vector to the standard input of the python process and read the answer back from its standard output.
There are many ways to do this.
The way I would do it is to popen() your "script" [which would be something like "python myscript.py -arg1 -arg2"]. Depending on how large your vector is, you could either store it in a file or pass it as part of the arguments [there is a limit in Windows of something like 8KB for the argument string].
The output would then appear as the result from popen()'s pipe.
Use Boost.Python. It will help you to embed python in your app.
Is there a platform-independent method to embed file data into a c++ program? For instance, I am making a game, and the levels are stored in text format. But I don't want to distribute those text files with the game itself. What are my options?
This has been asked here before. Basically, you just name the data with an accessible symbol. I like this method best:
You can always write a small program or script to convert your text file into a header > > file and run it as part of your build process.
You can always put the text in the code. Say, for example, as an array of Strings or array of pointers to characters.
String txt[] = {
"My first line.\n",
"My second line.\n"
}
Of course there are better structures in the standard libraries, but in any case you put the text in a source file.
You could also consider putting the text into the package and encrypting it, if you're worried about people accessing it.
If the levels are really stored simply as text, you could just declare static char arrays in your source:
static const char level1[] = "abcdeabcdeabcde....";
You can compile and link this right into your application and reference it just like any other variable.
How do you pass options to an executable? Is there an easier way than making the options boolean arguments?
EDIT: The last two answers have suggested using arguments. I know I can code a workable solution like that, but I'd rather have them be options.
EDIT2: Per requests for clarification, I'll use this simple example:
It's fairly easy to handle arguments because they automatically get parsed into an array.
./printfile file.txt 1000
If I want to know what the name of the file the user wants to print, I access it via argv[1].
Now about how this situation:
./printfile file.txt 1000 --nolinebreaks
The user wants to print the file with no line breaks. This is not required for the program to be able to run (as the filename and number of lines to print are), but the user has the option of using if if s/he would like. Now I could do this using:
./printfile file.txt 1000 true
The usage prompt would inform the user that the third argument is used to determine whether to print the file with line breaks or not. However, this seems rather clumsy.
Command-line arguments is the way to go. You may want to consider using Boost.ProgramOptions to simplify this task.
You seem to think that there is some fundamental difference between "options" that start with "--" and "arguments" that don't. The only difference is in how you parse them.
It might be worth your time to look at GNU's getopt()/getopt_long() option parser. It supports passing arguments with options such as --number-of-line-breaks 47.
I use two methods for passing information:
1/ The use of command line arguments, which are made easier to handle with specific libraries such as getargs.
2/ As environment variables, using getenv.
Pax has the right idea here.
If you need more thorough two-way communication, open the process with pipes and send stuff to stdin/listen on stdout.
You can also use Window's PostMessage() function. This is very handy if the executable you want to send the options to is already running. I can post some example code if you are interested in this technique.
The question isn't blazingly clear as to the context and just what you are trying to do - you mean running an executable from within a C++ program? There are several standard C library functions with names like execl(), execv(), execve(), ... that take the options as strings or pointer to an array of strings. There's also system() which takes a string containing whatever you'd be typing at a bash prompt, options and all.
I like the popt library. It is C, but works fine from C++ as well.
It doesn't appear to be cross-platform though. I found that out when I had to hack out my own API-compatible version of it for a Windows port of some Linux software.
You can put options in a .ini file and use the GetPrivateProfileXXX API's to create a class that can read the type of program options you're looking for from the .ini.
You can also create an interactive shell for your app to change certain settings real-time.
EDIT:
From your edits, can't you just parse each option looking for special keywords associated with that option that are "optional"?