-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.cpp
More file actions
330 lines (270 loc) · 6.68 KB
/
Copy patharchive.cpp
File metadata and controls
330 lines (270 loc) · 6.68 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <vector>
#include <string>
#include <fstream>
#include <streambuf>
#include <cstring>
#include "archive.h"
#include "compression.h"
std::vector<std::string> archives;
void stringRead(std::string str, int* pos, char* data, int size)
{
// Loop over part of the string
for(int i = 0; i < size && i < str.size(); i++)
{
// Set part of the data to part of the string
data[i] = str[*pos+i];
}
// Add to the position
*pos += size;
}
void stringWrite(std::string &str, const char* data, int size)
{
// Loop over the data
for(int i = 0; i < size; i++)
{
// Add it to the string
str += data[i];
}
}
int archiveGen(const char* data, int size, bool compression)
{
// Create the loaded archive data file
std::string sdata;
// Is there decompression needed
if(compression)
{
// Get the size of the uncompressed string
uint64_t csize;
// Decompress the string
const char* cdata = decompress_string(data, size, &csize);
// Load the archives data
sdata.append(cdata, csize);
}
else
{
// Load the archives data
sdata.append(data, size);
}
// Push back the archive
archives.push_back(sdata);
// Return the archives position
return archives.size()-1;
}
int archiveLoad(const char* dir, bool compression)
{
// Open the file
std::ifstream file(dir, std::ios::binary);
// Is the file good
if(file.good())
{
// Read the data
std::istreambuf_iterator<char> begin(file), end;
std::string data(begin, end);
// Is there decompression needed
if(compression)
{
// Convert the string to a c string
int csize = data.size();
char* cdata = new char[csize];
// Loop over the string
for(int i=0;i<csize;i++)
{
// Push the string item to the cstring
cdata[i] = data[i];
}
// Get the decompressed size
uint64_t nsize;
// Clear the data varible
data = "";
// Decompress the data
const char* ndata = decompress_string(cdata, csize, &nsize);
data.append(ndata, nsize);
}
// Push back the archive
archives.push_back(data);
// Return the archives position
return archives.size()-1;
}
else
{
// Return an error message, -1
return -1;
}
}
const char* archiveGetData(int a, uint64_t* size, bool compression)
{
// Get the data
const char* data = archives[a].c_str();
uint64_t dsize = archives[a].size();
// Does the user want compression
if(compression)
{
// Return compressed data
return compress_string(data, dsize, size);
}
else
{
// Set the size
*size = dsize;
// Return the normal data
return data;
}
}
bool archiveSave(int a, const char* dir, bool compression)
{
// Open the file
std::ofstream file(dir, std::ios::binary);
// Is the file good
if(file.good())
{
// Is there compression
if(compression)
{
// Convert string to a c string
uint64_t c_size = archives[a].size();
char* c_str = new char[c_size];
// Loop over the string
for(int i=0;i<c_size;i++)
{
// Push the string element to the c string
c_str[i] = archives[a][i];
}
// Get the compressed size
uint64_t csize;
// Get the compressed data
const char* cdata = compress_string(c_str, c_size, &csize);
// Write the compressed data to the file
file.write(cdata, csize);
}
else
{
// Write the archive to the file
file.write(archives[a].c_str(), archives[a].size());
}
// Close the file
file.close();
// Return true
return true;
}
else
{
// Close the file
file.close();
// Return false
return false;
}
}
void archiveFree(int a)
{
// Delete the archives reference
delete &archives[a];
}
ArchivePos archiveGetEnd()
{
// Create the position
ArchivePos pos;
pos.end = true;
pos.found = false;
// Return the position
return pos;
}
ArchivePos archiveGetPos(int a, const char* dir)
{
// Loop over the archive
int i=0;
while(i<archives[a].size())
{
// Get the name size
uint32_t namesize;
stringRead(archives[a], &i, (char*)&namesize, sizeof(namesize));
// Get the name
char* name = new char[namesize];
stringRead(archives[a], &i, name, namesize);
// Get the data size
uint32_t datasize;
stringRead(archives[a], &i, (char*)&datasize, sizeof(datasize));
// Does the name size equal the directory name size
if(namesize == strlen(dir))
{
// Check if both files are the same
bool same = true;
// Loop over the name size
for(int i=0;i<namesize;i++)
{
// Do the names not match
if(name[i] != dir[i])
{
// Set same to false
same = false;
}
}
// Are the directories the same
if(same)
{
// Set the position
ArchivePos pos;
pos.end = false;
pos.found = true;
pos.size = datasize;
pos.pos = i;
// Return the position
return pos;
}
}
// Free the name
free(name);
// Skip the datas bytes
i += datasize;
}
// Set the position
ArchivePos pos;
pos.found = false;
pos.end = true;
// Return the pos variable
return pos;
}
void archiveWrite(int a, ArchivePos pos, const char* filename, const char* data, int size)
{
// Is this for the end of the file
if(pos.end)
{
// Write the filename to the end of the file
uint32_t filename_size = strlen(filename);
stringWrite(archives[a], (char*)&filename_size, sizeof(filename_size));
stringWrite(archives[a], filename, filename_size);
// Write the data to the end of the file
uint32_t usize = size;
stringWrite(archives[a], (char*)&usize, sizeof(usize));
stringWrite(archives[a], data, size);
}
else
{
// Setup a variable the substring
int substr_pos;
// Is the size too large for substr
if(pos.pos+size >= archives[a].size())
{
// Set the substr pos to the archives size
substr_pos = archives[a].size();
}
else
{
// Set the substr pos to the write bytes and size
substr_pos = pos.pos + size;
}
// Get the first part and the last part of the string
std::string start = archives[a].substr(0, pos.pos-sizeof(uint32_t));
std::string end = archives[a].substr(substr_pos);
// Write the data to the end of the start sring
stringWrite(start, (char*)&size, sizeof(size));
stringWrite(start, data, size);
// Add the start and end to the archive
archives[a] = start + end;
}
}
void archiveRead(int a, ArchivePos pos, char* data)
{
// Get the data from the position
int ipos = pos.pos;
stringRead(archives[a], &ipos, data, pos.size);
}