parent
a66176a1d9
commit
e714fba690
@ -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]);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue