-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnc_cli.cpp
More file actions
225 lines (175 loc) · 6.21 KB
/
Copy pathnc_cli.cpp
File metadata and controls
225 lines (175 loc) · 6.21 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "nc_cli.h"
#include "nc_arena.h"
API_INTERNAL Str8List
StrListFromCLIArgs(Arena* MemPool, int ArgC, char** ArgV)
{
Str8List Result = {};
for (u32 I = 0; I < ArgC; ++I) {
Str8 String = CStr8(ArgV[I]);
ListPush(MemPool, &Result, String);
}
return Result;
}
API_INTERNAL CommandLineOption**
CommandLineSlotFromStr(CommandLine* CLI, Str8 String)
{
CommandLineOption** Slot = NULL;
if (CLI->OptionTableSize) {
u64 StringHash = Hash(String);
u64 Bucket = StringHash % CLI->OptionTableSize;
Slot = &CLI->OptionTable[Bucket];
}
return Slot;
}
API_INTERNAL CommandLineOption*
CommandLineOptionFromSlot(CommandLineOption** Slot, Str8 String)
{
CommandLineOption* Result = NULL;
for (CommandLineOption* Opt = *Slot; Opt; Opt = Opt->HashNext) {
if (StrMatch(String, Opt->String, 0)) {
Result = Opt;
break;
}
}
return Result;
}
API_INTERNAL void
CommandLinePushOption(CommandLineOptionList* List, CommandLineOption* Opt)
{
SLL_QUEUE_PUSH(List->Head, List->Tail, Opt);
++List->Count;
}
API_INTERNAL CommandLineOption*
CommandLineInsertOption(Arena* MemPool, CommandLine* CLI, Str8 String, Str8List Opts)
{
CommandLineOption* Opt = NULL;
CommandLineOption** Slot = CommandLineSlotFromStr(CLI, String);
CommandLineOption* FoundOpt = CommandLineOptionFromSlot(Slot, String);
if (FoundOpt) {
Opt = FoundOpt;
} else {
Opt = ArenaPushArrayZero(MemPool, CommandLineOption, 1);
Opt->HashNext = *Slot;
Opt->Hash = Hash(String);
Opt->String = ArenaPushStrCpy(MemPool, String);
Opt->ValueStrings = Opts;
Str8JoinPart JoinPart = {};
JoinPart.Pre = Str8Lit("");
JoinPart.Sep = Str8Lit(",");
JoinPart.Post = Str8Lit("");
Opt->ValueString = StrListJoin(MemPool, &Opt->ValueStrings, &JoinPart);
*Slot = Opt;
CommandLinePushOption(&CLI->Options, Opt);
}
return Opt;
}
API_INTERNAL CommandLine
CommandLineFromStrList(Arena* MemPool, Str8List Arguments)
{
CommandLine Result = {};
Result.ProgramName = Arguments.Head->String;
Result.OptionTableSize = 64;
Result.OptionTable = ArenaPushArrayZero(MemPool, CommandLineOption*, Result.OptionTableSize);
b32 AfterPassthroughOption = FALSE;
b32 FirstPassthrough = TRUE;
for (
Str8Node* Node = Arguments.Head->Next, *Next = NULL;
Node;
Node = Next
) {
Next = Node->Next;
b32 IsOption = FALSE;
Str8 OptionName = Node->String;
if (!AfterPassthroughOption) {
IsOption = TRUE;
if (StrMatch(Node->String, Str8Lit("--"), 0)) {
AfterPassthroughOption = TRUE;
IsOption = FALSE;
} else if (StrMatch(StrPrefix(Node->String, 2), Str8Lit("--"), 0)) {
OptionName = StrSkip(OptionName, 2);
} else if (StrMatch(StrPrefix(Node->String, 1), Str8Lit("-"), 0)) {
OptionName = StrSkip(OptionName, 1);
#if defined(NC_OS_WIN)
} else if (StrMatch(StrPrefix(Node->String, 1), Str8Lit("/"), 0)) {
OptionName = StrSkip(OptionName, 1);
#endif
} else {
IsOption = FALSE;
}
}
if (IsOption) {
b32 HasValues = FALSE;
u64 ValueSignifierPosition1 = StrFindSubStr(OptionName, Str8Lit(":"), 0, 0);
u64 ValueSignifierPosition2 = StrFindSubStr(OptionName, Str8Lit("="), 0, 0);
u64 ValueSignifierPosition = MIN(ValueSignifierPosition1, ValueSignifierPosition2);
Str8 ValuePortion = StrSkip(OptionName, ValueSignifierPosition + 1);
if (ValueSignifierPosition < OptionName.Size)
HasValues = TRUE;
OptionName = StrPrefix(OptionName, ValueSignifierPosition);
Str8List Values = {};
if (HasValues) {
for (Str8Node* SNode = Node; SNode; SNode = SNode->Next) {
Next = SNode->Next;
Str8 String = SNode->String;
if (SNode == Node)
String = ValuePortion;
u8 Splits[] = { ',' };
Str8List ValuesInString = StrSplit(MemPool, String, Splits, ARRAY_COUNT(Splits), FALSE);
for (Str8Node* SubVal = ValuesInString.Head; SubVal; SubVal = SubVal->Next)
ListPush(MemPool, &Values, SubVal->String);
if (!StrMatch(StrPostfix(SNode->String, 1), Str8Lit(","), 0) && (SNode != Node || ValuePortion.Size)) {
break;
}
}
}
CommandLineInsertOption(MemPool, &Result, OptionName, Values);
} else if (!StrMatch(Node->String, Str8Lit("--"), 0) || !FirstPassthrough) {
ListPush(MemPool, &Result.Inputs, Node->String);
FirstPassthrough = FALSE;
}
}
Result.ArgC = Arguments.Count;
Result.ArgV = ArenaPushArrayZero(MemPool, char*, Result.ArgC);
{
u64 I = 0;
for (Str8Node* Node = Arguments.Head; Node; Node = Node->Next) {
Result.ArgV[I++] = (char*) ArenaPushStrCpy(MemPool, Node->String).Str;
}
}
return Result;
}
API_INTERNAL CommandLineOption*
CommandLineOptionFromStr(CommandLine* CLI, Str8 Name)
{
return CommandLineOptionFromSlot(CommandLineSlotFromStr(CLI, Name), Name);
}
API_INTERNAL Str8List
CommandLineStrings(CommandLine* CLI, Str8 Name)
{
Str8List Result = {};
CommandLineOption* Opt = CommandLineOptionFromStr(CLI, Name);
if (Opt)
Result = Opt->ValueStrings;
return Result;
}
API_INTERNAL Str8
CommandLineString(CommandLine* CLI, Str8 Name)
{
Str8 Result = {};
CommandLineOption* Opt = CommandLineOptionFromStr(CLI, Name);
if (Opt)
Result = Opt->ValueString;
return Result;
}
API_INTERNAL b32
CommandLineHasFlag(CommandLine* CLI, Str8 Name)
{
CommandLineOption* Opt = CommandLineOptionFromStr(CLI, Name);
return !!Opt;
}
API_INTERNAL b32
CommandLineHasArgument(CommandLine* CLI, Str8 Name)
{
CommandLineOption* Opt = CommandLineOptionFromStr(CLI, Name);
return (!!Opt && Opt->ValueStrings.Count);
}