config.c (3079B)
1 /* 2 * Copyright (c) 2014 Mohamed Aslan <maslan@sce.carleton.ca> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <stdio.h> /* printf(3) */ 18 #include <stdlib.h> /* EXIT_SUCCESS */ 19 #include <string.h> /* strchr(3), strcmp(3) */ 20 21 #include "config.h" 22 23 #define N_OPTIONS 3 24 25 static const char config_sample[] = 26 "#\n" 27 "# baseline configuration file\n" 28 "# edit as you wish, but be cautious!\n" 29 "#\n" 30 "\n" 31 "username = Anonymous\n" 32 "useremail = anon@localhost.localdomain\n" 33 "\n"; 34 35 struct option { 36 char *key; 37 char val[32]; 38 }; 39 40 static struct option options[] = { 41 {.key = "username", .val = ""}, 42 {.key = "useremail", .val = ""}, 43 {.key = "editor", .val = ""} 44 }; 45 46 static char* 47 skip_spaces(char *str) 48 { 49 if (str == NULL) 50 return NULL; 51 while ((*str == ' ' || *str == '\t') && *str != '\0') 52 str++; 53 return str; 54 } 55 56 int 57 baseline_config_create(const char *path) 58 { 59 FILE *fp; 60 if (path == NULL) 61 return EXIT_FAILURE; 62 if ((fp = fopen(path, "w")) == NULL) 63 return EXIT_FAILURE; 64 fwrite(config_sample, sizeof(char), sizeof(config_sample) - 1, fp); 65 fclose(fp); 66 return EXIT_SUCCESS; 67 } 68 69 70 int 71 baseline_config_load(const char *path) 72 { 73 char *line = NULL; /* must be initialized to NULL or getline()'s realloc() might complain */ 74 char *ptr, *tmp; 75 char key[32], val[32]; 76 int i; 77 size_t size = 0; 78 ssize_t len; 79 FILE *fp; 80 if (path == NULL) 81 return EXIT_FAILURE; 82 if ((fp = fopen(path, "r")) == NULL) 83 return EXIT_FAILURE; 84 while ((len = getline(&line, &size, fp)) != -1) { 85 /* skip blank lines */ 86 if (line[0] == '\n') 87 continue; 88 ptr = skip_spaces(line); 89 /* skip comments */ 90 if (ptr[0] == '#' || ptr[0] == ';') 91 continue; 92 /* find the end of the key */ 93 tmp = ptr; 94 while(*tmp != ' ' && *tmp != '\t'&& *tmp != '=') 95 tmp++; 96 *tmp++ = '\0'; 97 strlcpy(key, ptr, sizeof(key)); 98 tmp = skip_spaces(tmp); 99 /* skip the '=' */ 100 if (*tmp == '=') 101 tmp++; 102 /* skip white spaces */ 103 ptr = tmp = skip_spaces(tmp); 104 while (*tmp != '\n' && *tmp != '\0') 105 tmp++; 106 *tmp = '\0'; 107 strlcpy(val, ptr, sizeof(val)); 108 for (i = 0; i < N_OPTIONS ; i++) { 109 if (strcmp(options[i].key, key) == 0) { 110 strlcpy(options[i].val, val, sizeof(options[i].val)); 111 break; 112 } 113 } 114 } 115 fclose(fp); 116 return EXIT_SUCCESS; 117 } 118 119 const char * 120 baseline_config_get_val(const char *key) { 121 int i; 122 for (i=0 ; i<N_OPTIONS ; i++) 123 if(!strcmp(options[i].key, key)) 124 return options[i].val; 125 return NULL; 126 } 127