-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompFile.cpp
More file actions
127 lines (109 loc) · 2.37 KB
/
CompFile.cpp
File metadata and controls
127 lines (109 loc) · 2.37 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "CompFile.h"
CompFile::CompFile() : opened_(false)
{
}
CompFile::CompFile(const string &fileName, const string &header)
{
opened_ = false;
// store
fileName_ = fileName;
header_ = header;
open(fileName_,std::ifstream::in | std::ifstream::binary);
if (is_open())
{
opened_ = readDirectory();
} else
{
errorString_ = string("Unable to open file ") + fileName_;
}
}
bool CompFile::isOpen() const
{
return opened_;
}
bool CompFile::readDirectory()
{
// check header
string buffer = header_;
read(&buffer[0],header_.size());
if (buffer == header_)
{
// read # entries
unsigned short int N = 0;
read((char *) &N,2);
if (!N)
{
// no entries in file
errorString_ = "No entries in file";
return false;
} else
{
poslen_.clear();
poslen_.resize(N);
// create pos+len array
read((char *) &poslen_[0],N * sizeof(PosLen));
return true;
}
} else
{
// wrong header
errorString_ = "Wrong file header";
return false;
}
}
const string &CompFile::errorString() const
{
return errorString_;
}
void CompFile::closeFile()
{
close();
opened_ = false;
}
CompFile::~CompFile()
{
if (opened_)
closeFile();
}
int CompFile::numEntries() const
{
return static_cast<int>(poslen_.size());
}
bool CompFile::getEntryPosLen(int entryNo, int &pos, int &len)
{
if (!opened_) return false;
if (entryNo >= 0 && entryNo <= numEntries())
{
pos = poslen_[entryNo].pos;
len = poslen_[entryNo].len;
return true;
}
else
{
errorString_ = string("Composite file ") + fileName_ +
string(", wrong entry number when loading entry ") + to_string(entryNo) +
": there are only " + to_string(numEntries()) + " entries in the file";
return false;
}
}
bool CompFile::setEntryPos(int entryNo, int& len)
{
int pos;
if (getEntryPosLen(entryNo,pos,len))
{
seekg(poslen_[entryNo].pos);
return true;
}
else
{
return false;
}
}
const string &CompFile::fileName() const
{
return fileName_;
}
const string &CompFile::header() const
{
return header_;
}