-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.cpp
More file actions
56 lines (45 loc) · 1.2 KB
/
file.cpp
File metadata and controls
56 lines (45 loc) · 1.2 KB
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
#include "file.h"
#include <cassert>
#include <trace/utils.h>
#define TRACE_TAG "rasutl2::file"
namespace OpenSsl_RsaUtl2 {
long file_getSize(FILE *file)
{
long fileSize;
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
return fileSize;
}
std::vector<char> file_readAll(FILE *file)
{
assert(file != nullptr);
int fileSize = file_getSize(file);
std::vector<char> buf(fileSize);
auto readin = fread(buf.data(), 1, fileSize, file);
if(readin != fileSize)
TRACE_ERROR_THROW("error occured when file read");
fclose(file);
return buf;
}
std::vector<char> file_readAll(std::string filename)
{
FILE *file = fopen(filename.c_str(), "rb");
return file_readAll(file);
}
void file_writeAll(const std::vector<char> &data, FILE *file)
{
int dataSize = data.size();
int wrs = 0; // wrote size
int errCnt = 0;
const int maxErrCnt = 13;
while(wrs < dataSize)
{
int w = fwrite(data.data() + wrs, 1, data.size() - wrs, file);
if(w > 0) wrs += w;
else errCnt++;
if(errCnt > maxErrCnt)
TRACE_ERROR_THROW("write failed exceed %d times", errCnt);
}
}
} // namespace OpenSsl_RsaUtl2