-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinputfile.cc
52 lines (43 loc) · 937 Bytes
/
inputfile.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
48
49
50
51
52
/*
* inputfile.cc
*
* Author: sheayun
*/
#include <stdio.h>
#include <inputfile.h>
InputFile::InputFile(const char* fileName)
{
fp = fopen(fileName, "rb");
if (!fp)
{
fprintf(stderr, "File open error: %s\n", fileName);
}
}
InputFile::~InputFile()
{
if (fp)
{
fclose(fp);
}
}
bool InputFile::readMessage(MessageLite* msg)
{
std::string s;
std::unique_ptr<uint8_t[]> bytes;
size_t size = readMessageBytesFromFile(fp, &bytes);
if (!size)
return false;
msg->Clear();
return msg->ParseFromArray(bytes.get(), size);
}
size_t InputFile::readMessageBytesFromFile(FILE* file,
std::unique_ptr<uint8_t[]>* bytes)
{
int32_t size = 0;
if (fread(&size, sizeof(size), 1, file) != 1)
return 0;
if (size <= 0)
return 0;
bytes->reset(new uint8_t[size]);
return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
}