modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / aio-test / nsync.cpp
blob7e7d8c96d9ebd9958f2cd613328d2929e6a5878f
1 /*
2 * nsync.cpp
4 * Created on: Jul 5, 2010
5 * Author: Aqua
6 */
8 #include"nsync.h"
10 using namespace std;
11 using namespace boost;
13 namespace bp = ::boost::process;
14 namespace ba = ::boost::asio;
16 #define BUFFER_SIZE 0xFFFFFF
18 int ImportFileList(nSyncBuf_s & buf, char * fn)
20 char errStr[4096];
21 int count(0);
23 if(access(fn, R_OK) == -1)
25 snprintf(errStr, 4095, "[%s][%s][%d]: File->%s", __FILE__, __FUNCTION__, __LINE__, fn);
26 perror(errStr);
27 return -1;
29 ifstream I(fn);
30 string strTmp;
31 while(true)
33 getline(I, strTmp, '\n');
34 if(!I)
35 break;
36 if(access(strTmp.c_str(), R_OK) == -1)
38 snprintf(errStr, 4095, "[%s][%s][%d]: File->%s", __FILE__, __FUNCTION__, __LINE__, strTmp.c_str());
39 perror(errStr);
40 continue;
42 buf.fnList.push_back(strTmp);
43 ++count;
46 return count;
49 int InsertFile(nSyncBuf_s & buf, char * fn)
51 char errStr[4096];
53 if(access(fn, R_OK) == -1)
55 snprintf(errStr, 4095, "[%s][%s][%d]: File->%s", __FILE__, __FUNCTION__, __LINE__, fn);
56 perror(errStr);
57 return -1;
59 else
60 buf.fnList.push_back(fn);
61 return 1;
64 bp::child StartChild(nSyncBuf_s & buf)
66 string exec = bp::find_executable_in_path("zcat");
68 vector<string> args;
69 args.push_back(bp::find_executable_in_path("zcat"));
70 args.push_back("-f");
72 for(uint i(0); i < buf.fnList.size(); ++i)
74 args.push_back(buf.fnList[i]);
77 bp::context ctx;
78 ctx.stdout_behavior = bp::capture_stream();
79 ctx.stderr_behavior = bp::inherit_stream();
80 ctx.environment = bp::self::get_environment();
82 return bp::launch(exec, args, ctx);
85 void endRead(nSyncBuf_s &, const boost::system::error_code & ec, size_t bytes_transferred);
87 void nSyncRead(nSyncBuf_s & buf)
89 buf.in.async_read_some(boost::asio::buffer(buf.buffer, BUFFER_SIZE),
90 boost::bind(&endRead, boost::ref(buf), ba::placeholders::error, ba::placeholders::bytes_transferred));
93 void endRead(nSyncBuf_s & buf, const boost::system::error_code & ec, size_t bytes_transferred)
95 if(!ec)
97 if(buf.atomEnabler)
98 buf.lock();
99 buf.Routine(buf, bytes_transferred);
100 nSyncRead(buf);
104 void nSync(nSyncBuf_s & buf)
106 if(buf.fnList.empty())
108 perror("Empty filename vector. Program Terminated");
109 exit(EXIT_FAILURE);
111 bp::child c = StartChild(buf);
113 buf.in.assign(c.get_stdout().handle().release());
115 nSyncRead(buf);
117 buf.io_service.run();
119 c.wait();
122 nSyncBuf_s::nSyncBuf_s(_ConsumeBufferRoutine_f func, bool atomic) : atomEnabler(atomic), Routine(func), in(io_service)
124 buffer = new char[BUFFER_SIZE];
125 if(!buffer)
127 perror("[nSyncBuf_s]: Buffer allocation error.");
128 throw bad_alloc();
132 nSyncBuf_s::~nSyncBuf_s()
134 if(buffer)
136 delete[] buffer;
138 else
140 perror("[nSyncBuf_s]: Null ptr of buffer when destruction.");
141 throw bad_alloc();
145 inline void nSyncBuf_s::lock()
147 atom.lock();
150 inline void nSyncBuf_s::unlock()
152 atom.unlock();
155 void nSyncBuf_s::release()
157 atom.unlock();
160 nSyncBufAry_t nSyncBuf_s::ptr()
162 return buffer;
165 nSyncBufAry_t nSyncBuf_s::operator *()
167 return buffer;