-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathProgram.cs
More file actions
158 lines (140 loc) · 5.62 KB
/
Program.cs
File metadata and controls
158 lines (140 loc) · 5.62 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
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
using System;
using System.IO;
using System.Reflection;
using SharpKml.Engine;
namespace Examples
{
class Program
{
private static readonly Tuple<string, Type>[] Samples =
{
Tuple.Create("BalloonFeatures.cs", typeof(BalloonFeatures)),
Tuple.Create("Change.cs", typeof(Change)),
Tuple.Create("Clone.cs", typeof(Clone)),
Tuple.Create("CreateIconStyle.cs", typeof(CreateIconStyle)),
Tuple.Create("CreateKml.cs", typeof(CreateKml)),
Tuple.Create("CreateKmz.cs", typeof(CreateKmz)),
Tuple.Create("InlineStyles.cs", typeof(InlineStyles)),
Tuple.Create("ParseKml.cs", typeof(ParseKml)),
Tuple.Create("ShowStyles.cs", typeof(ShowStyles)),
Tuple.Create("SortPlacemarks.cs", typeof(SortPlacemarks)),
Tuple.Create("SplitStyles.cs", typeof(SplitStyles)),
Tuple.Create("ExtendKml.cs", typeof(ExtendKml))
};
public static void Main()
{
CreateSampleData();
while (true)
{
Console.Title = string.Empty;
Console.ResetColor();
Console.Clear();
int choice = GetChoice();
if (choice == 0) // Exit
{
break;
}
// Run the test
Console.Clear();
Console.Title = Samples[choice - 1].Item1;
Type type = Samples[choice - 1].Item2;
type.GetMethod("Run").Invoke(null, null); // This is a static method
// Display the end message
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine(); // Add a bit of a space between the end of the test and our message
int bottom = Math.Max(Console.CursorTop, Console.WindowHeight - 1);
Console.SetCursorPosition(0, bottom);
Console.Write("Sample completed. Press any key to continue...".PadRight(Console.WindowWidth));
Console.SetCursorPosition(46, bottom);
Console.ReadKey();
}
}
// Helper method to ask for a filename and try and open it.
// Not used by this class but put here for so all samples can use it.
public static void DisplayError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
}
public static string GetInputFile(string prompt, string defaultFile)
{
Console.WriteLine(prompt);
Console.Write("(Leave blank to use " + defaultFile + ")");
Console.SetCursorPosition(prompt.Length + 1, 0);
string filename = Console.ReadLine();
Console.Clear();
if (string.IsNullOrEmpty(filename))
{
return defaultFile;
}
return filename;
}
// Helper method to ask for a filename and try and open it.
// Not used by this class but put here for so all samples can use it.
public static KmlFile OpenFile(string prompt)
{
string filename = GetInputFile(prompt, "Data/Sample.kml");
KmlFile file;
try
{
using (FileStream stream = File.Open(filename, FileMode.Open))
{
file = KmlFile.Load(stream);
}
}
catch (Exception ex)
{
DisplayError(ex.GetType() + "\n" + ex.Message);
return null;
}
if (file.Root == null)
{
DisplayError("Unable to find any recognized Kml in the specified file.");
return null;
}
return file;
}
private static void CopyResource(string name, string destination)
{
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
using (FileStream file = File.Create(destination))
{
stream.CopyTo(file);
}
}
}
private static void CreateSampleData()
{
Directory.CreateDirectory("Data/files"); // Need the sub-directory for the Kmz test
CopyResource("Examples.Data.doc.kml", "Data/doc.kml");
CopyResource("Examples.Data.Sample.kml", "Data/Sample.kml");
CopyResource("Examples.Data.camera_mode.png", "Data/files/camera_mode.png");
CopyResource("Examples.Data.zermatt.jpg", "Data/files/zermatt.jpg");
}
private static int GetChoice()
{
Console.WriteLine(" 0 - Exit the program.\n");
int index = 1;
foreach (Tuple<string, Type> sample in Samples)
{
Console.WriteLine("{0,2} - {1}", index++, sample.Item1);
}
Console.WriteLine();
while (true)
{
Console.Write("Please enter a number: ");
string input = Console.ReadLine();
// Keep looping until we get a sensible value
if (int.TryParse(input, out int choice))
{
if ((choice >= 0) && (choice <= Samples.Length))
{
return choice;
}
}
}
}
}
}