Beispiel #1
0
void print_inorder_avltree(struct katcp_dispatch *d, struct avl_node *n, void (*fn_print)(struct katcp_dispatch *d, char *key, void *data), int flags)
{
  /* TODO: this should just be a special case of complex_inorder_traverse_avltree */
  struct avl_node *c;
#if 0
  while ((c = walk_inorder_avltree(n)) != NULL){

#ifdef DEBUG
    fprintf(stderr, "avl_tree: <%s>\n", c->n_key);
#endif
    if (flags){
      append_args_katcp(d, KATCP_FLAG_FIRST, "#%s", c->n_key);
      append_args_katcp(d, KATCP_FLAG_LAST, "with data %p", c->n_data);
    }

    if (fn_print != NULL)
      (*fn_print)(d, c->n_data);

  }
#endif
#if 1
  struct katcp_stack *s;

  if (n == NULL)
    return;
  
  s = create_stack_katcp();
  if (s == NULL)
    return;

  c = n;

  while (c != NULL) {
    if (push_stack_katcp(s, c, NULL) < 0){
#if DEBUG>2
      fprintf(stderr, "avl_tree: stack push error <%s>\n", c->n_key);
#endif
      destroy_stack_katcp(s);
      return;
    }
#if DEBUG>2
    fprintf(stderr, "avl_tree: stack 1 push  <%s>\n", c->n_key);
#endif
    c = c->n_left;
  }
  
#if DEBUG>2
  fprintf(stderr, "avl_tree: stack state: %d\n", is_empty_stack_katcp(s));
#endif

  while (!is_empty_stack_katcp(s)){
    c = pop_data_stack_katcp(s);
    if (c != NULL){

#if DEBUG>2
      fprintf(stderr, "avl_tree: <%s>\n", c->n_key);
#endif
      if (flags){
        append_args_katcp(d, KATCP_FLAG_FIRST, "#%s", c->n_key);
        append_args_katcp(d, KATCP_FLAG_LAST, "with data %p", c->n_data);
      }

      if (fn_print != NULL)
        (*fn_print)(d, c->n_key, c->n_data);

      c = c->n_right;

      while(c != NULL){
        if (push_stack_katcp(s, c, NULL) < 0){
#if DEBUG>2
          fprintf(stderr, "avl_tree: stack push error <%s>\n", c->n_key);
#endif
          destroy_stack_katcp(s);
          return;
        }
#if DEBUG>2
        fprintf(stderr, "avl_tree: stack 2 push  <%s>\n", c->n_key);
#endif
        c = c->n_left;
      }
      
    }
  }
  
  destroy_stack_katcp(s);
#endif
}
Beispiel #2
0
int parse_csv_mod(struct katcp_dispatch *d, struct katcp_stack *stack, struct katcp_tobject *o)
{
#if 0
  struct config_setting *cs;
#endif
  struct katcp_type *strtype; 
  char *str, c, *temp[2];
  int len, pos, i, count;

  void *data;
  
  strtype = find_name_type_katcp(d, KATCP_TYPE_STRING);
  if (strtype == NULL || strtype->t_parse == NULL)
    return -1;
#if 0
  cs = pop_data_expecting_stack_katcp(d, stack, KATCP_TYPE_CONFIG_SETTING);
  if (cs == NULL)
    return -1;
#endif

  str = pop_data_type_stack_katcp(stack, strtype);
  if (str == NULL)
    return -1;

  len = strlen(str);
  pos = 0;
  temp[1] = NULL;
  count = 0;

  for (i=0; i<=len; i++){
    c = str[i];
    switch(c){
      case EOL:
      case CR:
      case LF:
      case VALUE:
        temp[0] = strndup(str + pos, i-pos);
        if (temp[0] != NULL){
#ifdef DEBUG
          fprintf(stderr, "csv: <%s>\n", temp[0]);
#endif
        
          data = search_type_katcp(d, strtype, temp[0], (*strtype->t_parse)(d, temp));
#if 0 
          if (push_named_stack_katcp(d, stack, data, KATCP_TYPE_STRING) < 0){
#endif
          if (push_stack_katcp(stack, data, strtype) < 0){
#ifdef DEBUG
            fprintf(stderr, "csv: push named stack katcp error\n");
#endif
          }
          count++;
          free(temp[0]);
        }
        pos = i+1;
        break;
    }
    
  }
#if 0
  if (count > 1){
    inttype = find_name_type_katcp(d, KATCP_TYPE_INTEGER);
    if (inttype == NULL || inttype->t_getkey == NULL)
      return -1;
      
    data = create_integer_type_kcs(count);
    if (data == NULL)
      return -1;

    str = (*inttype->t_getkey)(data);
    if (str == NULL)
      return -1;

#ifdef DEBUG
    fprintf(stderr, "csv: <%s>\n", str);
#endif
    data = search_type_katcp(d, inttype, str, data);
    push_named_stack_katcp(d, stack, data, KATCP_TYPE_INTEGER);

    free(str);
  }
#endif

  return 0;
}

struct kcs_sm_op *parse_csv_setup_mod(struct katcp_dispatch *d, struct kcs_sm_state *s)
{
  return create_sm_op_kcs(&parse_csv_mod, NULL);
}

#if 0
struct config_setting *search_config_settings_mod(struct katcp_dispatch *d, void *data)
{
  char *str;
  str = data;
  return get_key_data_type_katcp(d, KATCP_TYPE_CONFIG_SETTING, str);
}
Beispiel #3
0
struct avl_node *walk_inorder_avltree(struct avl_node *n)
{
  static struct katcp_stack *s = NULL;
  static int state = WALK_INIT;
  static struct avl_node *c;

  struct avl_node *rtn;

  while (1){

    switch (state) {

      case WALK_INIT:

#if DEBUG>2
        fprintf(stderr, "walk: about to init\n");
#endif

        s = create_stack_katcp();
        if (s == NULL)
          return NULL;

        c = n;

      case WALK_PUSH:

#if DEBUG>2
        fprintf(stderr, "walk: about to push\n");
#endif

        if (c != NULL){ 

          if (push_stack_katcp(s, c, NULL) < 0) {
            destroy_stack_katcp(s);
            s = NULL;
            state = WALK_INIT;
            return NULL;
          }

          c = c->n_left;
        } 
          
        if (c == NULL)
          state = WALK_POP;
        else
          state = WALK_PUSH;

        break;

      case WALK_POP:
#if DEBUG>2
        fprintf(stderr, "walk: about to pop\n");
#endif

        if (!is_empty_stack_katcp(s)){
          
          c = pop_data_stack_katcp(s);
          if (c != NULL){
            
            rtn = c;
            c = c->n_right;
            state = WALK_PUSH;

            return rtn;            
            
          } 

       } else {
         destroy_stack_katcp(s);
         s = NULL;
         state = WALK_INIT;
         return NULL;
       }


        break;

    }
  }
  
  return NULL;  
}