📄 registry.c
字号:
/******************************************************************
* SEAL 2.0 *
* Copyright (c) 1999-2002 SEAL Developers. All Rights Reserved. *
* *
* Web site: http://sealsystem.sourceforge.net/ *
* E-mail (current maintainer): orudge@users.sourceforge.net *
******************************************************************/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <io.h>
#include <registry.h>
////////////////////////////////////////////////////////////////////////////////
// Seal 2.00 Registry !
p_key rootkey;
////////////////////////////////////////////////////////////////////////////////
p_key found_key_in ( p_key o, char * name, char lnk ) {
//DEBUG_printf("Look for '%s' in '%s'\n",name,o->name);
if ( o && name ) {
uint a = 0;
if ( lnk && o->type == KEY_LINK ) o = (p_key)o->data;
if ( name[0] == '/' || name[0] == '\\' ) name++;
if ( !name[0] ) return o; // If name is "" : it the key, so return it
if ( o->chl ) {
p_key f = o->chl;
p_key p = f;
while ( name[a] ) {
if ( name[a] == '/' || name[a] == '\\' ) {
do {
if ( !strnicmp( name,p->name,a) ) {
if ( !p->name[a] )
return found_key_in(p,name+a+1,lnk);
};
p = p->nxt;
} while ( p != f);
return NULL; // Not found here ...
};
a++;
};
f = o->chl;
p = f;
do {
if ( !stricmp( name,p->name) ) {
if ( lnk && p->type == KEY_LINK ) p = (p_key)p->data;
return p;
};
p = p->nxt;
} while ( p != f);
};
};
return NULL; // Not found at all !
};
////////////////////////////////////////////////////////////////////////////////
p_key found_key_ex ( char * name, char lnk ) {
if ( rootkey && name ) return found_key_in(rootkey,name,lnk);
return NULL;
};
////////////////////////////////////////////////////////////////////////////////
p_key found_key ( char * name ) {
return found_key_ex(name,1);
};
////////////////////////////////////////////////////////////////////////////////
char key_exists ( char * name ) {
return found_key(name) ? 1 : 0;
};
////////////////////////////////////////////////////////////////////////////////
char create_key ( char * parent, char * name ) {
if ( parent && name ) {
p_key own = found_key(parent);
p_key o;
if ( !own ) return RE_PATH_NOT_FOUND; // Unknow parent
if ( found_key_in(own,name,1) ) return RE_SUCCESS; // Exists so sucess
o = (p_key)malloc(sizeof(t_key));
if ( !o ) return RE_UNKOWN; // No memory for ..
memset(o,0,sizeof(t_key));
o->name = strdup(name);
o->own = own;
if ( !own->chl ) {
own->chl = o;
o->nxt = o;
o->prv = o;
} else {
o->prv = own->chl;
o->nxt = own->chl->nxt;
o->prv->nxt = o;
o->nxt->prv = o;
own->chl = o;
};
return RE_SUCCESS; // All right
};
return RE_UNKOWN; // ...
};
////////////////////////////////////////////////////////////////////////////////
void free_key ( p_key o ) {
if ( o ) {
if ( o->name ) free(o->name);
if ( o->data && o->type != KEY_LINK ) free(o->data);
free(o);
};
};
////////////////////////////////////////////////////////////////////////////////
char delete_key_ex ( p_key o ) {
if ( o && o != rootkey ) {
while ( o->chl ) delete_key_ex(o->chl);
if ( o->own ) {
p_key m = o->own;
if ( m->chl == o ) m->chl = o->prv;
if ( m->chl == o ) m->chl = NULL;
};
if ( o->nxt ) {
p_key x = o->prv;
x->nxt = o->nxt;
o->nxt->prv = x;
};
free_key(o);
return RE_SUCCESS;
};
if ( o == rootkey ) return RE_ROOT_KEY_SPECIFIED;
return RE_KEY_NOT_FOUND;
};
////////////////////////////////////////////////////////////////////////////////
//// Delete a key ///////////////
// return
// 0 -> Sucess
// 1 -> Unknow key
char delete_key ( char * name ) {
return delete_key_ex(found_key_ex(name,0));
};
////////////////////////////////////////////////////////////////////////////////
void *copy_of_data ( void *data, uint size ) {
if ( size && data ) {
void *o = (void*)malloc(size);
if ( !o ) return NULL;
memcpy(o,data,size);
return o;
};
return NULL;
};
////////////////////////////////////////////////////////////////////////////////
char set_key_data ( char *name, char type, uint size, void *data ) {
if ( name ) {
p_key o = found_key ( name );
if ( !o ) return RE_KEY_NOT_FOUND;
if ( o->data && o->type != KEY_LINK ) free(o->data);
o->type = type;
o->size = size;
if ( type == KEY_LINK )
o->data = data;
else
o->data = copy_of_data(data,size);
return RE_SUCCESS;
};
return RE_KEY_NOT_FOUND;
};
////////////////////////////////////////////////////////////////////////////////
//// Get key data ///////////////
// return
// 0 -> Sucess
// 1 -> Unknow key
char get_key_data ( char *name, char *type, uint *size, void **data ) {
if ( name ) {
p_key o = found_key ( name );
if ( !o ) return RE_KEY_NOT_FOUND;
if ( type ) (*type) = o->type;
if ( size ) (*size) = o->size;
if ( data ) (*data) = o->data;
return RE_SUCCESS;
};
return RE_KEY_NOT_FOUND;
};
////////////////////////////////////////////////////////////////////////////////
char *key_in_path ( char *path, char *key ) {
char *result = (char *) malloc(strlen(path)+strlen(key)+2);
strcpy(result, path);
strcat(result, "/");
strcat(result, key);
return result;
}
////////////////////////////////////////////////////////////////////////////////
uint make_key_id ( p_key o, uint id ) {
o->id = id;
id++;
if ( o->chl ) {
p_key f = o->chl;
p_key p = f;
do {
id = make_key_id(p,id);
p = p->nxt;
} while ( p != f);
};
return id;
};
////////////////////////////////////////////////////////////////////////////////
void write_key ( FILE *f, p_key o ) {
uint nlen = o->name ? strlen(o->name)+1 : 0;
uint none = 0;
//fwrite(&o->id, sizeof(int), 1, f);
fwrite(&nlen, sizeof(int), 1, f);
if ( nlen ) {
fwrite(o->name, nlen, 1, f);
};
fwrite(&o->type, sizeof(char), 1, f);
if ( o->type == KEY_LINK ) {
uint lnk = o->data ? ((p_key)(o->data))->id : 0;
uint size = sizeof(uint);
fwrite(&size, sizeof(uint), 1, f);
fwrite(&lnk, size, 1, f);
} else {
fwrite(&o->size, sizeof(uint), 1, f);
if ( o->size ) {
fwrite(o->data, o->size, 1, f);
};
};
fwrite(o->own?&o->own->id:&none,sizeof(uint),1,f);
fwrite(o->nxt?&o->nxt->id:&none,sizeof(uint),1,f);
fwrite(o->prv?&o->prv->id:&none,sizeof(uint),1,f);
fwrite(o->chl?&o->chl->id:&none,sizeof(uint),1,f);
if ( o->chl ) { // Write sub keys if exists
p_key ff = o->chl;
p_key p = ff;
do {
write_key ( f, p );
p = p->nxt;
} while ( p != ff);
};
};
////////////////////////////////////////////////////////////////////////////////
void write_registry ( void ) {
FILE *f = fopen("registry.dat","wb");
uint numberkey, magic;
if ( !f ) return;
magic = REGISTRY_MAGIC;
numberkey = make_key_id(rootkey,1);
numberkey--;
fwrite(&magic,sizeof(uint),1,f);
fwrite(&numberkey,sizeof(uint),1,f); // For dynamic loading !
write_key(f,rootkey);
fclose(f);
};
p_key *dynamic = NULL; // For dynamic loading
////////////////////////////////////////////////////////////////////////////////
void read_key ( FILE *f, p_key o ) {
uint nlen;
uint id = 0;
//fread(&o->id, sizeof(int), 1, f);
fread(&nlen, sizeof(int), 1, f);
if ( nlen ) {
o->name = (char*)malloc(nlen);
fread(o->name, nlen, 1, f);
} else o->name = NULL;
fread(&o->type, sizeof(char), 1, f);
fread(&o->size, sizeof(uint), 1, f);
if ( o->type == KEY_LINK ) {
fread(&id,sizeof(uint),1,f);
if ( id ) o->data = (void*)dynamic[id-1];
else o->data = NULL;
} else {
if ( o->size ) {
o->data = (void*)malloc(o->size);
fread(o->data, o->size, 1, f);
} else o->data = NULL;
};
fread(&id,sizeof(uint),1,f);
if ( id ) o->own = dynamic[id-1];
else o->own = NULL;
fread(&id,sizeof(uint),1,f);
if ( id ) o->nxt = dynamic[id-1];
else o->nxt = NULL;
fread(&id,sizeof(uint),1,f);
if ( id ) o->prv = dynamic[id-1];
else o->prv = NULL;
fread(&id,sizeof(uint),1,f);
if ( id ) o->chl = dynamic[id-1];
else o->chl = NULL;
};
////////////////////////////////////////////////////////////////////////////////
char read_registry ( void ) {
FILE *f = fopen("registry.dat","rb");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -