Changeset 31 for trunk/test/lib-regfio.c


Ignore:
Timestamp:
07/16/05 15:05:19 (19 years ago)
Author:
tim
Message:

Added new lightweight stack library

rewrote test program to use this instead of string concatenation/recursion.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/lib-regfio.c

    r30 r31  
    11/*
    2  * A utility to test functionality of Gerald Carter''s regio interface.
     2 * A utility to test functionality of Gerald Carter''s regfio interface.
    33 *
    44 * Copyright (C) 2005 Timothy D. Morgan
     
    2525#include <string.h>
    2626#include "../include/regfio.h"
     27#include "../include/void_stack.h"
    2728
    28 void printKeyTree(REGF_FILE* f,  REGF_NK_REC* cur, char* prefix)
     29
     30char* getStackPath(void_stack* nk_stack)
    2931{
     32  REGF_NK_REC* cur;
     33  unsigned int buf_left = 127;
     34  unsigned int buf_len = buf_left+1;
     35  unsigned int name_len = 0;
     36  unsigned int grow_amt;
     37  char* buf;
     38  char* new_buf;
     39  void_stack_iterator* iter;
     40 
     41  buf = (char*)malloc((buf_len)*sizeof(char));
     42  if (buf == NULL)
     43    return NULL;
     44  buf[0] = '\0';
     45
     46  iter = void_stack_iterator_new(nk_stack);
     47  if (iter == NULL)
     48  {
     49    free(buf);
     50    return NULL;
     51  }
     52
     53  while((cur = void_stack_iterator_next(iter)) != NULL)
     54  {
     55    name_len = strlen(cur->keyname);
     56    if(name_len+1 > buf_left)
     57    {
     58      grow_amt = (unsigned int)(buf_len/3);
     59      buf_len += name_len+1+grow_amt-buf_left;
     60      if((new_buf = realloc(buf, buf_len)) == NULL)
     61      {
     62        free(buf);
     63        free(iter);
     64        return NULL;
     65      }
     66      buf = new_buf;
     67      buf_left = grow_amt + name_len + 1;
     68    }
     69    strncpy(buf+(buf_len-buf_left-1), cur->keyname, name_len);
     70    buf_left -= name_len;
     71    buf[buf_len-buf_left-1] = '/';
     72    buf_left -= 1;
     73    buf[buf_len-buf_left-1] = '\0';
     74  }
     75
     76  return buf;
     77}
     78
     79
     80void printKeyTree(REGF_FILE* f,  void_stack* nk_stack)
     81{
     82  REGF_NK_REC* cur;
    3083  REGF_NK_REC* sub;
    31   char* sub_prefix;
     84  char* path;
    3285
    33   if(prefix != NULL)
     86  while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
    3487  {
    35     sub_prefix = (char*)zalloc(strlen(prefix)+strlen(cur->keyname)+2);
    36     strcpy(sub_prefix, prefix);
    37     strcat(sub_prefix, "/");
     88    if((sub = regfio_fetch_subkey(f, cur)) != NULL)
     89    {
     90      path = getStackPath(nk_stack);
     91      if(path != NULL)
     92      {
     93        printf("%s%s:KEY\n", path, sub->keyname);
     94        free(path);
     95      }
     96      void_stack_push(nk_stack, sub);
     97    }
     98    else
     99    {
     100      cur = void_stack_pop(nk_stack);
     101      /* XXX: This is just a shallow free.  Need to write deep free
     102       * routines to replace the Samba code for this.
     103       */
     104      if(cur != NULL)
     105        free(cur);
     106    }
    38107  }
    39   else
    40     sub_prefix = (char*)zalloc(strlen(cur->keyname)+2);
    41 
    42   strcat(sub_prefix, cur->keyname);
    43 
    44   printf("%s:KEY\n", sub_prefix);
    45   while ((sub = regfio_fetch_subkey(f, cur)) != NULL)
    46     printKeyTree(f, sub, sub_prefix);
    47 
    48   free(sub_prefix);
    49108}
    50109
     
    52111int main(int argc, char** argv)
    53112{
     113  void_stack* nk_stack;
     114  REGF_FILE* f;
     115  REGF_NK_REC* root;
     116
    54117  if(argc < 2)
    55118  {
     
    58121  }
    59122
    60   REGF_FILE* f = regfio_open( argv[1] );
    61   REGF_NK_REC* root = regfio_rootkey(f);
     123  f = regfio_open( argv[1] );
     124  root = regfio_rootkey(f);
    62125
    63   printKeyTree(f, root, NULL);
     126  nk_stack = void_stack_new(1024);
     127  if(void_stack_push(nk_stack, root))
     128    printKeyTree(f, nk_stack);
     129  void_stack_destroy(nk_stack);
     130
    64131  regfio_close(f);
    65132/*
Note: See TracChangeset for help on using the changeset viewer.