~muirrum/cmud

bdded4fadebc8ebb97e11e9cd578846806f9dbf9 — Cara Salter 7 months ago b6a5143
add basic error handling to configuration

returns errors if it can't open the file or if the key isn't found
4 files changed, 30 insertions(+), 10 deletions(-)

M src/config.c
M src/log.c
M src/main.c
M src/server.c
M src/config.c => src/config.c +20 -6
@@ 18,6 18,7 @@
#include <features.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "atconfig.h"


@@ 34,12 35,14 @@
 * =====================================================================================
 */
int config_read_string(const char *key, char *const buf, const char *fname) {
    FILE *conf = fopen(fname, "r");
    FILE *conf;
    if ((conf = fopen(fname, "r"))) {

    
//    char* line = (char*) malloc(sizeof(char)*1024);
    char* line = NULL;
    size_t len = 0;
    ssize_t read = 0;
    size_t read = 0;
    int foundKey = 0;
    while ((read = getline(&line, &len, conf)) != -1) {
        char* cursor = line; // create a cursor we can use to iterate over the


@@ 91,19 94,25 @@ int config_read_string(const char *key, char *const buf, const char *fname) {
    fclose(conf);

    if (!foundKey) {
        error("Could not find key");
        error("Could not find key: %s", key);
        return 5;
    }
    } else {
        error("Couldn't open config file! Check to make sure that it exists: %d", errno);
        error("%s", strerror(errno));
        return -1;
    }
    
    return 0;
}

int config_read_int(const char *key, int *const ret, const char *fname) {
    FILE *conf = fopen(fname, "r");
    FILE *conf;
    if ((conf = fopen(fname, "r"))) {

    char* line = NULL;
    size_t len=0;
    ssize_t read = 0;
    size_t read = 0;
    int foundKey = 0;
    while ((read = getline(&line, &len, conf)) != -1) {
        char* cursor = line; // create a cursor we can use to iterate over the


@@ 148,9 157,14 @@ int config_read_int(const char *key, int *const ret, const char *fname) {
    fclose(conf);

    if (!foundKey) {
        error("No key found...");
        error("Could not find key: %s", key);
        return 5;
    }
    } else {
        error("Couldn't open config file! %d", errno);
        error("%s", strerror(errno));
        return -1;
    }
    
    return 0;
}

M src/log.c => src/log.c +0 -1
@@ 111,7 111,6 @@ int error(char msg[], ...) {
    char *fmt_string;
    vasprintf(&fmt_string, msg, args);
    printf(" \033[31mERROR\033[0m %s\n", fmt_string);
    fprintf(stderr, " \033[31mERROR\033[0m %s\n", fmt_string);
    free(fmt_string);
    va_end(args);
    return 0;

M src/main.c => src/main.c +5 -2
@@ 16,10 16,13 @@
int main() {
  info("Starting up %s version %s", PACKAGE_NAME, VERSION);
  char* name = (char*) malloc(32 * sizeof(char));
  config_read_string("name", name, "config");
  int err = config_read_string("name", name, "config");
  if (err) {
    exit(err);
  }
  info("Starting up %s game server", name);
  free(name);
  int err = 0;
  err = 0;
  server_t server = {0};
  err = server_listen(&server); 
  if (err) {

M src/server.c => src/server.c +5 -1
@@ 44,7 44,11 @@ int server_listen(server_t *server) {
 * 
 */
  int* port = (int*) malloc(sizeof(int));
  config_read_int("port", port, "config");
  err = config_read_int("port", port, "config");
  if (err) {
    warning("No port defined, using 8000 as default.");
    *port = 8000;
  }
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(*port);