-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
187 lines (163 loc) · 7.63 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace KMZRebuilder
{
static class Program
{
public static KMZRebuilederForm mainForm;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//FormKMZ.ConvertImageToBmp8bppIndexed(@"C:\Downloads\CD-REC\KMZRebuilder[Sources]\bin\Debug\dot[red].png",
// @"C:\Downloads\CD-REC\KMZRebuilder[Sources]\bin\Debug\dot[red].bmp");
//return;
if ((args != null) && (args.Length > 2) && ((args[0].ToLower() == "/kmz2gpi") || (args[0].ToLower() == "/kml2gpi")))
{
WinConsoleApplication.Initialize(true, true, false);
KMZRebuilederForm.ConvertKMZ2GPI(args[1], args[2]);
WinConsoleApplication.DeInitialize();
return;
};
if ((args != null) && (args.Length > 2) && ((args[0].ToLower() == "/gpi2kmz") || (args[0].ToLower() == "/gpi2kml")))
{
WinConsoleApplication.Initialize(true, true, false);
KMZRebuilederForm.ConvertGPI2KMZ(args[1], args[2]);
WinConsoleApplication.DeInitialize();
return;
};
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
//AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
if ((args != null) && (args.Length > 0) && (args[0] == "/bpa"))
{
Application.Run(new BenzinPriceAnalizer.BenzinPriceAnalizerForm());
return;
};
if((args != null) && (args.Length > 0) && (args[0] == "/km"))
{
Application.Run(new KMNumeratorForm());
return;
};
if((args != null) && (args.Length > 0) && (args[0] == "/ilp"))
{
Application.Run(new InterLessForm(null));
return;
};
if ((args != null) && (args.Length > 0) && (args[0] == "/tsp"))
{
Application.Run(new TrackSplitter(null));
return;
};
if((args != null) && (args.Length > 0) && (args[0] == "/mpc"))
{
Application.Run(new PolyCreator(null));
return;
};
if ((args != null) && (args.Length > 0) && (args[0] == "/tax"))
{
Application.Run(new GPX_Tacho.GPXTachograph());
return;
};
if ((args != null) && (args.Length > 0) && (args[0] == "/llc"))
{
Application.Run(new WGSFormX());
return;
};
Application.Run(mainForm = new KMZRebuilederForm(args));
}
//private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
//{
// MessageBox.Show("The method or operation is not implemented.");
//}
//private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
//{
// throw new Exception("The method or operation is not implemented.");
//}
}
public static class WinConsoleApplication
{
public static Encoding ConsoleEncoding = Encoding.GetEncoding(866);
public static void Initialize(bool createNewConsole, bool allowWrite, bool allowRead)
{
bool consoleAttached = true;
if (createNewConsole || ((AttachConsole(ATTACH_PARRENT) == 0) && (Marshal.GetLastWin32Error() != ERROR_ACCESS_DENIED)))
consoleAttached = AllocConsole() != 0;
if (consoleAttached)
{
if (allowWrite) InitStdOut();
if (allowRead) InitStdIn();
};
try
{
Console.OutputEncoding = ConsoleEncoding;
Console.InputEncoding = ConsoleEncoding;
}
catch { };
}
public static void DeInitialize()
{
Console.Out.Close();
Console.In.Close();
FreeConsole();
}
private static void InitStdOut()
{
FileStream fs = CreateFileStream("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, FileAccess.Write);
if (fs != null)
{
StreamWriter writer = new StreamWriter(fs, ConsoleEncoding);
writer.AutoFlush = true;
Console.SetOut(writer);
Console.SetError(writer);
};
}
private static void InitStdIn()
{
FileStream fs = CreateFileStream("CONIN$", GENERIC_READ, FILE_SHARE_READ, FileAccess.Read);
if (fs != null)
{
Console.SetIn(new StreamReader(fs, ConsoleEncoding));
};
}
private static FileStream CreateFileStream(string name, uint win32DesiredAccess, uint win32ShareMode, FileAccess dotNetFileAccess)
{
Microsoft.Win32.SafeHandles.SafeFileHandle file = new Microsoft.Win32.SafeHandles.SafeFileHandle(CreateFileW(name, win32DesiredAccess, win32ShareMode, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero), true);
if (!file.IsInvalid)
{
FileStream fs = new FileStream(file, dotNetFileAccess);
return fs;
};
return null;
}
[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
[DllImport("kernel32.dll", EntryPoint = "AttachConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern UInt32 AttachConsole(UInt32 dwProcessId);
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr CreateFileW(string lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("kernel32.dll", EntryPoint = "FreeConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool FreeConsole();
private const UInt32 GENERIC_WRITE = 0x40000000;
private const UInt32 GENERIC_READ = 0x80000000;
private const UInt32 FILE_SHARE_READ = 0x00000001;
private const UInt32 FILE_SHARE_WRITE = 0x00000002;
private const UInt32 OPEN_EXISTING = 0x00000003;
private const UInt32 FILE_ATTRIBUTE_NORMAL = 0x80;
private const UInt32 ERROR_ACCESS_DENIED = 5;
private const UInt32 ATTACH_PARRENT = 0xFFFFFFFF;
}
}