-
Notifications
You must be signed in to change notification settings - Fork 10
/
CacheFile.cpp
86 lines (75 loc) · 1.9 KB
/
CacheFile.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
/* Copyright (C) Teemu Suutari */
#include "onekpaq_common.h"
#include "CacheFile.hpp"
void CacheFile::clear(uint numBlocks)
{
_numBlocks=numBlocks;
_combine.clear();
_header.clear();
}
bool CacheFile::readFile(const std::string &fileName)
{
bool ret=false;
std::ifstream file(fileName.c_str(),std::ios::in|std::ios::binary);
if (file.is_open()) {
_shift=readUint(file);
_numBlocks=readUint(file);
uint numCombinedBlocks=readUint(file);
_combine.clear();
_header.clear();
for (uint i=0;file&&i<numCombinedBlocks;i++) {
_combine.push_back(readUint(file));
_header.push_back(readVector(file));
}
ret=bool(file);
file.close();
}
return ret;
}
bool CacheFile::writeFile(const std::string &fileName)
{
bool ret=false;
std::ofstream file(fileName.c_str(),std::ios::out|std::ios::binary|std::ios::trunc);
if (file.is_open()) {
writeUint(file,_shift);
writeUint(file,_numBlocks);
writeUint(file,uint(_header.size()));
for (uint i=0;file&&i<_header.size();i++) {
writeUint(file,_combine[i]);
writeVector(file,_header[i]);
}
ret=bool(file);
file.close();
}
return ret;
}
uint CacheFile::readUint(std::ifstream &stream)
{
if (!stream) return 0;
// x86 only
u32 ret=0;
stream.read(reinterpret_cast<char*>(&ret),sizeof(ret));
return ret;
}
std::vector<u8> CacheFile::readVector(std::ifstream &stream)
{
std::vector<u8> ret;
uint length=readUint(stream);
if (stream&&length) {
ret.resize(length);
stream.read(reinterpret_cast<char*>(ret.data()),length);
}
return ret;
}
void CacheFile::writeUint(std::ofstream &stream,uint value)
{
if (!stream) return;
// x86 only
u32 tmp=value;
stream.write(reinterpret_cast<const char*>(&tmp),sizeof(tmp));
}
void CacheFile::writeVector(std::ofstream &stream,const std::vector<u8> &vector)
{
writeUint(stream,uint(vector.size()));
if (stream&&vector.size()) stream.write(reinterpret_cast<const char*>(vector.data()),vector.size());
}