#include #include #include "elog.h" #include "common.h" #include "language_hashmap.h" #if 1 #define LOGD log_d #define LOGE log_e #define LOGW log_w #else #define LOGD printf #define LOGE printf #define LOGW printf #endif unsigned int _hash(HashTable *table,const char *key) { unsigned int hash = 0; while (*key) { hash = hash * 31 + *key++; } return hash % table->size; // 假设哈希表大小为101 } HashTable *create_hash_table(int size) { HashTable *table = (HashTable *)malloc(sizeof(HashTable)); if(table) { table->size = size; table->ptable = (HashEntry **)calloc(1, size*sizeof(HashEntry *)); if(table->ptable==NULL) { free(table); return NULL; } } return table; } // 在哈希表中插入键值对 int insert_Newhash(HashTable *table, const char *key, const char **value,int valuelens) { unsigned int index = _hash(table,key); if(table->ptable[index]) { //LOGW("hash value is repeat, index: %d, ___%s___, ___%s___\n", index, table->ptable[index]->key, key); return HASH_REPEAT; } HashEntry *entry = (HashEntry *)malloc(sizeof(HashEntry)); if(entry==NULL) { return HASH_MALLOC_FAIL; } entry->key = strdup(key); entry->value = (char**)malloc(sizeof(char*)*valuelens); if(entry->value==NULL) { free(entry); return HASH_MALLOC_FAIL; } for (int i = 0; i < valuelens; i++) { entry->value[i] = strdup(value[i]); } entry->valueLength = valuelens; entry->next = table->ptable[index]; table->ptable[index] = entry; //LOGD("___key: %s, index: %d\n", key, index); return HASH_OK; } // 在哈希表中查找键对应的值 const HashEntry*find_hash(HashTable *table, const char *key) { unsigned int index = _hash(table,key); if(index>=table->size) { return NULL; } HashEntry *entry = table->ptable[index]; while (entry && entry->key) { if (strcmp(entry->key, key) == 0) { return entry; } entry = entry->next; } return NULL; } // 释放哈希表占用的内存 void free_hash_table(HashTable *table) { for (int i = 0; i < table->size; i++) { HashEntry *entry = table->ptable[i]; while (entry) { HashEntry *temp = entry; entry = entry->next; free(temp->key); for (int i = 0; i < temp->valueLength; i++) { free(temp->value[i]); } free(temp->value); free(temp); } } free(table->ptable); free(table); } void replace_sequences(char *str, HashTable *table) { char *token = strtok(str, DELIMITER); char output[MAX_LEN] = ""; int output_len = 0; int token_len; while (token != 0) { if (token[0] == '$' && token[1] != '\0' && token[strlen(token) - 1] == '$') { // 去除包裹的`$`符号,并查找替换值 char *key = strdup(token + 1); // 跳过开头的`$` key[strlen(key) - 1] = '\0'; // 去除结尾的`$` const char *replacement=NULL; HashEntry* pEnty= (HashEntry*)find_hash(table, key); if (pEnty) { replacement=pEnty->value[__globalDeviceManage._globalDevInfo.product.language]; } free(key); // 释放key占用的内存 if (replacement) { // 将替换值追加到输出字符串中 strncat(output, replacement, MAX_LEN - output_len - 1); output_len += strlen(replacement); } else { // 如果没有找到替换值,可以选择保留原序列或抛出一个错误 // 这里选择保留原序列 strncat(output, DELIMITER, MAX_LEN - output_len - 1); strncat(output, token, MAX_LEN - output_len - 1); strncat(output, DELIMITER, MAX_LEN - output_len - 1); output_len += strlen(token) + 2 * strlen(DELIMITER); } } else { // 不是`$xxxx$`格式,直接追加到输出字符串中 strncat(output, token, MAX_LEN - output_len - 1); output_len += strlen(token); } token = strtok(0, DELIMITER); // 继续获取下一个token } // 将最终的输出字符串复制回原字符串 strncpy(str, output, output_len + 1); str[output_len] = '\0'; // 确保字符串以null结尾 } int init_hash_table(void) { int r,nMaxSize=LANG_ID_MAX; int retry=0,step=100,cnt; HashTable *table=NULL; GlobalDeviceManager *dm=&__globalDeviceManage; dm->alarmTab = NULL; retry: cnt = step*(retry+1); printf("___ hash table init, retry: %d, table size: %d\n", retry, cnt); table = create_hash_table(cnt); if(table==NULL) goto failed; r = insert_Newhash(table,(const char*)language_alarm_Ch[0],language_alarm_Ch,nMaxSize); if(r==HASH_REPEAT) goto repeat; else if(ralarmTab = table; log_d("___ hash table init OK, table size: %d\n", table->size); return 0; repeat: retry++; free_hash_table(table); goto retry; failed: free_hash_table(table); return -1; }