Changeset 199


Ignore:
Timestamp:
06/02/10 21:53:31 (14 years ago)
Author:
tim
Message:

reworked part of regfi C API to make python wrappers simpler
continued work on python wrappers
fixed some issues in pyregfi-smoketest. WIP

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • test/pyregfi-smoketest.py

    r198 r199  
    1111    for value in iter.list_values():
    1212        i += 1
     13       
     14    return i
     15
    1316
    1417def walk_tree(iter):
    15     total_keys = 0
    16     total_values = 0
    17 
    18     for sub_key in iter:       
    19         num_keys,num_values = walk_tree(sub_key)
    20         total_keys += num_keys + 1
     18    total_keys = 1
     19    total_values = iter_values(iter)
     20    print "total_values:", total_values
     21   
     22    for sub_key in iter:
     23        print sub_key.keyname
     24       
     25        print iter.down()
     26        num_keys,num_values = walk_tree(iter)
     27        total_keys += num_keys
    2128        total_values += num_values
    22 
    23         num_values += iter_values(iter)
     29        iter.up()
    2430
    2531    return (total_keys, total_values)
     
    3036    iter = rf.get_key()
    3137    print walk_tree(iter)
    32    
  • trunk/include/regfi.h

    r193 r199  
    10781078 * @ingroup regfiIteratorLayer
    10791079 */
    1080 const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i);
     1080bool regfi_iterator_first_subkey(REGFI_ITERATOR* i);
    10811081
    10821082
     
    11051105 * @ingroup regfiIteratorLayer
    11061106 */
    1107 const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i);
     1107bool regfi_iterator_next_subkey(REGFI_ITERATOR* i);
    11081108
    11091109
     
    11341134 * @ingroup regfiIteratorLayer
    11351135 */
    1136 const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i);
     1136bool regfi_iterator_first_value(REGFI_ITERATOR* i);
    11371137
    11381138
     
    11611161 * @ingroup regfiIteratorLayer
    11621162 */
    1163 const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i);
     1163bool regfi_iterator_next_value(REGFI_ITERATOR* i);
    11641164
    11651165
  • trunk/lib/regfi.c

    r193 r199  
    18481848
    18491849  /* XXX: this alloc/free of each sub key might be a bit excessive */
    1850   subkey = (REGFI_NK_REC*)regfi_iterator_first_subkey(i);
    1851   while((subkey != NULL) && (found == false))
     1850  regfi_iterator_first_subkey(i);
     1851  while((subkey = regfi_iterator_cur_subkey(i)) != NULL && (found == false))
    18521852  {
    18531853    if(subkey->keyname != NULL
     
    18571857    {
    18581858      talloc_unlink(NULL, subkey);
    1859       subkey = (REGFI_NK_REC*)regfi_iterator_next_subkey(i);
     1859      regfi_iterator_next_subkey(i);
    18601860    }
    18611861  }
     
    19181918/******************************************************************************
    19191919 *****************************************************************************/
    1920 const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i)
     1920bool regfi_iterator_first_subkey(REGFI_ITERATOR* i)
    19211921{
    19221922  i->cur_subkey = 0;
    1923   return regfi_iterator_cur_subkey(i);
     1923 
     1924  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
     1925          && (i->cur_subkey < i->cur_key->num_subkeys));
    19241926}
    19251927
     
    19311933  uint32_t nk_offset;
    19321934
    1933   /* see if there is anything left to report */
    1934   if (!(i->cur_key) || (i->cur_key->subkeys_off==REGFI_OFFSET_NONE)
    1935       || (i->cur_subkey >= i->cur_key->num_subkeys))
    1936     return NULL;
    1937 
    1938   nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset;
    1939 
    1940   return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE,
    1941                         i->string_encoding, true);
     1935  if((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
     1936     && (i->cur_subkey < i->cur_key->num_subkeys))
     1937  {
     1938    nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset;
     1939
     1940    return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE,
     1941                          i->string_encoding, true);
     1942  }
     1943
     1944  return NULL;
    19421945}
    19431946
     
    19451948/******************************************************************************
    19461949 *****************************************************************************/
    1947 /* XXX: some way of indicating reason for failure should be added. */
    1948 const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i)
    1949 {
    1950   const REGFI_NK_REC* subkey;
    1951 
     1950bool regfi_iterator_next_subkey(REGFI_ITERATOR* i)
     1951{
    19521952  i->cur_subkey++;
    1953   subkey = regfi_iterator_cur_subkey(i);
    1954 
    1955   if(subkey == NULL)
    1956     i->cur_subkey--;
    1957 
    1958   return subkey;
     1953
     1954  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
     1955          && (i->cur_subkey < i->cur_key->num_subkeys));
    19591956}
    19601957
     
    19741971    return false;
    19751972
    1976   cur = regfi_iterator_first_value(i);
    1977   while((cur != NULL) && (found == false))
     1973  regfi_iterator_first_value(i);
     1974  while((cur = regfi_iterator_cur_value(i)) != NULL && (found == false))
    19781975  {
    19791976    if((cur->valuename != NULL)
     
    19831980    {
    19841981      regfi_free_record(cur);
    1985       cur = regfi_iterator_next_value(i);
     1982      regfi_iterator_next_value(i);
    19861983    }
    19871984  }
     
    20001997/******************************************************************************
    20011998 *****************************************************************************/
    2002 const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i)
     1999bool regfi_iterator_first_value(REGFI_ITERATOR* i)
    20032000{
    20042001  i->cur_value = 0;
    2005   return regfi_iterator_cur_value(i);
     2002  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL
     2003          && (i->cur_value < i->cur_key->values->num_values));
    20062004}
    20072005
     
    20142012  uint32_t voffset;
    20152013
    2016   if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL)
    2017   {
    2018     if(i->cur_value < i->cur_key->values->num_values)
    2019     {
    2020       voffset = i->cur_key->values->elements[i->cur_value];
    2021       ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE,
    2022                                  i->string_encoding, true);
    2023     }
     2014  if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL
     2015     && (i->cur_value < i->cur_key->values->num_values))
     2016  {
     2017    voffset = i->cur_key->values->elements[i->cur_value];
     2018    ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE,
     2019                               i->string_encoding, true);
    20242020  }
    20252021
     
    20302026/******************************************************************************
    20312027 *****************************************************************************/
    2032 const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i)
    2033 {
    2034   const REGFI_VK_REC* ret_val;
    2035 
     2028bool regfi_iterator_next_value(REGFI_ITERATOR* i)
     2029{
    20362030  i->cur_value++;
    2037   ret_val = regfi_iterator_cur_value(i);
    2038   if(ret_val == NULL)
    2039     i->cur_value--;
    2040 
    2041   return ret_val;
     2031  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL
     2032          && (i->cur_value < i->cur_key->values->num_values));
    20422033}
    20432034
  • trunk/python2/SConstruct

    r198 r199  
    2929nenv.config = config
    3030if config.DEBUG:
    31     nenv.Append(CFLAGS = "-ggdb -pedantic -Wall -std=gnu99")
     31    nenv.Append(CFLAGS = "-ggdb -O0 -pedantic -Wall -std=gnu99")
    3232    nenv.Append(CPPPATH=['../include', 'include'])
    3333    nenv.python_cppflags = '-I/usr/include/python2.5_d'
  • trunk/python2/regfi/pyregfi.h

    r198 r199  
    4949CLASS(KeyIterator, Object)
    5050     PRIVATE REGFI_ITERATOR *iter;
    51      PRIVATE const REGFI_NK_REC *next_item;
     51     PRIVATE bool first_called;
    5252
    5353     KeyIterator METHOD(KeyIterator, Con, struct RegistryFile_t *file, char **path,
     
    5656     struct ValueIterator_t *METHOD(KeyIterator, list_values);
    5757
    58      void METHOD(KeyIterator, __iter__);
     58     KeyIterator METHOD(KeyIterator, __iter__);
    5959     REGFI_NK_REC *METHOD(KeyIterator, iternext);
     60
     61     int METHOD(KeyIterator, down);
     62     int METHOD(KeyIterator, up);
    6063END_CLASS
    6164
    6265/** This is an iterator for reading values from the registry */
    6366CLASS(ValueIterator, Object)
    64     PRIVATE REGFI_ITERATOR *iter;
    65     PRIVATE const REGFI_VK_REC *next_item;
     67     PRIVATE REGFI_ITERATOR *iter;
     68     PRIVATE bool first_called;
     69     
     70     ValueIterator METHOD(ValueIterator, Con, KeyIterator key);
    6671
    67     ValueIterator METHOD(ValueIterator, Con, KeyIterator key);
    68 
    69     void METHOD(ValueIterator, __iter__);
    70     RawData METHOD(ValueIterator, iternext);
     72     void METHOD(ValueIterator, __iter__);
     73     RawData METHOD(ValueIterator, iternext);
    7174END_CLASS
    7275
  • trunk/python2/regfi/regfi.c

    r198 r199  
    5858
    5959  regfi_iterator_free(this->iter);
    60   if(this->next_item) {
    61     regfi_free_record(this->next_item);
    62   }
    63 
    6460  return 0;
    6561}
     
    7672  talloc_set_destructor((void*)self, KeyIterator_dest);
    7773
    78   // Traverse to the path
     74  /* Traverse to the path */
    7975  if(path[0]) {
    8076    if(!regfi_iterator_walk_path(self->iter, (const char **)path)) {
     
    8480  }
    8581
    86   // Get the first key in the list
    87   self->next_item = regfi_iterator_first_subkey(self->iter);
     82  fprintf(stderr, "Con called\n");
     83  self->first_called = false;
    8884
    8985  return self;
     
    9288}
    9389
    94 static void KeyIterator__iter__(KeyIterator self) {
    95   if(self->next_item) {
    96     regfi_free_record(self->next_item);
    97   };
    98 
    99   self->next_item = regfi_iterator_first_subkey(self->iter);
    100 }
     90static KeyIterator KeyIterator__iter__(KeyIterator self) {
     91  return self;
     92}
     93
    10194
    10295static const REGFI_NK_REC* KeyIterator_next(KeyIterator self) {
    103   const REGFI_NK_REC* result;
    104 
    105   if(!self->next_item) return NULL;
    106 
    107   result = self->next_item;
    108 
    109   self->next_item = regfi_iterator_next_subkey(self->iter);
    110 
     96
     97  fprintf(stderr, "next called\n");
     98
     99  if(!self->first_called)
     100  {
     101    regfi_iterator_first_subkey(self->iter);
     102    self->first_called = true;
     103  }
     104  else
     105    regfi_iterator_next_subkey(self->iter);
     106   
     107  return regfi_iterator_cur_subkey(self->iter);
     108}
     109
     110
     111static int KeyIterator_down(KeyIterator self) {
     112  fprintf(stderr, "down cur_subkey: %d\n", self->iter->cur_subkey);
     113  int result = regfi_iterator_down(self->iter);
     114  fprintf(stderr, "down result: %d\n", result);
     115  regfi_iterator_first_subkey(self->iter);
     116  regfi_iterator_first_value(self->iter);
    111117  return result;
     118}
     119
     120static int KeyIterator_up(KeyIterator self) {
     121  return regfi_iterator_up(self->iter);
    112122}
    113123
     
    119129  VMETHOD(Con) = KeyIterator_Con;
    120130  VMETHOD(iternext) = KeyIterator_next;
     131  VMETHOD(down) = KeyIterator_down;
     132  VMETHOD(up) = KeyIterator_up;
    121133  VMETHOD(__iter__) = KeyIterator__iter__;
    122134  VMETHOD(list_values) = KeyIterator_list_values;
     
    126138  ValueIterator this = (ValueIterator)self;
    127139
    128   if(this->next_item) regfi_free_record(this->next_item);
     140  talloc_unlink(this, this->iter);
    129141
    130142  return 0;
     
    136148  talloc_reference(self, self->iter);
    137149
    138   self->next_item = regfi_iterator_first_value(self->iter);
    139 
    140150  talloc_set_destructor((void *)self, ValueIterator_dest);
    141151
     
    143153}
    144154
    145 static void ValueIterator__iter__(ValueIterator self) {
    146   if(self->next_item) regfi_free_record(self->next_item);
    147 
    148   self->next_item = regfi_iterator_first_value(self->iter);
     155static ValueIterator ValueIterator__iter__(ValueIterator self) {
     156  return self;
    149157}
    150158
    151159static RawData ValueIterator_iternext(ValueIterator self) {
    152160  RawData result;
    153   const REGFI_DATA *data;
    154   const REGFI_VK_REC *rec = self->next_item;
     161  const REGFI_DATA* data;
     162  const REGFI_VK_REC* rec;
     163
     164  if(!self->first_called)
     165  {
     166    regfi_iterator_first_value(self->iter);
     167    self->first_called = true;
     168  }
     169  else
     170    regfi_iterator_next_value(self->iter);
     171
     172  rec = regfi_iterator_cur_value(self->iter);
    155173
    156174  if(!rec) return NULL;
    157175
     176  /* XXX: shouldn't parse data here every time we walk over a value. 
     177   *      Instead, make data fetching a method and parse it then.
     178   */
    158179  data = (REGFI_DATA *)regfi_iterator_fetch_data(self->iter, rec);
    159180  if(!data) {
     
    162183  }
    163184
    164   switch(self->next_item->type) {
     185  switch(rec->type) {
    165186  case REG_EXPAND_SZ:
    166187  case REG_SZ:
     
    178199  }
    179200
    180 
    181   /*  if(self->next_item) {
    182     regfi_free_record(self->next_item);
    183   };
    184   */
    185 
    186   self->next_item = regfi_iterator_next_value(self->iter);
    187 
    188201  return result;
    189202 error:
     
    255268
    256269void pyregfi_init() {
     270  regfi_init();
    257271  INIT_CLASS(RegistryFile);
    258 
    259 }
     272}
  • trunk/python2/utils.py

    r198 r199  
    267267    """ Implementation from Richard Levitte email to
    268268    org.tigris.scons.dev dated Jan 26, 2006 7:05:10 am."""
    269     python_cppflags = distutils.util.split_quoted(
    270         "-I"+sysconfig.get_python_inc())
     269    python_cppflags = "-I"+sysconfig.get_python_inc()
    271270
    272271    def PythonModule(self, libname, lib_objs=[], **kwargs):
  • trunk/src/reglookup.c

    r185 r199  
    277277  const REGFI_VK_REC* value;
    278278
    279   value = regfi_iterator_first_value(iter);
    280   while(value != NULL)
     279  regfi_iterator_first_value(iter);
     280  while((value = regfi_iterator_cur_value(iter)) != NULL)
    281281  {
    282282    if(!type_filter_enabled || (value->type == type_filter))
    283283      printValue(iter, value, prefix);
    284284    regfi_free_record(value);
    285     value = regfi_iterator_next_value(iter);
     285    regfi_iterator_next_value(iter);
    286286    printMsgs(iter->f);
    287287  }
     
    379379
    380380  root = cur = regfi_iterator_cur_key(iter);
    381   sub = regfi_iterator_first_subkey(iter);
     381  regfi_iterator_first_subkey(iter);
     382  sub = regfi_iterator_cur_subkey(iter);
    382383  printMsgs(iter->f);
    383384
     
    419420        }
    420421       
    421         sub = regfi_iterator_next_subkey(iter);
     422        regfi_iterator_next_subkey(iter);
     423        sub = regfi_iterator_cur_subkey(iter);
    422424      }
    423425      print_this = false;
     
    435437      cur = regfi_iterator_cur_key(iter);
    436438      regfi_free_record(sub);
    437       sub = regfi_iterator_first_subkey(iter);
     439      regfi_iterator_first_subkey(iter);
     440      sub = regfi_iterator_cur_subkey(iter);
    438441      print_this = true;
    439442    }
Note: See TracChangeset for help on using the changeset viewer.