Index: /test/pyregfi-smoketest.py
===================================================================
--- /test/pyregfi-smoketest.py	(revision 198)
+++ /test/pyregfi-smoketest.py	(revision 199)
@@ -11,15 +11,21 @@
     for value in iter.list_values():
         i += 1
+        
+    return i
+
 
 def walk_tree(iter):
-    total_keys = 0
-    total_values = 0
-
-    for sub_key in iter:        
-        num_keys,num_values = walk_tree(sub_key)
-        total_keys += num_keys + 1
+    total_keys = 1
+    total_values = iter_values(iter)
+    print "total_values:", total_values
+    
+    for sub_key in iter:
+        print sub_key.keyname
+        
+        print iter.down()
+        num_keys,num_values = walk_tree(iter)
+        total_keys += num_keys
         total_values += num_values
-
-        num_values += iter_values(iter)
+        iter.up()
 
     return (total_keys, total_values)
@@ -30,3 +36,2 @@
     iter = rf.get_key()
     print walk_tree(iter)
-    
Index: /trunk/include/regfi.h
===================================================================
--- /trunk/include/regfi.h	(revision 198)
+++ /trunk/include/regfi.h	(revision 199)
@@ -1078,5 +1078,5 @@
  * @ingroup regfiIteratorLayer
  */
-const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i);
+bool regfi_iterator_first_subkey(REGFI_ITERATOR* i);
 
 
@@ -1105,5 +1105,5 @@
  * @ingroup regfiIteratorLayer
  */
-const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i);
+bool regfi_iterator_next_subkey(REGFI_ITERATOR* i);
 
 
@@ -1134,5 +1134,5 @@
  * @ingroup regfiIteratorLayer
  */
-const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i);
+bool regfi_iterator_first_value(REGFI_ITERATOR* i);
 
 
@@ -1161,5 +1161,5 @@
  * @ingroup regfiIteratorLayer
  */
-const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i);
+bool regfi_iterator_next_value(REGFI_ITERATOR* i);
 
 
Index: /trunk/lib/regfi.c
===================================================================
--- /trunk/lib/regfi.c	(revision 198)
+++ /trunk/lib/regfi.c	(revision 199)
@@ -1848,6 +1848,6 @@
 
   /* XXX: this alloc/free of each sub key might be a bit excessive */
-  subkey = (REGFI_NK_REC*)regfi_iterator_first_subkey(i);
-  while((subkey != NULL) && (found == false))
+  regfi_iterator_first_subkey(i);
+  while((subkey = regfi_iterator_cur_subkey(i)) != NULL && (found == false))
   {
     if(subkey->keyname != NULL 
@@ -1857,5 +1857,5 @@
     {
       talloc_unlink(NULL, subkey);
-      subkey = (REGFI_NK_REC*)regfi_iterator_next_subkey(i);
+      regfi_iterator_next_subkey(i);
     }
   }
@@ -1918,8 +1918,10 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i)
+bool regfi_iterator_first_subkey(REGFI_ITERATOR* i)
 {
   i->cur_subkey = 0;
-  return regfi_iterator_cur_subkey(i);
+  
+  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE) 
+	  && (i->cur_subkey < i->cur_key->num_subkeys)); 
 }
 
@@ -1931,13 +1933,14 @@
   uint32_t nk_offset;
 
-  /* see if there is anything left to report */
-  if (!(i->cur_key) || (i->cur_key->subkeys_off==REGFI_OFFSET_NONE)
-      || (i->cur_subkey >= i->cur_key->num_subkeys))
-    return NULL;
-
-  nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset;
-
-  return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, 
-			i->string_encoding, true);
+  if((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE) 
+     && (i->cur_subkey < i->cur_key->num_subkeys))
+  {
+    nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset;
+
+    return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, 
+			  i->string_encoding, true);
+  }
+
+  return NULL;
 }
 
@@ -1945,16 +1948,10 @@
 /******************************************************************************
  *****************************************************************************/
