G__load_text problems

View: New views
4 Messages — Rating Filter:   Alert me  

G__load_text problems

by Joost Kraaijeveld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am trying to run a null terminated string a source code (for the full
source of my program see below). It fails however with the following
output:

+ callMemoryString()
tempFileName: (tmp0)
result G__exec_text( test()): 0
Error: Function test() is not defined in current scope + callMemoryString()
 FILE:(tmpfile) LINE:2
!!!Dictionary position rewound... !!!Error recovered!!!

Using this as actual source code works:

std::string code =
"void test() "
"{"
        "printf(\"Kees Kroket\\n\");"
"}

What is the correct way of doing this?

TIA


/////////////////////////////// The code

#include <iostream>
#include "G__ci.h"
#include <string>

void callMemoryString()
{
        std::cout << "+ callMemoryString()" << std::endl;

        int state = G__init_cint( "cint");
        switch (state)
        {
                case G__INIT_CINT_SUCCESS:
                {
                        std::string code =
                        "#include <iostream> "
                        "void test() "
                        "{"
                                "std::cout << \"test()\" << std::endl;"
                        "}";

                        const char* tempFileName = G__load_text(code.c_str());
                        if(tempFileName)
                        {
                                std::cout << "tempFileName: " << tempFileName << std::endl;

                                long result = 0;
                                G__value resultValue;

                                resultValue = G__exec_text( "test()");
                                result= G__int(resultValue);
                                std::cout << "result G__exec_text( test()): " << result << std::endl;

                                G__unloadfile(tempFileName);
                        }else
                        {
                                std::cout << "Could not create a temp file" << std::endl;
                        }

                        break;
                }
                case G__INIT_CINT_SUCCESS_MAIN:
                {
                        std::cout << "G__INIT_CINT_SUCCESS_MAIN" << std::endl;
                        break;
                }
                case G__INIT_CINT_FAILURE:
                {
                        std::cout << "G__INIT_CINT_FAILURE" << std::endl;
                        break;
                }
                default:
                {
                        std::cout << "G__init_cint default" << std::endl;
                }
        }
        G__scratch_all();

        std::cout << "+ callMemoryString()" << std::endl;
}
int main(int argc, char** argv)
{
        callMemoryString();
        return 0;
}

--
Groeten,

Joost Kraaijeveld
Askesis B.V.
Molukkenstraat 14
6524NB Nijmegen
tel: 024-3888063 / 06-51855277
fax: 024-3608416
web: www.askesis.nl



Re: G__load_text problems

by Axel Naumann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Joost,

