|
a/src/utils/readfile.cpp |
|
b/src/utils/readfile.cpp |
|
... |
|
... |
34 |
#ifndef NO_NAMESPACES
|
34 |
#ifndef NO_NAMESPACES
|
35 |
using std::string;
|
35 |
using std::string;
|
36 |
#endif /* NO_NAMESPACES */
|
36 |
#endif /* NO_NAMESPACES */
|
37 |
|
37 |
|
38 |
#include "readfile.h"
|
38 |
#include "readfile.h"
|
|
|
39 |
#include "smallut.h"
|
39 |
|
40 |
|
40 |
#ifndef MIN
|
41 |
#ifndef MIN
|
41 |
#define MIN(A,B) ((A) < (B) ? (A) : (B))
|
42 |
#define MIN(A,B) ((A) < (B) ? (A) : (B))
|
42 |
#endif
|
43 |
#endif
|
43 |
|
|
|
44 |
static void caterrno(string *reason, const char *what, int _errno)
|
|
|
45 |
{
|
|
|
46 |
if (reason) {
|
|
|
47 |
reason->append(what);
|
|
|
48 |
reason->append(": errno: ");
|
|
|
49 |
char nbuf[20];
|
|
|
50 |
sprintf(nbuf, "%d", _errno);
|
|
|
51 |
reason->append(nbuf);
|
|
|
52 |
reason->append(" : ");
|
|
|
53 |
#ifdef sun
|
|
|
54 |
// Note: sun strerror is noted mt-safe ??
|
|
|
55 |
reason->append(strerror(_errno));
|
|
|
56 |
#else
|
|
|
57 |
#define ERRBUFSZ 200
|
|
|
58 |
char errbuf[ERRBUFSZ];
|
|
|
59 |
// There are 2 versions of strerror_r.
|
|
|
60 |
// - The GNU one returns a pointer to the message (maybe
|
|
|
61 |
// static storage or supplied buffer).
|
|
|
62 |
// - The POSIX one always stores in supplied buffer and
|
|
|
63 |
// returns 0 on success. As the possibility of error and
|
|
|
64 |
// error code are not specified, we're basically doomed
|
|
|
65 |
// cause we can't use a test on the 0 value to know if we
|
|
|
66 |
// were returned a pointer...
|
|
|
67 |
// Also couldn't find an easy way to disable the gnu version without
|
|
|
68 |
// changing the cxxflags globally, so forget it.
|
|
|
69 |
// At worse we get no message at all here.
|
|
|
70 |
errbuf[0] = 0;
|
|
|
71 |
strerror_r(_errno, errbuf, ERRBUFSZ);
|
|
|
72 |
reason->append(errbuf);
|
|
|
73 |
#endif
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
44 |
|
77 |
class FileToString : public FileScanDo {
|
45 |
class FileToString : public FileScanDo {
|
78 |
public:
|
46 |
public:
|
79 |
FileToString(string& data) : m_data(data) {}
|
47 |
FileToString(string& data) : m_data(data) {}
|
80 |
string& m_data;
|
48 |
string& m_data;
|
|
... |
|
... |
85 |
}
|
53 |
}
|
86 |
bool data(const char *buf, int cnt, string *reason) {
|
54 |
bool data(const char *buf, int cnt, string *reason) {
|
87 |
try {
|
55 |
try {
|
88 |
m_data.append(buf, cnt);
|
56 |
m_data.append(buf, cnt);
|
89 |
} catch (...) {
|
57 |
} catch (...) {
|
90 |
caterrno(reason, "append", errno);
|
58 |
catstrerror(reason, "append", errno);
|
91 |
return false;
|
59 |
return false;
|
92 |
}
|
60 |
}
|
93 |
return true;
|
61 |
return true;
|
94 |
}
|
62 |
}
|
95 |
};
|
63 |
};
|
|
... |
|
... |
127 |
|
95 |
|
128 |
// If we have a file name, open it, else use stdin.
|
96 |
// If we have a file name, open it, else use stdin.
|
129 |
if (!fn.empty()) {
|
97 |
if (!fn.empty()) {
|
130 |
fd = open(fn.c_str(), O_RDONLY|O_STREAMING);
|
98 |
fd = open(fn.c_str(), O_RDONLY|O_STREAMING);
|
131 |
if (fd < 0 || fstat(fd, &st) < 0) {
|
99 |
if (fd < 0 || fstat(fd, &st) < 0) {
|
132 |
caterrno(reason, "open/stat", errno);
|
100 |
catstrerror(reason, "open/stat", errno);
|
133 |
return false;
|
101 |
return false;
|
134 |
}
|
102 |
}
|
135 |
noclosing = false;
|
103 |
noclosing = false;
|
136 |
}
|
104 |
}
|
137 |
|
105 |
|
|
... |
|
... |
144 |
}
|
112 |
}
|
145 |
|
113 |
|
146 |
off_t curoffs = 0;
|
114 |
off_t curoffs = 0;
|
147 |
if (startoffs > 0 && !fn.empty()) {
|
115 |
if (startoffs > 0 && !fn.empty()) {
|
148 |
if (lseek(fd, startoffs, SEEK_SET) != startoffs) {
|
116 |
if (lseek(fd, startoffs, SEEK_SET) != startoffs) {
|
149 |
caterrno(reason, "lseek", errno);
|
117 |
catstrerror(reason, "lseek", errno);
|
150 |
return false;
|
118 |
return false;
|
151 |
}
|
119 |
}
|
152 |
curoffs = startoffs;
|
120 |
curoffs = startoffs;
|
153 |
}
|
121 |
}
|
154 |
|
122 |
|
|
... |
|
... |
163 |
if (cnttoread != size_t(-1)) {
|
131 |
if (cnttoread != size_t(-1)) {
|
164 |
toread = MIN(toread, cnttoread - totread);
|
132 |
toread = MIN(toread, cnttoread - totread);
|
165 |
}
|
133 |
}
|
166 |
int n = read(fd, buf, toread);
|
134 |
int n = read(fd, buf, toread);
|
167 |
if (n < 0) {
|
135 |
if (n < 0) {
|
168 |
caterrno(reason, "read", errno);
|
136 |
catstrerror(reason, "read", errno);
|
169 |
goto out;
|
137 |
goto out;
|
170 |
}
|
138 |
}
|
171 |
if (n == 0)
|
139 |
if (n == 0)
|
172 |
break;
|
140 |
break;
|
173 |
|
141 |
|