Line | |
---|
1 | #include <stdio.h>
|
---|
2 | #include <string.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 |
|
---|
5 | //#define DEBUG
|
---|
6 | #define MAX_CHAR 255
|
---|
7 |
|
---|
8 | typedef struct node {
|
---|
9 | void *data; //config values
|
---|
10 | char *name; //config key
|
---|
11 | struct node *next;
|
---|
12 | } node;
|
---|
13 |
|
---|
14 | static node *head = NULL;
|
---|
15 |
|
---|
16 | void configPrint() {
|
---|
17 | node *cNode = head;
|
---|
18 | while(cNode != NULL) {
|
---|
19 | printf("Config Entry: %s\n",cNode->name);
|
---|
20 | cNode = cNode->next;
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | void* configGet(char *name) {
|
---|
25 | node *cNode = head;
|
---|
26 | while(cNode != NULL && strcmp(cNode->name, name) != 0) {
|
---|
27 | cNode = cNode->next;
|
---|
28 | }
|
---|
29 | if(cNode == NULL) {
|
---|
30 | #ifdef DEBUG
|
---|
31 | char msg[MAX_CHAR];
|
---|
32 | strcpy(msg, "Configsetting not found: ");
|
---|
33 | perror(strcat(msg, name));
|
---|
34 | #endif
|
---|
35 | return NULL;
|
---|
36 | } else {
|
---|
37 | return(void*)cNode->data;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | void configDispose() {
|
---|
42 | node *cNode = head;
|
---|
43 | while(cNode != NULL) {
|
---|
44 | node *nextN = cNode->next;
|
---|
45 | free(cNode->data);
|
---|
46 | cNode->data = NULL;
|
---|
47 | free(cNode->name);
|
---|
48 | cNode->name = NULL;
|
---|
49 | free(cNode);
|
---|
50 | cNode = nextN;
|
---|
51 | }
|
---|
52 | head = NULL;
|
---|
53 | }
|
---|
54 |
|
---|
55 | void configPrepend(char *key, void *data) {
|
---|
56 | node *n = (node *) malloc(sizeof(node));
|
---|
57 | n->data = data;
|
---|
58 | n->name = key;
|
---|
59 | n->next = head;
|
---|
60 | head = n;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | void configTest() {
|
---|
65 | configPrepend("pge", (void *) 10);
|
---|
66 | configPrepend("key2", (void *) 20);
|
---|
67 | configPrepend("key3", (void *)30);
|
---|
68 | configPrepend("key4", (void *)1);
|
---|
69 | configPrepend("key5", (void *) 40);
|
---|
70 | configPrepend("key6", (void *)56);
|
---|
71 |
|
---|
72 | printf("Found %d\n", *(int *)configGet("key4"));
|
---|
73 | printf("Found %d\n", *(int *)configGet("key6"));
|
---|
74 | printf("Found %d\n", *(int *)configGet("pge"));
|
---|
75 | printf("Found %d\n", *(int *)configGet("nix"));
|
---|
76 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.