registry.c

来自「SEAL是DOS 下的32位保护模式的GUI程序」· C语言 代码 · 共 616 行 · 第 1/2 页

C
616
字号
  uint numberkey,a,magic;
  if ( !f ) return 0;
  fread(&magic,sizeof(uint),1,f);
  if ( magic != REGISTRY_MAGIC ) {
	fclose(f);
	return 0;
  };
  fread(&numberkey,sizeof(uint),1,f);
  dynamic = (p_key*)malloc(sizeof(p_key)*numberkey);
  a=0;
  while ( a < numberkey ) {
	p_key key = (p_key)malloc(sizeof(t_key));
	memset(key,0,sizeof(t_key));
	dynamic[a] = key;
	a++;
  };
  a=0;
  while ( a < numberkey ) {
	read_key(f,dynamic[a]);
	a++;
  };
  rootkey = dynamic[0];
  free(dynamic);
  dynamic = NULL,
  fclose(f);
  return 1;
};
////////////////////////////////////////////////////////////////////////////////
void make_registry ( void ) {
  rootkey = (p_key)malloc(sizeof(t_key));
  memset(rootkey,0,sizeof(t_key));
};
////////////////////////////////////////////////////////////////////////////////
/*void keymap ( p_key o, int level ) {
  if ( o->chl ) {
    p_key ff = o->chl->nxt;
    p_key	p = ff;
    do {
	  int a = level;
	  while ( a ) {
		printf("  ");
		a--;
	  };
	  printf("%c %s\n",p->chl?'+':'-',p->name);
      keymap ( p, level + 1);
	  p = p->nxt;
    } while ( p != ff);
  };
};

void registry_map ( void ) {
  printf("+ ROOT (\"\")\n");
  keymap(rootkey,1);
};*/
////////////////////////////////////////////////////////////////////////////////
char free_registry ( void ) {
  if ( rootkey ) {
    while ( rootkey->chl ) delete_key_ex(rootkey->chl);
    free_key(rootkey);
    rootkey = NULL;
  };
};
////////////////////////////////////////////////////////////////////////////////
char set_key ( char *key, char *text ) {
  return set_key_data(key,KEY_TEXT,strlen(text)+1,text);
};
char set_key_byte(char *key, unsigned char b) {
  return set_key_data(key,KEY_BYTE,sizeof(unsigned char),&b);
};
char set_key_small(char *key, short s) {
  return set_key_data(key,KEY_SMALL,sizeof(short),&s);
};
char set_key_integer(char *key, long i) {
  return set_key_data(key,KEY_INTEGER,sizeof(long),&i);
};
char set_key_big(char *key, long long big) {
  return set_key_data(key,KEY_BIG,sizeof(long long),&big);
};
char set_key_float(char *key, float f) {
  return set_key_data(key,KEY_FLOAT,sizeof(float),&f);
};
char set_key_link(char*key,p_key lnk) {
  return set_key_data(key,KEY_LINK,sizeof(int),lnk);
};
////////////////////////////////////////////////////////////////////////////////
char *get_key ( char *key ) {
  void *data;
  char type;
  if ( get_key_data(key,&type,NULL,&data) != RE_SUCCESS ) return NULL;
  if ( type != KEY_TEXT ) return NULL;
  return data?strdup((char*)data):NULL;
};
unsigned char get_key_byte ( char *key ) {
  void *data;
  char type;
  if ( get_key_data(key,&type,NULL,&data) != RE_SUCCESS ) return NULL;
  if ( type != KEY_BYTE ) return NULL;
  return *((unsigned char*)data);
};
short get_key_small ( char *key ) {
  void *data;
  char type;
  if ( get_key_data(key,&type,NULL,&data) != RE_SUCCESS ) return NULL;
  if ( type != KEY_SMALL ) return NULL;
  return *((short*)data);
};
long get_key_integer ( char *key ) {
  void *data;
  char type;
  if ( get_key_data(key,&type,NULL,&data) != RE_SUCCESS ) return NULL;
  if ( type != KEY_INTEGER ) return NULL;
  return *((long*)data);
};
long long get_key_big ( char *key ) {
  void *data;
  char type;
  if ( get_key_data(key,&type,NULL,&data) != RE_SUCCESS ) return NULL;
  if ( type != KEY_BIG ) return NULL;
  return *((long long*)data);
};
float get_key_float ( char *key ) {
  void *data;
  char type;
  if ( get_key_data(key,&type,NULL,&data) != RE_SUCCESS ) return NULL;
  if ( type != KEY_FLOAT ) return NULL;
  return *((float*)data);
};
////////////////////////////////////////////////////////////////////////////////
char reg_find_first(char *path, p_registry_search info) {
  p_key mum = found_key(path);
  if ( !mum ) return 0;
  if ( !mum->chl ) return 0;
  info->key = mum->chl->nxt; // CHL is last, CHL->NXT is first

  info->parent_name = strdup(path);

  info->name = key_in_path(info->parent_name, info->key->name);

  return 1;
};
////////////////////////////////////////////////////////////////////////////////
char reg_find_next(p_registry_search info) {

  if ( info->name ) { free(info->name); info->name = NULL;};

  if ( info->key->own->chl == info->key ) {
	//if ( info->parent_name ) { free(info->parent_name);	info->parent_name = NULL;};
	return 0;
  };
  info->key = info->key->nxt;
  info->name = key_in_path(info->parent_name, info->key->name);
  return 1;
};
////////////////////////////////////////////////////////////////////////////////
char *key_full_name ( p_key o ) {
  char *r = NULL;
  while ( o ) {
	if ( o->name ) {
	  if ( r ) {
	    char *nr = key_in_path(o->name,r);
	    free(r);
	    r = nr;
      } else {
	    r = strdup(o->name);
      };
    };
	o = o->own;
  };
  return r;
};
////////////////////////////////////////////////////////////////////////////////
char *key_owner_full_name ( char* key ) {
  p_key o = found_key(key);
  //DEBUG_printf("Search for '%s' owner full name\n",key);
  if ( !o ) return 0;
  if ( !o->own ) return 0;
  return key_full_name(o->own);
};
////////////////////////////////////////////////////////////////////////////////
char *eliminate_extra ( char *t ) {
  int st=0,a=0,fn=0;
  char *m;
  while ( t[a] ) {
	if ( t[a] != ' ' ) {
	  st = a;
	  break;
    };
	a++;
  };
  a=strlen(t)-1;
  while ( a >= 0 ) {
	if ( t[a] != ' ' && t[a] != '\n' && t[a] != '\r' ) {
	  fn = a;
	  break;
    };
	a--;
  };
  m = malloc(fn-st+2);
  memset(m,0,fn-st+2);
  if ( fn-st+1 ) memcpy(m,t+st,fn-st+1);
  free(t);
  return m;
};
////////////////////////////////////////////////////////////////////////////////
char read_reg_file ( char * file, char show ) {

  FILE *f = fopen ( file, "rt" );

  char *curkey = NULL;
  char *subkey = NULL;
  char *buffer = NULL;
  uint  l=1,a=0,st=0,size=0;
  char doing = 0; // 1->curkey, 2->value

  if(show)printf("Read file %s ...\n",file);
  l++;
  if ( !f ) return 0;

  size = filelength(fileno(f));
  buffer = malloc(size+1);
  if ( !buffer ) return 0;
  memset(buffer,0, size+1);
  fread(buffer, size, 1, f);
  fclose(f);

  while ( 1 ) {
    if ( buffer[a] == '\n' || !buffer[a] ) {
	  if ( doing == 2 && curkey && subkey ) {
	    char *value = malloc(a-st);
	    memset(value,0,a-st);
	    if ( a-st-1 ) memcpy(value,buffer+st+1,a-st-1);
	    value = eliminate_extra(value);
	    subkey = eliminate_extra(subkey);
	    if ( !strcmp(subkey,"@" ) ) {
		  free(subkey);
		  subkey = strdup( curkey );
	    } else {
		  create_key(curkey,subkey);
		  if ( strcmp("",curkey ) ) {
		    char *old = subkey;
		    subkey = key_in_path(curkey,subkey);
		    free(old);
	      };
	    };
	    if(show)printf("    Set key %s to %s\n",subkey,value);
	    l++;
	    if ( value[0] == '"' ) {
		  uint len = strlen(value);
		  char *val = malloc(len-1);
          memset(val,0,len-1);
	      if ( len-2 ) memcpy(val,value+1,len-2);
	      set_key(subkey,val);
          free(val);
        } else if ( value[0] == '(' ) {
	      if ( !stricmp (value,"(nodata)") ) set_key_data(subkey,0,0,0);
        } else {
	      set_key_integer(subkey,atoi(value));
        };
        free(subkey);
        free(value);
        subkey = NULL;
      };
      st = a;
      doing = 0;
      if ( !buffer[a] ) break;
    } else if ( buffer[a] == '[' && !doing ) {
	  if ( curkey ) free(curkey);
	  curkey = NULL;
	  st = a;
	  doing = 1;
    } else if ( buffer[a] == ']' && doing == 1 ) {
	  curkey = malloc(a-st);
	  memset(curkey,0,a-st);
	  if ( a-st-1 ) memcpy(curkey,buffer+st+1,a-st-1);
	  if(show)printf("  In %s\n",curkey);
	  l++;
	  doing = 0;
    } else if ( buffer[a] == '=' && !doing ) {
	  subkey = malloc(a-st);
	  memset(subkey,0,a-st);
	  if ( a-st-1 ) memcpy(subkey,buffer+st+1,a-st-1);
	  doing = 2;
	  st=a;
    };
	a++;
  };
  return 1;
};
////////////////////////////////////////////////////////////////////////////////
void registry_init ( void ) {
  if ( !read_registry() ) {
    make_registry();
    create_key("","USERS");
    create_key("USERS","DEFAULT");
    create_key("","CURRENT");
    create_key("","GLOBAL");
    create_key("","CONFIG");
    create_key("","SOFTWARE");
    create_key("","SYSTEM");
    set_key_link("CURRENT",found_key("USERS/DEFAULT"));
  };
};
////////////////////////////////////////////////////////////////////////////////
void registry_done ( void ) {
  //fclose(debug);
  write_registry();
  free_registry();
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?