source: trunk/test/lib-regfio.c @ 31

Last change on this file since 31 was 31, checked in by tim, 19 years ago

Added new lightweight stack library

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

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/*
2 * A utility to test functionality of Gerald Carter''s regfio interface.
3 *
4 * Copyright (C) 2005 Timothy D. Morgan
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
18 *
19 * $Id: lib-regfio.c 31 2005-07-16 19:05:19Z tim $
20 */
21
22
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include "../include/regfio.h"
27#include "../include/void_stack.h"
28
29
30char* getStackPath(void_stack* nk_stack)
31{
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;
83  REGF_NK_REC* sub;
84  char* path;
85
86  while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
87  {
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    }
107  }
108}
109
110
111int main(int argc, char** argv)
112{
113  void_stack* nk_stack;
114  REGF_FILE* f;
115  REGF_NK_REC* root;
116
117  if(argc < 2)
118  {
119    printf("ERROR: Requires 1 argument.\n");
120    return 1;
121  }
122
123  f = regfio_open( argv[1] );
124  root = regfio_rootkey(f);
125
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
131  regfio_close(f);
132/*
133REGF_NK_REC*  regfio_rootkey( REGF_FILE *file );
134REGF_NK_REC*  regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk );
135*/
136
137  return 0;
138}
Note: See TracBrowser for help on using the repository browser.