a b/src/readfile.h
1
/* Copyright (C) 2004 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#ifndef _READFILE_H_INCLUDED_
18
#define _READFILE_H_INCLUDED_
19
20
#include <sys/types.h>
21
22
#include <string>
23
24
/**
25
 * Read file in chunks, calling an accumulator for each chunk. Can be used
26
 * for reading in a file, computing an md5...
27
 */
28
class FileScanDo {
29
public:
30
    virtual ~FileScanDo() {}
31
    virtual bool init(size_t size, std::string *reason) = 0;
32
    virtual bool data(const char *buf, int cnt, std::string* reason) = 0;
33
};
34
bool file_scan(const std::string& filename, FileScanDo* doer, std::string *reason = 0);
35
/* Same but only process count cnt from offset offs. Set cnt to size_t(-1)
36
 * for no limit */
37
bool file_scan(const std::string& fn, FileScanDo* doer, off_t offs, size_t cnt,
38
               std::string *reason = 0);
39
40
/**
41
 * Read file into string.
42
 * @return true for ok, false else
43
 */
44
bool file_to_string(const std::string& filename, std::string& data, std::string *reason = 0);
45
46
/** Read file chunk into string. Set cnt to size_t(-1) for whole file */
47
bool file_to_string(const std::string& filename, std::string& data,
48
                    off_t offs, size_t cnt, std::string *reason = 0);
49
50
#endif /* _READFILE_H_INCLUDED_ */