-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshared.cc
148 lines (122 loc) · 2.81 KB
/
shared.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* shared: basic shared utilities and structures for tblutils - implementation
*
* Copyright(c) 2008-2010 Yuri D'Elia <[email protected]>
* Copyright(c) 2008-2010 EURAC, Institute of Genetic Medicine
*/
/*
* Headers
*/
// interface
#include "shared.hh"
// base headers
#include <memory>
using std::auto_ptr;
// c headers
#include <unistd.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
/*
* Generics
*/
Progress::Progress(long max, const char* type)
: max(max), type(type), clean(false)
{
tty = isatty(STDERR_FILENO);
}
string
sprintf2(const char* fmt, ...)
{
int n;
vector<char> buf(64);
va_list vl;
for(;;)
{
va_start(vl, fmt);
n = vsnprintf(buf.data(), buf.size(), fmt, vl);
va_end(vl);
if(n > -1 && static_cast<size_t>(n) < buf.size()) break;
buf.resize((n > -1? n + 1: buf.size() * 2));
}
return string(buf.data(), n);
}
vector<string>&
tokenize(vector<string>& dst, const string& buf,
const string& sep, bool coalesce)
{
string::size_type s = 0;
string::size_type e = buf.find_first_of(sep, s);
while(s != string::npos || e != string::npos)
{
if(e != string::npos)
dst.push_back(buf.substr(s, e - s));
else
{
dst.push_back(buf.substr(s));
break;
}
if(coalesce)
s = buf.find_first_not_of(sep, e);
else
s = e + 1;
e = buf.find_first_of(sep, s);
}
return dst;
}
/*
* I/O
*/
fix_string_matrix*
mapFixStringMatrix(const char** addr, const char* file, const char sep, int* fd)
{
// open the file
*addr = NULL;
int _fd = open(file, O_RDONLY);
if(fd) *fd = _fd;
if(_fd < 0)
throw runtime_error(sprintf2("%s: error: cannot open file!", file));
// map the file
struct stat stBuf;
fstat(_fd, &stBuf);
size_t addrLen = stBuf.st_size;
*addr = (const char*)mmap(NULL, addrLen, PROT_READ, MAP_SHARED, _fd, 0);
if(!fd) close(_fd);
if(!*addr)
throw runtime_error(sprintf2("%s: error: cannot map file!", file));
// start reading
auto_ptr<fix_string_matrix> m(new fix_string_matrix);
vector<fix_string> row;
const char* s = *addr;
const char* p;
for(p = s; p != (*addr + addrLen); ++p)
{
if(*p == '\n')
{
size_t len = p - s;
if(*(p - 1) == '\r') --len;
row.push_back(fix_string(s, len));
s = p + 1;
m->push_back(row);
row.clear();
if(m->front().size() != m->back().size())
throw runtime_error(sprintf2("%s: error: variable number of columns", file));
}
else if(*p == sep)
{
row.push_back(fix_string(s, p - s));
s = p + 1;
}
}
if(row.size())
{
// missing final newline
cerr << file << ": warning: missing final newline!\n";
row.push_back(fix_string(s, p - s));
m->push_back(row);
}
return m.release();
}