/services/fconf/fconf.c

http://ftk.googlecode.com/ · C · 133 lines · 89 code · 13 blank · 31 comment · 65 complexity · 1aa0e5e6df7c887d69d975a3d0e6a672 MD5 · raw file

  1. /*
  2. * File: fconf.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: common used functions for FConf interface.
  5. *
  6. * Copyright (c) 2009 - 2010 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. * 2010-08-01 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "fconf.h"
  31. static Ret on_changed(void* ctx, int change_by_self, FConfChangeType type, const char* xpath, const char* value)
  32. {
  33. ftk_logd("%s: %d %x %s %s\n", __func__, change_by_self, type, xpath, value);
  34. return RET_OK;
  35. }
  36. Ret fconf_test(FConf* thiz)
  37. {
  38. int i = 0;
  39. int j = 0;
  40. int k = 0;
  41. int n = 0;
  42. char* value = NULL;
  43. char* child = NULL;
  44. char name[32] = {0};
  45. fconf_reg_changed_notify(thiz, on_changed, NULL);
  46. assert(fconf_set(thiz, "/a/b/cc/name1", "1") == RET_OK);
  47. assert(fconf_set(thiz, "/a/b/cc/name2", "2") == RET_OK);
  48. assert(fconf_set(thiz, "/a/b/cc/name3", "2") == RET_OK);
  49. assert(fconf_get(thiz, "/a/b/cc/name3", &value) == RET_OK);
  50. assert(strcmp(value, "2") == 0);
  51. assert(fconf_set(thiz, "/a/b/cc/name3", "3") == RET_OK);
  52. assert(fconf_get(thiz, "/a/b/cc/name1", &value) == RET_OK);
  53. assert(strcmp(value, "1") == 0);
  54. assert(fconf_get(thiz, "/a/b/cc/name2", &value) == RET_OK);
  55. assert(strcmp(value, "2") == 0);
  56. assert(fconf_get(thiz, "/a/b/cc/name3", &value) == RET_OK);
  57. assert(strcmp(value, "3") == 0);
  58. assert(fconf_set(thiz, "/a/b/dd/name1", "1") == RET_OK);
  59. assert(fconf_set(thiz, "/a/b/dd/name2", "2") == RET_OK);
  60. assert(fconf_set(thiz, "/a/b/dd/name3", "3") == RET_OK);
  61. assert(fconf_get(thiz, "/a/b/dd/name1", &value) == RET_OK);
  62. assert(strcmp(value, "1") == 0);
  63. assert(fconf_get(thiz, "/a/b/dd/name2", &value) == RET_OK);
  64. assert(strcmp(value, "2") == 0);
  65. assert(fconf_get(thiz, "/a/b/dd/name3", &value) == RET_OK);
  66. assert(strcmp(value, "3") == 0);
  67. assert(fconf_set(thiz, "/b/b/dd/name1", "1") == RET_OK);
  68. assert(fconf_set(thiz, "/b/b/dd/name2", "2") == RET_OK);
  69. assert(fconf_set(thiz, "/b/b/dd/name3", "3") == RET_OK);
  70. assert(fconf_get_child_count(thiz, "/a/", &n) == RET_OK && n == 1);
  71. assert(fconf_get_child_count(thiz, "/a/b", &n) == RET_OK && n == 2);
  72. assert(fconf_get_child_count(thiz, "/a/b/cc", &n) == RET_OK && n == 3);
  73. //fconf_reg_changed_notify(thiz, NULL, NULL);
  74. for(i = 0; i < n; i++)
  75. {
  76. snprintf(name, sizeof(name)-1, "name%d", i+1);
  77. assert(fconf_get_child(thiz, "/a/b/cc", i, &child) == RET_OK);
  78. assert(strcmp(name, child) == 0);
  79. }
  80. assert(fconf_get_child_count(thiz, "/a/b/dd", &n) == RET_OK && n == 3);
  81. for(i = 0; i < n; i++)
  82. {
  83. snprintf(name, sizeof(name)-1, "name%d", i+1);
  84. assert(fconf_get_child(thiz, "/a/b/dd", i, &child) == RET_OK);
  85. assert(strcmp(name, child) == 0);
  86. }
  87. assert(fconf_remove(thiz, "/a/b/dd/name") != RET_OK);
  88. assert(fconf_remove(thiz, "/a/b/dd/name1") == RET_OK);
  89. assert(fconf_get_child_count(thiz, "/a/b/dd", &n) == RET_OK && n == 2);
  90. assert(fconf_remove(thiz, "/a/b/dd/name2") == RET_OK);
  91. assert(fconf_get_child_count(thiz, "/a/b/dd", &n) == RET_OK && n == 1);
  92. assert(fconf_remove(thiz, "/a/b/dd") == RET_OK);
  93. assert(fconf_get_child_count(thiz, "/a/b/dd", &n) != RET_OK);
  94. assert(fconf_remove(thiz, "/a/b") == RET_OK);
  95. assert(fconf_get_child_count(thiz, "/a/b", &n) != RET_OK);
  96. assert(fconf_remove(thiz, "/a") == RET_OK);
  97. assert(fconf_get_child_count(thiz, "/a", &n) != RET_OK);
  98. int times = 10;
  99. for(i = 0; i < times; i++)
  100. {
  101. for(j = 0; j < times; j++)
  102. {
  103. for(k = 0; k < times; k++)
  104. {
  105. snprintf(name, sizeof(name)-1, "/%d/%04d/%08d", i, j, k);
  106. assert(fconf_set(thiz, name, name) == RET_OK);
  107. }
  108. snprintf(name, sizeof(name)-1, "/%d/%04d", i, j);
  109. assert(fconf_get_child_count(thiz, name, &n) == RET_OK);
  110. assert(n == times);
  111. for(k = 0; k < n; k++)
  112. {
  113. snprintf(name, sizeof(name)-1, "/%d/%04d", i, j);
  114. assert(fconf_get_child(thiz, name, k, &child) == RET_OK);
  115. }
  116. }
  117. }
  118. //fconf_reg_changed_notify(thiz, on_changed, NULL);
  119. return RET_OK;
  120. }