Changeset 249 for trunk/src


Ignore:
Timestamp:
05/04/11 10:55:22 (14 years ago)
Author:
tim
Message:

reorganized iterator code for simpler locking and easier future key caching

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/reglookup.c

    r233 r249  
    5252
    5353
     54static bool keysEqual(const REGFI_NK* x, const REGFI_NK* y)
     55{
     56  return (x != NULL && y != NULL && x->offset == y->offset);
     57}
     58
    5459void printValue(REGFI_ITERATOR* iter, const REGFI_VK* vk, char* prefix)
    5560{
     61  const REGFI_NK* cur_key;
    5662  const REGFI_DATA* data;
    5763  char* quoted_value = NULL;
     
    100106  if(print_value_mtime)
    101107  {
    102     *tmp_time = regfi_nt2unix_time(&iter->cur_key->mtime);
     108    cur_key = regfi_iterator_cur_key(iter);
     109    *tmp_time = regfi_nt2unix_time(&cur_key->mtime);
    103110    tmp_time_s = gmtime(tmp_time);
    104111    strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
     112    regfi_free_record(iter->f, cur_key);
    105113  }
    106114  else
     
    201209
    202210
    203 /* Returns a quoted path from an iterator's stack */
     211/* Returns a quoted path of the current iterator's position */
    204212char* iter2Path(REGFI_ITERATOR* i)
    205213{
    206   const REGFI_ITER_POSITION* cur;
    207   const REGFI_NK* tmp_key;
     214  const REGFI_NK** path;
     215  uint32_t k;
    208216  uint32_t buf_left = 127;
    209217  uint32_t buf_len = buf_left+1;
     
    213221  char* new_buf;
    214222  char* name;
    215   void_stack_iterator* iter;
    216223 
    217224  buf = (char*)malloc((buf_len)*sizeof(char));
     
    220227  buf[0] = '\0';
    221228
    222   iter = void_stack_iterator_new(i->key_positions);
    223   if (iter == NULL)
     229  path = regfi_iterator_cur_path(i);
     230  if(path == NULL)
    224231  {
    225232    free(buf);
     
    227234  }
    228235
    229   /* skip root element */
    230   if(void_stack_size(i->key_positions) < 1)
    231   {
    232     buf[0] = '/';
    233     buf[1] = '\0';
    234     return buf;
    235   }
    236   cur = void_stack_iterator_next(iter);
    237 
    238   do
    239   {
    240     cur = void_stack_iterator_next(iter);
    241     if (cur == NULL)
    242       tmp_key = i->cur_key;
     236  for(k=0; path[k] != NULL; k++)
     237  {
     238    /* skip root element's name */
     239    if(k == 0)
     240    {
     241      buf[0] = '/';
     242      buf[1] = '\0';
     243    }
    243244    else
    244       tmp_key = cur->nk;
    245 
    246     name = get_quoted_keyname(tmp_key);
    247 
    248     buf[buf_len-buf_left-1] = '/';
    249     buf_left -= 1;
    250     name_len = strlen(name);
    251     if(name_len+1 > buf_left)
    252     {
    253       grow_amt = (uint32_t)(buf_len/2);
    254       buf_len += name_len+1+grow_amt-buf_left;
    255       if((new_buf = realloc(buf, buf_len)) == NULL)
     245    {
     246      name = get_quoted_keyname(path[k]);
     247
     248      buf[buf_len-buf_left-1] = '/';
     249      buf_left -= 1;
     250      name_len = strlen(name);
     251      if(name_len+1 > buf_left)
    256252      {
    257         free(name);
    258         free(buf);
    259         free(iter);
    260         return NULL;
     253        grow_amt = (uint32_t)(buf_len/2);
     254        buf_len += name_len+1+grow_amt-buf_left;
     255        if((new_buf = realloc(buf, buf_len)) == NULL)
     256        {
     257          regfi_free_record(i->f, path);
     258          free(name);
     259          free(buf);
     260          return NULL;
     261        }
     262        buf = new_buf;
     263        buf_left = grow_amt + name_len + 1;
    261264      }
    262       buf = new_buf;
    263       buf_left = grow_amt + name_len + 1;
    264     }
    265     strncpy(buf+(buf_len-buf_left-1), name, name_len);
    266     buf_left -= name_len;
    267     buf[buf_len-buf_left-1] = '\0';
    268     free(name);
    269   } while(cur != NULL);
    270 
     265      strncpy(buf+(buf_len-buf_left-1), name, name_len);
     266      buf_left -= name_len;
     267      buf[buf_len-buf_left-1] = '\0';
     268      free(name);
     269    }
     270  }
     271
     272  regfi_free_record(i->f, path);
    271273  return buf;
    272274}
     
    378380  bool print_this = true;
    379381
    380   root = cur = regfi_iterator_cur_key(iter);
     382  root = regfi_iterator_cur_key(iter);
     383  regfi_reference_record(iter->f, root);
     384  cur = root;
    381385  regfi_iterator_first_subkey(iter);
    382386  sub = regfi_iterator_cur_subkey(iter);
     
    404408    if(sub == NULL)
    405409    {
    406       if(cur != root)
     410      if(!keysEqual(cur, root))
    407411      {
    408412        regfi_free_record(iter->f, cur);
     413        cur = NULL;
    409414        /* We're done with this sub-tree, going up and hitting other branches. */
    410415        if(!regfi_iterator_up(iter))
     
    431436       */
    432437      regfi_free_record(iter->f, cur);
     438      cur = NULL;
    433439      if(!regfi_iterator_down(iter))
    434440      {
     
    444450    }
    445451    printMsgs(iter->f);
    446   } while(!((cur == root) && (sub == NULL)));
     452  } while(!(keysEqual(cur, root) && (sub == NULL)));
     453  if(cur != NULL)
     454    regfi_free_record(iter->f, cur);
    447455  regfi_free_record(iter->f, root);
    448456
Note: See TracChangeset for help on using the changeset viewer.