I am trying to print a string with universal characters stored in it. If i initialized the string with the following:
string test = "\u000D\u000A\u000D\u000Aclass Solution {\u000D\u000Apublic:\u000D\u000A
cout << test << endl;
it would print out the output I want:
class Solution {
but if I get the same string from Curl result, like following:
curl_easy_setopt(curl, CURLOPT_WRITEDATA, addressof(test));
res = curl_easy_perform(curl);
cout << test << endl;
it would print out:
\u000D\u000A\u000D\u000Aclass Solution {\u000D\u000Apublic:\u000D\u000A
I tried to turn the test into const char * by c_str and then print it out, but it is still not working. I tried to google it for a few hours but unfortunately I cannot find the answer.
I am using Mac and compile with
clang++ -std=c++11 -Wall -Wextra -lcurl
May anyone help me on this?
Thanks a lot!
Just in case anyone facing the same issue, I get this done by using ICU library with UnicodeString Converter.
Related
I am trying to create an table from a csv file. In the csv file I have the three fields that are already filtered so that it does not generate problems, but when I run the code, the report file does not generate any output and it must be an error when going through the file or I do not know where else is the failure:
The csv input looks similar to this:
I cannot see an obvious error in your file but was able to generate the required html file using an awk script file as follows (the correct #! path can be found using which awk in terminal):
#! /usr/bin/awk -f
BEGIN {
FS =","
print "<!DOCTYPE html>\n<head><title>Report</title>"
print "<link rel=\"stylesheet\" href=\"style.css\">"
print "<meta charset=\"utf-8\"/>"
print "</head>\n<body>\n<div class=\"head-style\"<h2>Report</h2>\n</div>"
}
NR<2 {
print "<table>\n<tr><th>"$1"</th><th>"$2"</th><th>"$3"</th>"
}
NR>1 {
print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td></tr>"
}
END {
print "</table>\n<div class=\"footer\">\n<p>0000 st</p>\n</div>\n</body>\n</html>"
}
I formatted the printing using field references $1 etc. between quotted string. Note also, quotes can be escaped for printing.
I saved the script as awkScript.awk and made it executable from the command line using:
chmod +x awkScript.awk
This can then be executed on the csv file with the command:
./awkScript.awk rm.csv > rep.html
It looks like you forgot to pass the fields as parameters to function print_line(Platan, Recl, Tror).
# Since "Platan" and "Recl" are strings, I think the format string
# should be: "%s %s %.2f %s\n" (BTW, I included "\n" to improve readability).
function print_line(Platan, Recl, Tror) {
printf("%s %s %.2f %s\n", "<tr><td>"Platan"</td>", "<td>"Recl"</td><td>", Tror, "</td></tr>") ;
}
{
if (NR > 1) {
print_line($1, $2, $3) # should solve
}
}
I'm trying to write a very simple Tcl application in C++:
#include <tcl.h>
#include <iostream>
int main (int argc, char *argv[])
{
std::cout << "Calling Tcl_FindExecutable." << std::endl;
Tcl_FindExecutable (argv[0]);
std::cout << "Calling Tcl_CreateInterp." << std::endl;
Tcl_Interp *pInterp = Tcl_CreateInterp ();
if (Tcl_Eval (pInterp, "puts stdout {Hello, World!}") != TCL_OK)
{
std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
return (0);
}
if (Tcl_Eval (pInterp, "puts stdout [info nameofexecutable]") != TCL_OK)
{
std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
return (0);
}
return (1);
}
I can compile it via g++ -c Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o
but when I link it, with g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o
I get errors saying that all the Tcl library procedures are undefined.
What am I doing wrong, please?
Edit
The actual commands were
$ g++ -c -Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o
$ g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o
noddy.o: In function 'main':
noddy.cpp:(.text+0x37): undefined reference to 'Tcl_FindExecutable'
noddy.cpp:(.text+0x60): undefined reference to 'Tcl_CreateInterp'
noddy.cpp:(.text+0x78): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0x8d): undefined reference to 'Tcl_GetStringResult'
noddy.cpp:(.text+0xda): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0xef): undefined reference to 'Tcl_GetStringResult'
collect2: ld returned 1 exit status
In link statements the order of object modules and libraries is significant. You should include the object first and then the libraries (redone as C to avoid installing g++):
> gcc -Wall -I/opt/tcl/include -c noddy.c
> gcc -o noddy.exe noddy.o -L/opt/tcl/lib -ltcl86
> noddy
Calling Tcl_FindExecutable.
Calling Tcl_CreateInterp.
Hello, World!
C:/Code/noddy.exe
But:
> gcc -o noddy.exe -L/opt/tcl/lib -ltcl86 noddy.o
noddy.o:noddy.c:(.text+0x23): undefined reference to '_imp__Tcl_FindExecutable'
noddy.o:noddy.c:(.text+0x36): undefined reference to '_imp__Tcl_CreateInterp'
noddy.o:noddy.c:(.text+0x50): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0x62): undefined reference to '_imp__Tcl_GetStringResult'
noddy.o:noddy.c:(.text+0x9b): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0xad): undefined reference to '_imp__Tcl_GetStringResult'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: noddy.o: bad reloc address 0x20 in section '.eh_frame'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
I am trying to learn Yesod and trying to implement a simple REST app where everytime a I get a GET request I write something to a file. Right now I have the following handler function:
getTestR =
do
return $ writeFile "test.txt" "Just something"
return $ object ["result" .= "Ok"]
What I was expecting is that the file test.txt would be created and I would obtain a JSON with {result=Ok}. However, I am obtaining the JSON, but the file is not being created.
I guess the writeFile is not being evaluated because of the lazy evaluation, but I have no idea how to overcome this problem. Thanks in advance.
just use liftIO:
getTestR =
do
liftIO $ writeFile "test.txt" "Just something"
return $ object ["result" .= "Ok"]
I'm trying to replace an old Tcl interface to C++ using SWIG. Here is an example class:
class test {
std::string str;
public:
test(const char * s):str(s) {}
void print() const {std::cout << str << std::endl;}
};
and here is the standard way to use it:
load ./example.so example
test s "this is a test string"
s print
But I want to preserve the simplicity of the old interface which does not use the "". I've found that I can do something like:
load ./example.so example
proc TEST {args} { test [lindex $args 0] [lrange $args 1 end] }
TEST s2 this is another test string
s2 print
Which looks simple and works flawlessly, however, of course, I cannot have the proc definition in the user script. I'm not sure where else I could place it. Is there a way to put it in the .i file?
I think you can put this in your .i file:
%init %{
Tcl_Eval(interp, "proc TEST {args} { ... }");
%}
That will insert a call to Tcl_Eval (which will make the procedure) into your module's initialization function, and as long as the interp context is available there (WARNING! Check this!) then you'll get everything nicely packaged.
If you've got lots of code, consider calling Tcl_EvalFile instead, or calling Tcl code that will source the script code (necessary if you've got a need for complex scripting to locate the file to read).
Script works well when run manually, but when I schdule it in cronjob it shows :
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "<html>\r\n<head><tit...") at /usr/local/lib/perl5/site_perl/5.14.2/JSON.pm line 171.
script itself:
#rest config vaiables
$ENV{'PERL_LWP_SSL_VERIFY_NONE'} = 0;
print "test\n";
my $client = REST::Client->new();
$client->addHeader('Authorization', 'Basic YWRtaW46cmFyaXRhbg==');
$client->addHeader('content_type', 'application/json');
$client->addHeader('accept', 'application/json');
$client->setHost('http://10.10.10.10');
$client->setTimeout(1000);
$useragent = $client->getUseragent();
print "test\n";
#Getting racks by pod
$req = '/api/v2/racks?name_like=2t';
#print " rekvest {$req}\n";
$client->request('GET', qq($req));
$racks = from_json($client->responseContent());
$datadump = Dumper (from_json($client->responseContent()));
crontab -l
*/2 * * * * /usr/local/bin/perl /folder/api/2t.pl > /dmitry/api/damnitout 2>&1
Appreciate any suggestion
Thank you,
Dmitry
It is difficult to say what is really happening, but in my experience 99% issues of running stuff in crontab stems from differences in environment variables.
Typical way to debug this: in the beginning of your script add block like this:
foreach my $key (keys %ENV) {
print "$key = $ENV{$key}\n";
}
Run it in console, look at the output, save it in log file.
Now, repeat the same in crontab and save it into log file (you have already done that - this is good).
See if there is any difference in environment variables when trying to run it both ways and try to fix it. In Perl, probably easiest is to alter environment by changing %ENV. After all differences are sorted out, there is no reason for this to not work right.
Good luck!