-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
38 lines (32 loc) · 955 Bytes
/
Utils.cpp
File metadata and controls
38 lines (32 loc) · 955 Bytes
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
#include "Utils.h"
#include "Logging.h"
namespace ZipSync {
StdioFileHolder::~StdioFileHolder()
{}
StdioFileHolder::StdioFileHolder(FILE *f)
: StdioFileUniquePtr(f, fclose)
{}
StdioFileHolder::StdioFileHolder(const char *path, const char *mode)
: StdioFileUniquePtr(fopen(path, mode), fclose)
{
if (!get())
g_logger->errorf(lcCantOpenFile, "Failed to open file \"%s\"", path);
}
std::vector<uint8_t> ReadWholeFile(const std::string &filename) {
StdioFileHolder f(filename.c_str(), "rb");
fseek(f.get(), 0, SEEK_END);
int size = ftell(f.get());
fseek(f.get(), 0, SEEK_SET);
std::vector<uint8_t> res;
res.resize(size);
int numRead = fread(res.data(), 1, size, f.get());
ZipSyncAssert(numRead == size);
return res;
}
int GetFileSize(const std::string &filename) {
StdioFileHolder f(filename.c_str(), "rb");
fseek(f.get(), 0, SEEK_END);
int size = ftell(f.get());
return size;
}
}