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

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

Added key name retrieval to test utility

Code formatting changes to regfio.c

Added type<->string mapping functions to regfio library

  • Property svn:keywords set to Id
File size: 3.5 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 32 2005-07-16 23:16:17Z 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  /* Cut trailing slash */ 
77  if(buf[buf_len-buf_left-2] == '/')
78    buf[buf_len-buf_left-2] = '\0';
79
80  return buf;
81}
82
83
84void printKeyList(REGF_NK_REC* nk, char* prefix)
85{
86  unsigned int i;
87  const char* str_type;
88 
89  for(i=0; i < nk->num_values; i++)
90  {
91    str_type = type_val2str(nk->values[i].type);
92    printf("%s/%s:%s=\n", prefix, nk->values[i].valuename, str_type);
93  }
94}
95
96
97void printKeyTree(REGF_FILE* f, void_stack* nk_stack)
98{
99  REGF_NK_REC* cur;
100  REGF_NK_REC* sub;
101  char* path;
102
103  if((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
104  {
105    printf("%s:KEY\n", cur->keyname);
106    while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
107    {
108      if((sub = regfio_fetch_subkey(f, cur)) != NULL)
109      {
110        void_stack_push(nk_stack, sub);
111        path = getStackPath(nk_stack);
112        if(path != NULL)
113        {
114          printKeyList(cur, path);
115          printf("%s:KEY\n", path);
116          free(path);
117        }
118      }
119      else
120      {
121        cur = void_stack_pop(nk_stack);
122        /* XXX: This is just a shallow free.  Need to write deep free
123         * routines to replace the Samba code for this.
124         */ 
125        if(cur != NULL)
126          free(cur);
127      }
128    }
129  }
130}
131
132
133int main(int argc, char** argv)
134{
135  void_stack* nk_stack;
136  REGF_FILE* f;
137  REGF_NK_REC* root;
138
139  if(argc < 2)
140  {
141    printf("ERROR: Requires 1 argument.\n");
142    return 1;
143  }
144
145  f = regfio_open( argv[1] );
146  root = regfio_rootkey(f);
147
148  nk_stack = void_stack_new(1024);
149  if(void_stack_push(nk_stack, root))
150    printKeyTree(f, nk_stack);
151  void_stack_destroy(nk_stack);
152
153  regfio_close(f);
154/*
155REGF_NK_REC*  regfio_rootkey( REGF_FILE *file );
156REGF_NK_REC*  regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk );
157*/
158
159  return 0;
160}
Note: See TracBrowser for help on using the repository browser.