Skip to content

Commit 97c9a69

Browse files
CopilotJohnAmadis
andcommitted
Use dmini_section_count/dmini_section_name for section iteration
Replace dmini_generate_string + manual string parsing with the proper dmini@0.5 section iteration API: dmini_section_count() and dmini_section_name(index). This gives direct access to section names from the in-memory context without allocating a serialised string or parsing it manually. The dmini_set_active_section / dmini_clear_active_section usage for scoping the context per driver is unchanged. Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent eae43be commit 97c9a69

1 file changed

Lines changed: 35 additions & 88 deletions

File tree

src/dmdevfs.c

Lines changed: 35 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -966,112 +966,59 @@ static driver_node_t* configure_driver(const char* driver_name, dmini_context_t
966966
/**
967967
* @brief Configure drivers for non-main sections that contain a driver_name key
968968
*
969-
* Uses dmini_generate_string to obtain the full INI content from the
970-
* already-parsed context (avoiding a second read of the config file from
971-
* disk). Each non-main section that contains a driver_name key is configured
972-
* as a separate driver with the INI context restricted to that section via
973-
* dmini_set_active_section, so the driver only sees the keys belonging to its
974-
* own section.
969+
* Iterates over all sections in the dmini context using dmini_section_count
970+
* and dmini_section_name. For each non-main section that contains a driver_name
971+
* key, the INI context is restricted to that section via dmini_set_active_section
972+
* so the driver only sees the keys belonging to its own section.
975973
*
976974
* Returns the number of section-specific drivers that were successfully added.
977975
* A non-zero return value signals to the caller that the file is a multi-driver
978976
* config and no fallback main driver should be configured.
979977
*/
980978
static int configure_section_drivers(dmfsi_context_t ctx, dmini_context_t config_ctx)
981979
{
982-
// Generate the full INI content from the already-parsed dmini context.
983-
// This avoids re-reading the file from disk; we work entirely with the
984-
// in-memory representation that dmini built during dmini_parse_file.
985-
int content_size = dmini_generate_string(config_ctx, NULL, 0);
986-
if (content_size <= 0)
987-
{
988-
return 0;
989-
}
980+
int num_added = 0;
981+
int section_count = dmini_section_count(config_ctx);
990982

991-
char* ini_content = (char*)Dmod_Malloc((size_t)content_size);
992-
if (ini_content == NULL)
983+
for (int i = 0; i < section_count; i++)
993984
{
994-
DMOD_LOG_ERROR("Failed to allocate buffer for INI content scan\n");
995-
return 0;
996-
}
985+
const char* section_name = dmini_section_name(config_ctx, i);
997986

998-
if (dmini_generate_string(config_ctx, ini_content, (size_t)content_size) <= 0)
999-
{
1000-
Dmod_Free(ini_content);
1001-
return 0;
1002-
}
987+
// Skip the global (unnamed) section and the [main] section
988+
if (section_name == NULL || strcmp(section_name, INI_MAIN_SECTION) == 0) continue;
1003989

1004-
int num_added = 0;
1005-
const char* p = ini_content;
1006-
while (*p != '\0')
1007-
{
1008-
// Skip leading whitespace on the line
1009-
while (*p == ' ' || *p == '\t') p++;
990+
// Only process sections that declare a driver_name
991+
if (!dmini_has_key(config_ctx, section_name, "driver_name")) continue;
1010992

1011-
if (*p == '[')
1012-
{
1013-
// Extract section name between '[' and ']'
1014-
const char* name_start = p + 1;
1015-
const char* name_end = name_start;
1016-
while (*name_end != '\0' && *name_end != ']' && *name_end != '\n') name_end++;
993+
const char* drv_name = dmini_get_string(config_ctx, section_name, "driver_name", NULL);
994+
if (drv_name == NULL) continue;
1017995

1018-
if (*name_end == ']')
1019-
{
1020-
// Trim leading whitespace inside the brackets
1021-
while (name_start < name_end && (*name_start == ' ' || *name_start == '\t')) name_start++;
1022-
// Trim trailing whitespace inside the brackets
1023-
while (name_end > name_start && (*(name_end - 1) == ' ' || *(name_end - 1) == '\t')) name_end--;
996+
char module_name[DMOD_MAX_MODULE_NAME_LENGTH];
997+
strncpy(module_name, drv_name, sizeof(module_name));
998+
module_name[sizeof(module_name) - 1] = '\0';
1024999

1025-
size_t name_len = (size_t)(name_end - name_start);
1026-
if (name_len > 0 && name_len < DMOD_MAX_MODULE_NAME_LENGTH)
1027-
{
1028-
char section_name[DMOD_MAX_MODULE_NAME_LENGTH];
1029-
strncpy(section_name, name_start, name_len);
1030-
section_name[name_len] = '\0';
1000+
// Restrict the INI context to this section and configure the driver.
1001+
// Token 0 means no owner-token protection (context was created with dmini_create).
1002+
dmini_set_active_section(config_ctx, section_name, 0);
1003+
driver_node_t* driver_node = configure_driver(module_name, config_ctx);
1004+
dmini_clear_active_section(config_ctx, 0);
10311005

1032-
// Skip the [main] section; it is handled by existing logic
1033-
if (strcmp(section_name, INI_MAIN_SECTION) != 0 &&
1034-
dmini_has_key(config_ctx, section_name, "driver_name"))
1035-
{
1036-
const char* drv_name = dmini_get_string(config_ctx, section_name, "driver_name", NULL);
1037-
if (drv_name != NULL)
1038-
{
1039-
char module_name[DMOD_MAX_MODULE_NAME_LENGTH];
1040-
strncpy(module_name, drv_name, sizeof(module_name));
1041-
module_name[sizeof(module_name) - 1] = '\0';
1042-
1043-
// Restrict the INI context to this section and configure the driver.
1044-
// Token 0 means no owner-token protection (context was created with dmini_create).
1045-
dmini_set_active_section(config_ctx, section_name, 0);
1046-
driver_node_t* driver_node = configure_driver(module_name, config_ctx);
1047-
dmini_clear_active_section(config_ctx, 0);
1048-
1049-
if (driver_node == NULL)
1050-
{
1051-
DMOD_LOG_ERROR("Failed to configure driver for section [%s]: %s\n",
1052-
section_name, module_name);
1053-
}
1054-
else if (!dmlist_push_back(ctx->drivers, driver_node))
1055-
{
1056-
DMOD_LOG_ERROR("Failed to add driver to list: %s\n", module_name);
1057-
Dmod_Free(driver_node);
1058-
}
1059-
else
1060-
{
1061-
num_added++;
1062-
}
1063-
}
1064-
}
1065-
}
1066-
}
1006+
if (driver_node == NULL)
1007+
{
1008+
DMOD_LOG_ERROR("Failed to configure driver for section [%s]: %s\n",
1009+
section_name, module_name);
1010+
}
1011+
else if (!dmlist_push_back(ctx->drivers, driver_node))
1012+
{
1013+
DMOD_LOG_ERROR("Failed to add driver to list: %s\n", module_name);
1014+
Dmod_Free(driver_node);
1015+
}
1016+
else
1017+
{
1018+
num_added++;
10671019
}
1068-
1069-
// Advance to the next line
1070-
while (*p != '\0' && *p != '\n') p++;
1071-
if (*p == '\n') p++;
10721020
}
10731021

1074-
Dmod_Free(ini_content);
10751022
return num_added;
10761023
}
10771024

0 commit comments

Comments
 (0)