/*
FILE:     CifItemValue.C
*/
/*
VERSION:  4
*/
/*
DATE:     6/17/2002
*/
/*
  Comments and Questions to: sw-help@rcsb.rutgers.edu
*/
/*
COPYRIGHT 1999-2002 Rutgers - The State University of New Jersey

This software is provided WITHOUT WARRANTY OF MERCHANTABILITY OR
FITNESS FOR A PARTICULAR PURPOSE OR ANY OTHER WARRANTY, EXPRESS OR
IMPLIED.  RUTGERS MAKE NO REPRESENTATION OR WARRANTY THAT THE
SOFTWARE WILL NOT INFRINGE ANY PATENT, COPYRIGHT OR OTHER
PROPRIETARY RIGHT.

The user of this software shall indemnify, hold harmless and defend
Rutgers, its governors, trustees, officers, employees, students,
agents and the authors against any and all claims, suits,
losses, liabilities, damages, costs, fees, and expenses including
reasonable attorneys' fees resulting from or arising out of the
use of this software.  This indemnification shall include, but is
not limited to, any and all claims alleging products liability.
*/
/*
               PDB SOFTWARE LICENSE AGREEMENT

BY CLICKING THE ACCEPTANCE BUTTON OR INSTALLING OR USING 
THIS "SOFTWARE, THE INDIVIDUAL OR ENTITY LICENSING THE  
SOFTWARE ("LICENSEE") IS CONSENTING TO BE BOUND BY AND IS 
BECOMING A PARTY TO THIS AGREEMENT.  IF LICENSEE DOES NOT 
AGREE TO ALL OF THE TERMS OF THIS AGREEMENT
THE LICENSEE MUST NOT INSTALL OR USE THE SOFTWARE.

1. LICENSE AGREEMENT

This is a license between you ("Licensee") and the Protein Data Bank (PDB) 
at Rutgers, The State University of New Jersey (hereafter referred to 
as "RUTGERS").   The software is owned by RUTGERS and protected by 
copyright laws, and some elements are protected by laws governing 
trademarks, trade dress and trade secrets, and may be protected by 
patent laws. 

2. LICENSE GRANT

RUTGERS grants you, and you hereby accept, non-exclusive, royalty-free 
perpetual license to install, use, modify, prepare derivative works, 
incorporate into other computer software, and distribute in binary 
and source code format, or any derivative work thereof, together with 
any associated media, printed materials, and on-line or electronic 
documentation (if any) provided by RUTGERS (collectively, the "SOFTWARE"), 
subject to the following terms and conditions: (i) any distribution 
of the SOFTWARE shall bind the receiver to the terms and conditions 
of this Agreement; (ii) any distribution of the SOFTWARE in modified 
form shall clearly state that the SOFTWARE has been modified from 
the version originally obtained from RUTGERS.  

2. COPYRIGHT; RETENTION OF RIGHTS.  

The above license grant is conditioned on the following: (i) you must 
reproduce all copyright notices and other proprietary notices on any 
copies of the SOFTWARE and you must not remove such notices; (ii) in 
the event you compile the SOFTWARE, you will include the copyright 
notice with the binary in such a manner as to allow it to be easily 
viewable; (iii) if you incorporate the SOFTWARE into other code, you 
must provide notice that the code contains the SOFTWARE and include 
a copy of the copyright notices and other proprietary notices.  All 
copies of the SOFTWARE shall be subject to the terms of this Agreement.  

3. NO MAINTENANCE OR SUPPORT; TREATMENT OF ENHANCEMENTS 

RUTGERS is under no obligation whatsoever to: (i) provide maintenance 
or support for the SOFTWARE; or (ii) to notify you of bug fixes, patches, 
or upgrades to the features, functionality or performance of the 
SOFTWARE ("Enhancements") (if any), whether developed by RUTGERS 
or third parties.  If, in its sole discretion, RUTGERS makes an 
Enhancement available to you and RUTGERS does not separately enter 
into a written license agreement with you relating to such bug fix, 
patch or upgrade, then it shall be deemed incorporated into the SOFTWARE 
and subject to this Agreement. You are under no obligation whatsoever 
to provide any Enhancements to RUTGERS or the public that you may 
develop over time; however, if you choose to provide your Enhancements 
to RUTGERS, or if you choose to otherwise publish or distribute your 
Enhancements, in source code form without contemporaneously requiring 
end users or RUTGERS to enter into a separate written license agreement 
for such Enhancements, then you hereby grant RUTGERS a non-exclusive,
royalty-free perpetual license to install, use, modify, prepare
derivative works, incorporate into the SOFTWARE or other computer
software, distribute, and sublicense your Enhancements or derivative
works thereof, in binary and source code form.

4. FEES.  There is no license fee for the SOFTWARE.  If Licensee
wishes to receive the SOFTWARE on media, there may be a small charge
for the media and for shipping and handling.  Licensee is
responsible for any and all taxes.

5. TERMINATION.  Without prejudice to any other rights, Licensor
may terminate this Agreement if Licensee breaches any of its terms
and conditions.  Upon termination, Licensee shall destroy all
copies of the SOFTWARE.

6. PROPRIETARY RIGHTS.  Title, ownership rights, and intellectual
property rights in the Product shall remain with RUTGERS.  Licensee 
acknowledges such ownership and intellectual property rights and will 
not take any action to jeopardize, limit or interfere in any manner 
with RUTGERS' ownership of or rights with respect to the SOFTWARE.  
The SOFTWARE is protected by copyright and other intellectual 
property laws and by international treaties.  Title and related 
rights in the content accessed through the SOFTWARE is the property 
of the applicable content owner and is protected by applicable law.  
The license granted under this Agreement gives Licensee no rights to such
content.

7. DISCLAIMER OF WARRANTY.  THE SOFTWARE IS PROVIDED FREE OF 
CHARGE, AND, THEREFORE, ON AN "AS IS" BASIS, WITHOUT WARRANTY OF 
ANY KIND, INCLUDING WITHOUT LIMITATION THE WARRANTIES THAT IT 
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE 
OR NON-INFRINGING.  THE ENTIRE RISK AS TO THE QUALITY AND 
PERFORMANCE OF THE SOFTWARE IS BORNE BY LICENSEE.  SHOULD THE 
SOFTWARE PROVE DEFECTIVE IN ANY RESPECT, THE LICENSEE AND NOT 
LICENSOR ASSUMES THE ENTIRE COST OF ANY SERVICE AND REPAIR.  
THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF 
THIS AGREEMENT.  NO USE OF THE PRODUCT IS AUTHORIZED HEREUNDER 
EXCEPT UNDER THIS DISCLAIMER.

8. LIMITATION OF LIABILITY.  TO THE MAXIMUM EXTENT PERMITTED BY
APPLICABLE LAW,  IN NO EVENT WILL LICENSOR BE LIABLE FOR ANY 
INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 
OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, INCLUDING, 
WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, WORK 
STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL 
OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF ADVISED OF THE
POSSIBILITY THEREOF. 
*/
/* ******************************************************************

  PURPOSE:      This file contains the methods of class CIF_ITEMVALUE

 * **************************************************************** */

