Skip to content

Commit ff7ab03

Browse files
authored
Merge pull request #31 from choco-technologies/copilot/update-driver-name-per-section
Use dmini section iteration API in configure_section_drivers
2 parents 6d2e808 + 97c9a69 commit ff7ab03

1 file changed

Lines changed: 16 additions & 48 deletions

File tree

src/dmdevfs.c

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#define DMDEVFS_CONTEXT_MAGIC 0x444D4456 // 'DMDV'
2424
#define ROOT_DIRECTORY_NAME "/"
2525
#define MAX_PATH_LENGTH (DMOD_MAX_MODULE_NAME_LENGTH + 20)
26-
#define INI_LINE_BUFFER_SIZE 256
2726
#define INI_MAIN_SECTION "main"
2827

2928
/**
@@ -75,7 +74,7 @@ struct dmfsi_context
7574
// ============================================================================
7675
static int configure_drivers(dmfsi_context_t ctx, const char* driver_name, const char* config_path);
7776
static driver_node_t* configure_driver(const char* driver_name, dmini_context_t config_ctx);
78-
static int configure_section_drivers(dmfsi_context_t ctx, dmini_context_t config_ctx, const char* config_path);
77+
static int configure_section_drivers(dmfsi_context_t ctx, dmini_context_t config_ctx);
7978
static int unconfigure_drivers(dmfsi_context_t ctx);
8079
static bool is_file(const char* path);
8180
static bool is_driver( const char* name);
@@ -857,7 +856,7 @@ static int configure_drivers(dmfsi_context_t ctx, const char* driver_name, const
857856
// Section-specific driver_name entries take priority over the file/directory
858857
// derived driver name. Only configure the main driver when no section-level
859858
// drivers are present in the file.
860-
int section_drivers_added = configure_section_drivers(ctx, config_ctx, full_path);
859+
int section_drivers_added = configure_section_drivers(ctx, config_ctx);
861860
if (section_drivers_added == 0)
862861
{
863862
driver_node_t* driver_node = configure_driver(module_name, config_ctx);
@@ -967,57 +966,28 @@ static driver_node_t* configure_driver(const char* driver_name, dmini_context_t
967966
/**
968967
* @brief Configure drivers for non-main sections that contain a driver_name key
969968
*
970-
* Scans the config file line by line for INI section headers. For each section
971-
* other than "main" that contains a driver_name key, a new driver is configured
972-
* with the INI context restricted to that section via dmini_set_active_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
973972
* so the driver only sees the keys belonging to its own section.
974973
*
975974
* Returns the number of section-specific drivers that were successfully added.
976975
* A non-zero return value signals to the caller that the file is a multi-driver
977976
* config and no fallback main driver should be configured.
978977
*/
979-
static int configure_section_drivers(dmfsi_context_t ctx, dmini_context_t config_ctx, const char* config_path)
978+
static int configure_section_drivers(dmfsi_context_t ctx, dmini_context_t config_ctx)
980979
{
981-
void* file = Dmod_FileOpen(config_path, "r");
982-
if (file == NULL)
983-
{
984-
DMOD_LOG_ERROR("Failed to open config file for section scan: %s\n", config_path);
985-
return 0;
986-
}
987-
988980
int num_added = 0;
989-
char line[INI_LINE_BUFFER_SIZE];
990-
while (Dmod_FileReadLine(line, sizeof(line), file) != NULL)
991-
{
992-
// Skip lines that don't start with '[' (ignore leading whitespace)
993-
char* p = line;
994-
while (*p == ' ' || *p == '\t') p++;
995-
if (*p != '[') continue;
996-
997-
// Find closing bracket
998-
char* end = strchr(p, ']');
999-
if (end == NULL || end <= p) continue;
1000-
1001-
// Extract section name
1002-
size_t name_len = (size_t)(end - p - 1);
1003-
if (name_len == 0 || name_len >= DMOD_MAX_MODULE_NAME_LENGTH) continue;
981+
int section_count = dmini_section_count(config_ctx);
1004982

1005-
char section_name[DMOD_MAX_MODULE_NAME_LENGTH];
1006-
strncpy(section_name, p + 1, name_len);
1007-
section_name[name_len] = '\0';
1008-
1009-
// Trim trailing whitespace from section name
1010-
size_t slen = strlen(section_name);
1011-
while (slen > 0 && (section_name[slen - 1] == ' ' || section_name[slen - 1] == '\t'))
1012-
{
1013-
slen--;
1014-
}
1015-
section_name[slen] = '\0';
983+
for (int i = 0; i < section_count; i++)
984+
{
985+
const char* section_name = dmini_section_name(config_ctx, i);
1016986

1017-
// Skip empty section names and the "main" section (handled by existing logic)
1018-
if (slen == 0 || strcmp(section_name, INI_MAIN_SECTION) == 0) continue;
987+
// Skip the global (unnamed) section and the [main] section
988+
if (section_name == NULL || strcmp(section_name, INI_MAIN_SECTION) == 0) continue;
1019989

1020-
// Check if this section has a driver_name key
990+
// Only process sections that declare a driver_name
1021991
if (!dmini_has_key(config_ctx, section_name, "driver_name")) continue;
1022992

1023993
const char* drv_name = dmini_get_string(config_ctx, section_name, "driver_name", NULL);
@@ -1035,11 +1005,10 @@ static int configure_section_drivers(dmfsi_context_t ctx, dmini_context_t config
10351005

10361006
if (driver_node == NULL)
10371007
{
1038-
DMOD_LOG_ERROR("Failed to configure driver for section [%s]: %s\n", section_name, module_name);
1039-
continue;
1008+
DMOD_LOG_ERROR("Failed to configure driver for section [%s]: %s\n",
1009+
section_name, module_name);
10401010
}
1041-
1042-
if (!dmlist_push_back(ctx->drivers, driver_node))
1011+
else if (!dmlist_push_back(ctx->drivers, driver_node))
10431012
{
10441013
DMOD_LOG_ERROR("Failed to add driver to list: %s\n", module_name);
10451014
Dmod_Free(driver_node);
@@ -1050,7 +1019,6 @@ static int configure_section_drivers(dmfsi_context_t ctx, dmini_context_t config
10501019
}
10511020
}
10521021

1053-
Dmod_FileClose(file);
10541022
return num_added;
10551023
}
10561024

0 commit comments

Comments
 (0)