-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_file.cc
47 lines (41 loc) · 1.35 KB
/
map_file.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
/**
sdhash file processing code via sdhash.org under apache license
lightly modified by @candicenonsense
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>
#include "util.h"
using namespace std;
/**
* Open & read in a file (compile w/ -D_FILE_OFFSET_BITS=64)
*/
processed_file_t *process_file(const char *fname) {
processed_file_t *mfile = (processed_file_t *) alloc_check( ALLOC_ZERO, sizeof( processed_file_t), "map_file", "mfile", ERROR_EXIT);
struct stat file_stat;
ifstream *is= new ifstream();
if( stat( fname, &file_stat)) {
fprintf( stderr, "Warning: Could not stat file '%s'. Skipping.\n", fname);
return NULL;
}
is->open(fname,ios::binary);
mfile->buffer = (uint8_t*)alloc_check(ALLOC_ZERO,sizeof(uint8_t)*file_stat.st_size, "read_file", "mfile", ERROR_EXIT);
is->read((char*)mfile->buffer,file_stat.st_size);
int64_t res=is->gcount();
if( res != file_stat.st_size) {
fprintf( stderr, "read failed: %s.\n", strerror( errno));
free( mfile);
is->close();
return NULL;
}
mfile->name = (char*)fname;
mfile->size = file_stat.st_size;
is->close();
return mfile;
}