#include "CifItemValue.h"
#include "ciflib.h"

// *********************************************************************
//      Constructor, destructor, DeleteElement(), 
//      operators, copy functions 
// *********************************************************************

 CifItemValue:: CifItemValue()
// ---------------------------------------------------------------------
//   CifItemValue(): constructor does initialization
// ---------------------------------------------------------------------
{
  _status = CIF_DATA_IS_NULL;
  _dataType = CIF_STRING_VALUE;
  _flags._expression = TRUE;
  _flags._range = TRUE;
  _flags._case = TRUE;
  _flags._valueInParent= TRUE;
  _structureNumValue = 1;
  _data._intVal = 0;
  _data._dblVal = 0.0;
  _data._strVal = NULL;
  _data._arrayVal = NULL;
  _buf.Clear();
//  lowercase__buf.Clear();
}

 CifItemValue:: CifItemValue(const int new_datatype, const int new_structureNumValue,
		       const int new_case_sensitive)
// ---------------------------------------------------------------------
//   CifItemValue(): constructor does initialization
//      Parameter: const int new_datatype
// ---------------------------------------------------------------------
{
  _dataType = new_datatype;
  _flags._expression = TRUE;
  _flags._case = new_case_sensitive;
  _flags._range = TRUE;
  _flags._valueInParent= TRUE;
  _structureNumValue = new_structureNumValue;
  if (_dataType != CIF_INTEGER_VALUE && _dataType != CIF_DOUBLE_VALUE &&
      _dataType != CIF_STRING_VALUE)
    _status = CIF_DATA_TYPE_MISMATCH;
  else _status = CIF_DATA_IS_NULL;

  if (_structureNumValue > 1)
    _data._arrayVal = new ReVarCifArray <CifItemValue>;
  else {
    _data._intVal = 0;
    _data._dblVal = 0.0;
    _data._strVal = NULL;
    _data._arrayVal = NULL;
  }
  _buf.Clear();
//  lowercase__buf.Clear();
}

 CifItemValue:: CifItemValue(const int a)
