WIP hash table functions

pull/61/head
parent a66176a1d9
commit e714fba690
No known key found for this signature in database
GPG Key ID: FF1D842BF4DDE92B

@ -1149,8 +1149,9 @@ config_str yon_config_get_save_parameters_by_list(int *size, config_str paramete
} }
config_str yon_config_get_save_parameters_by_key(int *size,...){ config_str yon_config_get_save_parameters_by_key(int *size,...){
va_list list;
(*size)=0; (*size)=0;
check_config{
va_list list;
va_start(list,size); va_start(list,size);
int removed_size; int removed_size;
config_str removed = NULL; config_str removed = NULL;
@ -1197,6 +1198,8 @@ config_str yon_config_get_save_parameters_by_key(int *size,...){
final = yon_char_parsed_merge(updated,updated_size,removed,removed_size,size); final = yon_char_parsed_merge(updated,updated_size,removed,removed_size,size);
return final; return final;
} }
return NULL;
}
config_str yon_config_find_keys(char *target, int *size){ config_str yon_config_find_keys(char *target, int *size){
(*size)=0; (*size)=0;

@ -0,0 +1,88 @@
#include "libublsettings.h"
yon_hash *yon_hash_new(int size, unsigned int(*hash_func)(const char *)){
yon_hash *hash = malloc(sizeof(yon_hash));
memset(hash,0,sizeof(yon_hash));
hash->data = malloc(sizeof(yon_hash_element)*size);
hash->size = size;
hash->hash_func = hash_func;
return hash;
}
yon_hash_element *yon_hash_element_new(const char *key, const char *data){
yon_hash_element *element = malloc(sizeof(yon_hash_element));
memset(element,0,sizeof(yon_hash_element));
element->key = yon_char_new(key);
element->data = yon_char_new(data);
return element;
}
int yon_hash_insert(yon_hash *target, const char *key, void *data){
if (target&&target->hash_func&&!yon_char_is_empty(key)&&data){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
yon_hash_element *element = yon_hash_element_new(key,data);
yon_hash_element *last = (yon_hash_element *)yon_dictionary_get_last((dictionary*)target->data[hash]);
last->next=(struct yon_hash_element*)element;
element->prev=(struct yon_hash_element*)last;
element->first=(struct yon_hash_element*)last->first;
return 1;
} else {
target->data = malloc(sizeof(yon_hash_element*));
target->data[0] = yon_hash_element_new(key,data);
return 1;
}
}
return 0;
}
int yon_hash_remove(yon_hash *target, const char *key){
if (target&&target->hash_func&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
dictionary *dict = yon_dictionary_get((dictionary**)&target->data[hash],(char*)key);
target->data[hash]=(yon_hash_element*)yon_dictionary_rip(dict);
}
}
return 0;
}
void *yon_hash_lookup(yon_hash *target, const char *key){
if (target&&target->size>0&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
dictionary *dict = yon_dictionary_get((dictionary**)&target->data[hash],(char*)key);
return yon_dictionary_get_data(dict,void*);
}
}
return NULL;
}
int yon_hash_contains(yon_hash *target, const char *key){
if (target&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
if (yon_dictionary_get((dictionary**)&target->data[hash],key)) return 1;
}
}
return 0;
}
int yon_hash_add(yon_hash *target,const char *key){
if (target&&target->size>0&&!yon_char_is_empty(key)){
unsigned int hash = target->hash_func(key)%target->size;
if (target->data[hash]){
yon_hash_element *element = yon_hash_element_new(key,NULL);
yon_hash_element *last = (yon_hash_element *)yon_dictionary_get_last((dictionary*)target->data[hash]);
last->next=(struct yon_hash_element*)element;
element->prev=(struct yon_hash_element*)last;
element->first=(struct yon_hash_element*)last->first;
return 1;
} else {
target->data = malloc(sizeof(yon_hash_element*));
target->data[0] = yon_hash_element_new(key,NULL);
return 1;
}
}
}

@ -0,0 +1,42 @@
#include "libublsettings.h"
#define locales_path "/usr/share/i18n/locales/"
struct yon_locale {
char *code;
char *lang_ab;
char *territory;
char *language;
char *lang_name;
char *title;
};
struct yon_locale *yon_lang_new(){
struct yon_locale *cur_locale = malloc(sizeof(struct yon_locale));
memset(cur_locale,0,sizeof(struct yon_locale));
return cur_locale;
}
void yon_locale_set(struct yon_locale *target,char *locale_name){
int size;
char *path = yon_char_unite(locales_path,locale_name,NULL);
config_str *locale_string = yon_file_open(path,&size);
target->code = yon_char_new(locale_name);
target->lang_ab = yon_char_parsed_check_exist_begins_with(locale_string,size,"lang_ab");
target->territory = yon_char_parsed_check_exist_begins_with(locale_string,size,"territory");
target->language = yon_char_parsed_check_exist_begins_with(locale_string,size,"language");
target->lang_name = yon_char_parsed_check_exist_begins_with(locale_string,size,"lang_name");
target->title = yon_char_parsed_check_exist_begins_with(locale_string,size,"title");
yon_char_parsed_free(locale_string,size);
free(path);
}
void yon_locale_init(){
int size;
config_str *locales_list = yon_dir_get_contents(locales_path,&size);
for (int i=0;i<size;i++){
if (!stcmp(locales_list,".")||!strcmp(locales_list,"..")) continue;
struct yon_locale *cur_locale = yon_lang_new();
yon_locale_set(cur_locale,locales_list[i]);
}
}

@ -1098,4 +1098,21 @@ void yon_launch(char *command);
// void *yon_malloc(int size, char *group_key); // void *yon_malloc(int size, char *group_key);
typedef struct {
dictionary_fields(yon_hash_element);
} yon_hash_element;
typedef struct {
yon_hash_element **data;
unsigned int size;
unsigned int(*hash_func)(const char *);
} yon_hash;
yon_hash *yon_hash_new(int size, unsigned int(*hash_func)(const char *));
int yon_hash_insert(yon_hash *target, const char *key, void *data);
int yon_hash_remove(yon_hash *target, const char *key);
void *yon_hash_lookup(yon_hash *target, const char *key);
int yon_hash_add(yon_hash *target,const char *key);
int yon_hash_contains(yon_hash *target, const char *key);
#endif #endif
Loading…
Cancel
Save