-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
234 lines (206 loc) · 7.14 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Timers;
namespace sharParse
{
public partial class sharParse : Form
{
string cDir;
string sTxt;
string rTxt;
bool parse;
System.IO.StreamReader fileStream;
System.IO.StreamWriter writeStream;
private System.Timers.Timer Time;
//Work around for case insensitive comparisons
public static bool Contains(string src, string cmp, StringComparison method)
{
return src.IndexOf(cmp, method) >= 0;
}
//
// Author: Huisheng Chen, Architect www.xnlab.com
public static string ReplaceEx(string original,
string pattern, string replacement)
{
int count, position0, position1;
count = position0 = position1 = 0;
string upperString = original.ToUpper();
string upperPattern = pattern.ToUpper();
int inc = (original.Length / pattern.Length) *
(replacement.Length - pattern.Length);
char[] chars = new char[original.Length + Math.Max(0, inc)];
while ((position1 = upperString.IndexOf(upperPattern,
position0)) != -1)
{
for (int i = position0; i < position1; ++i)
chars[count++] = original[i];
for (int i = 0; i < replacement.Length; ++i)
chars[count++] = replacement[i];
position0 = position1 + pattern.Length;
}
if (position0 == 0) return original;
for (int i = position0; i < original.Length; ++i)
chars[count++] = original[i];
return new string(chars, 0, count);
}
//
public sharParse()
{
cDir = "c:/";
InitializeComponent();
tb_fileDirectory.Text = cDir;
}
//
public bool parseFile()
{
int i;
try
{
// Read file and save to 'txt'
fileStream = new System.IO.StreamReader(cDir);
string txt = fileStream.ReadToEnd();
fileStream.Close();
// Case sensitivity implementation
if (!caseSensitiveToolStripMenuItem.Checked)
{
if (!Contains(txt, sTxt, StringComparison.OrdinalIgnoreCase))
{
MessageBox.Show("No matches found for '" + sTxt + "'.\n Please ensure your settings match your Search Text respectively.", "Error: No Matches Found!");
return (false);
}
else
txt = ReplaceEx(txt, sTxt, rTxt);
}
else
{
if (!txt.Contains(sTxt))
{
MessageBox.Show("No matches found for '" + sTxt + "'.\n Please ensure your settings match your Search Text respectively.", "Error: No Matches Found!");
return (false);
}
else
txt = txt.Replace(sTxt, rTxt);
}
// Commit changes back to file
writeStream = new System.IO.StreamWriter(cDir);
writeStream.Write(txt);
// writeStream.Close();
return (true);
}
catch
{
return (false);
}
finally
{
fileStream.Close();
writeStream.Close();
// To check against 'Time' for progressBar
parse = true;
}
}
//
private void btn_browse_Click(object sender, EventArgs e)
{
while (true)
{
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
return;
else
{
if (!openFileDialog1.CheckFileExists) //Verify respective file exists
{
if (MessageBox.Show("File not found at specified directory, try again?", "Error locating file!", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
}
else
{
cDir = openFileDialog1.FileName; // Store selected path to var 'cDir'
tb_fileDirectory.Text = cDir;
return;
}
}
}
}
//
private void tb_fileDirectory_TextChanged(object sender, EventArgs e)
{
cDir = tb_fileDirectory.Text;
}
//
private void tb_fileDirectory_Leave(object sender, EventArgs e)
{
cDir = tb_fileDirectory.Text;
}
//
private void tb_fileDirectory_DoubleClick(object sender, EventArgs e)
{
tb_fileDirectory.SelectAll();
}
//
private void tb_searchText_Leave(object sender, EventArgs e)
{
sTxt = tb_searchText.Text;
}
//
private void tb_replaceText_Leave(object sender, EventArgs e)
{
rTxt = tb_replaceText.Text;
}
//
private void btn_parse_Click(object sender, EventArgs e)
{
if (!parseFile())
{
MessageBox.Show("Unable to parse file @ " + cDir, "Error!");
}
}
//
private void partialMatchesToolStripMenuItem_Click(object sender, EventArgs e)
{
set_partialMatches partialMatchSettings = new set_partialMatches();
partialMatchSettings.ShowDialog();
partialMatchesToolStripMenuItem.Checked = set_partialMatches.buffer_mnuItem.Checked;
}
//
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//
private void tb_searchText_TextChanged(object sender, EventArgs e)
{
}
//
private void progressBar1_Click(object sender, EventArgs e)
{
// To ensure the progress bar has adequate time to display progress before execution is complete.
prog_Timer.Enabled = true;
prog_Timer.Interval = 2000;
// Init progress bar 'process'
progressBar1.Style = ProgressBarStyle.Marquee;
if (!parseFile())
{
MessageBox.Show("Unable to parse file @ " + cDir, "Error!");
}
}
//
private void prog_Timer_Tick(object sender, EventArgs e)
{
if (parse)
{
prog_Timer.Enabled = false;
progressBar1.Style = ProgressBarStyle.Blocks;
}
}
}
}