// ---------------------------------------------------------------------
//   CifItemValue(): constructor does initialization
//      Parameter: const int new_datatype and itemValue a to put in union data 
// --------------------------------------------------------------------- 
{
  char tmpchar[50];

  _flags._expression = TRUE;
  _flags._range = TRUE;
  _flags._case = TRUE;
  _flags._valueInParent= TRUE;
  _dataType = CIF_INTEGER_VALUE ;
  _structureNumValue = 1;
  sprintf(tmpchar, "%ld\0", a);
  _buf.Copy(tmpchar);
  _status = CIF_DATA_IS_VALID;
  _data._intVal = a;
  
}

 CifItemValue:: CifItemValue(const long a)
// ---------------------------------------------------------------------
//   CifItemValue(): constructor does initialization
//      Parameter: const int new_datatype 
//                 const long itemValue a to put in union data 
// --------------------------------------------------------------------- 
{
  char tmpchar[50];

  _flags._expression = TRUE;
  _flags._range = TRUE;
  _flags._case = TRUE;
  _flags._valueInParent= TRUE;
  _dataType = CIF_INTEGER_VALUE ;
  _structureNumValue = 1;
  sprintf(tmpchar, "%ld\0", a);
  _buf.Copy(tmpchar);
  _status = CIF_DATA_IS_VALID;
  _data._intVal = a;
}
 CifItemValue:: CifItemValue(const double a)
// ---------------------------------------------------------------------
//   CifItemValue(): constructor does initialization
//      Parameter: const int new_datatype 
//                 const double itemValue a to put in union data 
// --------------------------------------------------------------------- 
{
  char tmpchar[50];

  _flags._expression = TRUE;
  _flags._range = TRUE;
  _flags._case = TRUE;
  _flags._valueInParent= TRUE;
  sprintf(tmpchar, "%lf", a);
  _buf.Copy(tmpchar);
  _dataType = CIF_DOUBLE_VALUE ;
  _structureNumValue = 1;
  _status = CIF_DATA_IS_VALID;
  _data._dblVal = a;
}
 CifItemValue:: CifItemValue(const int new_case_sensitive, const CifString s)
// ---------------------------------------------------------------------
//   CifItemValue(): constructor does initialization
//      Parameter: const int new_datatype 
//                 const CifString itemValue a to put in union data 
// --------------------------------------------------------------------- 
{
  _flags._expression = TRUE;
  _flags._range = TRUE;
  _flags._case = new_case_sensitive;
  _flags._valueInParent= TRUE;
  _structureNumValue = 1;
  _buf.Copy(s);
  _dataType = CIF_STRING_VALUE;

  _data._intVal = 0;
  _data._dblVal = 0.0;
  _data._strVal = NULL;
  _data._arrayVal = NULL;
  if (!strcmp(s.Text(),"")) 
    _status = CIF_DATA_IS_NULL;
  else  if (!strcmp(s.Text(),".")) 
    _status =  CIF_DATA_NOT_APPROPRIATE ;
  else {
    _status = CIF_DATA_IS_VALID;
    _data._strVal= new CifString;
    _data._strVal->Copy(_buf);
  }
}

CifItemValue:: CifItemValue(const int new_dataType, const int new_case_sensitive,
		      const int new_status,	 const char *s)
