-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.cs
98 lines (86 loc) · 3.29 KB
/
FileManager.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
using System;
using System.Text;
namespace AutoRegex
{
class FileManager
{
private FilePathSet fileSets;
public FileManager(string inputFilePath, string regexFilePath, string outputFilePath)
{
fileSets = new FilePathSet(inputFilePath, regexFilePath, outputFilePath);
}
public string[] ReadRegexes() { return ReadFile(fileSets.RegexFile); }
public string[] ReadInput() { return ReadFile(fileSets.InputFile); }
public void PrintOutput(String[] content) { WriteFile(fileSets.OutputFile, false, content); }
/// <summary>
/// Reads all lines in the file of given file-path
/// </summary>
/// <param name="filePath">Absolute path of file to be read</param>
/// <returns>Returns an Array of strings, one item for each line</returns>
private static string[] ReadFile(String filePath)
{
// Console.WriteLine("Reading " + filePath);
string[] lines = { "" };
try
{
lines = System.IO.File.ReadAllLines(filePath);
}
catch (Exception e)
{
String errorMsg = e.Message + "\n" + e.StackTrace;
if (!log(errorMsg))
{
Console.WriteLine(errorMsg);
}
}
return lines;
}
/// <summary>
/// Logs messages to a text file
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static Boolean log(String message)
{
try
{
String[] msg = {
"[" + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "]",
"----------------------------------------" ,
message,
"----------------------------------------",
""};
WriteFile("error.log", true, msg);
}
catch (Exception e)
{
return false;
}
return true;
}
/// <summary>
/// Write to a file
/// </summary>
/// <param name="filePath">File-path of the file to write to</param>
/// <param name="append">Decides if file should be re-written, or appended</param>
/// <param name="output">Lines that should be written to file</param>
private static void WriteFile(String filePath, Boolean append, params String[] output)
{
if (append) System.IO.File.AppendAllLines(filePath, output, Encoding.UTF8);
else System.IO.File.WriteAllLines(filePath, output, Encoding.UTF8);
}
struct FilePathSet
{
public String InputFile { get; set; }
public String RegexFile { get; set; }
public String OutputFile { get; set; }
public FilePathSet(String inputFilePath, String regexFilePath, String outputFilePath)
{
InputFile = inputFilePath;
RegexFile = regexFilePath;
OutputFile = outputFilePath;
if (OutputFile.Equals("")) OutputFile = inputFilePath.Replace(".txt", "") + ".output.txt";
}
}
}
}