we got your email but sadly none of us has had the time yet to look at
the issue you reported :-( I expect that we will react by the end of the
week. Apologies for the delay.

Cheers, Axel.

Joost Kraaijeveld wrote on 10/31/2009 08:23 AM:

> Hi,
>
> I am trying to run a null terminated string a source code (for the full
> source of my program see below). It fails however with the following
> output:
>
> + callMemoryString()
> tempFileName: (tmp0)
> result G__exec_text( test()): 0
> Error: Function test() is not defined in current scope + callMemoryString()
>  FILE:(tmpfile) LINE:2
> !!!Dictionary position rewound... !!!Error recovered!!!
>
> Using this as actual source code works:
>
> std::string code =
> "void test() "
> "{"
> "printf(\"Kees Kroket\\n\");"
> "}
>
> What is the correct way of doing this?
>
> TIA
>
>
> /////////////////////////////// The code
>
> #include <iostream>
> #include "G__ci.h"
> #include <string>
>
> void callMemoryString()
> {
> std::cout << "+ callMemoryString()" << std::endl;
>
> int state = G__init_cint( "cint");
> switch (state)
> {
> case G__INIT_CINT_SUCCESS:
> {
> std::string code =
> "#include <iostream> "
> "void test() "
> "{"
> "std::cout << \"test()\" << std::endl;"
> "}";
>
> const char* tempFileName = G__load_text(code.c_str());
> if(tempFileName)
> {
> std::cout << "tempFileName: " << tempFileName << std::endl;
>
> long result = 0;
> G__value resultValue;
>
> resultValue = G__exec_text( "test()");
> result= G__int(resultValue);
> std::cout << "result G__exec_text( test()): " << result << std::endl;
>
> G__unloadfile(tempFileName);
> }else
> {
> std::cout << "Could not create a temp file" << std::endl;
> }
>
> break;
> }
> case G__INIT_CINT_SUCCESS_MAIN:
> {
> std::cout << "G__INIT_CINT_SUCCESS_MAIN" << std::endl;
> break;
> }
> case G__INIT_CINT_FAILURE:
> {
> std::cout << "G__INIT_CINT_FAILURE" << std::endl;
> break;
> }
> default:
> {
> std::cout << "G__init_cint default" << std::endl;
> }
> }
> G__scratch_all();
>
> std::cout << "+ callMemoryString()" << std::endl;
> }
> int main(int argc, char** argv)
> {
> callMemoryString();
> return 0;
> }
>


Re: G__load_text problems

by Axel Naumann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Joost,

you're missing the newlines in the text source, at least after the
#include line. This works for me:

                        "#include <iostream> \n"
                        "void test() \n"
                        "{\n"
                                "std::cout << \"test()\" << std::endl;\n"
                        "}\n";


Cheers, Axel.

Joost Kraaijeveld wrote on 10/31/2009 08:23 AM:

> Hi,
>
> I am trying to run a null terminated string a source code (for the full
> source of my program see below). It fails however with the following
> output:
>
> + callMemoryString()
> tempFileName: (tmp0)
> result G__exec_text( test()): 0
> Error: Function test() is not defined in current scope + callMemoryString()
>  FILE:(tmpfile) LINE:2
> !!!Dictionary position rewound... !!!Error recovered!!!
>
> Using this as actual source code works:
>
> std::string code =
> "void test() "
> "{"
> "printf(\"Kees Kroket\\n\");"
> "}
>
> What is the correct way of doing this?
>
> TIA
>
>
> /////////////////////////////// The code
>
> #include <iostream>
> #include "G__ci.h"
> #include <string>
>
> void callMemoryString()
> {
> std::cout << "+ callMemoryString()" << std::endl;
>
> int state = G__init_cint( "cint");
> switch (state)
> {
> case G__INIT_CINT_SUCCESS:
> {
> std::string code =
> "#include <iostream> "
> "void test() "
> "{"
> "std::cout << \"test()\" << std::endl;"
> "}";
>
> const char* tempFileName = G__load_text(code.c_str());
> if(tempFileName)
> {
> std::cout << "tempFileName: " << tempFileName << std::endl;
>
> long result = 0;
> G__value resultValue;
>
> resultValue = G__exec_text( "test()");
> result= G__int(resultValue);
> std::cout << "result G__exec_text( test()): " << result << std::endl;
>
> G__unloadfile(tempFileName);
> }else
> {
> std::cout << "Could not create a temp file" << std::endl;
> }
>
> break;
> }
> case G__INIT_CINT_SUCCESS_MAIN:
> {
> std::cout << "G__INIT_CINT_SUCCESS_MAIN" << std::endl;
> break;
> }
> case G__INIT_CINT_FAILURE:
> {
> std::cout << "G__INIT_CINT_FAILURE" << std::endl;
> break;
> }
> default:
> {
> std::cout << "G__init_cint default" << std::endl;
> }
> }
> G__scratch_all();
>
> std::cout << "+ callMemoryString()" << std::endl;
> }
> int main(int argc, char** argv)
> {
> callMemoryString();
> return 0;
> }
>


Re: G__load_text problems

by Joost Kraaijeveld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Axel,

On Mon, 2009-11-16 at 16:56 +0100, Axel Naumann wrote:
> you're missing the newlines in the text source, at least after the
> #include line. This works for me:
>
> "#include <iostream> \n"
> "void test() \n"
> "{\n"
> "std::cout << \"test()\" << std::endl;\n"
> "}\n";

OK, this works. Thanks.



--
Groeten,

Joost Kraaijeveld
Askesis B.V.
Molukkenstraat 14
6524NB Nijmegen
tel: 024-3888063 / 06-51855277
fax: 024-3608416
web: www.askesis.nl