// ---------------------------------------------------------------------
//   CifItemValue(): constructor does initialization
//      Parameter: const int new_datatype 
//                 const char *itemValue a to put in union data 
// --------------------------------------------------------------------- 
{
  _flags._expression = TRUE;
  _flags._range = TRUE;
  _flags._case = new_case_sensitive;
  _flags._valueInParent= TRUE;
  if (s == NULL) {
    _buf.Clear();
  }
  else {
    _buf.Copy(s);
  }

  _data._intVal = 0;
  _data._dblVal = 0.0;
  _data._strVal = NULL;
  _data._arrayVal = NULL;
  
  _dataType = new_dataType;
  _structureNumValue = 1;

  if (_dataType != CIF_INTEGER_VALUE && _dataType != CIF_DOUBLE_VALUE &&
      _dataType != CIF_STRING_VALUE )
    _status = CIF_DATA_TYPE_MISMATCH;
  else {
    if (new_status == CIF_DATA_NOT_APPROPRIATE)  {
      _status =  CIF_DATA_NOT_APPROPRIATE ;
      _buf.Clear();
    }
    else if (new_status == CIF_DATA_IS_NULL || s == NULL) {
      _status = CIF_DATA_IS_NULL;
      _buf.Clear();
    }
    else {
      _status = new_status;
      if (_dataType == CIF_STRING_VALUE) {
	_data._strVal= new CifString;
	_data._strVal->Copy(_buf);
      }
      else if (_dataType == CIF_DOUBLE_VALUE) {
	/*
	   dot = FALSE;
	   for (i=0; i< _buf.Length(); i++) {
	   if ((i != 0 || (_buf.Text()[i] != '-' && _buf.Text()[i] != '+'))
	   && !isdigit(_buf.Text()[i])  && 
	   (_buf.Text()[i] != '.' || dot != FALSE)) break;
	   if (_buf.Text()[i] == '.') dot = TRUE;
	   }
	   if (i != _buf.Length())
	   _status = CIF_VALUE_SHOULD_BE_DOUBLE;
	   else
	   */
	_data._dblVal = atof(_buf.Text());
      }
      else if (_dataType == CIF_INTEGER_VALUE) {
	/*
	   dot = FALSE;
	   for (i=0; i< _buf.Length(); i++) {
	   if (_buf.Text()[i] == '.') dot = TRUE;
	   if ((i != 0 || (_buf.Text()[i] != '-' && _buf.Text()[i] != '+'))
	   && !isdigit(_buf.Text()[i])) break;
	   }
	   if (i != _buf.Length() && dot == TRUE)  
	   for (i++;i < _buf.Length(); i++)
	   if (_buf.Text()[i] != '0') break;
	   if (i != _buf.Length())  
	   _status = CIF_VALUE_SHOULD_BE_INTEGER ;
	   else
	   */
	_data._intVal = atoi(_buf.Text());
      }
    }
  }
}

 CifItemValue:: CifItemValue(const CifItemValue &newitemValue)
// ---------------------------------------------------------------------
//   CifItemValue(): copy constructor does initialization
//      Parameter: const CifItemValue &newitemValue
// --------------------------------------------------------------------- 
{
  _flags._expression = newitemValue._flags._expression;
  _flags._range = newitemValue._flags._range;
  _flags._case = newitemValue._flags._case;
  _flags._valueInParent= newitemValue._flags._valueInParent;
  _dataType = newitemValue._dataType;
  _status = newitemValue._status;
  _structureNumValue = newitemValue._structureNumValue;
  _buf.Copy(newitemValue._buf);
  _data._intVal = 0;
  _data._dblVal = 0.0;
  _data._strVal = NULL;

  if (_structureNumValue > 1) {
    _data._arrayVal = new ReVarCifArray <CifItemValue>;
    for (unsigned i =0; i< newitemValue._data._arrayVal->Length(); i++)
      _data._arrayVal->Add((*newitemValue._data._arrayVal)[i]);
  }
  else {
    if (_status == CIF_DATA_IS_VALID) {
      if (_dataType == CIF_STRING_VALUE ) {
	if (newitemValue._data._strVal != NULL) {
	  _data._strVal = new CifString;
	  _data._strVal->Copy(newitemValue._data._strVal);
	}
      }
      else if (_dataType == CIF_INTEGER_VALUE)
	_data._intVal = newitemValue._data._intVal;
      else if (_dataType == CIF_DOUBLE_VALUE)
	_data._dblVal = newitemValue._data._dblVal;
      else
	_status = CIF_DATA_TYPE_MISMATCH;
    }
  }
}

 CifItemValue::~CifItemValue()
// ---------------------------------------------------------------------
//   ~CifItemValue(): destructor 
// ---------------------------------------------------------------------
{
  DeleteElement();
}

