Skip to content
Snippets Groups Projects
Select Git revision
  • 01dd7804b87d60b2deab16712eccb3b97351a9b7
  • wip-bootstrap default
  • dualcore
  • ch3/leds
  • ch3/time
  • master
6 results

mpu_wrappers.h

Blame
  • config.c 12.63 KiB
    #include "modules/log.h"
    #include "modules/config.h"
    #include "modules/filesystem.h"
    #include "epicardium.h"
    
    #include <assert.h>
    #include <stdbool.h>
    #include <ctype.h>
    #include <string.h>
    #include <strings.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stddef.h>
    #include <stdio.h>
    
    #define MAX_LINE_LENGTH 80
    #define KEYS_PER_BLOCK 16
    #define KEY_LENGTH 16
    #define NOT_INT_MAGIC ((int)0x80000000)
    
    // one key-value pair representing a line in the config
    typedef struct {
    	char key[KEY_LENGTH];
    
    	// the value in the config file, if it's an integer.
    	// for strings it's set to NOT_INT_MAGIC
    	int value;
    
    	// the byte offset in the config file to read the value string
    	size_t value_offset;
    } config_slot;
    
    // a block of 16 config slots
    // if more are needed, this becomes a linked list
    typedef struct {
    	config_slot slots[KEYS_PER_BLOCK];
    	void *next;
    } config_block;
    
    static config_block *config_data = NULL;
    
    // returns the config slot for a key name
    static config_slot *find_config_slot(const char *key)
    {
    	config_block *current = config_data;
    
    	while (current) {
    		for (int i = 0; i < KEYS_PER_BLOCK; i++) {
    			config_slot *k = &current->slots[i];
    
    			if (strcmp(k->key, key) == 0) {
    				// found what we're looking for
    				return k;
    
    			} else if (*k->key == '\0') {
    				// found the first empty key
    				return NULL;
    			}
    		}
    		current = current->next;
    	}
    
    	return NULL;
    }
    
    // returns the next available config slot, or allocates a new block if needed
    static config_slot *allocate_config_slot()
    {
    	config_block *current;