|
a/src/utils/readfile.cpp |
|
b/src/utils/readfile.cpp |
1 |
#ifndef lint
|
1 |
#ifndef lint
|
2 |
static char rcsid[] = "@(#$Id: readfile.cpp,v 1.3 2006-01-23 13:32:28 dockes Exp $ (C) 2004 J.F.Dockes";
|
2 |
static char rcsid[] = "@(#$Id: readfile.cpp,v 1.4 2007-06-02 08:30:42 dockes Exp $ (C) 2004 J.F.Dockes";
|
3 |
#endif
|
3 |
#endif
|
4 |
/*
|
4 |
/*
|
5 |
* This program is free software; you can redistribute it and/or modify
|
5 |
* This program is free software; you can redistribute it and/or modify
|
6 |
* it under the terms of the GNU General Public License as published by
|
6 |
* it under the terms of the GNU General Public License as published by
|
7 |
* the Free Software Foundation; either version 2 of the License, or
|
7 |
* the Free Software Foundation; either version 2 of the License, or
|
|
... |
|
... |
30 |
using std::string;
|
30 |
using std::string;
|
31 |
#endif /* NO_NAMESPACES */
|
31 |
#endif /* NO_NAMESPACES */
|
32 |
|
32 |
|
33 |
#include "readfile.h"
|
33 |
#include "readfile.h"
|
34 |
|
34 |
|
35 |
bool file_to_string(const string &fn, string &data)
|
35 |
bool file_to_string(const string &fn, string &data, string *reason)
|
36 |
{
|
36 |
{
|
|
|
37 |
#define ERRBUFSZ 200
|
|
|
38 |
char errbuf[ERRBUFSZ];
|
37 |
bool ret = false;
|
39 |
bool ret = false;
|
38 |
|
40 |
|
39 |
int fd = open(fn.c_str(), O_RDONLY|O_STREAMING);
|
41 |
int fd = open(fn.c_str(), O_RDONLY|O_STREAMING);
|
40 |
if (fd < 0) {
|
42 |
if (fd < 0) {
|
41 |
// perror("open");
|
43 |
if (reason) {
|
|
|
44 |
strerror_r(errno, errbuf, ERRBUFSZ);
|
|
|
45 |
*reason += string("file_to_string: open failed: ") + errbuf;
|
|
|
46 |
}
|
42 |
return false;
|
47 |
return false;
|
43 |
}
|
48 |
}
|
44 |
char buf[4096];
|
49 |
char buf[4096];
|
45 |
for (;;) {
|
50 |
for (;;) {
|
46 |
int n = read(fd, buf, 4096);
|
51 |
int n = read(fd, buf, 4096);
|
47 |
if (n < 0) {
|
52 |
if (n < 0) {
|
48 |
// perror("read");
|
53 |
if (reason) {
|
|
|
54 |
strerror_r(errno, errbuf, ERRBUFSZ);
|
|
|
55 |
*reason += string("file_to_string: read failed: ") + errbuf;
|
|
|
56 |
}
|
49 |
goto out;
|
57 |
goto out;
|
50 |
}
|
58 |
}
|
51 |
if (n == 0)
|
59 |
if (n == 0)
|
52 |
break;
|
60 |
break;
|
53 |
|
61 |
|
54 |
try {
|
62 |
try {
|
55 |
data.append(buf, n);
|
63 |
data.append(buf, n);
|
56 |
} catch (...) {
|
64 |
} catch (...) {
|
57 |
// fprintf(stderr, "file_to_string: out of memory\n");
|
65 |
if (reason) {
|
|
|
66 |
strerror_r(errno, errbuf, ERRBUFSZ);
|
|
|
67 |
*reason += string("file_to_string: out of memory? : ") +errbuf;
|
|
|
68 |
}
|
58 |
goto out;
|
69 |
goto out;
|
59 |
}
|
70 |
}
|
60 |
}
|
71 |
}
|
61 |
|
72 |
|
62 |
ret = true;
|
73 |
ret = true;
|