void CifItemValue::DeleteElement()
// ---------------------------------------------------------------------
//   DeleteElement(): Cleans up space
// --------------------------------------------------------------------- 
{
  if (_structureNumValue > 1)
    delete _data._arrayVal;
  else if (_dataType == CIF_STRING_VALUE && _status == CIF_DATA_IS_VALID && 
      _data._strVal != NULL) 
	 {
    delete _data._strVal;
	 _data._strVal = NULL;
	 }

  _flags._expression = TRUE;
  _flags._range = TRUE;
  _flags._case = TRUE;
  _flags._valueInParent = TRUE;
  _data._strVal = NULL;
  _data._arrayVal = NULL;
  _data._intVal = 0;
  _data._dblVal = 0.0;
  _dataType = CIF_STRING_VALUE;
  _status = CIF_DATA_IS_NULL;
  _structureNumValue = 1;
  _buf.Clear();
//  lowercase__buf.Clear();
}


CifItemValue &CifItemValue::operator=(const CifItemValue &olditemValue)
// ---------------------------------------------------------------------
//   operator=(): assignment operator =
// --------------------------------------------------------------------- 
{
  return(Copy(olditemValue));
}

CifItemValue &CifItemValue::Copy(const CifItemValue &newitemValue)
// ---------------------------------------------------------------------
//   Copy(): Copy itemValue from a CifItemValue class
// --------------------------------------------------------------------- 
{
  DeleteElement();
  _flags._expression = newitemValue._flags._expression;
  _flags._range = newitemValue._flags._range;
  _flags._case = newitemValue._flags._case;
  _flags._valueInParent= newitemValue._flags._valueInParent;
  _buf.Copy(newitemValue._buf);
  _dataType = newitemValue._dataType;
  _status = newitemValue._status;
  _structureNumValue = newitemValue._structureNumValue;

  if (_structureNumValue > 1 ) {
    _data._arrayVal = new ReVarCifArray <CifItemValue>;
    for (unsigned i =0; i< newitemValue._data._arrayVal->Length(); i++)
      _data._arrayVal->Add((*newitemValue._data._arrayVal)[i]);
  }
  else {
    if (_status == CIF_DATA_IS_VALID) {
      if (newitemValue._dataType == CIF_STRING_VALUE) {
	if ( newitemValue._data._strVal != NULL) {
	  _data._strVal = new CifString;
	  _data._strVal->Copy(newitemValue._data._strVal);
	}
      }
      else if (newitemValue._dataType == CIF_INTEGER_VALUE)
	_data._intVal = newitemValue._data._intVal;
      else if (newitemValue._dataType == CIF_DOUBLE_VALUE)
	_data._dblVal = newitemValue._data._dblVal;
      else _status = CIF_DATA_TYPE_MISMATCH;
    }
  }
  return(*this);
}

// *********************************************************************
//      PrintTable(), and Print() prints the itemValue of the structure
// *********************************************************************