-/* XXX: some way of indicating reason for failure should be added. */
-const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i)
-{
-  const REGFI_NK_REC* subkey;
-
+bool regfi_iterator_next_subkey(REGFI_ITERATOR* i)
+{
   i->cur_subkey++;
-  subkey = regfi_iterator_cur_subkey(i);
-
-  if(subkey == NULL)
-    i->cur_subkey--;
-
-  return subkey;
+
+  return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE) 
+	  && (i->cur_subkey < i->cur_key->num_subkeys)); 
 }
 
@@ -1974,6 +1971,6 @@
     return false;
 
-  cur = regfi_iterator_first_value(i);
-  while((cur != NULL) && (found == false))
+  regfi_iterator_first_value(i);
+  while((cur = regfi_iterator_cur_value(i)) != NULL && (found == false))
   {
     if((cur->valuename != NULL)
@@ -1983,5 +1980,5 @@
     {
       regfi_free_record(cur);
-      cur = regfi_iterator_next_value(i);
+      regfi_iterator_next_value(i);
     }
   }
@@ -2000,8 +1997,9 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i)
+bool regfi_iterator_first_value(REGFI_ITERATOR* i)
 {
   i->cur_value = 0;
-  return regfi_iterator_cur_value(i);
+  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL 
+	  && (i->cur_value < i->cur_key->values->num_values));
 }
 
@@ -2014,12 +2012,10 @@
   uint32_t voffset;
 
-  if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL)
-  {
-    if(i->cur_value < i->cur_key->values->num_values)
-    {
-      voffset = i->cur_key->values->elements[i->cur_value];
-      ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, 
-				 i->string_encoding, true);
-    }
+  if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL 
+     && (i->cur_value < i->cur_key->values->num_values))
+  {
+    voffset = i->cur_key->values->elements[i->cur_value];
+    ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, 
+			       i->string_encoding, true);
   }
 
@@ -2030,14 +2026,9 @@
 /******************************************************************************
  *****************************************************************************/
-const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i)
-{
-  const REGFI_VK_REC* ret_val;
-
+bool regfi_iterator_next_value(REGFI_ITERATOR* i)
+{
   i->cur_value++;
-  ret_val = regfi_iterator_cur_value(i);
-  if(ret_val == NULL)
-    i->cur_value--;
-
-  return ret_val;
+  return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL 
+	  && (i->cur_value < i->cur_key->values->num_values));
 }
 
Index: /trunk/python2/SConstruct
===================================================================
--- /trunk/python2/SConstruct	(revision 198)
+++ /trunk/python2/SConstruct	(revision 199)
@@ -29,5 +29,5 @@
 nenv.config = config
 if config.DEBUG:
-    nenv.Append(CFLAGS = "-ggdb -pedantic -Wall -std=gnu99")
+    nenv.Append(CFLAGS = "-ggdb -O0 -pedantic -Wall -std=gnu99")
     nenv.Append(CPPPATH=['../include', 'include'])
     nenv.python_cppflags = '-I/usr/include/python2.5_d'
Index: /trunk/python2/regfi/pyregfi.h
===================================================================
--- /trunk/python2/regfi/pyregfi.h	(revision 198)
+++ /trunk/python2/regfi/pyregfi.h	(revision 199)
@@ -49,5 +49,5 @@
 CLASS(KeyIterator, Object)
      PRIVATE REGFI_ITERATOR *iter;
