I’ve been playing around in C more lately, and made a really simple configuration loader that behaves kind of like a simplified version of INI files. So far it just supports string, int and float configuration settings, and a fixed number of total configuration settings.
Sample Usage Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
settings config; settings_load(&config, "settings.ini"); //for(int i = 0; i < config.count; i++) // printf("%s, %s\n", config.list[i].key, config.list[i].value); char *name = settings_read_string(&config, "name", "<none>"); int age = settings_read_int(&config, "age", 0); float cool_factor = settings_read_float(&config, "cool_factor", 10.0); printf("name: %s\n", name); printf("age: %d\n", age); printf("cool_factor: %4.1f\n", cool_factor); |
settings.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#ifndef _SETTINGS_H_ #define _SETTINGS_H_ #define MAX_SETTINGS 512 #define MAX_KEY_LENGTH 255 #define MAX_VALUE_LENGTH 255 typedef struct keyvaluepair_t { char key[MAX_KEY_LENGTH]; char value[MAX_VALUE_LENGTH]; } keyvaluepair; typedef struct settings_t { int count; keyvaluepair list[MAX_SETTINGS]; } settings; int settings_load(settings *settings, const char *name); char *settings_read_string(settings *settings, const char *name, char *default_value); int settings_read_int(settings *settings, const char *name, int default_value); float settings_read_float(settings *settings, const char *name, float default_value); #endif |
settings.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include "settings.h" #include <stdlib.h> #include <stdio.h> #include <string.h> int settings_load(settings *settings, const char *filename) { FILE *pFile; char buffer[1024]; char *key, *value; int index = 0; pFile = fopen(filename, "r"); if (pFile != NULL) { while(fgets(buffer, 1024, pFile) != NULL) { key = strtok(buffer, "=\r\n"); value = strtok(NULL, "=\r\n"); if(key != NULL && value != NULL) { strcpy_s(settings->list[settings->count].key, MAX_KEY_LENGTH, key); strcpy_s(settings->list[settings->count].value, MAX_VALUE_LENGTH, value); settings->count++; } } fclose(pFile); } return 0; } char *settings_read_string(settings *settings, const char *name, char *default_value) { for(int i = 0; i < settings->count; i++) { if(strcmp(name, settings->list[i].key) == 0) { return settings->list[i].value; } } return default_value; } int settings_read_int(settings *settings, const char *name, int default_value) { for(int i = 0; i < settings->count; i++) { if(strcmp(name, settings->list[i].key) == 0) { return strtol(settings->list[i].value, NULL, 10); } } return default_value; } float settings_read_float(settings *settings, const char *name, float default_value) { for(int i = 0; i < settings->count; i++) { if(strcmp(name, settings->list[i].key) == 0) { return strtof(settings->list[i].value, NULL); } } return default_value; } |