|
a/src/readfile.h |
|
b/src/readfile.h |
|
... |
|
... |
19 |
|
19 |
|
20 |
#include <sys/types.h>
|
20 |
#include <sys/types.h>
|
21 |
|
21 |
|
22 |
#include <string>
|
22 |
#include <string>
|
23 |
|
23 |
|
24 |
/**
|
24 |
class FileScanUpstream;
|
25 |
* Read file in chunks, calling an accumulator for each chunk. Can be used
|
25 |
|
26 |
* for reading in a file, computing an md5...
|
26 |
/** Data sink for the file reader. */
|
27 |
*/
|
|
|
28 |
class FileScanDo {
|
27 |
class FileScanDo {
|
29 |
public:
|
28 |
public:
|
30 |
virtual ~FileScanDo() {}
|
29 |
virtual ~FileScanDo() {}
|
|
|
30 |
/* Initialize and allocate.
|
|
|
31 |
* @param size if set, lower bound of data size.
|
|
|
32 |
* @param reason[output] set to error message in case of error.
|
|
|
33 |
* @return false for error (file_scan will return), true if ok.
|
|
|
34 |
*/
|
31 |
virtual bool init(size_t size, std::string *reason) = 0;
|
35 |
virtual bool init(int64_t size, std::string *reason) = 0;
|
|
|
36 |
/* Process chunk of data
|
|
|
37 |
* @param buf the data buffer.
|
|
|
38 |
* @param cnt byte count.
|
|
|
39 |
* @param reason[output] set to error message in case of error.
|
|
|
40 |
* @return false for error (file_scan will return), true if ok.
|
|
|
41 |
*/
|
32 |
virtual bool data(const char *buf, int cnt, std::string* reason) = 0;
|
42 |
virtual bool data(const char *buf, int cnt, std::string *reason) = 0;
|
|
|
43 |
|
|
|
44 |
virtual void setUpstream(FileScanUpstream*) {}
|
33 |
};
|
45 |
};
|
34 |
bool file_scan(const std::string& filename, FileScanDo* doer, std::string *reason = 0);
|
46 |
|
35 |
/* Same but only process count cnt from offset offs. Set cnt to size_t(-1)
|
47 |
/** Open and read file, calling the FileScanDo data() method for each chunk.
|
36 |
* for no limit */
|
48 |
*
|
|
|
49 |
* @param filename File name. Use empty value for stdin
|
|
|
50 |
|
|
|
51 |
* @param doer the data processor. The init() method will be called
|
|
|
52 |
* initially witht a lower bound of the data size (may be used to
|
|
|
53 |
* reserve a buffer), or with a 0 size if nothing is known about the
|
|
|
54 |
* size. The data() method will be called for every chunk of data
|
|
|
55 |
* read.
|
|
|
56 |
* @param offs Start offset. If not zero, will disable decompression
|
|
|
57 |
* (set to -1 to start at 0 with no decompression).
|
|
|
58 |
* @param cnt Max bytes in output. Set cnt to -1 for no limit.
|
|
|
59 |
* @param[output] md5p If not null, points to a string to store the hex ascii
|
|
|
60 |
* md5 of the uncompressed data.
|
|
|
61 |
* @param[output] reason If not null, points to a string for storing an
|
|
|
62 |
* error message if the return value is false.
|
|
|
63 |
* @return true if the operation ended normally, else false.
|
|
|
64 |
*/
|
37 |
bool file_scan(const std::string& fn, FileScanDo* doer, int64_t offs, size_t cnt,
|
65 |
bool file_scan(const std::string& fn, FileScanDo* doer, int64_t startoffs,
|
|
|
66 |
int64_t cnttoread, std::string *reason
|
|
|
67 |
#ifdef READFILE_ENABLE_MD5
|
|
|
68 |
, std::string *md5p
|
|
|
69 |
#endif
|
|
|
70 |
);
|
|
|
71 |
|
|
|
72 |
/** Same as above, not offset/cnt/md5 */
|
|
|
73 |
bool file_scan(const std::string& filename, FileScanDo* doer,
|
38 |
std::string *reason = 0);
|
74 |
std::string *reason);
|
|
|
75 |
|
|
|
76 |
/** Same as file_scan, from a memory buffer. No libz processing */
|
|
|
77 |
bool string_scan(const char *data, size_t cnt, FileScanDo* doer,
|
|
|
78 |
std::string *reason
|
|
|
79 |
#ifdef READFILE_ENABLE_MD5
|
|
|
80 |
, std::string *md5p
|
|
|
81 |
#endif
|
|
|
82 |
);
|
|
|
83 |
|
|
|
84 |
#if defined(READFILE_ENABLE_MINIZ)
|
|
|
85 |
/* Process a zip archive member */
|
|
|
86 |
bool file_scan(const std::string& filename, const std::string& membername,
|
|
|
87 |
FileScanDo* doer, std::string *reason);
|
|
|
88 |
bool string_scan(const char* data, size_t cnt, const std::string& membername,
|
|
|
89 |
FileScanDo* doer, std::string *reason);
|
|
|
90 |
#endif
|
39 |
|
91 |
|
40 |
/**
|
92 |
/**
|
41 |
* Read file into string.
|
93 |
* Read file into string.
|
42 |
* @return true for ok, false else
|
94 |
* @return true for ok, false else
|
43 |
*/
|
95 |
*/
|
44 |
bool file_to_string(const std::string& filename, std::string& data, std::string *reason = 0);
|
96 |
bool file_to_string(const std::string& filename, std::string& data,
|
|
|
97 |
std::string *reason = 0);
|
45 |
|
98 |
|
46 |
/** Read file chunk into string. Set cnt to size_t(-1) for whole file */
|
99 |
/** Read file chunk into string. Set cnt to -1 for going to
|
|
|
100 |
* eof, offs to -1 for going from the start without decompression */
|
47 |
bool file_to_string(const std::string& filename, std::string& data,
|
101 |
bool file_to_string(const std::string& filename, std::string& data,
|
48 |
int64_t offs, size_t cnt, std::string *reason = 0);
|
102 |
int64_t offs, size_t cnt, std::string *reason = 0);
|
49 |
|
103 |
|
|
|
104 |
|
50 |
#endif /* _READFILE_H_INCLUDED_ */
|
105 |
#endif /* _READFILE_H_INCLUDED_ */
|