-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_file.cpp
159 lines (128 loc) · 2.95 KB
/
util_file.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#if defined(__linux__) || defined(__APPLE__)
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "scoped.hpp"
#include "stream.hpp"
#include "util_file.hpp"
namespace testbed
{
template < typename T >
class generic_free
{
public:
void operator()(T* arg)
{
assert(0 != arg);
free(arg);
}
};
template <>
class scoped_functor< FILE >
{
public:
void operator()(FILE* arg)
{
assert(0 != arg);
fclose(arg);
}
};
#if defined(__linux__) || defined(__APPLE__)
static bool
get_file_size(
const char* const filename,
size_t& size)
{
assert(0 != filename);
struct stat filestat;
if (-1 == stat(filename, &filestat))
{
stream::cerr << __FUNCTION__ << " failed to stat file '" << filename << "'\n";
return false;
}
if (!S_ISREG(filestat.st_mode))
{
stream::cerr << __FUNCTION__ << " encountered non-regular file '" << filename << "'\n";
return false;
}
size = filestat.st_size;
return true;
}
char*
get_buffer_from_file(
const char* const filename,
size_t& length)
{
assert(nullptr != filename);
if (!get_file_size(filename, length))
{
stream::cerr << __FUNCTION__ << " cannot get size of file '" << filename << "'\n";
return nullptr;
}
const scoped_ptr< FILE, scoped_functor > file(fopen(filename, "r"));
if (nullptr == file())
{
stream::cerr << __FUNCTION__ << " cannot open file '" << filename << "'\n";
return nullptr;
}
scoped_ptr< char, generic_free > source(
reinterpret_cast< char* >(malloc(length)));
if (nullptr == source())
{
stream::cerr << __FUNCTION__ << " cannot allocate memory for file '" << filename << "'\n";
return nullptr;
}
if (1 != fread(source(), length, 1, file()))
{
stream::cerr << __FUNCTION__ << " cannot read from file '" << filename << "'\n";
return nullptr;
}
char* const ret = source();
source.reset();
return ret;
}
#else // stat() unavailable
char*
get_buffer_from_file(
const char* const filename,
size_t& length)
{
assert(nullptr != filename);
const scoped_ptr< FILE, scoped_functor > file(fopen(filename, "r"));
if (!file())
{
stream::cerr << __FUNCTION__ << " cannot open file '" << filename << "'\n";
return nullptr;
}
if (fseek(file(), 0, SEEK_END))
{
stream::cerr << __FUNCTION__ << " cannot seek to the end of '" << filename << "'\n";
return nullptr;
}
length = ftell(file());
if (fseek(file(), 0, SEEK_SET))
{
stream::cerr << __FUNCTION__ << " cannot seek to the start of '" << filename << "'\n";
return nullptr;
}
scoped_ptr< char, generic_free > source(
reinterpret_cast< char* >(malloc(length)));
if (nullptr == source())
{
stream::cerr << __FUNCTION__ << " cannot allocate memory for file '" << filename << "'\n";
return nullptr;
}
if (1 != fread(source(), length, 1, file()))
{
stream::cerr << __FUNCTION__ << " cannot read from file '" << filename << "'\n";
return nullptr;
}
char* const ret = source();
source.reset();
return ret;
}
#endif
} // namespace testbed