refactor: dont use typedef struct

This commit is contained in:
Jozef Steinhübl 2024-07-22 14:49:56 +02:00
parent 8b522693a7
commit 7a34db763c
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
3 changed files with 24 additions and 13 deletions

View file

@ -5,9 +5,11 @@
#define INIT_CONFIG "[providers]\n" #define INIT_CONFIG "[providers]\n"
void cli_command_config(int argc, char **argv) { void cli_command_config(int argc, char **argv) {
gimi_config *cfg = config_read(); struct gimi_config *cfg = config_read();
for (int i = 0; i < cfg->providers_size; i++) { for (int i = 0; i < cfg->providers_size; i++) {
gimi_config_provider *provider = cfg->providers[i]; struct gimi_config_provider *provider = cfg->providers[i];
printf("ssh: %s | primary: %d\n", provider->ssh, provider->primary); printf("ssh: %s | primary: %d\n", provider->ssh, provider->primary);
} }
config_free(cfg);
} }

View file

@ -5,7 +5,7 @@
#include <string.h> #include <string.h>
#include <toml.h> #include <toml.h>
gimi_config *config_read() { struct gimi_config *config_read() {
FILE *file_ptr; FILE *file_ptr;
char errbuf[200]; char errbuf[200];
@ -22,18 +22,21 @@ gimi_config *config_read() {
size++; size++;
} }
gimi_config *cfg = (gimi_config *)malloc(sizeof(gimi_config)); struct gimi_config *cfg =
(struct gimi_config *)malloc(sizeof(struct gimi_config));
cfg->providers_size = size; cfg->providers_size = size;
cfg->providers = malloc(cfg->providers_size * sizeof(gimi_config_provider)); cfg->providers =
malloc(cfg->providers_size * sizeof(struct gimi_config_provider));
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
const char *key = toml_key_in(toml_providers, i); const char *key = toml_key_in(toml_providers, i);
toml_table_t *toml_provider = toml_table_in(toml_providers, key); toml_table_t *toml_provider = toml_table_in(toml_providers, key);
gimi_config_provider *provider = struct gimi_config_provider *provider =
(gimi_config_provider *)malloc(sizeof(gimi_config_provider)); (struct gimi_config_provider *)malloc(
sizeof(struct gimi_config_provider));
toml_datum_t ssh = toml_string_in(toml_provider, "ssh"); toml_datum_t ssh = toml_string_in(toml_provider, "ssh");
provider->ssh = strdup(ssh.u.s); provider->ssh = strdup(ssh.u.s);
@ -54,3 +57,8 @@ gimi_config *config_read() {
return cfg; return cfg;
} }
void config_free(struct gimi_config *cfg) {
free(cfg->providers);
free(cfg);
}

View file

@ -1,13 +1,14 @@
#include <stdbool.h> #include <stdbool.h>
typedef struct { struct gimi_config_provider {
char *ssh; char *ssh;
bool primary; bool primary;
} gimi_config_provider; };
typedef struct { struct gimi_config {
int providers_size; int providers_size;
gimi_config_provider **providers; struct gimi_config_provider **providers;
} gimi_config; };
gimi_config *config_read(); struct gimi_config *config_read();
void config_free(struct gimi_config *cfg);