/
db-extract-file.cpp
61 lines (50 loc) · 1.24 KB
/
db-extract-file.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
/* simple tool to extract a single file from the DB
* by Albert Zeyer, 2011
* code under LGPL
*/
#include "Png.h"
#include "DbDefBackend.h"
#include "DbPng.h"
#include "StringUtils.h"
#include "FileUtils.h"
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;
Return _main(const std::string& filename) {
DbDefBackend db;
db.setReadOnly(true);
ASSERT( db.init() );
DbEntryId fileEntryId;
ASSERT( db.getFileRef(fileEntryId, filename) );
cout << "entry id: " << hexString(fileEntryId) << endl;
std::string extract_fn = baseFilename(filename);
FILE* f = fopen(extract_fn.c_str(), "wb");
if(f == NULL)
return "cannot open " + extract_fn;
if(!fileEntryId.empty()) {
FileWriteCallback writer(f);
DbPngEntryReader dbPngReader(&writer, &db, fileEntryId);
while(dbPngReader)
ASSERT( dbPngReader.next() );
}
cout << "wrote " << ftell(f) << " bytes" << endl;
fclose(f);
return true;
}
int main(int argc, char** argv) {
if(argc <= 1) {
cerr << "please give me a filename" << endl;
return 1;
}
std::string filename = argv[1];
srandom(time(NULL));
Return r = _main(filename);
if(!r) {
cerr << "error: " << r.errmsg << endl;
return 1;
}
cout << "success" << endl;
return 0;
}