language_hashmap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "language_hashmap.h"
  2. #include "common.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. unsigned int _hash(HashTable *table,const char *key)
  6. {
  7. unsigned int hash = 0;
  8. while (*key) {
  9. hash = hash * 31 + *key++;
  10. }
  11. return hash % table->size; // 假设哈希表大小为101
  12. }
  13. HashTable *create_hash_table(int size)
  14. {
  15. HashTable *table = (HashTable *)malloc(sizeof(HashTable));
  16. if(table) {
  17. table->size = size;
  18. table->ptable = (HashEntry **)calloc(1, size*sizeof(HashEntry *));
  19. if(table->ptable==NULL) {
  20. free(table);
  21. return NULL;
  22. }
  23. }
  24. return table;
  25. }
  26. // 在哈希表中插入键值对
  27. int insert_Newhash(HashTable *table, const char *key, const char value[][64],int valuelens)
  28. {
  29. unsigned int index = _hash(table,key);
  30. if(table->ptable[index]) {
  31. printf("hash value is repeat, index: %d, ___%s___, ___%s___\n", index, table->ptable[index]->key, key);
  32. return HASH_REPEAT;
  33. }
  34. HashEntry *entry = (HashEntry *)malloc(sizeof(HashEntry));
  35. if(entry==NULL) {
  36. return HASH_MALLOC_FAIL;
  37. }
  38. entry->key = strdup(key);
  39. entry->value = (char**)malloc(sizeof(char*)*valuelens);
  40. if(entry->value==NULL) {
  41. free(entry);
  42. return HASH_MALLOC_FAIL;
  43. }
  44. for (int i = 0; i < valuelens; i++)
  45. {
  46. entry->value[i] = strdup(value[i]);
  47. }
  48. entry->valueLength = valuelens;
  49. entry->next = table->ptable[index];
  50. table->ptable[index] = entry;
  51. //printf("___key: %s, index: %d\n", key, index);
  52. return HASH_OK;
  53. }
  54. // 在哈希表中查找键对应的值
  55. const HashEntry*find_hash(HashTable *table, const char *key) {
  56. unsigned int index = _hash(table,key);
  57. if(index>=table->size) {
  58. return NULL;
  59. }
  60. HashEntry *entry = table->ptable[index];
  61. while (entry && entry->key) {
  62. if (strcmp(entry->key, key) == 0) {
  63. return entry;
  64. }
  65. entry = entry->next;
  66. }
  67. return NULL;
  68. }
  69. // 释放哈希表占用的内存
  70. void free_hash_table(HashTable *table) {
  71. for (int i = 0; i < table->size; i++) {
  72. HashEntry *entry = table->ptable[i];
  73. while (entry) {
  74. HashEntry *temp = entry;
  75. entry = entry->next;
  76. free(temp->key);
  77. for (int i = 0; i < temp->valueLength; i++)
  78. {
  79. free(temp->value[i]);
  80. }
  81. free(temp->value);
  82. free(temp);
  83. }
  84. }
  85. free(table->ptable);
  86. free(table);
  87. }
  88. void replace_sequences(char *str, HashTable *table) {
  89. char *token = strtok(str, DELIMITER);
  90. char output[MAX_LEN] = "";
  91. int output_len = 0;
  92. int token_len;
  93. while (token != 0) {
  94. if (token[0] == '$' && token[1] != '\0' && token[strlen(token) - 1] == '$') {
  95. // 去除包裹的`$`符号,并查找替换值
  96. char *key = strdup(token + 1); // 跳过开头的`$`
  97. key[strlen(key) - 1] = '\0'; // 去除结尾的`$`
  98. const char *replacement=NULL;
  99. HashEntry* pEnty= (HashEntry*)find_hash(table, key);
  100. if (pEnty)
  101. {
  102. replacement=pEnty->value[__globalDeviceManage._globalDevInfo.product_language];
  103. }
  104. free(key); // 释放key占用的内存
  105. if (replacement) {
  106. // 将替换值追加到输出字符串中
  107. strncat(output, replacement, MAX_LEN - output_len - 1);
  108. output_len += strlen(replacement);
  109. } else {
  110. // 如果没有找到替换值,可以选择保留原序列或抛出一个错误
  111. // 这里选择保留原序列
  112. strncat(output, DELIMITER, MAX_LEN - output_len - 1);
  113. strncat(output, token, MAX_LEN - output_len - 1);
  114. strncat(output, DELIMITER, MAX_LEN - output_len - 1);
  115. output_len += strlen(token) + 2 * strlen(DELIMITER);
  116. }
  117. } else {
  118. // 不是`$xxxx$`格式,直接追加到输出字符串中
  119. strncat(output, token, MAX_LEN - output_len - 1);
  120. output_len += strlen(token);
  121. }
  122. token = strtok(0, DELIMITER); // 继续获取下一个token
  123. }
  124. // 将最终的输出字符串复制回原字符串
  125. strncpy(str, output, output_len + 1);
  126. str[output_len] = '\0'; // 确保字符串以null结尾
  127. }