-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepend.cpp
99 lines (74 loc) · 2.46 KB
/
prepend.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
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (C) Ilya Taranov 2017
* Distributed under the MIT License (http://opensource.org/licenses/MIT).
*/
#include <iostream>
#include <fstream>
#include <memory>
using namespace std;
/*
* Reusable add prefix context operation with preserving buffers
*/
struct AddPrefix {
size_t bufSz;
unique_ptr<char[]> buf1;
unique_ptr<char[]> buf2;
const char * prefix;
size_t prefixSize;
AddPrefix(const char * a_prefix, size_t a_prefixSize)
: prefix(a_prefix), prefixSize(a_prefixSize)
{
constexpr static int bufferAlign = 4*1024;
/* Initial block must be greater than prefix */
bufSz = ((prefixSize / bufferAlign) + 1) * bufferAlign;
buf1.reset(new char[bufSz]);
buf2.reset(new char[bufSz]);
}
int operator ()(const char * fileName) {
fstream bigFile(fileName);
/* Read the initial block */
size_t sz = bigFile.readsome(buf1.get(), bufSz);
/* Write the prefix */
bigFile.seekp(0);
bigFile.write(prefix, prefixSize);
size_t c = 0;
/* TODO : We don't really need the whole prefix-sized
* block after the prefix is written, this can be optimized */
do {
bigFile.seekg((c + 1) * bufSz);
size_t sz2 = bigFile.readsome(buf2.get(), bufSz);
bigFile.seekp(c * bufSz + prefixSize);
bigFile.write(buf1.get(), sz);
buf1.swap(buf2);
sz = sz2;
++c;
} while (sz == bufSz);
/* Write the leftover */
bigFile.seekp(c * bufSz + prefixSize);
bigFile.write(buf1.get(), sz);
bigFile.close();
return 0;
}
};
int main(int argc, char **argv)
{
if (argc < 3) {
/* Print help and exit */
cout << "Prepend v0.01 - "
"Add data to the beginning of the file inplace (without copying the file)\n\n"
" USAGE: prepend PREFIXFILE FILE [FILE]*\n\n"
" WARNING: The process is non-transactional. If interrupted in the middle,\n"
"the file is left corrupted.\n";
return -1;
}
ifstream prefixFile(argv[1], ios::binary | ios::ate);
size_t prefixSize = prefixFile.tellg();
unique_ptr<char[]> prefix(new char[prefixSize]);
prefixFile.seekg(0);
prefixFile.read(prefix.get(), prefixSize);
AddPrefix addPrefixOperator(prefix.get(), prefixSize);
for (int i = 2; i < argc; ++i) {
addPrefixOperator(argv[i]);
}
return 0;
}