/*
    Copyright (C) 1998  Dennis Roddeman
    email: dennis.roddeman@uibk.ac.at

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.


    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation 
    59 Temple Place, Suite 330, Boston, MA, 02111-1307, USA
*/

#include "tochnog.h"

void data( void )

{
  long int idat=0, in=0, index=0, range_length=0, icontrol=0,
    length=0, any_element_deleted=0, any_node_deleted=0,
    swit=0, max_index=0, ldum=0, idum[1], integer_range[MRANGE], 
    *data_delete=NULL, *data_put=NULL;
  double ddum[1];

  db( ICONTROL, 0, &icontrol, ddum, ldum, VERSION_NORMAL, GET );

  if ( db_active_index( CONTROL_DATA_DELETE, icontrol, VERSION_NORMAL )  ) {
    swit = set_swit(-1,-1,"data");
    if ( swit ) pri( "In routine DATA" );
    length = db_len( CONTROL_DATA_DELETE, icontrol, VERSION_NORMAL );
    data_delete = db_int( CONTROL_DATA_DELETE, icontrol, VERSION_NORMAL );
    idat = data_delete[0];
    if ( data_put[1]>0 ) {
      index = data_delete[1];
      if      ( idat==-ELEMENT ) {
        any_element_deleted = 1;
        delete_element( index, VERSION_NORMAL );
      }
      else if ( idat==-NODE ) {
        any_node_deleted = 1;
        delete_node( index, VERSION_NORMAL );
      }
      else
        db_delete_index( idat, index, VERSION_NORMAL );
    }
    else if ( data_put[1]==-RA ) {
      range_expand( &data_delete[1], integer_range, length, range_length );
      for ( in=0; in<range_length; in++ ) {
        index = integer_range[in];
        if      ( idat==-ELEMENT ) {
          any_element_deleted = 1;
          delete_element( index, VERSION_NORMAL );
        }
        else if ( idat==-NODE ) {
          any_node_deleted = 1;
          delete_node( index, VERSION_NORMAL );
        }
        else
          db_delete_index( idat, index, VERSION_NORMAL );
      }
    }
    else if ( data_put[1]==-ALL ) {
      db_max_index( idat, max_index, VERSION_NORMAL, GET );
      for ( index=0; index<max_index; index++ ) {
        if      ( idat==-ELEMENT ) {
          any_element_deleted = 1;
          delete_element( index, VERSION_NORMAL );
        }
        else if ( idat==-NODE ) {
          any_node_deleted = 1;
          delete_node( index, VERSION_NORMAL );
        }
        else
          db_delete_index( idat, index, VERSION_NORMAL );
      }
    }
    else
      db_error( CONTROL_DATA_DELETE, icontrol );
    if ( swit ) pri( "Out routine DATA" );
  }
  if ( any_element_deleted || any_node_deleted ) 
    db_delete( NODE_NODE, VERSION_NORMAL );

  if ( db_active_index( CONTROL_DATA_PUT, icontrol, VERSION_NORMAL )  ) {
    swit = set_swit(-1,-1,"data");
    if ( swit ) pri( "In routine DATA" );
    length = db_len( CONTROL_DATA_PUT, icontrol, VERSION_NORMAL );
    data_put = db_int( CONTROL_DATA_PUT, icontrol, VERSION_NORMAL );
    idat = data_put[0];
    if ( data_put[1]>0 ) {
      index = data_put[1];
      if ( db_type(idat)==INTEGER ) {
        length = db_len( CONTROL_DATA_PUT_INTEGER, icontrol, VERSION_NORMAL );
        db( idat, index, db_int(CONTROL_DATA_PUT_INTEGER, icontrol, 
          VERSION_NORMAL), ddum, length, VERSION_NORMAL, PUT );
      }
      else {
        length = db_len( CONTROL_DATA_PUT_DOUBLE, icontrol, VERSION_NORMAL );
        db( idat, index, idum, db_dbl(CONTROL_DATA_PUT_DOUBLE, icontrol, 
          VERSION_NORMAL), length, VERSION_NORMAL, PUT );
      }
    }
    else if ( data_put[1]==-RA ) {
      range_expand( &data_put[1], integer_range, length, range_length );
      for ( in=0; in<range_length; in++ ) {
        index = integer_range[in];
        if ( db_type(idat)==INTEGER ) {
          length = db_len( CONTROL_DATA_PUT_INTEGER, icontrol, VERSION_NORMAL );
          db( idat, index, db_int(CONTROL_DATA_PUT_INTEGER, icontrol, 
            VERSION_NORMAL), ddum, length, VERSION_NORMAL, PUT );
        }
        else {
          length = db_len( CONTROL_DATA_PUT_DOUBLE, icontrol, VERSION_NORMAL );
          db( idat, index, idum, db_dbl(CONTROL_DATA_PUT_DOUBLE, icontrol, 
            VERSION_NORMAL), length, VERSION_NORMAL, PUT );
        }
      }
    }
    else if ( data_put[1]==-ALL ) {
      db_max_index( idat, max_index, VERSION_NORMAL, GET );
      for ( index=0; index<=max_index; index++ ) {
        if ( db_type(idat)==INTEGER ) {
          length = db_len( CONTROL_DATA_PUT_INTEGER, icontrol, VERSION_NORMAL );
          db( idat, index, db_int(CONTROL_DATA_PUT_INTEGER, icontrol, 
            VERSION_NORMAL), ddum, length, VERSION_NORMAL, PUT );
        }
        else {
          length = db_len( CONTROL_DATA_PUT_DOUBLE, icontrol, VERSION_NORMAL );
          db( idat, index, idum, db_dbl(CONTROL_DATA_PUT_DOUBLE, icontrol, 
            VERSION_NORMAL), length, VERSION_NORMAL, PUT );
        }
      }
    }
    else
      db_error( CONTROL_DATA_DELETE, icontrol );
    if ( swit ) pri( "Out routine DATA" );
  }

  if ( idat==-NODE || idat==-ELEMENT ) mesh_has_changed( VERSION_NORMAL );
}
