-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
113 lines (93 loc) · 3.67 KB
/
Program.cs
File metadata and controls
113 lines (93 loc) · 3.67 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
using System;
using System.Collections.Generic;
using System.IO;
namespace AutoRegex
{
class Program
{
private List<FindReplace> regexSequence = new List<FindReplace>();
static void Main(string[] args)
{
String inputFilePath = "input.txt";
String regexFilePath = "default.regex";
String outputFilePath = "";
for (int i = 0; i < args.Length; i++)
{
String arg = args[i];
if (File.Exists(arg))
{
switch (i)
{
case 0:
inputFilePath = arg;
break;
case 1:
regexFilePath = arg;
break;
case 2:
outputFilePath = arg;
break;
}
}
}
Console.WriteLine("Initiating!\n\n");
Console.WriteLine("Set to run Regex File:\n" + regexFilePath);
Console.WriteLine("\n... on the Text File:\n" + inputFilePath);
new Program(inputFilePath, regexFilePath, outputFilePath);
}
public Program(String inputFilePath, String regexFilePath , String outputFilePath)
{
// Init structures
FileManager fm = new FileManager(inputFilePath, regexFilePath, outputFilePath);
regexSequence = new List<FindReplace>();
// Read Sequence of Regex Operations and save in regexSequence
parseRegexes(fm.ReadRegexes());
// Read the input data
String[] input = fm.ReadInput();
String[] output = new String[input.Length];
// Necessary step, in order to simplify logic in below nesteled for-foreach statement.
for (int i = 0; i < output.Length; ++i)
{
// Set initial value of each Output item to input.
output[i] = input[i];
}
// Executes Find & Replace operation on all lines, for each FindReplace pattern-set
foreach (FindReplace fr in regexSequence)
{
for (int i = 0; i < output.Length; ++i)
{
output[i] = fr.Execute(output[i]);
}
}
// Print results to file
fm.PrintOutput(output);
Console.WriteLine("All is good, check output-file to see results!");
Console.WriteLine("\nOutput File:\n" + outputFilePath);
PressEnter("to Quit");
}
private static void PressEnter(string motivation)
{
Console.WriteLine("Press Enter " + motivation + "...");
Console.ReadLine();
}
private void parseRegexes(String[] lines)
{
bool willHavePairNextTime = false;
String RegexMatchPattern = "";
foreach (String l in lines)
{
// Break this itteration if line is empty.
if (l.Trim().Equals("") && !willHavePairNextTime) continue;
if (!willHavePairNextTime) RegexMatchPattern = l;
else regexSequence.Add(new FindReplace(RegexMatchPattern, l));
willHavePairNextTime = !willHavePairNextTime;
}
if (regexSequence.Count == 0)
{
Console.WriteLine("Regex File either is empty, has invalid/incomplete match-replace patterns or does not exist...");
PressEnter("to Quit");
System.Environment.Exit(0);
}
}
}
}