void CifItemValue::PrintTable() const
// ---------------------------------------------------------------------
//   PrintTable(): Print table like itemValues
// --------------------------------------------------------------------- 
{
  if (_status != CIF_DATA_IS_NULL && _status != CIF_DATA_NOT_APPROPRIATE)
    cout << '\t' << "\"" << _buf.Text() << "\"";
  else if (_status == CIF_DATA_IS_NULL)
    cout << '\t' << "\"null\"";
  else cout << '\t' << "\"not_appropriate\"";
}
void CifItemValue::PermanentWrite(FILE *fp) 
{
  fprintf(fp, "%d %d %d %d %d %d %d\n",
          _flags._case, _flags._expression,
          _flags._range,  _flags._valueInParent,
          _dataType, _structureNumValue, _status);
  _buf.PermanentWrite(fp);

  if (_structureNumValue > 1) {
    if (_data._arrayVal == NULL)
      fprintf(fp, "%d\n", 0);
    else {
      fprintf(fp, "%d\n", _data._arrayVal->Length());
      for (unsigned i=0; i<_data._arrayVal->Length(); i++)
	(*_data._arrayVal)[i].PermanentWrite(fp);
    }
  }
}
void CifItemValue::PermanentRead(FILE *fp) 
{
  int tmpitemValue; 
  int tmplength;
  CifItemValue tmpval;
  
  fscanf(fp, "%d ", &tmpitemValue);
  _flags._case = tmpitemValue;

  fscanf(fp, "%d ", &tmpitemValue);
  _flags._expression = tmpitemValue;

  fscanf(fp, "%d ", &tmpitemValue);
  _flags._range = tmpitemValue;

  fscanf(fp, "%d ", &tmpitemValue);
  _flags._valueInParent= tmpitemValue;

  fscanf(fp, "%d %d %d",
	 &_dataType, &_structureNumValue, &_status);
  _buf.PermanentRead(fp);
  if (_structureNumValue > 1) {
    fscanf(fp, "%d\n", &tmplength);
    if (tmplength >= 1) {
      _data._arrayVal = new ReVarCifArray <CifItemValue>;
      if (tmplength > 0) _data._arrayVal->GrowBy(tmplength);
      for (unsigned i=0; i < tmplength; i++) {
	tmpval.DeleteElement();
	tmpval.PermanentRead(fp);
	_data._arrayVal->Add(tmpval);
      }
    }
  }
  else {
    if (_dataType == CIF_INTEGER_VALUE)
      _data._intVal = atoi(_buf.Text());
    else if (_dataType == CIF_DOUBLE_VALUE)
      _data._dblVal = atof(_buf.Text());
    else {
      if (_status == CIF_DATA_IS_VALID) {
	_data._strVal = new CifString;
	_data._strVal->Copy(_buf);
      }
    }
  }
}
void CifItemValue::Print() const
// ---------------------------------------------------------------------
//   Print(): Dump the structure
// --------------------------------------------------------------------- 
{

  if (_structureNumValue <= 1) {
    if (_status == CIF_DATA_IS_NULL)
      cout <<  "null ";
    else if (_status == CIF_DATA_NOT_APPROPRIATE)
          cout << "not appropriate ";
    else  if (_status == CIF_DATA_IS_VALID) {
      if (_dataType == CIF_INTEGER_VALUE) 
    	cout << _data._intVal << ' ';
      else if (_dataType == CIF_DOUBLE_VALUE) 
    	cout << _data._dblVal << ' ';
      else 
        cout << '"' << _data._strVal->Text() << '"' << ' ';
    }
    else
      cout << '"' << _buf.Text() << '"' << ' ';
    cout << endl;
  }
  else {
    cout << "arrayVal.Length() = " << _data._arrayVal->Length() << endl;
    cout << "\t\t" ;
    for (unsigned i=0; i< _data._arrayVal->Length(); i++) {
      cout << i <<  ": " ;
      (*_data._arrayVal)[i].Print();
    }
  }
}
void CifItemValue::PrintDetails() const
// ---------------------------------------------------------------------
//   Print(): Dump the structure
// --------------------------------------------------------------------- 
{

  cout << "_status = " << _status ; //<< " _flags._range = " << _range ;
  cout << " _structureNumValue = " << _structureNumValue ;
  cout << " case_sensitive = " << _flags._case ;
  cout << " _dataType = " << _dataType << ": ";
  if (_structureNumValue <= 1) {
    if (_status == CIF_DATA_IS_NULL)
      cout <<  "null ";
    else if (_status == CIF_DATA_NOT_APPROPRIATE)
          cout << "not appropriate ";
    else  if (_status == CIF_DATA_IS_VALID) {
      if (_dataType == CIF_INTEGER_VALUE) 
    	cout << _data._intVal << ' ';
      else if (_dataType == CIF_DOUBLE_VALUE) 
    	cout << _data._dblVal << ' ';
      else 
        cout << '"' << _data._strVal->Text() << '"' << ' ';
    }
    else
      cout << '"' << _buf.Text() << '"' << ' ';
  }
  else {
    cout << "arrayVal.Length() = " << _data._arrayVal->Length() << endl;
    cout << "\t\t" ;
    for (unsigned i=0; i< _data._arrayVal->Length(); i++) {
      cout << i <<  ": " ;
      (*_data._arrayVal)[i].Print();
    }
  }
  cout << endl;
}

// *********************************************************************
//     UpdateItemValue updates itemValue
// *********************************************************************
int CifItemValue::UpdateItemValue(const unsigned new_type, const int new_status,
			   const char *newitemValue)