-     PRIVATE const REGFI_NK_REC *next_item;
+     PRIVATE bool first_called;
 
      KeyIterator METHOD(KeyIterator, Con, struct RegistryFile_t *file, char **path,
@@ -56,17 +56,20 @@
      struct ValueIterator_t *METHOD(KeyIterator, list_values);
 
-     void METHOD(KeyIterator, __iter__);
+     KeyIterator METHOD(KeyIterator, __iter__);
      REGFI_NK_REC *METHOD(KeyIterator, iternext);
+
+     int METHOD(KeyIterator, down);
+     int METHOD(KeyIterator, up);
 END_CLASS
 
 /** This is an iterator for reading values from the registry */
 CLASS(ValueIterator, Object)
-    PRIVATE REGFI_ITERATOR *iter;
-    PRIVATE const REGFI_VK_REC *next_item;
+     PRIVATE REGFI_ITERATOR *iter;
+     PRIVATE bool first_called;
+     
+     ValueIterator METHOD(ValueIterator, Con, KeyIterator key);
 
-    ValueIterator METHOD(ValueIterator, Con, KeyIterator key);
-
-    void METHOD(ValueIterator, __iter__);
-    RawData METHOD(ValueIterator, iternext);
+     void METHOD(ValueIterator, __iter__);
+     RawData METHOD(ValueIterator, iternext);
 END_CLASS
 
Index: /trunk/python2/regfi/regfi.c
===================================================================
--- /trunk/python2/regfi/regfi.c	(revision 198)
+++ /trunk/python2/regfi/regfi.c	(revision 199)
@@ -58,8 +58,4 @@
 
   regfi_iterator_free(this->iter);
-  if(this->next_item) {
-    regfi_free_record(this->next_item);
-  }
-
   return 0;
 }
@@ -76,5 +72,5 @@
   talloc_set_destructor((void*)self, KeyIterator_dest);
 
-  // Traverse to the path
+  /* Traverse to the path */
   if(path[0]) {
     if(!regfi_iterator_walk_path(self->iter, (const char **)path)) {
@@ -84,6 +80,6 @@
   }
 
-  // Get the first key in the list
-  self->next_item = regfi_iterator_first_subkey(self->iter);
+  fprintf(stderr, "Con called\n");
+  self->first_called = false;
 
   return self;
@@ -92,22 +88,36 @@
 }
 
-static void KeyIterator__iter__(KeyIterator self) {
-  if(self->next_item) {
-    regfi_free_record(self->next_item);
-  };
-
-  self->next_item = regfi_iterator_first_subkey(self->iter);
-}
+static KeyIterator KeyIterator__iter__(KeyIterator self) {
+  return self;
+}
+
 
 static const REGFI_NK_REC* KeyIterator_next(KeyIterator self) {
-  const REGFI_NK_REC* result;
-
-  if(!self->next_item) return NULL;
-
-  result = self->next_item;
-
-  self->next_item = regfi_iterator_next_subkey(self->iter);
-
+
+  fprintf(stderr, "next called\n");
+
+  if(!self->first_called)
+  {
+    regfi_iterator_first_subkey(self->iter);
+    self->first_called = true;
+  }
+  else
+    regfi_iterator_next_subkey(self->iter);
+    
+  return regfi_iterator_cur_subkey(self->iter);
+}
+
+
+static int KeyIterator_down(KeyIterator self) {
+  fprintf(stderr, "down cur_subkey: %d\n", self->iter->cur_subkey);
+  int result = regfi_iterator_down(self->iter);
+  fprintf(stderr, "down result: %d\n", result);
+  regfi_iterator_first_subkey(self->iter);
+  regfi_iterator_first_value(self->iter);
   return result;
+}
+
+static int KeyIterator_up(KeyIterator self) {
+  return regfi_iterator_up(self->iter);
 }
 
@@ -119,4 +129,6 @@
   VMETHOD(Con) = KeyIterator_Con;
   VMETHOD(iternext) = KeyIterator_next;
+  VMETHOD(down) = KeyIterator_down;
+  VMETHOD(up) = KeyIterator_up;
   VMETHOD(__iter__) = KeyIterator__iter__;
   VMETHOD(list_values) = KeyIterator_list_values;
@@ -126,5 +138,5 @@
   ValueIterator this = (ValueIterator)self;
 
-  if(this->next_item) regfi_free_record(this->next_item);
+  talloc_unlink(this, this->iter);
 
   return 0;
@@ -136,6 +148,4 @@
   talloc_reference(self, self->iter);
 
-  self->next_item = regfi_iterator_first_value(self->iter);
-
   talloc_set_destructor((void *)self, ValueIterator_dest);
 
