Repository navigation
Expand file tree
/
Copy pathfireplace.cpp
More file actions
229 lines (191 loc) · 7.35 KB
/
Copy pathfireplace.cpp
File metadata and controls
229 lines (191 loc) · 7.35 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
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// File: CatFiRe.cpp (Catalogue Find and Replace)
// Program: CatFiRe (FiRePlace)
// Purpose: Multiple word search-and-replace program
// Notes: 1. Searches file "CatFiRe.frd" (find & replace dictionary - text file) in current directory
// for 'search' and 'replace' word pairs. Format = [find][tab][replace with]
// 2. Opens file supplied on command line and replaces all 'to find' words with corresponding
// "replace with" replacement. Only one file to be supplied on command line.
// Wild-cards not accepted.
// Words 'to find' must be greater than 1 and less than 6 letters in length (easily changed)
// Designed especially to be spawned from 'CatView'. Usage: catfire file1
// Author: Peter van der Woude
// Last Updated: October 30, 1999
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <cstring.h>
#include <sys\stat.h>
#include "catfire.h"
#include "config_w.h"
// no-argument constructor
CatFiRe::CatFiRe() {
Word[0] = NULL;
ptrSA = NULL;
}
void CatFiRe::CreateDictionary(string catalogfile) {
ifstream catDicFile;
DicFilename = catalogfile.substr(catalogfile.length() - 3) + "frd";
catDicFile.open(DicFilename.c_str()); // use specialised dictionary if exists
if (!catDicFile) // (same name as file but "frd" extension)
{
catDicFile.open("CatView.frd"); // otherwise use default
if (!catDicFile)
throw FileOpenError("CatView.frd");
else
DicFilename = "CatView.frd";
}
hvString find, replace;
while (catDicFile) // read dictionary file
{
catDicFile >> find >> replace; // ensure words exist before adding
if (find.is_null() || replace.is_null())
return;
else if (find.length() > 80 || replace.length() > 80)
throw FileOpenError(DicFilename);
else {
strAssoc FR(find, replace);
dictionary.Add(FR); // add word-pair to dictionary
}
}
}
void CatFiRe::FindReplace(string catalogpath, string catalogname) {
const int LETTER = 0, DELIMITER = 1, DIGIT = 2;
const int A = 3, E = 4, I = 5, O = 6, U = 7, C = 8;
int counter = 0, delimiter;
long wordsReplaced = 0L, fileCounter = 0L;
// note: compile this file with all unsigned char option set
char lookup[256]; // lookup table
memset(lookup, LETTER, 256); // initialise look-up table
OpenFiles(catalogpath, catalogname);
for (delimiter = 32; delimiter <= 47; delimiter++) // non-alphabet characters (word delimiters)
lookup[delimiter] = DELIMITER;
for (delimiter = 58; delimiter <= 63; delimiter++)
lookup[delimiter] = DELIMITER;
for (delimiter = 91; delimiter <= 96; delimiter++)
lookup[delimiter] = DELIMITER;
lookup['\t'] = DELIMITER; // set extra delimiters in lookup table
lookup['\n'] = DELIMITER;
lookup['\r'] = DELIMITER;
lookup['\''] = LETTER; // recant apostrophe (not delimiter)
for (int digit = 48; digit <= 57; digit++) // set digits in lookup table
lookup[digit] = DIGIT;
if (config_getReplaceNonAnglo()) {
// replace foreign language character with english equivalent
for (int foreign = 131; foreign <= 134; foreign++) // a
lookup[foreign] = A;
lookup[142] = A;
lookup[143] = A;
lookup[160] = A;
lookup[166] = A;
for (int foreign = 136; foreign <= 138; foreign++) // e
lookup[foreign] = E;
for (int foreign = 144; foreign <= 146; foreign++)
lookup[foreign] = E;
lookup[130] = E;
for (int foreign = 139; foreign <= 141; foreign++) // i
lookup[foreign] = I;
lookup[161] = I;
for (int foreign = 147; foreign <= 149; foreign++) // o
lookup[foreign] = O;
lookup[153] = O;
lookup[162] = O;
lookup[167] = O;
lookup[129] = U;
lookup[150] = U;
lookup[151] = U; // u
lookup[154] = U;
lookup[163] = U;
lookup[128] = C;
lookup[135] = C;
}
while (oldCatFile) {
oldCatFile.get(Ch); // read character from catalogue file
switch (lookup[Ch]) {
case LETTER: // alphabetic character
Word[counter++] = Ch;
break;
case DIGIT : // same as LETTER - included for logic & future implementations
Word[counter++] = Ch;
break;
case DELIMITER:
// characters that are considered word delimiters
{ // create block to avoid skipping local variable declaration (tempAssoc)
Word[counter] = '\0'; // append null to word
if (counter < 2) // if couldn't be in dictionary
{
newCatFile << Word << Ch; // min = 2, max = 20
counter = 0; // (important for execution speed)
break; // output word, move to next word
}
strAssoc tempAssoc(hvString(Word), 0); // create association
ptrSA = dictionary.Find(tempAssoc); // look up word in dictionary
if (!ptrSA) // word not found, output 'as is'
newCatFile << Word << Ch; // (include delimiter character)
else // else output replacement
{
newCatFile << ptrSA->Value() << Ch;
wordsReplaced++;
}
counter = 0; // restart word array counter
}
break;
case A:
Word[counter++] = 'a';
break;
case E:
Word[counter++] = 'e';
break;
case I:
Word[counter++] = 'i';
break;
case O:
Word[counter++] = 'o';
break;
case U:
Word[counter++] = 'u';
break;
case C:
Word[counter++] = 'c';
break;
default:
Word[counter++] = Ch; // appropriate fall-back
}
if (counter >= 255)
counter = 0;
if (++fileCounter % 10000 == 0) {
gotoxy(24, 15);
cout << int(oldCatFile.tellg() / float(FileSize) * 100) << '%';
}
}
gotoxy(9, 15);
cout << "Search & Replace complete.";
cout << "\n\t" << wordsReplaced << " words replaced.";
oldCatFile.close(); // prepare file for deletion (must be closed)
newCatFile.close(); // prepare file for renaming
}
void CatFiRe::OpenFiles(string CatalogPath, string CatalogFile) {
struct date d;
oldCatFile.open(CatalogPath.c_str()); // open catalogue file
if (!oldCatFile)
throw FileOpenError(CatalogFile);
// throw exception (and quit) if file doesn't exist; else continue
oldCatFile.seekg(0, ios::end);
FileSize = oldCatFile.tellg();
oldCatFile.seekg(0);
CatalogFile.to_upper();
DicFilename.to_upper();
cout << "\n\tReplacing words in " << CatalogFile
<< " (using " << DicFilename << ')'
<< "\n\tPlease Wait...";
newCatFile.open("CatFiRe.tmp", ios::out); // open file to write to (will be renamed)
getdate(&d);
newCatFile << "CatView prepared file ("
<< int(d.da_day) << '-'
<< int(d.da_mon) << '-'
<< int(d.da_year)
<< ")\n"; // write date that file was "CatFiRed"
}