/* dBoxTools:Find.c */

#include <string.h>
#include <stdlib.h>

#include "clibExtra.h"
#include "error.h"

#include "dboxtools.h"
#include "Find.h"
#include "read.h"

Action *find_action( Action *a, Action *target)
{
  if( a == NULL ) return NULL;
  for( ; a; a = a->next )
  { if( a->object != target->object && a->object != -1 && target->object != -1 )
      continue;
    if( a->component != target->component && a->component != -1
        && target->component != -1 ) continue;
    if( a->event != target->event && a->event != -1 && target->event != -1 )
      continue;
    if( a->value == NULL || target->value == NULL ) break;
    if( strcmp( target->value, a->value ) == 0 ) break;
  }
  return a;
}

ObjectId find_object( char *name )
{ Object *o = firstObject;
  char *p;

  if( *name == 0 ) return -1;
  while( *name == ' ' ) name++;
  for( p = name; *p; p++ ) if( *p == ' ' ) *p = 0;
  for(o = firstObject; o; o = o->next )
    if( strcmp(name, o->name ) == 0 ) break;
  return o ? o->object : -1;
}

void find_objectAndComponent
( char *text, ObjectId *object, ComponentId *component)
{ char *p;
  char buffer[30];

  strncpy( buffer, text, 30 );
  p = strstr( buffer, ":" );
  if( p == NULL )
  { /* object name only */
    *component = -1;
  }
  else
  { *p++ = 0;
    *component = (ComponentId) strtol( p, &p, 16 );
  }    
  *object = find_object( buffer );
}

/* end of dBoxTools:Find.c */
