Skip to content

Commit

Permalink
Add automatic script detection #3;
Browse files Browse the repository at this point in the history
  • Loading branch information
onepiecefreak3 committed Jul 21, 2021
1 parent 364f6ba commit c71ae11
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
47 changes: 36 additions & 11 deletions XtractQuery/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Komponent.IO;
using XtractQuery.Interfaces;
using XtractQuery.Options;
using XtractQuery.Parsers;
Expand All @@ -26,13 +27,15 @@ static void Main(string[] args)
return;
}

#region Check arguments existing

if (!arguments.Operation.Exists)
{
Console.WriteLine("No operation mode was given. Specify an operation mode by using the -o argument.");
return;
}

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

operation = arguments.Operation.Values.First();
type = arguments.QueryType.Values.First();
file = arguments.QueryFile.Values.First();
#endregion

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

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

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

Expand All @@ -71,6 +75,7 @@ static void Main(string[] args)
switch (operation)
{
case "e":
type = DetermineType(file);
ExtractFile(type, file);
break;

Expand All @@ -80,6 +85,24 @@ static void Main(string[] args)
}
}

private static string DetermineType(string file)
{
using var fileStream = File.OpenRead(file);
using var br = new BinaryReaderX(fileStream);

switch (br.ReadString(4))
{
case "XQ32":
return "xq32";

case "XSEQ":
return "xseq";

default:
return string.Empty;
}
}

private static void ExtractFile(string type, string file)
{
IParser parser;
Expand Down Expand Up @@ -142,11 +165,13 @@ private static void PrintHelp()
Console.WriteLine(" Valid operations are: e for extraction, c for creation");
Console.WriteLine(" -t, --type\t\tThe type of file given");
Console.WriteLine(" Valid types are: xq32, xseq");
Console.WriteLine(" The type is automatically detected when extracting; This argument will not have any effect on operation 'e'");
Console.WriteLine(" -f, --file\t\tThe file to process");
Console.WriteLine();
Console.WriteLine("Example usage:");
Console.WriteLine($"\tExtract a xq32 query to human readable text: {Assembly.GetExecutingAssembly().Location} -o e -t xq32 -f [file]");
Console.WriteLine($"\tExtract any query to human readable text: {Assembly.GetExecutingAssembly().Location} -o e -f [file]");
Console.WriteLine($"\tCreate a xq32 query from human readable text: {Assembly.GetExecutingAssembly().Location} -o c -t xq32 -f [file]");
Console.WriteLine($"\tCreate a xseq query from human readable text: {Assembly.GetExecutingAssembly().Location} -o c -t xseq -f [file]");
}
}
}
2 changes: 1 addition & 1 deletion XtractQuery/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"XtractQuery": {
"commandName": "Project",
"commandLineArgs": "-o e -t xq32 -f D:\\Users\\Kirito\\Desktop\\strinput.xq.txt.xq"
"commandLineArgs": "-o c -f D:\\Users\\Kirito\\Desktop\\xq_sample\\02_020025.xq"
}
}
}

0 comments on commit c71ae11

Please sign in to comment.