Hello Sylvain,

You can get Scilab outputs by setting your own output function.

void scilab_print(const char *text)
{
    std::cout << text << std::endl;
}

//redirect output to your own function:
setScilabOutputMethod(&scilab_print);

Commands send to call_scilab by SendScilabJob are muted ( execstr("cmd", "errcatch"); ), so to show outputs you need to explicitly call "disp" function in your code or use another mechanism to put your commands directly in queue of execution by using StoreConsoleCommand function.

See attached file as example.
Have a good weekend.
Antoine
Le 02/03/2019 à 00:41, SylvainCorlay a écrit :
Hello everyone,

This is my first post on this mailing list. I hope that I am not breaking
any rule!

I am interested in embedding the scilab interpreter in a C++ application.
The `call_scilab` API appears to be the wayt to go for simple code
execution, however I wonder it is possible to capture / redirect output
streams? The motivation is to create a kernel for Jupyter. I am aware of the
existing pexpect-based kernel but I would like to create a new kernel that
would be running in-process and not rely on piping to capture textual
output.

Note: this is something that we have already done for other languages.
/xeus-cling/ is a  C++ kernel
<https://blog.jupyter.org/interactive-workflows-for-c-with-jupyter-fe9b54227d92>
based on the cling C++ interpreter from CERN. /xeus-python/ is an
alternative Python kernel. Both are based on the xeus C++ implementation of
the Jupyter protocol, support auto-complete, quick help, rich output
display, interactive widgets.

I would like to know how far we could go with the `call_scilab` API with
respect to redirection, and whether there would be some means to access
lower-level control on the interpreter (for e.g. inspection, auto-complete
requests, getting a handle on the last value returned).

Looking forward to hearing from you, although I will be travelling in the
next few days.

Best,



--
Sent from: 
http://mailinglists.scilab.org/Scilab-developers-Mailing-Lists-Archives-f2574944.html
_______________________________________________
dev mailing list
dev@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/dev

#include <iostream>
#include <exception>
#include <stdio.h> /* stderr */
#include <windows.h> /* stderr */

#include "call_scilab.h"
#include "scilabWrite.hxx" /**/

// Filename: simple_call_scilab.c

extern "C"
{
    int StoreConsoleCommand(const char *command, int iWaitFor);
}

const char LAUNCHER_VERSION[] = "1.0";

#define SENDSCILABJOB(x) \
    if(SendScilabJob(x) != 0) { \
        fprintf(stderr, "Failed to execute \"%s\" command\n", x); \
        return 0; \
        }

void scilab_print(const char *text)
{
    std::cout << text << std::endl;
}

int main()
{
    try
    {
        if (StartScilab(NULL, NULL, NULL) == FALSE)
        {
            fprintf(stderr, "Error while calling StartScilab\n");
            return -1;
        }
    }
    catch (std::exception e)
    {
        fprintf(stderr, "oops: %s\n", e.what());
    }

    //redirect output to your own function:
    setScilabOutputMethod(&scilab_print);

    /*change mode to output only outputs*/
    //SENDSCILABJOB((char*)"mode(2);");

    StoreConsoleCommand("1+1", 1);

    SENDSCILABJOB((char*)"__a__ = 1+1;disp(__a__);");

    StoreConsoleCommand("rand(3, 3)", 1);

    SENDSCILABJOB((char*)"__a__ = rand(3, 3);disp(__a__);");

    if (TerminateScilab(NULL) == FALSE)
    {
        fprintf(stderr, "Error while calling TerminateScilab\n");
        return -2;
    }
    return 0;
}
_______________________________________________
dev mailing list
dev@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/dev

Reply via email to