[c5c522c] | 1 | /********************************************* |
---|
| 2 | ** cfg.c example |
---|
| 3 | ** gareth@omnipotent.net 1/June/1998 |
---|
| 4 | ** |
---|
| 5 | ** This code will create cfg_testfile.cfg if |
---|
| 6 | ** it doesn't already exist and set some |
---|
| 7 | ** values within it. Simple really. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <stdlib.h> |
---|
| 12 | #include <unistd.h> |
---|
| 13 | |
---|
| 14 | #include "cfg.h" |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #define TESTFILE "cfg_testfile.cfg" |
---|
| 18 | |
---|
| 19 | int main() { |
---|
| 20 | cfg *cfgfile; |
---|
| 21 | |
---|
| 22 | /* First create our object */ |
---|
| 23 | if ((cfgfile=cfg_new())==NULL) { |
---|
| 24 | printf("Error creating config object\n"); |
---|
| 25 | exit(255); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | /* Then find a config file to open */ |
---|
| 29 | if (cfgfile->openfile(cfgfile,TESTFILE,CFG_READWRITE)!=0) { |
---|
| 30 | printf("Error reading file: %s\n",cfgfile->lasterror); |
---|
| 31 | cfgfile->free(cfgfile); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /* Save to disk on every update */ |
---|
| 35 | cfgfile->autoflush(cfgfile,1); |
---|
| 36 | |
---|
| 37 | /* print out the current settings - Will return NULL if */ |
---|
| 38 | /* they don't currently exist (eg. new file) */ |
---|
| 39 | printf("'A test string' is set to '%s'\n", |
---|
| 40 | cfgfile->getstring(cfgfile,"A test string")); |
---|
| 41 | printf("'A test integer' is set to %d\n\n", |
---|
| 42 | cfgfile->getint(cfgfile,"A test integer")); |
---|
| 43 | |
---|
| 44 | /* set to some new values */ |
---|
| 45 | cfgfile->setstring(cfgfile,"A test string","Marvin the paranoid android"); |
---|
| 46 | cfgfile->setint(cfgfile,"A test integer",42); |
---|
| 47 | |
---|
| 48 | /* print out the new settings */ |
---|
| 49 | printf("'A test string' is now set to '%s'\n", |
---|
| 50 | cfgfile->getstring(cfgfile,"A test string")); |
---|
| 51 | printf("'A test integer' is now set to %d\n\n", |
---|
| 52 | cfgfile->getint(cfgfile,"A test integer")); |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | /* set the values to something else with autoflush off */ |
---|
| 56 | cfgfile->autoflush(cfgfile,0); |
---|
| 57 | cfgfile->setstring(cfgfile,"A test string","Life, don't talk to me about life"); |
---|
| 58 | cfgfile->setint(cfgfile,"A test integer",417362); |
---|
| 59 | |
---|
| 60 | /* print out the new settings */ |
---|
| 61 | printf("'A test string' is finally set to '%s'\n", |
---|
| 62 | cfgfile->getstring(cfgfile,"A test string")); |
---|
| 63 | printf("'A test integer' is finally set to %d\n\n", |
---|
| 64 | cfgfile->getint(cfgfile,"A test integer")); |
---|
| 65 | |
---|
| 66 | /* flush the last set of data to disk as autoflush is off */ |
---|
| 67 | cfgfile->flush(cfgfile); |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | /* cleanup */ |
---|
| 71 | |
---|
| 72 | cfgfile->free(cfgfile); |
---|
| 73 | |
---|
| 74 | return(0); |
---|
| 75 | } |
---|
| 76 | |
---|