/
FileReader.cpp
64 lines (52 loc) · 1.14 KB
/
FileReader.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
#include "FileReader.h"
#include <fstream>
#include <string>
#include <cerrno>
FileReader::FileReader(std::string fileName) :
m_fileName(fileName)
{
}
void FileReader::ChangeFile(std::string fileName)
{
m_fileName = fileName;
m_fileData.empty();
}
void FileReader::Parse()
{
std::ifstream in(m_fileName.c_str(), std::ios::in | std::ios::binary);
if(in)
{
in.seekg(0, std::ios::end);
m_fileData.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&m_fileData[0], m_fileData.size());
in.close();
}
else
throw(errno);
}
std::string FileReader::GetFileString()
{
return m_fileData;
}
FileReader::MessageVec FileReader::GetMessages()
{
MessageVec messages;
int dataPos = 0;
//Calc number of frames
int numFrames = m_fileData.length() / 64;
if(m_fileData.length() % 64 > 0)
numFrames++;
for(int i = 0; i < numFrames; i++)
{
//Calculate length of frame
int frameLen = 64;
if(dataPos+64 > m_fileData.length())
frameLen = m_fileData.length() - dataPos;
std::string newMessageStr = m_fileData.substr(dataPos, frameLen);
Message msg(newMessageStr);
messages.push_back(msg);
dataPos += frameLen;
}
return messages;
}