Browse Source

Intercept exit then call gmt module

test1
Michael Uleysky 9 years ago
parent
commit
03ce0ef46c
  1. 40
      test/gmttest.cpp

40
test/gmttest.cpp

@ -14,34 +14,36 @@ struct gmtworkthreadpars
{
void* api;
const char* module;
void* opts;
struct GMT_OPTION* opts;
int fd;
int ret;
};
void gmtonexithandler(int ret, void* x)
{
reinterpret_cast<struct gmtworkthreadpars*>(x)->ret=ret;
close(reinterpret_cast<struct gmtworkthreadpars*>(x)->fd);
pthread_exit(&(reinterpret_cast<struct gmtworkthreadpars*>(x)->ret));
}
void* gmtworkthread(void* x)
{
struct gmtworkthreadpars* p=reinterpret_cast<struct gmtworkthreadpars*>(x);
char c='\n';
//unshare(CLONE_FILES);
//dup2(p->fd,1);
//write(p->fd,&c,1);
GMT_Append_Option(p->api,GMT_Make_Option(p->api,'>',const_cast<char*>(("/dev/fd/"+std::to_string(p->fd)).c_str())),reinterpret_cast<struct GMT_OPTION*>(p->opts));
//write(p->fd,&c,1); // Use this instead of mutex
on_exit(gmtonexithandler,x);
GMT_Append_Option(p->api,GMT_Make_Option(p->api,'>',const_cast<char*>(("/dev/fd/"+std::to_string(p->fd)).c_str())),p->opts);
p->ret=GMT_Call_Module(p->api,p->module,GMT_MODULE_OPT,p->opts);
//close(1);
close(p->fd);
exit(p->ret);
return 0;
}
int callgmtmodule(void *api, const char *module, void *opts, std::string& res)
int callgmtmodule(void *api, const char *module, struct GMT_OPTION *opts, std::string& res)
{
int pipefd[2];
ssize_t br;
char buffer[4096];
pthread_t wthr;
struct gmtworkthreadpars p;
int *pret;
pipe(pipefd);
p.api=api;
@ -52,17 +54,19 @@ int callgmtmodule(void *api, const char *module, void *opts, std::string& res)
pthread_create(&wthr,0,&gmtworkthread,&p);
res.erase();
read(pipefd[0],buffer,1);
close(pipefd[1]);
//read(pipefd[0],buffer,1);
//close(pipefd[1]);
do
{
br=read(pipefd[0],buffer,4096);
std::cout<<res.length()<<" "<<br<<std::endl;
res.append(buffer,br);
} while(0!=br);
close(pipefd[0]);
res.shrink_to_fit();
pthread_join(wthr,0);
return p.ret;
pthread_join(wthr,reinterpret_cast<void**>(&pret));
return *pret;
}
@ -77,15 +81,15 @@ int main()
char fname[16];
std::string out;
gmtapi=GMT_Create_Session(text,2,0,print_func);
gmtapi=GMT_Create_Session(text,2,3,print_func);
io=GMT_Register_IO(gmtapi,GMT_IS_TEXTSET,GMT_IS_DUPLICATE,GMT_IS_NONE,GMT_OUT,0,0);
GMT_Encode_ID(gmtapi,fname,io);
opts=GMT_Create_Options(gmtapi,0,(void*)args.c_str());
//ret=GMT_Init_IO(gmtapi,GMT_IS_TEXTSET,GMT_IS_NONE,GMT_OUT,5,0,opts);
std::cout<<io<<" "<<GMT_NOTSET<<" "<<fname<<" "<<ret<<std::endl;
callgmtmodule(gmtapi,"psbasemap",opts,out);
ret=callgmtmodule(gmtapi,"psbasemap",opts,out);
std::cout<<"Return value: "<<ret<<std::endl;
std::cout<<out.max_size()<<" "<<out.capacity()<<" "<<out.size()<<std::endl;
GMT_Destroy_Options(gmtapi,&opts);

Loading…
Cancel
Save