// ---------------------------------------------------------------------
//   UpdateItemValue(): Updates the itemValue
//   Parameter: CifItemValue *newitemValue
// --------------------------------------------------------------------- 
{
  unsigned save_dataType;
  save_dataType = _dataType;

  _buf.Clear();
  if (_structureNumValue > 1) return CIF_STRUCTURE_DIMENSION_NOT_EXIST ;
  _dataType = new_type;
  if (_status == CIF_DATA_IS_VALID && save_dataType == CIF_STRING_VALUE && 
      (_dataType == CIF_INTEGER_VALUE || _dataType == CIF_DOUBLE_VALUE) ){
    if (_data._strVal) {
		delete _data._strVal;
		_data._strVal = NULL;
	 }
  }
  if (new_status == CIF_DATA_NOT_APPROPRIATE) {
    if (save_dataType == CIF_STRING_VALUE && _status == CIF_DATA_IS_VALID) {
      if (_data._strVal) {
		  delete _data._strVal;
		  _data._strVal = NULL;
		}
	 }
    _status = CIF_DATA_NOT_APPROPRIATE;
    return _status;
  }
  else if (new_status == CIF_DATA_IS_NULL || newitemValue == NULL) {
    if (save_dataType == CIF_STRING_VALUE && _status == CIF_DATA_IS_VALID) {
      if (_data._strVal) {
		  delete _data._strVal;
		  _data._strVal = NULL;
		}
	 }
    _status = CIF_DATA_IS_NULL;
    return _status;
  }
  else if ((save_dataType == CIF_STRING_VALUE && _status != CIF_DATA_IS_VALID ||
				save_dataType != CIF_STRING_VALUE) && _dataType == CIF_STRING_VALUE){ 
    _data._strVal = new CifString;
  }
  if (_dataType == CIF_INTEGER_VALUE)
    _data._intVal = atoi(newitemValue);
  else   if (_dataType == CIF_DOUBLE_VALUE)
    _data._dblVal = atof(newitemValue);
  else 
    _data._strVal->Copy(newitemValue);
  _status = CIF_DATA_IS_VALID;
  if (newitemValue != NULL) _buf.Copy(newitemValue); 
  else _buf.Clear();
  return _status;
}

int CifItemValue::UpdateItemValue(const long newitemValue)
{
  char tmpchar[CIF_MAXSTRLEN];
  if (_structureNumValue > 1) return CIF_STRUCTURE_DIMENSION_NOT_EXIST ;
  if (_dataType != CIF_INTEGER_VALUE) return CIF_DATA_TYPE_MISMATCH  ;
  _status = CIF_DATA_IS_VALID;
  _data._intVal = newitemValue;
  sprintf(tmpchar, "%ld\0", newitemValue);
  _buf.Copy(tmpchar);
  return _status;
}
int CifItemValue::UpdateItemValue(const double newitemValue)
{
  char tmpchar[CIF_MAXSTRLEN];
  if (_structureNumValue > 1) return CIF_STRUCTURE_DIMENSION_NOT_EXIST ;
  if (_dataType != CIF_INTEGER_VALUE) return CIF_DATA_TYPE_MISMATCH  ;
  _status = CIF_DATA_IS_VALID;
  _data._dblVal = newitemValue;
  sprintf(tmpchar, "%ld\0", newitemValue);
  _buf.Copy(tmpchar);
  return _status;
}
int CifItemValue::UpdateItemValue(const CifItemValue *newitemValue)
// ---------------------------------------------------------------------
//   UpdateItemValue(): Updates the itemValue
//   Parameter: CifItemValue *newitemValue
// --------------------------------------------------------------------- 
{
  unsigned save_dataType, save_structureNumValue, save_case_sensitive;

  save_dataType = _dataType;
  save_structureNumValue = _structureNumValue;
  save_case_sensitive = _flags._case;
  
  DeleteElement();
  _dataType = save_dataType;
  _structureNumValue = save_structureNumValue;
  _flags._case = save_case_sensitive;
  _buf.Copy(newitemValue->_buf);
  if (_dataType != newitemValue->_dataType)
    _status = CIF_DATA_TYPE_MISMATCH;
  else Copy(*newitemValue);
  return _status;
}

