Guest User

Untitled

a guest
May 27th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. typedef struct node_t {
  2. char *key;
  3. node_type_t type;
  4. apr_array_header_t *arr_strings;
  5. apr_array_header_t *arr_numbers;
  6. } node_t;
  7.  
  8. typedef enum node_type_t {
  9. node_type_string,
  10. node_type_number,
  11. } node_type_t;
  12.  
  13. int add_node(apr_pool_t *p_pool, node_t *p_target_node, ...) {
  14. int rv = 0;
  15. va_list lmnts;
  16. va_start(lmnts, p_target_node);
  17. node_t *n = va_arg(lmnts, node_t *);
  18. apr_array_header_t *tbl;
  19. do {
  20. switch(n->type) {
  21. case node_type_string:
  22. tbl = p_target_node->arr_strings;
  23. break;
  24. case node_type_number:
  25. tbl = p_target_node->arr_numbers;
  26. break;
  27. default:
  28. break;
  29. }
  30. printf("tt%d - %sn", rv, n->key);
  31. *(const node_t**)apr_array_push(tbl) = n;
  32. rv++;
  33. } while (n && ((n = va_arg(lmnts, node_t *)) != NULL) && apr_strnatcmp(n->key, p_target_node->key) != 0);
  34. va_end(lmnts);
  35. return rv;
  36. }
  37.  
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40.  
  41. #include <apr_general.h>
  42. #include <apr_hash.h>
  43. #include <apr_pools.h>
  44. #include <apr_strings.h>
  45. #include <apr_tables.h>
  46.  
  47. typedef enum node_type_t {
  48. node_type_string,
  49. node_type_number,
  50. } node_type_t;
  51.  
  52. typedef struct node_t {
  53. char *key;
  54. node_type_t type;
  55. apr_array_header_t *arr_strings;
  56. apr_array_header_t *arr_numbers;
  57. } node_t;
  58.  
  59. node_t *create_node(apr_pool_t *p_pool, char *p_key, node_type_t p_type) {
  60. node_t *NODE = apr_palloc(p_pool, sizeof(node_t));
  61. NODE->key = p_key;
  62. NODE->type = p_type;
  63. NODE->arr_strings = apr_array_make(p_pool, 0, sizeof(node_t*));
  64. NODE->arr_numbers = apr_array_make(p_pool, 0, sizeof(node_t*));
  65. return NODE;
  66. }
  67.  
  68. int add_node(apr_pool_t *p_pool, node_t *p_target_node, ...) {
  69. int rv = 0;
  70. va_list lmnts;
  71. va_start(lmnts, p_target_node);
  72. node_t *n = va_arg(lmnts, node_t *);
  73. apr_array_header_t *tbl;
  74. do {
  75. switch(n->type) {
  76. case node_type_string:
  77. tbl = p_target_node->arr_strings;
  78. break;
  79. case node_type_number:
  80. tbl = p_target_node->arr_numbers;
  81. break;
  82. default:
  83. break;
  84. }
  85. printf("tt%d - %sn", rv, n->key);
  86. *(const node_t**)apr_array_push(tbl) = n;
  87. rv++;
  88. } while (n && ((n = va_arg(lmnts, node_t *)) != NULL) && apr_strnatcmp(n->key, p_target_node->key) != 0);
  89. va_end(lmnts);
  90. return rv;
  91. }
  92.  
  93. int add_node(apr_pool_t *p_pool, node_t *p_target_node, ...) {
  94. int rv = 0;
  95. va_list lmnts;
  96. va_start(lmnts, p_target_node);
  97. node_t *n = va_arg(lmnts, node_t *);
  98. while((n = va_arg(lmnts, node_t *)) != NULL && apr_strnatcmp(n->key, p_target_node->key) != 0) {
  99. printf("tt%d - %sn", rv, n->key);
  100. *(const node_t**)apr_array_push(p_target_node->arr_strings) = n;
  101. rv++;
  102. }
  103. va_end(lmnts);
  104. return rv;
  105. }
  106.  
  107. int main(int argc, const char *argv[]) {
  108. apr_status_t rv;
  109. apr_pool_t *mp;
  110.  
  111. rv = apr_initialize();
  112. if (rv != APR_SUCCESS) {
  113. return -1;
  114. }
  115. apr_pool_create(&mp, NULL);
  116.  
  117. node_t *ROOT_NODE = create_node(mp, "THE_ROOT", node_type_string);
  118. printf("tROOT_NODE { key: '%s', type: %d, [%d, %d]}n",
  119. ROOT_NODE->key, ROOT_NODE->type,
  120. ROOT_NODE->arr_strings->nelts, ROOT_NODE->arr_numbers->nelts);
  121.  
  122. node_t *NODE_A = create_node(mp, "A", node_type_string);
  123. printf("tNODE_A { key: '%s', type: %d, [%d, %d]}n",
  124. NODE_A->key, NODE_A->type,
  125. NODE_A->arr_strings->nelts, NODE_A->arr_numbers->nelts);
  126.  
  127. node_t *NODE_B = create_node(mp, "B", node_type_number);
  128. printf("tNODE_B { key: '%s', type: %d, [%d, %d]}n",
  129. NODE_B->key, NODE_B->type,
  130. NODE_B->arr_strings->nelts, NODE_B->arr_numbers->nelts);
  131.  
  132. node_t *NODE_C = create_node(mp, "C", node_type_string);
  133. printf("tNODE_C { key: '%s', type: %d, [%d, %d]}n",
  134. NODE_C->key, NODE_C->type,
  135. NODE_C->arr_strings->nelts, NODE_C->arr_numbers->nelts);
  136.  
  137. add_node(mp, ROOT_NODE, NODE_A, NODE_B, NODE_C);
  138.  
  139. printf("tn = %d, n = %dn", ROOT_NODE->arr_strings->nelts, ROOT_NODE->arr_numbers->nelts);
  140.  
  141. apr_pool_destroy(mp);
  142. apr_terminate();
  143. return 0;
  144. }
Add Comment
Please, Sign In to add comment