-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfastreplacestring.cpp
161 lines (135 loc) · 3.64 KB
/
fastreplacestring.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
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
#include <cstdio>
#include <iostream>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))
// Rabin-Karp search (modified from:
// http://www-igm.univ-mlv.fr/~lecroq/string/node5.html#SECTION0050)
int indexOf(const char *needle, size_t needleLen, const char *haystack,
size_t haystackLen) {
if (needleLen == 0) {
return 0;
} else if (needleLen > haystackLen) {
return -1;
}
int d, hx, hy, i, j;
/* Preprocessing */
/* computes d = 2^(m-1) with
the left-shift operator */
for (d = i = 1; i < needleLen; ++i)
d = (d << 1);
for (hy = hx = i = 0; i < needleLen; ++i) {
hx = ((hx << 1) + needle[i]);
hy = ((hy << 1) + haystack[i]);
}
/* Searching */
j = 0;
while (j <= haystackLen - needleLen - 1) {
if (hx == hy && memcmp(needle, haystack + j, needleLen) == 0) {
return j;
}
hy = REHASH(haystack[j], haystack[j + needleLen], hy);
++j;
}
return -1;
}
int main(int argc, char **argv) {
if (argc != 4) {
cout << "usage: fastreplacestring <filename> <term> <replacement>\n";
return 1;
}
string filename = argv[argc - 3];
const char *old = argv[argc - 2];
const char *newWord = argv[argc - 1];
FILE *in = fopen(filename.c_str(), "rb");
// Check if file exists and can is read-write
// This is actually a shortcut because fopen might fail for a number of
// reasons
if (in == NULL) {
cout << "error: " + filename + " doesn't exist\n";
return 1;
}
char *s = NULL;
vector<int> indexCache;
size_t r, newFilelen;
// read filelen given by fileState
// alternatively this could be determined with fseek && ftell
fseek(in, 0, SEEK_END);
size_t filelen = ftell(in);
fseek(in, 0, SEEK_SET);
if ((s = (char *)malloc(filelen)) == NULL) {
printf("malloc s filelen problem \n");
exit(1);
}
// Read in as much of specified file as possible
// If there isn't anything to read, finish succesfully :)
if ((r = fread(s, 1, filelen, in)) == 0) {
free(s);
fclose(in);
return 0;
}
char *t = NULL;
char *temp = NULL;
size_t oldLen = strlen(old);
size_t newLen = strlen(newWord);
/* Find all matches and cache their positions. */
const char *test = NULL;
test = s;
int j, start = 0, c = 0;
int index;
while ((index = indexOf(old, oldLen, test + start, filelen - start)) != -1) {
c++;
j = start;
j += index;
indexCache.push_back(j);
start = j + oldLen;
}
if (c == 0) {
free(s);
return 0;
} else {
const char *pstr = s;
// calculate new file len & allocate memory
newFilelen = filelen + c * (newLen - oldLen);
t = (char *)malloc(newFilelen);
int i = 0;
j = 0;
start = 0;
temp = t;
if (temp == NULL) {
free(s);
exit(1);
}
// replace the bytes
for (i = 0; i < c; i++) {
j = indexCache[i];
memcpy(temp, pstr, j - start);
temp += j - start;
pstr = s + j + oldLen;
memcpy(temp, newWord, newLen);
temp += newLen;
start = j + oldLen;
}
memcpy(temp, pstr, filelen - start);
free(s);
// stat file so we can restore st_mode
struct stat st;
stat(filename.c_str(), &st);
// change st_mode so we can open file for writing
chmod(filename.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
in = freopen(filename.c_str(), "wb", in);
if (in == NULL) {
cout << "error: " + filename + " cannot be written to.\n";
return 1;
}
fwrite(t, 1, newFilelen, in);
fclose(in);
// restore st_mode
chmod(filename.c_str(), st.st_mode);
}
return 0;
}