-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
171 lines (160 loc) · 5.67 KB
/
Program.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
using System;
using System.Text;
using System.Data;
using System.Data.SQLite;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using SqliteToCsv;
using System.IO;
namespace SqliteToCsv
{
class Program
{
static void Main(string[] args)
{
#if DEBUG
args = new string[2]
{
@"C:\Temp\forums.db",
@"C:\Temp\output"
};
#endif
string source = String.Empty;
string destination = String.Empty;
bool validArgs = false;
if(args.Length != 2 && args.Length != 0)
{
Console.WriteLine("Invalid Arguments");
return;
}
else if(args.Length == 2)
{
validArgs = ValidateArgs(args[0], args[1]);
if (validArgs)
{
source = args[0];
destination = args[1];
}
}
else if(args.Length == 0)
{
source = GetSourceFromUser();
destination = GetDestinationFromUser();
validArgs = true;
}
if (validArgs)
{
using (ParallelProcessor processor = new ParallelProcessor(source, destination))
using (ConsoleStats stats = new ConsoleStats(processor))
{
Task.Run(async () =>
{
await processor.Start();
}).GetAwaiter().GetResult();
}
}
Console.WriteLine("Completed Sqlite processing");
#if DEBUG
Console.ReadLine();
#endif
}
private static bool ValidateArgs(string source, string destination)
{
if (!File.Exists(source))
{
WriteLineColor("Source File Not Found", ConsoleColor.Red);
return false;
}
if (!Directory.Exists(destination))
{
WriteLineColor("Destination Directory Not Found", ConsoleColor.Red);
return false;
}
return true;
}
private static string GetSourceFromUser()
{
int cachedtop = Console.CursorTop;
Console.Write("Please enter the DB file path: ");
int i = 0;
while (true)
{
string source = Console.ReadLine();
if (File.Exists(source))
{
return source;
}
ClearCurrentLine(cachedtop);
WriteColor($"File not found, enter a valid path: ", ConsoleColor.DarkRed);
i++;
}
}
private static string GetDestinationFromUser()
{
int cachedtop = Console.CursorTop;
Console.Write("Please enter the destiantion directory: ");
int i = 0;
while (true)
{
string destination = Console.ReadLine();
if (Directory.Exists(destination))
{
return destination;
}
else
{
Console.Write("Directory not found. Do you wish to create this directory? [yes/no]");
while (true)
{
string answer = Console.ReadLine();
if (answer.ToLower() == "yes")
{
Directory.CreateDirectory(destination);
return destination;
}
else if(answer.ToLower() == "no")
{
Console.Write("Please enter a valid directory path");
break;
}
}
}
ClearCurrentAndBelowLine(cachedtop);
WriteColor($"Please enter a valid directory path ", ConsoleColor.DarkRed);
i++;
}
}
private static void WriteColor(string value, ConsoleColor color)
{
ConsoleColor current = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(value);
Console.ForegroundColor = current;
}
private static void WriteLineColor(string value, ConsoleColor color)
{
ConsoleColor current = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(value);
Console.ForegroundColor = current;
}
private static void ClearCurrentLine(int? top = null)
{
Console.SetCursorPosition(0, top.HasValue ? top.Value : Console.CursorTop);
Console.Write(new String(' ', Console.WindowWidth));
Console.SetCursorPosition(0, top.HasValue ? top.Value : Console.CursorTop);
}
private static void ClearCurrentAndBelowLine(int? top = null)
{
int startingPosition = top.HasValue ? top.Value : Console.CursorTop;
int num = Console.WindowHeight - Console.CursorTop;
for(int i = 0; i < num; i++)
{
Console.SetCursorPosition(0, startingPosition + i);
Console.Write(new String(' ', Console.WindowWidth));
}
Console.SetCursorPosition(0, top.HasValue ? top.Value : Console.CursorTop);
}
}
}