aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2023-08-23 12:25:46 -0500
committerjvech <jmvalenciae@unal.edu.co>2023-08-23 12:25:46 -0500
commitca92314f00cb41c3d9d2976cfc819b359d53cda1 (patch)
tree7489a3b7c477809ed6db310a499d5545118efe10 /src
parent23e665000e876f9ff8b521a060b0cdb7e58287cb (diff)
add: basic configuration parsing implemented
parameters implemented: - input_keys - label_keys - epochs - labels - weights_path Network weights and architecture loading is not implemented yet.
Diffstat (limited to 'src')
-rw-r--r--src/main.c5
-rw-r--r--src/util.c80
-rw-r--r--src/util.h3
3 files changed, 85 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index 0071426..7800495 100644
--- a/src/main.c
+++ b/src/main.c
@@ -92,9 +92,10 @@ int main(int argc, char *argv[]) {
struct Configs ml_configs = {
.epochs = 100,
.alpha = 1e-5,
- .config_filepath = "utils/config.yml",
+ .config_filepath = "utils/ml.cfg",
};
- util_load_config(&ml_configs);
+
+ util_load_config(&ml_configs, ml_configs.config_filepath);
util_load_cli(&ml_configs, argc, argv);
return 0;
}
diff --git a/src/util.c b/src/util.c
index 9c1dba3..22c1912 100644
--- a/src/util.c
+++ b/src/util.c
@@ -5,6 +5,9 @@
#include <getopt.h>
#include "util.h"
+#define BUFFER_SIZE 1024
+
+static char ** config_read_values(size_t *n_out_keys, char *first_value, char **strtok_ptr);
void die(const char *fmt, ...)
{
@@ -33,6 +36,13 @@ void * ecalloc(size_t nmemb, size_t size)
return p;
}
+char *e_strdup(const char *s)
+{
+ char *out = strdup(s);
+ if (out == NULL) die("strdup() Error:");
+ return out;
+}
+
void version()
{
@@ -121,3 +131,73 @@ void util_free_config(struct Configs *ml)
free(ml->label_keys);
}
}
+
+void util_load_config(struct Configs *ml, char *filepath)
+{
+ int line_number = 1;
+ char line_buffer[BUFFER_SIZE], line_buffer_original[BUFFER_SIZE];
+ char token_buffer[BUFFER_SIZE];
+ FILE *fp = fopen(filepath, "r");
+ if (fp == NULL) die("util_load_config() Error:");
+
+ while (fgets(line_buffer, BUFFER_SIZE, fp)) {
+ int ret = sscanf(line_buffer, "[%[-_a-zA-Z0-9]]", token_buffer);
+ if (ret >= 1) continue;
+
+ strcpy(line_buffer_original, line_buffer);
+
+ char *line_ptr = line_buffer;
+ while (*line_ptr == ' ') line_ptr++; // delete whitespaces
+
+ /* if the line start with comments or is a blank line ignore it */
+ if (*line_ptr == ';'
+ || *line_ptr == '#'
+ || *line_ptr == '\n') continue;
+
+ /* Verify that each line starts with [a-zA-Z] */
+ if ((*line_ptr < 0x41 && *line_ptr > 0x5A)
+ || (*line_ptr < 0x61 && *line_ptr > 0x7A))
+ goto util_load_config_error;
+
+ /* Load Key Value */
+ char *key, *value;
+ char *ptr_buffer;
+ strtok_r(line_buffer, ";#", &ptr_buffer); // omit comments
+ key = strtok_r(line_buffer, " =", &ptr_buffer);
+ value = strtok_r(NULL, "= ,\n", &ptr_buffer);
+ if (value == NULL) goto util_load_config_error;
+
+ if (!strcmp(key, "weights_path")) ml->weights_filepath = e_strdup(value);
+ else if (!strcmp(key, "loss")) ml->loss = e_strdup(value);
+ else if (!strcmp(key, "epochs")) ml->epochs = (size_t)atol(value);
+ else if (!strcmp(key, "alpha")) ml->alpha = (double)atof(value);
+ else if (!strcmp(key, "inputs")) ml->input_keys = config_read_values(&(ml->n_input_keys), value, &ptr_buffer);
+ else if (!strcmp(key, "labels")) ml->label_keys = config_read_values(&(ml->n_label_keys), value, &ptr_buffer);
+ else die("util_load_config() Error: Unknown parameter '%s' on line '%d'", key, line_number);
+ ptr_buffer = NULL;
+ value = NULL;
+
+ }
+ fclose(fp);
+ return;
+
+util_load_config_error:
+ die("util_load_config() Error: line %d has invalid format '%s'",
+ line_number, line_buffer_original);
+}
+
+char ** config_read_values(size_t *n_out_keys, char *first_value, char **strtok_ptr)
+{
+ *n_out_keys = 1;
+ char **out_keys = ecalloc(1, sizeof(char *));
+ out_keys[0] = e_strdup(first_value);
+
+ char *value;
+ while ((value = strtok_r(NULL, ", \n", strtok_ptr)) != NULL) {
+ out_keys = realloc(out_keys, sizeof(char *) * (*n_out_keys + 1));
+ out_keys[*n_out_keys] = e_strdup(value);
+ (*n_out_keys)++;
+ }
+ return out_keys;
+}
+#undef BUFFER_SIZE
diff --git a/src/util.h b/src/util.h
index e1bcf18..980895f 100644
--- a/src/util.h
+++ b/src/util.h
@@ -8,6 +8,7 @@ struct Configs {
double alpha;
char **input_keys, **label_keys;
size_t n_input_keys, n_label_keys;
+ char *loss;
char *in_filepath;
char *out_filepath;
char *weights_filepath;
@@ -17,6 +18,6 @@ struct Configs {
void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);
void util_load_cli(struct Configs *ml, int argc, char *argv[]);
-void util_load_config(struct Configs *ml);
+void util_load_config(struct Configs *ml, char *filepath);
void util_free_config(struct Configs *ml);
#endif
Feel free to download, copy and edit any repo