C++/Tk how to exequte general tcl code? - c++

So we have code like:
#include "cpptk.h"
#include <stdio.h>
using namespace Tk;
void hello() {
puts("Hello C++/Tk!");
}
int main(int, char *argv[])
{
static char* str = "button .a -text "Say Hello ppure TCL"\n"
"pack .a\n";
init(argv[0]);
button(".b") -text("Say Hello") -command(hello);
pack(".b") -padx(20) -pady(6);
runEventLoop();
}
imagine str is complex tcl code. We want to feed it to C++/Tk as a string. Also we want to have it exequted in the same TCL vm our general C++/Tk programm with gui we created in C++/Tk code runs. So the result of this code would be 2 buttons inside a window.
How to do such thing?
How to do such thing?

Have you got access to the Tcl_Interp* handle used inside C++/Tk? If so (and assuming here you've got it in a variable called interp) use:
int resultCode = Tcl_Eval(interp, str);
Next, check the resultCode to see if it is TCL_OK or TCL_ERROR (other values are possible, but uncommon in normal scripts). That tells you the interpretation of the “result”, which you get like this:
const char *result = Tcl_GetString(Tcl_GetObjResult(interp));
If the result code says its an error, result is now an error message. If it was ok, the result is the output of the script (NB: not what was written to standard out though). It's up to you what to do with that.
[EDIT]: I looked this up in more detail. It's nastier than it appears, because C++/Tk hides away Tcl quite deep inside itself. In so far as I can see, you do this (untested!):
#include "cpptk.h" // might need "base/cpptkbase.h" instead
#include <string>
// This next part is in a function or method...
std::string script("the script to evaluate goes here");
std::string result = Tk::details::Expr(script,true);

Related

Boost Asio, chat example: How do I manually write in the body of the message? [chat_message.hpp]

I am following the "chat example" from Boost Asio's tutorials. As I don't have much experience with Boost Asio, I am implementing my own client server application using the chat example and modifying it according to my needs.
Now I am defining a Protocol.hpp file, which contains keywords for the network protocol. For example:
Protocol.hpp
#ifndef PROTOCOL_HPP
#define PROTOCOL_HPP
#include <iostream>
extern const char ACK;
#endif
Protocol.cpp
#include "Protocol.hpp"
const char ACK = "1";
If you take a look at the "chat_message.hpp" class, you will find the following:
const char* data() const
{
return data_;
}
char* data()
{
return data_;
}
I have tried the following:
std::sprintf(write_msgs_.data(), ACK, 2);
As well as trying to assign directly the desired code like this —however I guess that I am obtaining the const function—:
write_msgs_.data() = ACK;
I have thought of using the string class and then somehow convert it to char, in order to copy it in the write_msgs_.data(), or even adding every character with a loop. I am relatively new to C++, and I don't seem to find a good solution for this. Is there any proper way of doing this?
Thank you very much in advance.
I found it. I should have checked how the example application does it, and it simply uses memcpy from the cstring library. Therefore, anyone having the same problem as me should use the following:
main of the chat_client.cpp file:
char line[chat_message::max_body_length + 1];
while (std::cin.getline(line, chat_message::max_body_length + 1))
{
using namespace std; // For strlen and memcpy.
chat_message msg;
msg.body_length(strlen(line));
memcpy(msg.body(), line, msg.body_length());
msg.encode_header();
c.write(msg);
}
As you can see there is a char line variable that will hold the written text. After this, memcpy is used to copy the line to the body of the message.

Force GCC not to optimize away an unused variable?

One of the namespaces in my program is spread between two files. One provides the "engine", the other uses the "engine" to perform various commands. All of the initializations are performed on the "engine" side, including caching parameters fetched from setup library.
So, there's engine.cpp with:
#include <stdio.h>
#include "ns.h"
namespace MyNS
{
unsigned char variable = 0;
void init()
{
variable = 5;
printf("Init: var = %d\n",variable);
}
void handler()
{
// printf("Handler: var = %d\n",variable);
}
}
The variable happens never to be used again in engine.cpp but it's extensively used in commands.cpp.
#include <stdio.h>
#include "ns.h"
namespace MyNS
{
extern unsigned char variable;
void command()
{
printf("Command: var = %d\n",variable);
}
}
After compiling and linking, I'm getting:
Init: var = 5
Command: var = 1
Now, if I uncomment the printf() in handler() I'm getting:
Engine: var = 5
Command: var = 5
Handler: var = 5
What would be the "correct" way to force GCC not to optimize it away in such a way that accessing it through extern from the other file would fetch the right value? Preferably without reducing the -O level for the rest of the application?
(for completeness case, main.h and ns.h: )
#include "ns.h"
int main(int argc, char** argv)
{
MyNS::init();
MyNS::command();
MyNS::handler();
return 0;
}
namespace MyNS
{
void init();
void command();
void handler();
}
This minimized testcase doesn't exhibit this particular behavior; it seems one needs this situation to occur in much more complex environment to happen...
eh... the solution was quite trivial.
I exchanged places of the declaration and definition of the variable.
engine.cpp:
extern unsigned char variable;
command.cpp:
unsigned char variable = 0;
That way the compiler has no doubts about need for this variable's existence while compiling commands and in engine it has to reach to the existing instance, it can't just create a temporary one on the spot.
EDIT: Now I've discovered another peculiarity. The value changes depending on where it's written to. The section of code in question is:
1: varso = SharedObject::Instance()->varso;
2: memset(det_map,0,sizeof(det_map));
3: memset(gr_map,0xFF,sizeof(gr_map));
4: memset(gr_ped,false,sizeof(gr_ped));
5: memset(&stan,0,sizeof(stan));
6: stan.SOTUstage = 1;
7: PR_SOTU = varso->NrPSOTU;
The variable occurs near a place where several arrays are initialized with memset. The variable in question is PR_SOTU (the uppercase is inherited from when it was still a macro, and since it acts along with several other macros acting in a very similar context, it's likely to stay that way).
If move the assignment from its line 7 and place it after lines 1, 2 or 3, it receives the correct value 5. Placed after line 4 it gets the value 18. Anything below, and the value is 1. I moved definition of the variable to a different place (it was the last on the list of all namespace-globals, now it's first) to exclude possibility something writes at that specific memory location, but the behavior remains.

Best practice for creating silent CLI option?

I have a program that writes occasional status updates, but I'm sure some will want it to run silently so I have provided a -s / --silent CLI option. What would be the best way to have my program abide by that parameter?
This method adds a global variable, and still executes the fprintf functions adding to the smell slightly.
FILE *outfile
if (silent) {
*outfile = NULL;}
else {
*outfile = stderr;}
fprintf (outfile, "This program can run silently");
This method also adds a global variable, and if statments littered throughout the code also adding to the smell.
if (!(silent)){
fprintf (stdout, "This program can run silently");
}
Ternary operators with a global variable might be more elegant:
(silent) ? : fprintf(stdout, "This program can run silently");
Are there better practices than any of these methods? Are there shortcomings to any one that I'm not seeing?
As Don Shankin said in his comment, the most maintainable way to do it is by wrapping fprintf() in a function that implements your filtering logic, and then have your code call the wrapper function rather than calling fprintf() directly. That way you only have to implement the filtering logic in a single location and not all over the place. Here's a program that demonstrates the technique:
#include <stdio.h>
#include <stdarg.h>
bool silent = false;
void my_fprintf(FILE * outFile, const char * fmt, ...)
{
if (silent == false)
{
va_list argsList;
va_start(argsList, fmt);
vfprintf(outFile, fmt, argsList);
va_end(argsList);
}
}
int main(int argc, char ** argv)
{
my_fprintf(stdout, "Not silent now...\n");
silent = true;
my_fprintf(stdout, "But now I'm silent, so you won't see this!\n");
silent = false;
my_fprintf(stdout, "Silent is false again!\n");
return 0;
}
... and here is the output from the program:
Not silent now...
Silent is false again!
Since it's also tagged C++, I'd like to point out that you can replace the output buffer of std::cout by calling std::cout.rdbuf(newbuf). A "devnull buff" is trivial to implement.

if statement in main function c++

Okay, probably a dumb question to you guys but I can't figure it out.
So I'm taking a c++ basics course in class and so far I'm struggling/crying.
I can't show you guys my code because I'm not allowed/there are consequences if I'm caught but I could probably give a example.
I'm using xcode. So when I compile, I get two errors below (image provided).
I searched for similar questions, but those seem too complex compared to what I'm doing. In addition, the only includes I have are iostream and string.
I know the problem occurs when I add an if statement in my main function. I know this because when I delete it, everything compiles as expected. Yet when I add it again to the main function, these errors occur.
So my question is, based on what I know, is it proper to add an if statements whenever in the main function?
Below is an example. I wrote the functions below and called above.
#include <iostream>
#include <string>
using namespace std;
// example functions that I just made up to explain the structure of my actual code.
//Don't bother trying to understand it. It's just to explain that
//I wrote my functions at the
// bottom and called it at the top.
int getNumberofWins(param1, param2);
string getTheName(int player1);
int executeCycle(string p1_name, string p2_name);
void stateWinner(string winner_name);
int main {
playerOne = getTheName(1);
playerTwo = getTheName(2);
r1 = executeCycle(playerOne, playerTwo);
r2= executeCycle(playerOne, playerTwo);
totalWin1 = getNumberOfWins(1, r1, r2);
totalWin2 = getNumberOfWins(2, r1, r2);
cout << totalWin1;
//This is the where I get the errors. When I delete the if statement,
//Everything compiles. When I add it, an error occurs.
if (totalWin1 == 2){
stateWinner(playerOne);
}
return 0;
}
string getTheName(int player1){
string playerOne;
string playerTwo;
if(player_number == 1){ code code code
}
}
int getNumberofWins (int param1, int param2){
code code code
}
int executeCycle(string p1_name, string p2_name){
code code code
}
void stateWinner(string winner_name){
if(!winner_name.empty()){
code code code
}
I hope it's fine if the code above isn't accurate. I think the point is that once I add my if statement to the main function, the two errors show up.
actually...now that I look at it, they both seem like similar errors. I just don't know why they both appear...
Sorry if this is an obvious answer or if it isn't clear.
The "announceWinner" function is not defined anywhere, ie there's no
void announceWinner () {
// code
}
anywhere. Either you haven't written it yet, or the file that contains it is not being compiled & linked with the main program.

How to redirect std::cout to a UITextView?

I'm adding C++ code to an iOS application, and I would like to use a UITextView as a way to display what's going through std::cout. I don't want to modify the C++ code too much.
So far, I have defined a string stream named stdcout, in the scope of the C++ code I'm interested in capturing the output, and I'm updating the UITextView after the C++ block returns. This is a bit intrusive, as I need to do some manual text replacing, and it's error prone.
Is there a better way to do this ?
You can look at rdbuf().
If you care about performance/flexibility, you could write a custom stream buffer and implement the overflow members so that you get "automatic" "live" updating.
Here's a simple example relaying to a stringstream:
#include <sstream>
#include <iostream>
int main()
{
std::ostringstream oss;
auto saved = std::cout.rdbuf(oss.rdbuf());
std::cout << "hello world" << std::endl;
std::cout.rdbuf(saved);
return oss.str().length();
}
This program exits with exitcode '12' on my cygwin shell:
./test.exe; echo $?
12