beta-xlogmaster
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Suggested code improvement


From: Jeffrey Taylor
Subject: Suggested code improvement
Date: Fri, 14 May 2004 10:23:00 -0500
User-agent: Mutt/1.3.28i

I was digging thru the code and noticed tha inserting a new filter
allocates the array of pointers twice, once to append the new filter,
and once to sort the array of filters.  I made some changes to
add_filter to insert the new filter in the proper place as the filters
are copied to the newly allocated larger array.

See below.  Could someone check that my understanding is correct?

The function sort_filter is no longer needed if this change is
correct.

Thanks,
  Jeffrey

from logclass.cc:

void
Log::add_filter(const gchar* str, gint md, const gchar* el)
{
  /* count amount of elements */
  gint amount = 0;

  if ( filter != NULL ) {
    while ( filter[amount] != NULL )
      amount++;
  }
  
  /* copy over list */
  Filter** old = filter;
  filter = (Filter**) new void*[amount+2];

  /* create  new filter */
  Filter *const new_filter = new Filter();
  new_filter->init( str, md | COMPILE_REGEX );
  if ( el != NULL )
    {
      new_filter->execline = new gchar[strlen(el) + 2];
      strcpy(new_filter->execline, el);
    }
  else
    {
      new_filter->execline = NULL;
    }

  /* insert into the new list */
  int k;
  
  k = 0;

  if ( old == NULL )
    filter[k++] = new_filter;
  else {
    for ( int i = 0; class0_filters[i] != -1; ++i ) {
      for ( int j = 0; old[j] != NULL; ++j ) {
        if (( old[j]->mode & CLASS0_MASK ) == class0_filters[i] ) {
          filter[k++] = old[j];
        }
      }
      if ( (new_filter->mode & CLASS0_MASK ) == class0_filters[i] )
        filter[k++] = new_filter;
    }
  }

  filter[k] = NULL;
  
  assert(k == amount + 1);

  /* delete old array */
  if ( old != NULL ) delete old;

  /* and now set the filter class bits accordingly */
  filterclass |= determine_filter_class( md );

  return;
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]