/src/ftk_pairs.c

http://ftk.googlecode.com/ · C · 167 lines · 114 code · 24 blank · 29 comment · 38 complexity · 7224fd10d13c442aa72657552cbd3c84 MD5 · raw file

  1. /*
  2. * File: ftk_pairs.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: key-value manager
  5. *
  6. * Copyright (c) 2009 - 2011 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2011-03-25 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_allocator.h"
  31. #include "ftk_log.h"
  32. #include "ftk_pairs.h"
  33. struct _FtkPairs
  34. {
  35. size_t nr;
  36. FtkCompare compare;
  37. FtkKeyValue pairs[ZERO_LEN_ARRAY];
  38. };
  39. FtkPairs* ftk_pairs_create(size_t nr, FtkCompare compare)
  40. {
  41. FtkPairs* thiz = NULL;
  42. return_val_if_fail(nr > 0 && compare != NULL, NULL);
  43. thiz = (FtkPairs*)FTK_ZALLOC(sizeof(FtkPairs) + nr * sizeof(FtkKeyValue));
  44. if(thiz != NULL)
  45. {
  46. thiz->nr = nr;
  47. thiz->compare = compare;
  48. }
  49. return thiz;
  50. }
  51. Ret ftk_pairs_remove(FtkPairs* thiz, const char* key)
  52. {
  53. size_t i = 0;
  54. Ret ret = RET_FAIL;
  55. return_val_if_fail(thiz != NULL && key != NULL, RET_FAIL);
  56. for(i = 0; i < thiz->nr; i++)
  57. {
  58. if(thiz->compare(key, thiz->pairs[i].key) == 0)
  59. {
  60. memset(thiz->pairs+i, 0x00, sizeof(FtkKeyValue));
  61. ret = RET_OK;
  62. break;
  63. }
  64. }
  65. return ret;
  66. }
  67. Ret ftk_pairs_set(FtkPairs* thiz, const char* key, const char* value)
  68. {
  69. size_t i = 0;
  70. Ret ret = RET_FAIL;
  71. return_val_if_fail(thiz != NULL && key != NULL, RET_FAIL);
  72. for(i = 0; i < thiz->nr; i++)
  73. {
  74. if(thiz->compare(key, thiz->pairs[i].key) == 0)
  75. {
  76. ftk_strncpy(thiz->pairs[i].value, value, FTK_VALUE_LEN);
  77. thiz->pairs[i].value[FTK_VALUE_LEN] = '\0';
  78. ret = RET_OK;
  79. break;
  80. }
  81. }
  82. if(ret != RET_OK)
  83. {
  84. ret = ftk_pairs_add(thiz, key, value);
  85. }
  86. return ret;
  87. }
  88. Ret ftk_pairs_add(FtkPairs* thiz, const char* key, const char* value)
  89. {
  90. size_t i = 0;
  91. Ret ret = RET_FAIL;
  92. return_val_if_fail(thiz != NULL && key != NULL && value != NULL, RET_FAIL);
  93. for(i = 0; i < thiz->nr; i++)
  94. {
  95. if(thiz->pairs[i].key[0] == '\0')
  96. {
  97. ret = RET_OK;
  98. ftk_strncpy(thiz->pairs[i].key, key, FTK_KEY_LEN);
  99. ftk_strncpy(thiz->pairs[i].value, value, FTK_VALUE_LEN);
  100. thiz->pairs[i].key[FTK_KEY_LEN] = '\0';
  101. thiz->pairs[i].value[FTK_VALUE_LEN] = '\0';
  102. break;
  103. }
  104. }
  105. return ret;
  106. }
  107. const char* ftk_pairs_find(FtkPairs* thiz, const char* key)
  108. {
  109. size_t i = 0;
  110. const char* value = NULL;
  111. return_val_if_fail(thiz != NULL && key != NULL, NULL);
  112. for(i = 0; i < thiz->nr; i++)
  113. {
  114. if(thiz->compare(key, thiz->pairs[i].key) == 0)
  115. {
  116. value = thiz->pairs[i].value;
  117. break;
  118. }
  119. }
  120. return value;
  121. }
  122. void ftk_pairs_dump(FtkPairs* thiz)
  123. {
  124. size_t i = 0;
  125. if(thiz != NULL)
  126. {
  127. for(i = 0; i < thiz->nr; i++)
  128. {
  129. if(thiz->pairs[i].key[0])
  130. {
  131. ftk_logd("%s=%s\n", thiz->pairs[i].key, thiz->pairs[i].value);
  132. }
  133. }
  134. }
  135. }
  136. void ftk_pairs_destroy(FtkPairs* thiz)
  137. {
  138. if(thiz != NULL)
  139. {
  140. FTK_FREE(thiz);
  141. }
  142. return;
  143. }