2
2
using System . IO ;
3
3
using System . Linq ;
4
4
using System . Reflection ;
5
+ using Komponent . IO ;
5
6
using XtractQuery . Interfaces ;
6
7
using XtractQuery . Options ;
7
8
using XtractQuery . Parsers ;
@@ -26,13 +27,15 @@ static void Main(string[] args)
26
27
return ;
27
28
}
28
29
30
+ #region Check arguments existing
31
+
29
32
if ( ! arguments . Operation . Exists )
30
33
{
31
34
Console . WriteLine ( "No operation mode was given. Specify an operation mode by using the -o argument." ) ;
32
35
return ;
33
36
}
34
37
35
- if ( ! arguments . QueryType . Exists )
38
+ if ( arguments . Operation . Values . First ( ) == "c" && ! arguments . QueryType . Exists )
36
39
{
37
40
Console . WriteLine ( "No query type was given. Specify a query type by using the -t argument." ) ;
38
41
return ;
@@ -44,25 +47,26 @@ static void Main(string[] args)
44
47
return ;
45
48
}
46
49
47
- operation = arguments . Operation . Values . First ( ) ;
48
- type = arguments . QueryType . Values . First ( ) ;
49
- file = arguments . QueryFile . Values . First ( ) ;
50
+ #endregion
50
51
51
- if ( operation != "e" && operation != "c" )
52
+ file = arguments . QueryFile . Values . First ( ) ;
53
+ if ( ! File . Exists ( file ) )
52
54
{
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 .") ;
54
56
return ;
55
57
}
56
58
57
- if ( type != "xq32" && type != "xseq" )
59
+ operation = arguments . Operation . Values . First ( ) ;
60
+ if ( operation != "e" && operation != "c" )
58
61
{
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 .") ;
60
63
return ;
61
64
}
62
65
63
- if ( ! File . Exists ( file ) )
66
+ type = arguments . QueryType . Values . FirstOrDefault ( ) ;
67
+ if ( operation == "c" && type != "xq32" && type != "xseq" )
64
68
{
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 .") ;
66
70
return ;
67
71
}
68
72
@@ -71,6 +75,7 @@ static void Main(string[] args)
71
75
switch ( operation )
72
76
{
73
77
case "e" :
78
+ type = DetermineType ( file ) ;
74
79
ExtractFile ( type , file ) ;
75
80
break ;
76
81
@@ -80,6 +85,24 @@ static void Main(string[] args)
80
85
}
81
86
}
82
87
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
+
83
106
private static void ExtractFile ( string type , string file )
84
107
{
85
108
IParser parser ;
@@ -142,11 +165,13 @@ private static void PrintHelp()
142
165
Console . WriteLine ( " Valid operations are: e for extraction, c for creation" ) ;
143
166
Console . WriteLine ( " -t, --type\t \t The type of file given" ) ;
144
167
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'" ) ;
145
169
Console . WriteLine ( " -f, --file\t \t The file to process" ) ;
146
170
Console . WriteLine ( ) ;
147
171
Console . WriteLine ( "Example usage:" ) ;
148
- Console . WriteLine ( $ "\t Extract a xq32 query to human readable text: { Assembly . GetExecutingAssembly ( ) . Location } -o e -t xq32 -f [file]") ;
172
+ Console . WriteLine ( $ "\t Extract any query to human readable text: { Assembly . GetExecutingAssembly ( ) . Location } -o e -f [file]") ;
149
173
Console . WriteLine ( $ "\t Create a xq32 query from human readable text: { Assembly . GetExecutingAssembly ( ) . Location } -o c -t xq32 -f [file]") ;
174
+ Console . WriteLine ( $ "\t Create a xseq query from human readable text: { Assembly . GetExecutingAssembly ( ) . Location } -o c -t xseq -f [file]") ;
150
175
}
151
176
}
152
177
}
0 commit comments