language_hashmap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. table->size = size;
  17. table->ptable = (HashEntry **)malloc(size*sizeof(HashEntry *));
  18. return table;
  19. }
  20. // 在哈希表中插入键值对
  21. void insert_Newhash(HashTable *table, const char *key, char value[][32],int valuelens)
  22. {
  23. unsigned int index = _hash(table,key);
  24. HashEntry *entry = (HashEntry *)malloc(sizeof(HashEntry));
  25. entry->key = strdup(key);
  26. entry->value=(char**)malloc(sizeof(char*)*valuelens);
  27. for (int i = 0; i < valuelens; i++)
  28. {
  29. entry->value[i] = strdup(value[i]);
  30. }
  31. entry->valueLength=valuelens;
  32. entry->next = table->ptable[index];
  33. table->ptable[index] = entry;
  34. }
  35. // 在哈希表中查找键对应的值
  36. const HashEntry*find_hash(HashTable *table, const char *key) {
  37. unsigned int index = _hash(table,key);
  38. HashEntry *entry = table->ptable[index];
  39. while (entry) {
  40. if (strcmp(entry->key, key) == 0) {
  41. return entry;
  42. }
  43. entry = entry->next;
  44. }
  45. return 0;
  46. }
  47. // 释放哈希表占用的内存
  48. void free_hash_table(HashTable *table) {
  49. for (int i = 0; i < table->size; i++) {
  50. HashEntry *entry = table->ptable[i];
  51. while (entry) {
  52. HashEntry *temp = entry;
  53. entry = entry->next;
  54. free(temp->key);
  55. for (int i = 0; i < temp->valueLength; i++)
  56. {
  57. free(temp->value[i]);
  58. }
  59. free(temp->value);
  60. free(temp);
  61. }
  62. }
  63. free(table->ptable);
  64. free(table);
  65. }
  66. void replace_sequences(char *str, HashTable *table) {
  67. char *token = strtok(str, DELIMITER);
  68. char output[MAX_LEN] = "";
  69. int output_len = 0;
  70. int token_len;
  71. while (token != 0) {
  72. if (token[0] == '$' && token[1] != '\0' && token[strlen(token) - 1] == '$') {
  73. // 去除包裹的`$`符号,并查找替换值
  74. char *key = strdup(token + 1); // 跳过开头的`$`
  75. key[strlen(key) - 1] = '\0'; // 去除结尾的`$`
  76. const char *replacement;
  77. HashEntry* pEnty= find_hash(table, key);
  78. if (pEnty)
  79. {
  80. replacement=pEnty->value[__globalDeviceManage._global_device_info->product_language];
  81. }
  82. free(key); // 释放key占用的内存
  83. if (replacement) {
  84. // 将替换值追加到输出字符串中
  85. strncat(output, replacement, MAX_LEN - output_len - 1);
  86. output_len += strlen(replacement);
  87. } else {
  88. // 如果没有找到替换值,可以选择保留原序列或抛出一个错误
  89. // 这里选择保留原序列
  90. strncat(output, DELIMITER, MAX_LEN - output_len - 1);
  91. strncat(output, token, MAX_LEN - output_len - 1);
  92. strncat(output, DELIMITER, MAX_LEN - output_len - 1);
  93. output_len += strlen(token) + 2 * strlen(DELIMITER);
  94. }
  95. } else {
  96. // 不是`$xxxx$`格式,直接追加到输出字符串中
  97. strncat(output, token, MAX_LEN - output_len - 1);
  98. output_len += strlen(token);
  99. }
  100. token = strtok(0, DELIMITER); // 继续获取下一个token
  101. }
  102. // 将最终的输出字符串复制回原字符串
  103. strncpy(str, output, output_len + 1);
  104. str[output_len] = '\0'; // 确保字符串以null结尾
  105. }