home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Boldly Go Collection
/
version40.iso
/
TS
/
17A
/
DRWIN101.ZIP
/
CFGUTIL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-24
|
5KB
|
204 lines
#include <stdio.h>
#include <string.h>
#include "strutil.h"
#include "cfgutil.h"
//===========================================================================
void cfg_updateval(CFG_TYPE* ini,int n,char* s)
{
int i;
unsigned long d;
double dd;
char* dp;
for (i=0;i<n;i++) if (stricmpshort(s,ini[i].name)==0) break;
if (i>=n) return;
if (ini[i].type & CFG_NOLOAD) return;
dp=(char*)ini[i].val;
s=&s[strlen(ini[i].name)]; //skip over name
while (*s && isspace(*s)) s++;
if (*s!='=') return;
s++;
while (*s && isspace(*s)) s++;
if (!*s) return;
switch (ini[i].type & CFG_TYPE_MASK) {
case CFG_CHAR:
if (*s=='\'') s++;
strcchr((char*)dp,s); //convert C-type char constant
break;
case CFG_BYTE:
if (strasc2ul(s,&d,10)) *(unsigned char*)dp=(unsigned char)d;
break;
case CFG_INT:
if (strasc2ul(s,&d,10)) *(int*)dp=(int)d;
break;
case CFG_UINT:
if (strasc2ul(s,&d,10)) *(unsigned int*)dp=(unsigned int)d;
break;
case CFG_LONG:
if (strasc2ul(s,&d,10)) *(long*)dp=(long)d;
break;
case CFG_ULONG:
if (strasc2ul(s,&d,10)) *(unsigned long*)dp=(unsigned long)d;
break;
case CFG_DOUBLE:
if (sscanf(s,"%lG",&dd)) *(double*)dp=dd;
break;
case CFG_STRING:
if (*s=='\"') {
s++;
while (*s && (*s!='\"')) s=strcchr(dp++,s);
*dp=0;
}
else {
while (*s && !isspace(*s)) *dp++=*s++;
*dp=0;
} //if not in quotes
break;
case CFG_CUSTOM:
if (ini[i].func) ini[i].func(i,dp,s,CFG_LOAD);
break;
default: ;
} //switch
} //cfg_updateval
//===========================================================================
int cfg_load(char* fn,char* obj,CFG_TYPE* ini,int n)
{
FILE* f;
char s[0x80];
char* p;
int l;
char found=0;
if (n==0) while (ini[n].name) n++;
if (!n) return 0;
l=strlen(obj); //get length of object to find
if (!l) return 0;
f=fopen(fn,"rt");
if (!f) return 0;
while (!found && fgets(s,sizeof(s),f)) { //get line, bomb on EOF
if (s[sizeof(s)-1]=='\n') s[sizeof(s)-1]=0; //kill '\n' from file i/o
p=s;
while (*p && isspace(*p)) p++; //skip whitespace
if (*p!='[') continue; //not "[<objectname>]"
p++;
if (stricmpshort(p,obj)) continue; //returns 0 if equal
p=&p[l];
while (*p && isspace(*p)) p++; //skip whitespace
found=(*p==']'); //check for final ']'
} //while
if (!found) { fclose(f); return 0; }
found=0; //we'll be looking again
while (!found && fgets(s,sizeof(s),f)) {
if (s[sizeof(s)-1]=='\n') s[sizeof(s)-1]=0; //kill '\n' from file i/o
p=s;
while (*p && isspace(*p)) p++; //skip whitespace
if (*p=='[') break; //if a new object starting, quit
strcomment(p,';'); //delete comments
cfg_updateval(ini,n,p);
} //while
fclose(f);
return 1;
} //cfg_load
//===========================================================================
int cfg_save(char* fn,char* obj,CFG_TYPE* ini,int n,int append)
{
FILE* f;
char s[0x100];
char* dp;
int i,l;
if (n==0) while (ini[n].name) n++;
if (!n) return 0;
l=strlen(obj); //get length of object to find
if (!l) return 0;
if (append)
f=fopen(fn,"at");
else
f=fopen(fn,"wt");
if (!f) return 0;
fprintf(f,"\n[%s]\n",obj); //write out object/application name
for (i=0;i<n;i++) {
if (ini[i].type & CFG_NOSAVE) continue;
dp=(char*)ini[i].val; //point to data item
fprintf(f,"%s=",ini[i].name);
switch (ini[i].type & CFG_TYPE_MASK) {
case CFG_CHAR:
l=str2cchr(s,*(char*)dp); //convert to C-type character
s[l]=0;
if (ini[i].type & CFG_QUOTES)
fprintf(f,"\'%s\'\n",s);
else
fprintf(f,"%s\n",s);
break;
case CFG_BYTE:
if (ini[i].type & CFG_HEX)
fprintf(f,"0x%X\n",*(unsigned char*)dp);
else
fprintf(f,"%u\n",*(unsigned char*)dp);
break;
case CFG_INT:
fprintf(f,"%d\n",*(int*)dp);
break;
case CFG_UINT:
if (ini[i].type & CFG_HEX)
fprintf(f,"0x%X\n",*(unsigned int*)dp);
else
fprintf(f,"%u\n",*(unsigned int*)dp);
break;
case CFG_LONG:
fprintf(f,"%ld\n",*(long*)dp);
break;
case CFG_ULONG:
if (ini[i].type & CFG_HEX)
fprintf(f,"0x%lX\n",*(unsigned long*)dp);
else
fprintf(f,"%lu\n",*(unsigned long*)dp);
break;
case CFG_DOUBLE:
fprintf(f,"%G\n",*(double*)dp);
break;
case CFG_STRING:
str2cstr(s,(char*)dp,sizeof(s)); //write to C-type char string
if (ini[i].type & CFG_QUOTES)
fprintf(f,"\"%s\"\n",s);
else
fprintf(f,"%s\n",s);
break;
case CFG_CUSTOM:
if (ini[i].func) {
ini[i].func(i,dp,s,CFG_SAVE);
fprintf(f,"%s\n",s);
}
break;
default:
fprintf(f,"\n");
} //switch on type
} //for
fclose(f);
return 1;
} //cfg_save