@@ -143,17 +153,28 @@
 }
 
-static void ValueIterator__iter__(ValueIterator self) {
-  if(self->next_item) regfi_free_record(self->next_item);
-
-  self->next_item = regfi_iterator_first_value(self->iter);
+static ValueIterator ValueIterator__iter__(ValueIterator self) {
+  return self;
 }
 
 static RawData ValueIterator_iternext(ValueIterator self) {
   RawData result;
-  const REGFI_DATA *data;
-  const REGFI_VK_REC *rec = self->next_item;
+  const REGFI_DATA* data;
+  const REGFI_VK_REC* rec;
+
+  if(!self->first_called)
+  {
+    regfi_iterator_first_value(self->iter);
+    self->first_called = true;
+  }
+  else
+    regfi_iterator_next_value(self->iter);
+
+  rec = regfi_iterator_cur_value(self->iter);
 
   if(!rec) return NULL;
 
+  /* XXX: shouldn't parse data here every time we walk over a value.  
+   *      Instead, make data fetching a method and parse it then. 
+   */
   data = (REGFI_DATA *)regfi_iterator_fetch_data(self->iter, rec);
   if(!data) {
@@ -162,5 +183,5 @@
   }
 
-  switch(self->next_item->type) {
+  switch(rec->type) {
   case REG_EXPAND_SZ:
   case REG_SZ:
@@ -178,12 +199,4 @@
   }
 
-
-  /*  if(self->next_item) {
-    regfi_free_record(self->next_item);
-  };
-  */
-
-  self->next_item = regfi_iterator_next_value(self->iter);
-
   return result;
  error:
@@ -255,5 +268,5 @@
 
 void pyregfi_init() {
+  regfi_init();
   INIT_CLASS(RegistryFile);
-
-}
+}
Index: /trunk/python2/utils.py
===================================================================
--- /trunk/python2/utils.py	(revision 198)
+++ /trunk/python2/utils.py	(revision 199)
@@ -267,6 +267,5 @@
     """ Implementation from Richard Levitte email to
     org.tigris.scons.dev dated Jan 26, 2006 7:05:10 am."""
-    python_cppflags = distutils.util.split_quoted(
-        "-I"+sysconfig.get_python_inc())
+    python_cppflags = "-I"+sysconfig.get_python_inc()
 
     def PythonModule(self, libname, lib_objs=[], **kwargs):
Index: /trunk/src/reglookup.c
===================================================================
--- /trunk/src/reglookup.c	(revision 198)
+++ /trunk/src/reglookup.c	(revision 199)
@@ -277,11 +277,11 @@
   const REGFI_VK_REC* value;
 
-  value = regfi_iterator_first_value(iter);
-  while(value != NULL)
+  regfi_iterator_first_value(iter);
+  while((value = regfi_iterator_cur_value(iter)) != NULL)
   {
     if(!type_filter_enabled || (value->type == type_filter))
       printValue(iter, value, prefix);
     regfi_free_record(value);
-    value = regfi_iterator_next_value(iter);
+    regfi_iterator_next_value(iter);
     printMsgs(iter->f);
   }
@@ -379,5 +379,6 @@
 
   root = cur = regfi_iterator_cur_key(iter);
-  sub = regfi_iterator_first_subkey(iter);
+  regfi_iterator_first_subkey(iter);
+  sub = regfi_iterator_cur_subkey(iter);
   printMsgs(iter->f);
 
@@ -419,5 +420,6 @@
 	}
 	
-	sub = regfi_iterator_next_subkey(iter);
+	regfi_iterator_next_subkey(iter);
+	sub = regfi_iterator_cur_subkey(iter);
       }
       print_this = false;
@@ -435,5 +437,6 @@
       cur = regfi_iterator_cur_key(iter);
       regfi_free_record(sub);
-      sub = regfi_iterator_first_subkey(iter);
+      regfi_iterator_first_subkey(iter);
+      sub = regfi_iterator_cur_subkey(iter);
       print_this = true;
     }
