|
|
|
#include <gmt.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
int print_func(FILE* fd, const char* str)
|
|
|
|
{
|
|
|
|
std::cout<<"PRINT: "<<str<<std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gmtworkthreadpars
|
|
|
|
{
|
|
|
|
void* api;
|
|
|
|
const char* module;
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
exit(p->ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
p.module=module;
|
|
|
|
p.opts=opts;
|
|
|
|
p.fd=pipefd[1];
|
|
|
|
|
|
|
|
pthread_create(&wthr,0,&gmtworkthread,&p);
|
|
|
|
|
|
|
|
res.erase();
|
|
|
|
//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,reinterpret_cast<void**>(&pret));
|
|
|
|
|
|
|
|
return *pret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
char* text="GMT";
|
|
|
|
//void* out;
|
|
|
|
void* gmtapi;
|
|
|
|
std::string args="-R130/160/40/60 -JM12c -Xa2c -Ya2c -B5/5/swNE --GMT_HISTORY=f --FONT_ANNOT_PRIMARY=14p,Times-Bold,red";
|
|
|
|
struct GMT_OPTION* opts;
|
|
|
|
int io,ret;
|
|
|
|
char fname[16];
|
|
|
|
std::string out;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
GMT_Destroy_Session(gmtapi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|