Sorry, problem with attach...
Here is source:
#include <dlfcn.h>
#include <iostream>
#include <stdexcept>
#include <string>
void* hJLib = NULL;
int hJ = 0;
typedef int(*f_JInit)(void); // returns 0 for error or handle
typedef int(*f_JFree)(int); // returns 0 or error code
typedef int(*f_JDo)(int handle, const char* sentence); // returns 0 or
error code
typedef int(*f_JGetM)(int handle, const char* name, int* type, int*
rank, int** shape, void* data); // returns 0 or error code
typedef int(*f_JSetM)(int handle, const char* name, int* type, int*
rank, int** shape, void* data); // returns 0 or error code
typedef void*(*f_JGetA)(int handle, int name_length, const char* name);
// returns 0 for error or pointer to 3!:1 format
typedef int(*f_JSetA)(int handle, int name_length, const char* name, int
data_length, void* data); // returns 0 or error code
// JSM
// JErrorTextM
// Jga
// JGetLocale
f_JInit JInit;
f_JFree JFree;
f_JDo JDo;
f_JGetM JGetM;
f_JSetM JSetM;
f_JGetA JGetA;
f_JSetA JSetA;
#define J_ATTACH_FUNCTION(x) if(!(x=(f_##x)dlsym(hJLib,#x))) throw
std::logic_error(dlerror());
void init()
{
if(!(hJLib=dlopen("libj601.so",RTLD_LAZY))) throw
std::logic_error(dlerror());
J_ATTACH_FUNCTION(JInit);
J_ATTACH_FUNCTION(JFree);
J_ATTACH_FUNCTION(JDo);
J_ATTACH_FUNCTION(JGetM);
J_ATTACH_FUNCTION(JSetM);
J_ATTACH_FUNCTION(JGetA);
J_ATTACH_FUNCTION(JSetA);
if(!(hJ=JInit())){dlclose(hJLib); throw std::logic_error("Error
initializing J");}
}
void cleanup()
{
JFree(hJ);
dlclose(hJLib);
}
void test()
{
long rv;
std::string sentence;
int type,rank,size;
int* shape;
void* data;
std::cout<<std::endl;
sentence="1+2";
std::cout<<"sentence: "<<sentence<<std::endl;
rv=JDo(hJ, (std::string("result =: ")+sentence).c_str());
if(rv!=0) std::logic_error("JDo error");
rv=JGetM(hJ, "result", &type, &rank, &shape, &data);
if(rv!=0) std::logic_error("JGetM error");
std::cout<<"result type: "<<type<<std::endl;
std::cout<<"result rank: "<<rank<<std::endl;
std::cout<<"result shape: ";for(int i=0; i<rank;
++i)std::cout<<shape[i]<<" "; std::cout<<std::endl;
std::cout<<"result data: "<<*(int*)data<<std::endl;
std::cout<<std::endl;
sentence="'1+2 is ', \":1+2";
std::cout<<"sentence: "<<sentence<<std::endl;
rv=JDo(hJ, (std::string("result =: ")+sentence).c_str());
if(rv!=0) std::logic_error("JDo error");
rv=JGetM(hJ, "result", &type, &rank, &shape, &data);
if(rv!=0) std::logic_error("JGetM error");
std::cout<<"result type: "<<type<<std::endl;
std::cout<<"result rank: "<<rank<<std::endl;
std::cout<<"result shape: ";for(int i=0; i<rank;
++i)std::cout<<shape[i]<<" "; std::cout<<std::endl;
size=1;
for(int i=0; i<rank; ++i) size*=shape[i];
std::cout<<"result data: "<<std::string((const
char*)data,size)<<std::endl;
std::cout<<std::endl;
}
int main(void)
{
try
{
init();
test();
cleanup();
}
catch(const std::exception& e)
{
std::cout<<e.what()<<std::endl;
}
catch(...)
{
std::cout<<"Unknown exception catched"<<std::endl;
}
return 0;
}
The attached is simple example of embedding J under unix.
It would be great if somebody extent this example...
------------------------------------------------------------------------
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
Yuri Burger,
Senior Researcher
Kiev Zoral Development Center
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm