/* t4info.cpp (c)Copyright Sequiter Software Inc., 1989-1993.  All rights reserved. */

#include "d4all.h"
#ifndef S4UNIX
#ifdef __TURBOC__
#pragma hdrstop
#endif  /* __TUROBC__ */
#endif  /* S4UNIX */

#include "d4data.hpp"

Tag4info::Tag4info( Code4 &code )
{
  size = 0 ;
  code_base = &code ;
  length = 0 ;
  tag = NULL ;
}

Tag4info::Tag4info( Index4 i )
{
  size = 0 ;
  code_base = (Code4 *) i.index->data->code_base ;
  length = 0 ;
  tag = NULL ;
  add_index_tags( i.index ) ;
}

Tag4info::Tag4info( Data4 d )
{
  INDEX4 *index = (INDEX4*) l4next( &(d.data->indexes), NULL ) ;

  size = 0 ;
  code_base = (Code4 *) d.data->code_base ;
  length = 0 ;
  tag = NULL ;

  for(; index != NULL ; index = (INDEX4*) l4next( &(d.data->indexes), index ) )
    add_index_tags( index ) ;
}

int Tag4info::add_index_tags( INDEX4 *index )
{
  TAG4 *tag = (TAG4 *) l4next( &(index->tags), NULL ) ;

  for( ; tag != NULL ; tag = (TAG4*) l4next( &(index->tags), tag ) )
    if( add( Tag4(tag) ) != 0) return -1 ;
  return 0 ;
}

int Tag4info::add( Tag4 tag )
{
  return add( tag.alias(), expr4source(tag.tag->expr), expr4source(tag.tag->filter),
             t4unique(tag.tag), t4is_descending(tag.tag) ) ;
}

int Tag4info::add( char *name, char *expr, char *filter,
                  int uniq, int desc )
{
  Str4ten st_name( name ) ;
  st_name.upper() ;
  st_name.trim() ;
#ifdef S4DEBUG
  if( name == 0 || expr == 0 )
    return code_base->error( e4parm, "Tag4info::add()" ) ;
#endif

  if( u4alloc_again( code_base, (char **)&tag, &length,
                    (size+2) * sizeof( TAG4INFO ))!=0 )
    return -1 ;

  tag[size].name = (char *)u4alloc_er( code_base, 11 ) ;
  if( tag[size].name )
    u4ncpy( tag[size].name, st_name.ptr(), 11 ) ;

  unsigned len = strlen(expr) + 1 ;
  tag[size].expression = (char *) u4alloc_er( code_base, len ) ;
  if( tag[size].expression != NULL )
    u4ncpy( tag[size].expression, expr, len ) ;

  if( filter )
  {
    tag[size].filter = (char *) u4alloc_er( code_base, len = strlen( filter )+1 ) ;
    if( tag[size].filter != NULL )
      u4ncpy( tag[size].filter, filter, len ) ;
  }

  if( code_base->errorCode < 0 )
  {
    u4free( tag[size].name ) ;
    u4free( tag[size].expression ) ;
    u4free( tag[size].filter ) ;
    memset( tag+size, 0, sizeof(TAG4INFO) ) ;
    return -1 ;
  }

  tag[size].unique = uniq ;
  tag[size].descending = desc ;
  size++ ;
  return 0 ;
}

void Tag4info::free( )
{
  for( ; size > 0 ; )
  {
    u4free( tag[size].name ) ;
    u4free( tag[size].expression ) ;
    u4free( tag[size].filter ) ;
    size -- ;
  }
  u4free( tag ) ;
  tag = 0 ;
  size = 0 ;
  length = 0 ;
}

Tag4info::~Tag4info()
{
  free( ) ;
}

int Tag4info::del( int index )
{
  if( index >= size || index < 0 )
    return code_base->error( e4parm ) ;

  u4free( tag[index].name ) ;
  u4free( tag[index].expression ) ;
  u4free( tag[index].filter ) ;

  memcpy( tag+index, tag+index+1, sizeof(TAG4INFO) * (size-index) ) ;
  size-- ;
  return 0 ;
}

int Tag4info::del( char *name )
{
  Str4ten st_tag( name ) ;
  st_tag.upper( ) ;
  st_tag.trim( ) ;
  for( int i = 0 ; i < size ; i++ )
  {
    if( memcmp( tag[i].name, st_tag.ptr( ), st_tag.len() ) == 0 )
    {
      del( i ) ;
      return 0 ;
    }
  }
  code_base->error( e4parm, "Tag4info::del()", name ) ;
  return -1 ;
}
