Skip to content

Commit c71ae11

Browse files
Add automatic script detection #3;
1 parent 364f6ba commit c71ae11

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

XtractQuery/Program.cs

+36-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using System.Reflection;
5+
using Komponent.IO;
56
using XtractQuery.Interfaces;
67
using XtractQuery.Options;
78
using XtractQuery.Parsers;
@@ -26,13 +27,15 @@ static void Main(string[] args)
2627
return;
2728
}
2829

30+
#region Check arguments existing
31+
2932
if (!arguments.Operation.Exists)
3033
{
3134
Console.WriteLine("No operation mode was given. Specify an operation mode by using the -o argument.");
3235
return;
3336
}
3437

35-
if (!arguments.QueryType.Exists)
38+
if (arguments.Operation.Values.First() == "c" && !arguments.QueryType.Exists)
3639
{
3740
Console.WriteLine("No query type was given. Specify a query type by using the -t argument.");
3841
return;
@@ -44,25 +47,26 @@ static void Main(string[] args)
4447
return;
4548
}
4649

47-
operation = arguments.Operation.Values.First();
48-
type = arguments.QueryType.Values.First();
49-
file = arguments.QueryFile.Values.First();
50+
#endregion
5051

51-
if (operation != "e" && operation != "c")
52+
file = arguments.QueryFile.Values.First();
53+
if (!File.Exists(file))
5254
{
53-
Console.WriteLine($"The operation mode '{operation}' is not valid. Use -h to see a list of valid operation modes.");
55+
Console.WriteLine($"File '{Path.GetFullPath(file)}' was not found.");
5456
return;
5557
}
5658

57-
if (type != "xq32" && type != "xseq")
59+
operation = arguments.Operation.Values.First();
60+
if (operation != "e" && operation != "c")
5861
{
59-
Console.WriteLine($"The query type '{type}' is not valid. Use -h to see a list of valid query types.");
62+
Console.WriteLine($"The operation mode '{operation}' is not valid. Use -h to see a list of valid operation modes.");
6063
return;
6164
}
6265

63-
if (!File.Exists(file))
66+
type = arguments.QueryType.Values.FirstOrDefault();
67+
if (operation == "c" && type != "xq32" && type != "xseq")
6468
{
65-
Console.WriteLine($"File '{Path.GetFullPath(file)}' was not found.");
69+
Console.WriteLine($"The query type '{type}' is not valid. Use -h to see a list of valid query types.");
6670
return;
6771
}
6872

@@ -71,6 +75,7 @@ static void Main(string[] args)
7175
switch (operation)
7276
{
7377
case "e":
78+
type = DetermineType(file);
7479
ExtractFile(type, file);
7580
break;
7681

@@ -80,6 +85,24 @@ static void Main(string[] args)
8085
}
8186
}
8287

88+
private static string DetermineType(string file)
89+
{
90+
using var fileStream = File.OpenRead(file);
91+
using var br = new BinaryReaderX(fileStream);
92+
93+
switch (br.ReadString(4))
94+
{
95+
case "XQ32":
96+
return "xq32";
97+
98+
case "XSEQ":
99+
return "xseq";
100+
101+
default:
102+
return string.Empty;
103+
}
104+
}
105+
83106
private static void ExtractFile(string type, string file)
84107
{
85108
IParser parser;
@@ -142,11 +165,13 @@ private static void PrintHelp()
142165
Console.WriteLine(" Valid operations are: e for extraction, c for creation");
143166
Console.WriteLine(" -t, --type\t\tThe type of file given");
144167
Console.WriteLine(" Valid types are: xq32, xseq");
168+
Console.WriteLine(" The type is automatically detected when extracting; This argument will not have any effect on operation 'e'");
145169
Console.WriteLine(" -f, --file\t\tThe file to process");
146170
Console.WriteLine();
147171
Console.WriteLine("Example usage:");
148-
Console.WriteLine($"\tExtract a xq32 query to human readable text: {Assembly.GetExecutingAssembly().Location} -o e -t xq32 -f [file]");
172+
Console.WriteLine($"\tExtract any query to human readable text: {Assembly.GetExecutingAssembly().Location} -o e -f [file]");
149173
Console.WriteLine($"\tCreate a xq32 query from human readable text: {Assembly.GetExecutingAssembly().Location} -o c -t xq32 -f [file]");
174+
Console.WriteLine($"\tCreate a xseq query from human readable text: {Assembly.GetExecutingAssembly().Location} -o c -t xseq -f [file]");
150175
}
151176
}
152177
}

XtractQuery/Properties/launchSettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"XtractQuery": {
44
"commandName": "Project",
5-
"commandLineArgs": "-o e -t xq32 -f D:\\Users\\Kirito\\Desktop\\strinput.xq.txt.xq"
5+
"commandLineArgs": "-o c -f D:\\Users\\Kirito\\Desktop\\xq_sample\\02_020025.xq"
66
}
77
}
88
}

0 commit comments

Comments
 (0)