📄 dataini.c
字号:
if ( dat && ixname ) {
char *p = dat;
int ok = 0;
while ( p && !ok ) {
p = strstr(p, ixname);
if ( p && (*(p-1) == '\n') ) ok = 1; /* if found it and it's placed at first char in line */
else if ( p ) p++;
};
if ( ok ) {
char *v = strstr(p+1, INI_EQUALS);
if ( v ) v = strtoend(v+strlen(INI_EQUALS), INI_ENDLINE);
p = transini_line(v);
_free(v);
return p;
};
};
return NULL;
};
static ini_data *searchini ( char *mem, char *ininame, int _alloc ) {
if ( mem && ininame ) {
int s = strlen(ininame);
char *p = mem;
int ok = 0;
while ( p && !ok ) {
p = strstr(p, ininame);
if ( p && (p != mem) && (*(p-1) == INI_FIRSTCHAR) && (*(p+s) == INI_LASTCHAR) )
ok = 1; /* I found ininame and first and last ini char */
else if ( p ) p++;
};
if ( ok ) { /* if I found ininame */
p = p+s+1; /* move to char after last ini char */
return _alloc?(ini_data *)strtoend(p, INI_FIRSTCHAR):p;
};
};
return NULL;
};
static char *getini_ixname ( ini_data **mem, char *ininame, char *ixname ) {
if ( mem && *mem && ininame && ixname ) {
long siz = 0;
ini_data *v = NULL;
*mem = searchini(*mem, ininame, 0);
v = (ini_data *)strtoend(*mem, INI_FIRSTCHAR);
siz = strlen(v);
if ( v ) { /* if I found ininame */
char *p = v;
int ok = 0;
while ( p && !ok ) {
p = strstr(p, ixname);
if ( (p != v) && p && (*(p-1) == '\n') )
ok = 1; /* I found ixname */
else if ( p ) p++;
};
if ( ok ) { /* if I found ixname */
*mem += p-v;
p = strtoend(p, '\n');
_free(v);
return p;
};
p = v+siz-1;
while ( (p != v) && (*p == '\n') ) {
p--;
siz--;
};
if ( p != v ) siz++;
_free(v);
*mem += siz;
};
};
return NULL;
};
/*
get number from ini file (filename), from section [ininame] and in the colum (ixname)
example of seal.ini :
...
[mouse]
speed = 1
....
long x = getininum_fromfile ("seal.ini", "mouse", "speed");
x contains 1
*/
long getininum_fromfile ( char *filename, char *ininame, char *ixname )
{
long *x = ((long*)getini_fromfile(filename, ininame, ixname));
if ( x ) return (*x);
else return 0;
};
/*
read data from file (filename) and structure (ininame).
This read all lines from structure [ininame] to next
structure or to end of the file.
*/
char *inisavefile=NULL; /* cache reading the ini file (should help unless multiple ini file calls are interleaved) */
char *inimem=NULL;
ini_data *getinidata_fromfile ( char *filename, char *ininame ) {
ini_data *d = NULL;
FILE *f;
if ((!inimem) || (!inisavefile) || (strlen(filename)!=strlen(inisavefile)) || strcmp(inisavefile,filename) ) {
if (inimem) {_free(inimem);inimem=NULL;}
if (inisavefile) {_free(inisavefile);inisavefile=NULL;}
f = fopen(filename, "rt");
if ( f ) {
long size = filelength(fileno(f));
inimem = (char *)_malloc(size+1);
if ( inimem ) {
bzero(inimem, size+1);
fread(inimem, size, 1, f);
d = searchini(inimem, ininame, 1);
};
/* remember the filename for the data we have cached */
inisavefile=(char *)_malloc(strlen(filename)+1);
if (inisavefile) strcpy(inisavefile,filename);
fclose(f);
};
} else {
d = searchini(inimem, ininame, 1);
}
return d;
};
/*
return color from INI file (filename), from structure (ininame),
from line (ixname).
typedef struct ini_rgb {
int r; - index of red color
int g; - index of green color
int b; - index of blue color
int x; - reserved
} ini_rgb;
example:
"seal.ini" file :
...
[colors]
desktop = "255, 0, 128"
...
ini_rgb rgb = geini_color("seal.ini", "colors", "desktop");
now rgb contains in r (255), in g (0) and in b (128).
*/
ini_rgb *getini_color ( char *filename, char *ininame, char *ixname )
{
static ini_rgb xcolor; /* static cos we are passing its address back */
char *scolor = getini_fromfile (filename, ininame, ixname);
xcolor.r = 0;
xcolor.g = 0;
xcolor.b = 0;
if ( scolor ) {
char **v = getini_values ( scolor );
int r = 0;
int g = 0;
int b = 0;
if ( v && v[0] && (*(v[0]) == INI_DECANUM) )
r = *((long*)&(v[0][1]));
if ( v && v[1] && (*(v[1]) == INI_DECANUM) )
g = *((long*)&(v[1][1]));
if ( v && v[2] && (*(v[2]) == INI_DECANUM) )
b = *((long*)&(v[2][1]));
xcolor.r = r;
xcolor.g = g;
xcolor.b = b;
freeini_values(v);
_free(scolor);
return &xcolor;
};
return NULL;
};
/*
read value that is set in file (filename) in structure (ininame)
and in line (ixname)
example :
seal.ini file :
...
[SEAL]
info = "desktop environment"
version = 1
...
...to get, what is set in [SEAL], [info] line you simple call
char *i = getini_fromfile("seal.ini", "SEAL", "info");
...to get, what is set in [SEAL], [version] line you simple call
long v = *((long *)getini_fromfile("seal.ini", "SEAL", "version"));
...(i) now contains text : "desktop environment" and (v) number 1.
*/
char *getini_fromfile ( char *filename, char *ininame, char *ixname ) {
ini_data *vx = getinidata_fromfile(filename, ininame);
char *p = NULL;
if ( vx ) {
p = getini_value(vx, ixname);
_free(vx);
}
return p;
};
/*static char *setini_toline ( char *ixname, char *value ) {
return addtomem(NULL, 0, ixname, "=", value, "\n");
};*/
static char *setini_toline ( char *ixname, char *value ) {
/* jdh adding support for set_key emulating BadSector's registry function.
a null string passed just sets ixname without any = or quotes
*/
if( strcmp(value,INI_NONE) )
return addtomem(NULL, 0, ixname, "=", value, "\n"); // value is not "(none)" so carry on as normal
else { // value would end up as ixname="(none)" but we ignore the ="(none)" totally
char *s=malloc(strlen(ixname)+2);
strcpy(s,ixname);
strcat(s,"\n");
return s;
}
};
#ifdef __RSXNT__
char *itoa(long value,char *buf,int radix) {sprintf(buf,(radix==16)?"%lx":((radix==10)?"%ld":"%lo"),value);return buf;}
#endif
static char *setini_todata ( char *ixname, char *value, int type ) {
if ( ixname ) {
switch ( type ) {
case INI_STRING: { /* string */
int size = strlen(value);
if ( size ) { /* string exist */
char *v = _malloc(size+3);
if ( v ) {
char *ok = NULL;
bzero(v, size+3);
v[0] = INI_TEXTCHAR;
strcat(v, value);
v[size+1] = INI_TEXTCHAR;
ok = setini_toline(ixname, v);
_free(v);
return ok;
};
} else { /* string not exist */
return setini_toline(ixname, INI_NONE);
};
}; break;
case INI_HEXANUM: { /* hexa number */
INI_NUMBER x = *((INI_NUMBER*)(value));
char cx1[33];
char cx2[33+2] = {'\0'};
strcat(cx2, "0x");
return setini_toline(ixname, strcat(cx2, itoa(x, cx1, 16)));
}; break;
case INI_DECANUM: { /* deca number */
INI_NUMBER x = *((INI_NUMBER*)(value));
char cx1[33];
return setini_toline(ixname, itoa(x, cx1, 10));
}; break;
};
};
return NULL;
};
/*
set or write "value" into file (filename) to structure
(ininame) and to line (ixname). Value is pointer to value ( text or number ),
type is type of pointer. ( INI_DECANUM, INI_HEXANUM, INI_STRING ).
example :
l_int i = 1;
setini_tofile("seal.ini", "SEAL", "info", "FREE desktop environment", INI_STRING);
setini_tofile("seal.ini", "SEAL", "version", &i, INI_DECANUM);
setini_tofile("seal.ini", "SEAL", "hexa version", &i, INI_HEXANUM);
Output of "seal.ini" file :
...
[SEAL]
info = "FREE desktop environment"
version = 1
hexa version = 0x1
...
*/
/*void setini_tofile ( char *filename, char *ininame, char *ixname, char *value, int type )
{
if ( ininame && ixname ) {
FILE *f = fopen(filename, "rt");
if ( !f ) {
f = fopen(filename, "wt");
fclose(f);
f = fopen(filename, "rt");
};
if ( f ) {
long size = filelength(fileno(f));
long six = 0;
char *mem = (char *)_malloc(size+1);
if ( mem ) {
char *p = mem;
char *ixptr;
bzero(mem, size+1);
fread(mem, size, 1, f);
ixptr = getini_ixname(&p, ininame, ixname);
fclose(f);
f = fopen(filename, "wt");
if ( f ) {
if ( p ) {
fwrite(mem, (p-mem), 1, f);
if ( ixptr ) {
six = strlen(ixptr);
_free(ixptr);
ixptr = setini_todata(ixname, value, type);
fwrite(ixptr, strlen(ixptr)-1, 1, f);
fwrite(p+six, size-(((long)p+six)-(long)mem), 1, f);
} else {
ixptr = setini_todata(ixname, value, type);
fwrite(ixptr, strlen(ixptr), 1, f);
fwrite(p, size-((long)p-(long)mem), 1, f);
};
} else {
long sz = strlen(mem);
fwrite(mem, sz, 1, f);
if ( mem[sz-1] != 10 )
fprintf(f, "\n");
fprintf(f, "\n");
ixptr = addtomem(NULL, 0, INI_CFIRSTCHAR, ininame, INI_CLASTCHAR, "\n");
fwrite(ixptr, strlen(ixptr), 1, f);
_free(ixptr);
ixptr = setini_todata(ixname, value, type);
fwrite(ixptr, strlen(ixptr), 1, f);
};
};
fclose(f);
_free(ixptr);
_free(mem);
};
};
fclose(f);
};
}; */
void setini_tofile ( char *filename, char *ininame, char *ixname, char *value, int type )
{
if ( ininame && ixname ) {
FILE *f = fopen(filename, "rt");
if ( !f ) { /* if file not exist then create new one */
f = fopen(filename, "wt");
fclose(f);
f = fopen(filename, "rt");
};
if ( f ) {
long size = filelength(fileno(f));
/* size ends up incorrect because you are using silly-putty MSDOS text files which add/remove /r all over the place.
You may rely on it to be at least large enough for the data.
*/
long six = 0;
char *mem = (char *)_malloc(size+1);
if ( mem ) {
char *p = mem;
char *ixptr=NULL;
bzero(mem, size+1);
fread(mem, size, 1, f);
size=strlen(mem);
/* should fix the above size problem */
if(value && *value){
ixptr = getini_ixname(&p, ininame, ixname);
} else {
type=INI_STRING; /* forces jdh special interpretation in setini_todata(). */
if(value)
ixptr = getini_ixname(&p, ininame, ixname);
else
p = searchini(p, ininame, 0);
}
fclose(f);
f = fopen(filename, "wt"); /* reopen file in write mode */
if ( f ) {
if ( p ) { /* if ininame was found */
fwrite(mem, (p-mem), 1, f); /* write to ixname */
if( value ) {
if ( ixptr ) { /* if ixname was found */
six = strlen(ixptr);
_free(ixptr);
ixptr = setini_todata(ixname, value, type);
fwrite(ixptr, strlen(ixptr)-1, 1, f);
fwrite(p+six, size-(((long)p+six)-(long)mem), 1, f);
} else { /* if ixname wasn't found */
ixptr = setini_todata(ixname, value, type);
fwrite(ixptr, strlen(ixptr), 1, f);
fwrite(p, size-((long)p-(long)mem), 1, f);
};
} else {
/* replace the entire section with the ixname parameter */
ixptr = setini_todata(ixname, value, type);
fprintf(f, "\n"); /* enter line */
fwrite(ixptr, strlen(ixptr)-1, 1, f);
fprintf(f, "\n\n"); /* add two enter lines */
while(*p && ((mem-p)<size) && (*p != INI_CFIRSTCHAR[0] )/* '[' */ ) p++;
fwrite(p, strlen(p), 1, f);
}
} else { /* if ininame wasn't found */
long sz = strlen(mem);
fwrite(mem, sz, 1, f); /* write to end of file */
if ( mem[sz-1] != 10 )
fprintf(f, "\n"); /* enter line */
fprintf(f, "\n"); /* enter line */
ixptr = addtomem(NULL, 0, INI_CFIRSTCHAR, ininame, INI_CLASTCHAR, "\n");
fwrite(ixptr, strlen(ixptr), 1, f);
_free(ixptr);
if(value)
ixptr = setini_todata(ixname, value, type);
else
ixptr = setini_todata(ixname, "", type);
fwrite(ixptr, strlen(ixptr), 1, f);
};
};
fclose(f);
if(ixptr)_free(ixptr);
if(mem)_free(mem);
};
};
fclose(f);
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -