coallpsed arena -> allocatorinfo : command_line.h/c

This commit is contained in:
ed
2025-02-09 12:07:44 -05:00
parent c8cb9f3995
commit 716f8a7f3f
7 changed files with 36 additions and 228 deletions
+1 -135
View File
@@ -34,39 +34,6 @@ cmd_line_opt_from_slot(CmdLineOpt** slot, String8 string) {
return result;
}
CmdLineOpt*
cmd_line_insert_opt(Arena* arena, CmdLine* cmd_line, String8 string, String8List values)
{
#if MD_DONT_MAP_ARENA_TO_ALLOCATOR_IMPL
CmdLineOpt *var = 0;
CmdLineOpt **slot = cmd_line_slot_from_string(cmd_line, string);
CmdLineOpt *existing_var = cmd_line_opt_from_slot(slot, string);
if(existing_var != 0)
{
var = existing_var;
}
else
{
var = push_array(arena, CmdLineOpt, 1);
var->hash_next = *slot;
var->hash = cmd_line_hash_from_string(string);
var->string = push_str8_copy(arena, string);
var->value_strings = values;
StringJoin join = {0};
join.pre = str8_lit("");
join.sep = str8_lit(",");
join.post = str8_lit("");
var->value_string = str8_list_join(arena, &var->value_strings, &join);
*slot = var;
cmd_line_push_opt(&cmd_line->options, var);
}
return var;
#else
return cmd_line_insert_opt_alloc(arena_allocator(arena), cmd_line, string, values);
#endif
}
CmdLineOpt*
cmd_line_insert_opt_alloc(AllocatorInfo ainfo, CmdLine* cmd_line, String8 string, String8List values)
{
@@ -97,108 +64,7 @@ cmd_line_insert_opt_alloc(AllocatorInfo ainfo, CmdLine* cmd_line, String8 string
return var;
}
CmdLine
cmd_line_from_string_list(Arena* arena, String8List command_line)
{
#if MD_DONT_MAP_ARENA_TO_ALLOCATOR_IMPL
CmdLine parsed = {0};
parsed.exe_name = command_line.first->string;
// NOTE(rjf): Set up config option table.
{
parsed.option_table_size = 4096;
parsed.option_table = push_array(arena, CmdLineOpt*, parsed.option_table_size);
}
// NOTE(rjf): Parse command line.
B32 after_passthrough_option = 0;
B32 first_passthrough = 1;
for (String8Node* node = command_line.first->next, *next = 0; node != 0; node = next)
{
next = node->next;
String8 option_name = node->string;
// NOTE(rjf): Look at -- or - at the start of an argument to determine if it's
// a flag option. All arguments after a single "--" (with no trailing string
// on the command line will be considered as input files.
B32 is_option = 1;
if(after_passthrough_option == 0)
{
if (str8_match(node->string, str8_lit("--"), 0)) {
after_passthrough_option = 1;
is_option = 0;
}
else if (str8_match(str8_prefix(node->string, 2), str8_lit("--"), 0)) {
option_name = str8_skip(option_name, 2);
}
else if (str8_match(str8_prefix(node->string, 1), str8_lit("-"), 0)) {
option_name = str8_skip(option_name, 1);
}
else {
is_option = 0;
}
}
else
{
is_option = 0;
}
// NOTE(rjf): This string is an option.
if(is_option)
{
B32 has_arguments = 0;
U64 arg_signifier_position1 = str8_find_needle(option_name, 0, str8_lit(":"), 0);
U64 arg_signifier_position2 = str8_find_needle(option_name, 0, str8_lit("="), 0);
U64 arg_signifier_position = min(arg_signifier_position1, arg_signifier_position2);
String8 arg_portion_this_string = str8_skip(option_name, arg_signifier_position+1);
if (arg_signifier_position < option_name.size) {
has_arguments = 1;
}
option_name = str8_prefix(option_name, arg_signifier_position);
String8List arguments = {0};
// NOTE(rjf): Parse arguments.
if (has_arguments)
{
for (String8Node* n = node; n; n = n->next)
{
next = n->next;
String8 string = n->string;
if (n == node) {
string = arg_portion_this_string;
}
U8 splits[] = { ',' };
String8List args_in_this_string = str8_split(arena, string, splits, array_count(splits), 0);
for (String8Node* sub_arg = args_in_this_string.first; sub_arg; sub_arg = sub_arg->next) {
str8_list_push(arena, &arguments, sub_arg->string);
}
if ( !str8_match(str8_postfix(n->string, 1), str8_lit(","), 0) && (n != node || arg_portion_this_string.size != 0)) {
break;
}
}
}
// NOTE(rjf): Register config variable.
cmd_line_insert_opt(arena, &parsed, option_name, arguments);
}
// NOTE(rjf): Default path, treat as a passthrough config option to be
// handled by tool-specific code.
else if ( !str8_match(node->string, str8_lit("--"), 0) || !first_passthrough) {
str8_list_push(arena, &parsed.inputs, node->string);
after_passthrough_option = 1;
first_passthrough = 0;
}
}
return parsed;
#else
return cmd_line_from_string_list_alloc(arena_allocator(arena), command_line);
#endif
}
CmdLine
cmd_line_from_string_list_alloc(AllocatorInfo ainfo, String8List command_line)
+19 -2
View File
@@ -52,11 +52,25 @@ String8 cmd_line_string (CmdLine* cmd_line, String8 name);
B32 cmd_line_has_flag (CmdLine* cmd_line, String8 name);
B32 cmd_line_has_argument (CmdLine* cmd_line, String8 name);
MD_API CmdLineOpt* cmd_line_insert_opt (Arena* arena, CmdLine* cmd_line, String8 string, String8List values);
inline CmdLineOpt* cmd_line_insert_opt_push (Arena* arena, CmdLine* cmd_line, String8 string, String8List values);
MD_API CmdLineOpt* cmd_line_insert_opt_alloc (AllocatorInfo ainfo, CmdLine* cmd_line, String8 string, String8List values);
MD_API CmdLine cmd_line_from_string_list (Arena* arena, String8List arguments);
inline CmdLine cmd_line_from_string_list_push (Arena* arena, String8List arguments);
MD_API CmdLine cmd_line_from_string_list_alloc(AllocatorInfo ainfo, String8List arguments);
#define cmd_line_insert_opt(allocator, cmd_line, string, values) \
_Generic(allocator, \
Arena*: cmd_line_insert_opt_push, \
AllocatorInfo: cmd_line_insert_opt_alloc, \
default: METADESK_NO_RESOLVED_GENERIC_SELECTION \
) MD_RESOLVED_FUNCTION_CALL(allocator, cmd_line, string, values)
#define cmd_line_from_string_list(allocator, arguments) \
_Generic(allocator, \
Arena*: cmd_line_from_string_list_push, \
AllocatorInfo: cmd_line_from_string_list_alloc, \
default: METADESK_NO_RESOLVED_GENERIC_SELECTION \
) MD_RESOLVED_FUNCTION_CALL(allocator, arguments)
inline U64
cmd_line_hash_from_string(String8 string) {
U64 result = 5381;
@@ -66,6 +80,9 @@ cmd_line_hash_from_string(String8 string) {
return result;
}
force_inline CmdLineOpt* cmd_line_insert_opt_push (Arena* arena, CmdLine* cmd_line, String8 string, String8List values) { return cmd_line_insert_opt_alloc (arena_allocator(arena), cmd_line, string, values); }
force_inline CmdLine cmd_line_from_string_list_push(Arena* arena, String8List command_line) { return cmd_line_from_string_list_alloc(arena_allocator(arena), command_line); }
inline CmdLineOpt* cmd_line_opt_from_string(CmdLine *cmd_line, String8 name) { return cmd_line_opt_from_slot(cmd_line_slot_from_string(cmd_line, name), name); }
inline B32 cmd_line_has_flag (CmdLine *cmd_line, String8 name) { CmdLineOpt *var = cmd_line_opt_from_string(cmd_line, name); return(var != 0); }
inline B32 cmd_line_has_argument (CmdLine *cmd_line, String8 name) { CmdLineOpt *var = cmd_line_opt_from_string(cmd_line, name); return(var != 0 && var->value_strings.node_count > 0); }
+1 -1
View File
@@ -22,7 +22,7 @@ void main_thread_base_entry_point(MainThread_EntryPointProc* entry_point, char**
TempArena scratch = scratch_begin(0, 0);
String8List command_line_argument_strings = os_string_list_from_argcv(scratch.arena, (int)arguments_count, arguments);
CmdLine cmdline = cmd_line_from_string_list(scratch.arena, command_line_argument_strings);
CmdLine cmdline = cmd_line_from_string_list_push(scratch.arena, command_line_argument_strings);
B32 capture = cmd_line_has_flag(&cmdline, str8_lit("capture"));
if (capture) {
prof_begin_capture(arguments[0]);
+1 -1
View File
@@ -42,7 +42,7 @@ void tctx_read_srcloc (char** file_name, U64* line_number);
inline TempArena
scratch__begin_alloc(AllocatorInfo ainfo) {
Arena* arena = extract_arena(ainfo);
TempArena scratch = temp_begin(tctx_get_scratch(arena, arena != nullptr));
TempArena scratch = temp_begin(tctx_get_scratch(&arena, arena != nullptr));
return scratch;
}