int CifItemValue::UpdateItemValue(const CifItemValue *newitemValue, const unsigned index)
// ---------------------------------------------------------------------
//   UpdateItemValue(): Updates the itemValue
//   Parameter: CifItemValue *newitemValue
// --------------------------------------------------------------------- 
{

  if (_structureNumValue >  1) {
    if (index <_data._arrayVal->Length()) {
      (*_data._arrayVal)[index].Copy(*newitemValue);
      return TRUE;
    }
    else return FALSE;
  }
  else return UpdateItemValue(newitemValue);
}

void CifItemValue::SetDataStatus(const unsigned new_status)
{
  CifString tmp_buf(_buf);

  if (_structureNumValue <= 1) {
    DeleteElement();
    _status = new_status;
    _buf.Copy(tmp_buf);

    if (_dataType != CIF_INTEGER_VALUE && _dataType != CIF_DOUBLE_VALUE &&
	_dataType != CIF_STRING_VALUE)
      _status = CIF_DATA_TYPE_MISMATCH;
    else if (_status == CIF_DATA_IS_VALID) {
      if (_dataType == CIF_INTEGER_VALUE)
	_data._intVal = atoi(_buf.Text());
      else if (_dataType == CIF_DOUBLE_VALUE)
	_data._dblVal = atof(_buf.Text());
      else if (_dataType == CIF_STRING_VALUE) {
	if (strcmp(_buf.Text(), "") != 0) {
	  _data._strVal = new CifString;
	  _data._strVal->Copy(_buf);
	}
      }
    }
  }
  else {
    for (unsigned i=0; i< _data._arrayVal->Length(); i++)
      (*_data._arrayVal)[i].SetDataStatus(new_status);
  }
}
void CifItemValue::SetDataType(int new_type)
{
  double tmpdbl;
  char   tmpstrval[CIF_MAXSTRLEN];

  if (new_type != CIF_INTEGER_VALUE && new_type != CIF_DOUBLE_VALUE)
    new_type = CIF_STRING_VALUE;

  if (_dataType == new_type) return;
  if (_structureNumValue <=1) {
    if (_status != CIF_DATA_IS_NULL && _status != CIF_DATA_NOT_APPROPRIATE) {
      if (_dataType == CIF_STRING_VALUE) {
	if (new_type == CIF_INTEGER_VALUE || new_type == CIF_DOUBLE_VALUE) {
	  tmpdbl = atof(_data._strVal->Text());
	  if (_data._strVal) {
		 delete _data._strVal;
		 _data._strVal = NULL;
	  }
	  if (new_type == CIF_INTEGER_VALUE)
	    _data._intVal = (int) tmpdbl;
	  else if (new_type == CIF_DOUBLE_VALUE)
	    _data._dblVal = tmpdbl;
	}
      }
      else {
	if (new_type == CIF_STRING_VALUE) {
	  if (_dataType == CIF_INTEGER_VALUE) 
	    sprintf(tmpstrval,"%d\0", _data._intVal);
	  else if (_dataType == CIF_DOUBLE_VALUE)
	    sprintf(tmpstrval,"%lf\0", _data._dblVal);
	  _data._strVal = new CifString;
	  _data._strVal->Copy(tmpstrval);
	} 
      }
    }
    _dataType = new_type;
  }
  else {
    for (unsigned i =0; i < _data._arrayVal->Length(); i++)
      (*_data._arrayVal)[i].SetDataType(new_type);
    _dataType = new_type;
  }
}
// *********************************************************************
//     GetItemValue gets itemValue
// *********************************************************************
int CifItemValue:: GetItemValue(CifString **a, const int index)
// ---------------------------------------------------------------------
//   GetItemValue(): Get the string itemValue 
//   Parameter: CifString *a
// --------------------------------------------------------------------- 
{
  if (_structureNumValue <= 1) {
//    if (_flags._case)
//      a->Copy(_buf);
      *a = &_buf;
//    else
      //a->Copy(lowercase__buf);
//      *a = &lowercase__buf;
    return _status;
  }
  else {
    *a = NULL;
    if (index < 0 || index >= _data._arrayVal->Length()) 
      return CIF_VALUE_NOT_EXIST;
    else return (*_data._arrayVal)[index].GetItemValue(a);
  }
}

int CifItemValue:: GetItemValue(CifItemValue **a)
// ---------------------------------------------------------------------
//   GetItemValue(): Get the string itemValue 
//   Parameter: CifString *a
// --------------------------------------------------------------------- 
{
//  a->Copy(*this);
  *a = this;
  return _status;
}



