/*
FILE:     DDLParserBase.C
*/
/*
VERSION:  1.600
*/
/*
DATE:     2/3/2005
*/
/*
  Comments and Questions to: sw-help@rcsb.rutgers.edu
*/
/*
COPYRIGHT 1999-2005 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:    A DDL 2.1 compliant CIF file parser.
*/

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define  MAX_CIF_NAME_LENGTH  100

#include "DDLScannerBase.h"
#include "DDLParserInt.h"
#include "DDLFileObj.h"
#include "DDLParserBase.h"

/* class CifFileObj;
class CifFileObjBase;
*/

extern FILE* ddlparser_in;
extern int ddlparser_parse();

char Glob_tBufKeywordSaveDDL[MAX_CIF_NAME_LENGTH];
char* Glob_pBufValueDDL;
CifString* Glob_sBufValueDDL;
char* Glob_dataBlockNameDDL;

DDLParser* DDLParserP = NULL;

DDLParser::DDLParser(FILE* in, DDLFileObj *fo, ISTable *format2, int verbose)
{
    ddlparser_in = in;
    _verbose=verbose;
    format=format2;
    Clear();
    errorLog.Clear();
    if (fo)
        _fobj = fo;
    else
        exit(-1);
    OpenLog("DDLParser.log",_verbose);

    DDLParserP = this;
}

void DDLParser::Parse()
{
    ddlparser_parse();
}

void ddlparser_error(const char *s)
{
    DDLParserP->Error(s);
}

DDLParser::~DDLParser()
{
    Reset();
    DDLParserP = NULL;
}


void DDLParser::Error(const char *s)
/* 
 * Purpose:  yyerror() Print errors in DDLScanner log.
 */
{
  errorLog += s;
  errorLog += " near line ";
  errorLog += NDBlineNo;
  errorLog += '\n';
  log << s << " near line " <<  NDBlineNo << endl;
} 

void DDLParser::Reset()
{
  if (_tbl && (_curItemNo > 0)) { // write the current table / management of _tbl by _fobj 
      if (_nTablesInBlock) {
      _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
      } else {
      _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
      }
      _nTablesInBlock++;
  }
  if (_rowBuf) delete _rowBuf;
  if (_fieldList) free(_fieldList);
  if (_colIndex) free(_colIndex);
  if (_rowBufSave) delete _rowBufSave;
  if (_fieldListSave) free(_fieldListSave);
}
void DDLParser::Clear()
{
  _nTablesInBlock=0;
  _numDataBlocks=0;
  _afterLoop = FALSE;
  _curItemNo=0;
  _curRow=0;
  _curCol=0;
  _curValueNo=0;
  _itemIndex=0;
  _fieldListAlloc=100;
  _fieldList=(int *) malloc(_fieldListAlloc*sizeof(int));
  _colIndex =(int *) malloc(_fieldListAlloc*sizeof(int));
  _pBufValue=NULL;
  _sBufValue=NULL;
  _rowBuf= new ReVarCifArray<CifString>;
  _fobj=NULL;
  _tbl=NULL;
  isSave = 0;
  _nTablesInBlockSave=0;
  _curItemNoSave=0;
  _saveobj=NULL;
  _savetbl=NULL;
  _fieldListAllocSave=100;
  _fieldListSave=(int *) malloc(_fieldListAllocSave*sizeof(int));
  _rowBufSave= new ReVarCifArray<CifString>;
  _uString.Copy(".");
  _mString.Copy("?");
  strcpy(_tBufKeyword,"");
  strcpy(_curCategoryName,"");
  strcpy(_curDataBlockName,"MISSING");
  strcpy(_prevDataBlockName,"MISSING");
  strcpy(_curCategoryNameSave,"");
  strcpy(_curDataBlockNameSave,"");
  strcpy(_prevDataBlockNameSave,"");
  ReVarCifArray<CifString> list;
/* table format keeps information about what tables are item related, what
   category related, and what datablock related
   column dbName has datablock name in it, since file can have more then one
   datablock, column type has type of table such as datablock, category or
   item; and column catName has table name such as dictionary_history,
   item_type_list... - datablock related; category, category_key... - category
   related, item_type, item_linked... - item related
   This table is nessery to write ddl in a save frame format
*/
  format->AddColumn("dbName");
  format->AddColumn("type");
  format->AddColumn("catName");
  list.Add("dbName");
  list.Add("type");
  list.Add("catName");
  format->CreateIndex("index0",list);
}


void DDLParser::ProcessLoopDeclaration(void)
/* ----------------------------------------------------------------------
     Purpose: DDLParser::ProcessLoopDeclaration(void)

              Handles initialization for a new loop, by creating a new
              category and adding the current item name to this category.
   ---------------------------------------------------------------------- */
{
  char  categoryName[MAX_CIF_NAME_LENGTH];

#if DEBUG
  if (_verbose) log << "Processing loop declaration at line " << NDBlineNo << " value " <<_tBufKeyword << endl;
#endif
  _afterLoop = TRUE;
  if (! CifFileObjBase::GetCategoryNameFromItemName(categoryName, _tBufKeyword) ) {
    log << "Error in category name at line " << NDBlineNo << " value " << _tBufKeyword << endl;
    errorLog += "Error in category name at line ";
    errorLog += NDBlineNo;
    errorLog += " value ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
  }

  if (_fobj->IsTablePresent(categoryName)) {
    if (!isSave) {
      log << "Warning - Duplicate category name " << categoryName << " at line " << NDBlineNo << endl;
      /* this is just a warning - rows are concatenated, this is
         happens only out of save frame, and in that case isSave=0.
         Because of that "else" is removed.
      */
    }
    if (_tbl) { // write the current table / management of _tbl by _fobj
      if (_nTablesInBlock) {
        _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
      } else {
        _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
      }
      _nTablesInBlock++;
    }
    _tbl = _fobj->GetTablePtr(_curDataBlockName,categoryName);
    _curRow = _tbl->GetNumRows();
    ProcessItemNameList();
    strcpy(_curCategoryName,categoryName);
  } else {
    if (_tbl) { // write the current table / management of _tbl by _fobj
      if (_nTablesInBlock) {
        _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
        } else {
          _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
        }
      _nTablesInBlock++;
    }
    _tbl = new ISTable(categoryName);
    _curRow = 0;
    strcpy(_curCategoryName,categoryName);
    ProcessItemNameList();
  }
}
void DDLParser::ProcessItemNameList(void)
/* ----------------------------------------------------------------------
   Purpose: DDLParser::ProcessItemNameList(void)

            Registers the item keyword for the the current item in the
            current category.  Maintains an index array of "valid" keyword
            names in fieldList[].  This array is used to indirectly
            reference between keywords and values ...
 * ----------------------------------------------------------------------*/
{

  char keywordName[MAX_CIF_NAME_LENGTH], categoryName[MAX_CIF_NAME_LENGTH];

#if DEBUG
  if (_verbose) log << "Processing item name list at line " <<  NDBlineNo << " keyword " <<  _tBufKeyword << endl;
#endif

  if (! CifFileObjBase::GetCategoryNameFromItemName(categoryName, _tBufKeyword) ) {
    log << "Error in category name at line " << NDBlineNo << " value " << _tBufKeyword << endl;
    errorLog += "Error in category name at line ";
    errorLog += NDBlineNo;
    errorLog += " value ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
  }

  if ( _curItemNo > _fieldListAlloc - 1) {
    _fieldListAlloc = _curItemNo + _fieldListAlloc;
    _fieldList = (int *) realloc(_fieldList, (_fieldListAlloc) * sizeof(int));
    _colIndex = (int *) realloc(_colIndex, (_fieldListAlloc) * sizeof(int));
  }

  ReVarCifArray<CifString> colNames, target;
  int errCode;
  colNames.Add("dbName");
  colNames.Add("type");
  colNames.Add("catName");
  target.Add(_curDataBlockName);
  if (isSave == 0) {
    target.Add("data");
  }
  if (isSave == 1) {
    target.Add("category");
  }
  if (isSave == 2) {
    target.Add("item");
  }
  target.Add(categoryName);
  format->FindFirst(target,colNames,errCode);
  if (errCode == NOT_FOUND) {
    format->AddRow();
    format->FillRow(target,format->GetNumRows()-1);
  }

    if (isSave) {
  if ( !strcmp(categoryName,_curCategoryName) && CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword)) {
#if DEBUG
    if (_verbose) log << "Line " << NDBlineNo << " keyword is " << keywordName << endl;
#endif
    _tbl->AddColumn(keywordName);
    _itemIndex++;
    _fieldList[_curItemNo] = _itemIndex;
    _colIndex[_curItemNo] = _tbl->GetColumnIndex(keywordName);//_curItemNo;
  }
  else {
    CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword);
    _itemIndex++;
    _fieldList[_curItemNo] = _itemIndex; //*****************
    _colIndex[_curItemNo] = _tbl->GetColumnIndex(keywordName);
  }
    }
    else {
      strcpy(_curCategoryName,categoryName);
  if ( !strcmp(categoryName,_curCategoryName) && CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword)) {
    if (_tbl->GetColumnIndex(keywordName) >= 0) {
      log << "Warning - Duplicate item name " <<_tBufKeyword  << " at line " << NDBlineNo << endl;
    }
/*    else*/ {
#if DEBUG
    if (_verbose) log << "Line " << NDBlineNo << " keyword is " << keywordName << endl;
#endif
    _tbl->AddColumn(keywordName);
    _itemIndex++;
    _fieldList[_curItemNo] = _itemIndex;
    _colIndex[_curItemNo] = _tbl->GetColumnIndex(keywordName);
    }
  } else {
    log << "Syntax error at line " << NDBlineNo << " at item " << _tBufKeyword << endl;
    errorLog += "*Syntax error at line ";
    errorLog += NDBlineNo;
    errorLog += " item ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
    _fieldList[_curItemNo] = -1;
  }
    }
  _curItemNo++;

}
void DDLParser::ProcessValueList(void)
/* ----------------------------------------------------------------------
     Purpose:  DDLParser::ProcessValueList(void)

               Add the current value to the appropriate column in the
               the current row.  Start a new row if necessary.
 * ----------------------------------------------------------------------*/
{
  CifString cs;
#if DEBUG
  if (_verbose) {
    if (_pBufValue)
      log << "Processing value at line " << NDBlineNo << " value [" << _pBufValue << "]" << endl;
    else
      log << "Processing value at line " << NDBlineNo << " value [" << (*_sBufValue).Text() << "]" << endl;
  }
#endif

  if (_fieldList[_curValueNo] > 0 ) {

    if (_fieldList[_curValueNo] == 1) {
      _rowBuf->Clear();
      _curRow++;
       _tbl->AddRow();
                 _tbl->FillRow(*_rowBuf,_curRow-1);
    }
    if (_pBufValue) {
      if (strlen(_pBufValue) > 0) {
                  cs.Clear(); cs.Copy(_pBufValue);
         _tbl->UpdateCell(cs,_colIndex[_curValueNo] , _curRow-1);
      } else {
         _tbl->UpdateCell(_mString,_colIndex[_curValueNo] , _curRow-1);
      }
    } else {
      if (_sBufValue->Length() > 0)
         _tbl->UpdateCell(*_sBufValue,_colIndex[_curValueNo] , _curRow-1);
     else
         _tbl->UpdateCell(_mString,_colIndex[_curValueNo] , _curRow-1);
    }
  }
  _curValueNo++;

  if (_curValueNo == _curItemNo) {
#if DEBUG
  if (_verbose) {
    log << "Loading row " << _curRow -1 << " with " <<  _rowBuf->Length() << " elements" << endl;
    for (int i=0; i < _rowBuf->Length(); i++) {
      log << "Column [" << i << "] value "<< (*_rowBuf)[i].Text() << endl;
    }
  }
#endif
    _curValueNo = 0;

  }
}
void DDLParser::ProcessItemValuePair(void)
/* ----------------------------------------------------------------------
      Purpose: ndb_cif_process_item_name_value_pair()

               Assign the current value to its associated item name.
 * ----------------------------------------------------------------------*/
{
  char categoryName[MAX_CIF_NAME_LENGTH], keywordName[MAX_CIF_NAME_LENGTH];
  CifString cs;

  _curItemNo  = 1;
  _curValueNo = 0;
#if DEBUG
  if (_verbose)  {
    if (_pBufValue)
      log << "Processing " << _tBufKeyword << " at " <<  NDBlineNo << " value " << _pBufValue  << endl;
    else
      log << "Processing " << _tBufKeyword << " at " <<  NDBlineNo << " value " << (*_sBufValue).Text()  << endl;
  }
#endif

  if (! CifFileObjBase::GetCategoryNameFromItemName(categoryName, _tBufKeyword) ) {
    log << "Error in category name at line " << NDBlineNo << " value " << _tBufKeyword << endl;
    errorLog += "Error in category name at line ";
    errorLog += NDBlineNo;
    errorLog += " value ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
    return;
  }
  ReVarCifArray<CifString> colNames, target;
  int errCode;
  colNames.Add("dbName");
  colNames.Add("type");
  colNames.Add("catName");
  target.Add(_curDataBlockName);
  if (isSave == 0) {
    target.Add("data");
  }
  if (isSave == 1) {
    target.Add("category");
  }
  if (isSave == 2) {
    target.Add("item");
  }
  target.Add(categoryName);
  format->FindFirst(target,colNames,errCode);
  if (errCode == NOT_FOUND) {
    format->AddRow();
    format->FillRow(target,format->GetNumRows()-1);
  }

  if (_fobj->IsTablePresent(categoryName)) { //  duplicates a persistent table?
    if (!isSave) {
      log << "Warning - Duplicate category name " << categoryName << " at line " << NDBlineNo << endl;
    }
  if (strcmp(categoryName,_curCategoryName)|| _afterLoop) {
    if (_tbl) { // write the current table / management of _tbl by _fobj
      if (_nTablesInBlock) {
        _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
      } else {
        _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
      }
      _nTablesInBlock++;
    }
      _tbl = _fobj->GetTablePtr(_curDataBlockName,categoryName);
      _tbl->AddRow();
      _curCol = 1;
      strcpy(_curCategoryName,categoryName);
  }
  _afterLoop=FALSE;
  if (_pBufValue) {
    CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword);
    _curCol = _tbl->GetColumnIndex(keywordName)+1;
    _curRow = _tbl->GetNumRows();
    if (_curCol < 0) {
      _tbl->AddColumn(keywordName);
      _curCol = _tbl->GetColumnIndex(keywordName)+1;
      for (int i=0; i<=_curRow; i++)
        _tbl->AddElementToColumn( _mString,i);
    }
    cs.Clear();
    if (strlen(_pBufValue) > 0) {
      cs.Copy(_pBufValue);
      _tbl->UpdateCell( cs, _curCol-1 , _curRow-1);
    } else {
      _tbl->UpdateCell( _mString, _curCol-1 , _curRow-1);
    }
  } else {
    CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword);
    _curCol = _tbl->GetColumnIndex(keywordName)+1;
    _curRow = _tbl->GetNumRows();
    if (_curCol < 0) {
      _tbl->AddColumn(keywordName);
      _curCol = _tbl->GetColumnIndex(keywordName)+1;
      for (int i=0; i<=_curRow; i++)
        _tbl->AddElementToColumn( _mString,i);
    }
    if (_sBufValue->Length() > 0) {
      _tbl->UpdateCell(*_sBufValue , _curCol-1 , _curRow-1);
    }
    else {
      _tbl->UpdateCell( _mString, _curCol-1 , _curRow-1);
    }
  }
  _curCol++;
  }
  else {
  if (strcmp(categoryName,_curCategoryName)) {
    if (_tbl) { // write the current table / management of _tbl by _fobj
      if (_nTablesInBlock) {
        _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
      } else {
        _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
      }
      _nTablesInBlock++;
    }
    _tbl = new ISTable(categoryName);
    _curCol = 0;
    strcpy(_curCategoryName,categoryName);
  }

  if ( CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword) ) {
#if DEBUG
    if (_verbose) log << "Line " << NDBlineNo << " keyword is " << keywordName << endl;
#endif
    _tbl->AddColumn(keywordName);
    _curCol++;
  }
  else {
    log << "Syntax error line " << NDBlineNo << " at item " << _tBufKeyword << endl;
    errorLog += "Syntax2 error at line ";
    errorLog += NDBlineNo;
    errorLog += " item ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
  }
  if (_pBufValue) {
    cs.Clear();
    if (strlen(_pBufValue) > 0) {
      cs.Copy(_pBufValue);
      _tbl->AddElementToColumn( cs, _curCol-1);
    } else
      _tbl->AddElementToColumn( _mString, _curCol-1);

  } else {
      if (_sBufValue->Length() > 0)
        _tbl->AddElementToColumn( *_sBufValue, _curCol-1);
      else
        _tbl->AddElementToColumn( _mString, _curCol-1);
  }
  }
}

/* These methods are the same as methods in CifParser; rules are the same,
   except these methods are applied to temporary _saveobj object to examine
   correctness of save frame
*/
//******************************

void DDLParser::ProcessLoopDeclarationSave(void)
/* ----------------------------------------------------------------------
     Purpose: DDLParser::ProcessLoopDeclarationSave(void)

              Handles initialization for a new loop, by creating a new
              category and adding the current item name to this category.
   ---------------------------------------------------------------------- */
{
  char  categoryName[MAX_CIF_NAME_LENGTH];

  _curItemNoSave = 0;  _curValueNoSave = 0;  _itemIndexSave = 0;
  if (! CifFileObjBase::GetCategoryNameFromItemName(categoryName, _tBufKeyword) ) {
    log << "Error in category name at line (save frame) " << NDBlineNo << " value " << _tBufKeyword << endl;
    errorLog += "Error in category name at line (save frame) ";
    errorLog += NDBlineNo;
    errorLog += " value ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
  }

  if (_saveobj->IsTablePresent(categoryName)) {
    log << "Duplicate category name in a save frame " << categoryName << " at line " << NDBlineNo << endl;
    errorLog += "Duplicate category name in a save frame ";
    errorLog += categoryName;
    errorLog += " at line ";
    errorLog += NDBlineNo;
    errorLog += '\n';
  } else {
    if (_savetbl) { // write the current table /
      if (_nTablesInBlockSave) {
      _saveobj->WriteTable(_savetbl,_curDataBlockNameSave,_curCategoryNameSave);
      } else {
      _saveobj->WriteTable(_savetbl,_prevDataBlockNameSave,_curCategoryNameSave); 
      }
      _nTablesInBlockSave++;
    }
    _savetbl = new ISTable(categoryName);
    _curRowSave = 0;
    strcpy(_curCategoryNameSave,categoryName);
    ProcessItemNameListSave();
  }
}

void DDLParser::ProcessItemNameListSave(void)
/* ----------------------------------------------------------------------
   Purpose: DDLParser::ProcessItemNameListSave(void)

            Registers the item keyword for the the current item in the
            current category.  Maintains an index array of "valid" keyword
            names in fieldList[].  This array is used to indirectly
            reference between keywords and values ...
 * ----------------------------------------------------------------------*/
{

  char keywordName[MAX_CIF_NAME_LENGTH], categoryName[MAX_CIF_NAME_LENGTH];

  if (! CifFileObjBase::GetCategoryNameFromItemName(categoryName, _tBufKeyword) ) {
    log << "Error in category name at line (save frame) " << NDBlineNo << " value " << _tBufKeyword << endl;
    errorLog += "Error in category name at line (save frame) ";
    errorLog += NDBlineNo;
    errorLog += " value ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
  }

  if ( _curItemNoSave > _fieldListAllocSave - 1) {
    _fieldListAllocSave = _curItemNoSave + _fieldListAllocSave;
    _fieldListSave = (int *) realloc(_fieldListSave, (_fieldListAllocSave) * sizeof(int));
  }

  if ( !strcmp(categoryName,_curCategoryNameSave) && CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword)) {
    _savetbl->AddColumn(keywordName);
    _itemIndexSave++;
    _fieldListSave[_curItemNoSave] = _itemIndexSave;
  } else {
    log << "Syntax error at line (save frame) " << NDBlineNo << " at item " << _tBufKeyword << endl;
    errorLog += "Syntax error at line (save frame) ";
    errorLog += NDBlineNo;
    errorLog += " item ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
    _fieldListSave[_curItemNoSave] = -1;
  }
  _curItemNoSave++;

}
void DDLParser::ProcessValueListSave(void)
/* ----------------------------------------------------------------------
     Purpose:  DDLParser::ProcessValueListSave(void)

               Add the current value to the appropriate column in the
               the current row.  Start a new row if necessary.
 * ----------------------------------------------------------------------*/
{
  CifString cs;

  if (_fieldListSave[_curValueNoSave] > 0 ) {

    if (_fieldListSave[_curValueNoSave] == 1) {
      _rowBufSave->Clear();
      _curRowSave++;
       _savetbl->AddRow();
                 _savetbl->FillRow(*_rowBufSave,_curRowSave-1);
    }
    if (_pBufValue) {
      if (strlen(_pBufValue) > 0) {
                  cs.Clear(); cs.Copy(_pBufValue);
         _savetbl->UpdateCell(cs,_curValueNoSave , _curRowSave-1);
      } else {
         _savetbl->UpdateCell(_mString,_curValueNoSave , _curRowSave-1);
      }
    } else {
      if (_sBufValue->Length() > 0)
         _savetbl->UpdateCell(*_sBufValue,_curValueNoSave , _curRowSave-1);
     else
         _savetbl->UpdateCell(_mString,_curValueNoSave , _curRowSave-1);
    }
  }
  _curValueNoSave++;

  if (_curValueNoSave == _curItemNoSave) {
    _curValueNoSave = 0;
  }
}





void DDLParser::ProcessItemValuePairSave(void)
/* ----------------------------------------------------------------------
      Purpose: ProcessItemValuePairSave(void)

               Assign the current value to its associated item name.
 * ----------------------------------------------------------------------*/
{
  char categoryName[MAX_CIF_NAME_LENGTH], keywordName[MAX_CIF_NAME_LENGTH];
  CifString cs;

  _curItemNoSave  = 1;
  _curValueNoSave = 0;

  if (! CifFileObjBase::GetCategoryNameFromItemName(categoryName, _tBufKeyword) ) {
    log << "Error in category name at line (save frame) " << NDBlineNo << " value " << _tBufKeyword << endl;
    errorLog += "Error in category name at line (save frame) ";
    errorLog += NDBlineNo;
    errorLog += " value ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
    return;
  }

  if (_saveobj->IsTablePresent(categoryName)) { //  duplicates a persistent table?
    log << "Duplicate category name in a save frame " << categoryName << " at line " << NDBlineNo << endl;
    errorLog += "Duplicate category name in a save frame ";
    errorLog += categoryName;
    errorLog += " at line ";
    errorLog += NDBlineNo;
    errorLog += '\n';
    return;
  }

  if (strcmp(categoryName,_curCategoryNameSave)) {

    if (_savetbl) { // write the current table
      if (_nTablesInBlockSave) {
      _saveobj->WriteTable(_savetbl,_curDataBlockNameSave,_curCategoryNameSave);
      } else {
      _saveobj->WriteTable(_savetbl,_prevDataBlockNameSave,_curCategoryNameSave); 
      }
      _nTablesInBlockSave++;
    }
    _savetbl = new ISTable(categoryName);
    _curColSave = 0;
    strcpy(_curCategoryNameSave,categoryName);
  }
  if ( CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword) ) {
    _savetbl->AddColumn(keywordName);
    _curColSave++;
  }
  else {
    log << "Syntax error line (save frame) " << NDBlineNo << " at item " << _tBufKeyword << endl;
    errorLog += "Syntax error at line (save frame) ";
    errorLog += NDBlineNo;
    errorLog += " item ";
    errorLog += _tBufKeyword;
    errorLog += '\n';
  }
  if (_pBufValue) {
    cs.Clear();
    if (strlen(_pBufValue) > 0) {
      cs.Copy(_pBufValue);
      _savetbl->AddElementToColumn( cs, _curColSave-1);
    } else
      _savetbl->AddElementToColumn( _mString, _curColSave-1);

  } else {
      if (_sBufValue->Length() > 0)
        _savetbl->AddElementToColumn( *_sBufValue, _curColSave-1);
      else
        _savetbl->AddElementToColumn( _mString, _curColSave-1);
  }

}

void DDLParser::ProcessAssignments(void)
{
  if (_curValueNo!=0)
  log <<"Number of data values is not exact multiples of the number of data names   (look above line)"<<NDBlineNo<<endl;

}

void DDLParser::ProcessOneAssignment(void)
{
/* isSave is set to 1 if the parser is in category save frame, to 2
   if parser in item save frame, and to 0 if there is no save frame
   Sytax checking is done into save frames for temporary Tbl object;
*/
  if (isSave)
    ProcessItemValuePairSave();
  ProcessItemValuePair();
}

void DDLParser::ProcessItemNameListLoop(void)
{
 if (isSave)
    ProcessLoopDeclarationSave();
  ProcessLoopDeclaration();
}

void DDLParser::ProcessItemNameListName(void)
{
  if (isSave)
    ProcessItemNameListSave();
  ProcessItemNameList();
}

void DDLParser::ProcessValueListItem(void)
{
  if (isSave)
    ProcessValueListSave();
  ProcessValueList();
}

void DDLParser::ProcessItemName(void)
{
  strcpy(_tBufKeyword,Glob_tBufKeywordSaveDDL);
}

void DDLParser::ProcessLoop(void)
{
    _curItemNo = 0;  _curValueNo = 0;  _itemIndex = 0;
    _curItemNoSave = 0;  _curValueNoSave = 0;  _itemIndexSave = 0;
}

void DDLParser::ProcessItemValue(void)
{
    _pBufValue = Glob_pBufValueDDL;
    _sBufValue = NULL;
}

void DDLParser::ProcessLsItemValue(void)
{
    _pBufValue = NULL;
    _sBufValue = Glob_sBufValueDDL;
}

void DDLParser::ProcessUnknownValue(void)
{
   _sBufValue = &_uString;
   _pBufValue = NULL;
}

void DDLParser::ProcessMissingValue(void)
{
   _sBufValue = &_mString;
   _pBufValue = NULL;
}

void DDLParser::ProcessSaveBegin(void)
{
/* making a new Tbl object for save frame; save frame has same meaning
   as datablock in cif data file; this is actually only for save frame
   syntax checking and this information is temporary just for that chacking
*/
  _nTablesInBlockSave=1;
  _saveobj = new TblFileObj((char*)("./test/tmp"), WRITE_MODE);
  _saveobj->SetDataBlock(&(Glob_dataBlockNameDDL)[5]);
  strcpy(_prevDataBlockNameSave,_curDataBlockNameSave);
  strcpy(_curDataBlockNameSave,&(Glob_dataBlockNameDDL)[5]);
  _savetbl=NULL;
  _pBufValue = Glob_dataBlockNameDDL;
}

void DDLParser::ProcessSaveEnd(void)
{
  delete _saveobj;
  delete _savetbl;
 _pBufValue = Glob_pBufValueDDL;
}

void DDLParser::ProcessDataBlockName(void)
{
  _numDataBlocks++;
  _nTablesInBlock=0;
  if (&(Glob_dataBlockNameDDL)[5] && (strlen(&(Glob_dataBlockNameDDL)[5])>0)) {
    _fobj->SetDataBlock(&(Glob_dataBlockNameDDL)[5]);
    strcpy(_curDataBlockName,&(Glob_dataBlockNameDDL)[5]);
  } else {
    char bCount[10];
    sprintf(bCount,"%d",_numDataBlocks);
    strcpy(_curDataBlockName,"UNNAMED-");
    strcat(_curDataBlockName,bCount);
  }
  if (strcmp(_prevDataBlockName,"MISSING") == 0)   strcpy(_prevDataBlockName,_curDataBlockName);

  if (_tbl && (_curItemNo > 0)) { // write the current table / management of _tbl by _fobj
      _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
      _nTablesInBlock++;
      _tbl=NULL;
  }
  if (strcmp(_prevDataBlockName,_curDataBlockName)!=0)
         strcpy(_curCategoryName,"");

#if DEBUG
  if (_verbose) log << " Previous data block is now  " <<  _prevDataBlockName << endl;
  if (_verbose) log << " Current  data block is now  " <<  _curDataBlockName << endl;
#endif
}

void ProcessAssignmentsFromDDLParser()
{
    DDLParserP->ProcessAssignments();
}

void ProcessOneAssignmentFromDDLParser()
{
    DDLParserP->ProcessOneAssignment();
}

void ProcessItemNameListLoopFromDDLParser()
{
    DDLParserP->ProcessItemNameListLoop();
}

void ProcessItemNameListNameFromDDLParser()
{
    DDLParserP->ProcessItemNameListName();
}

void ProcessValueListItemFromDDLParser()
{
    DDLParserP->ProcessValueListItem();
}

void ProcessItemNameFromDDLParser()
{
    DDLParserP->ProcessItemName();
}

void ProcessLoopFromDDLParser()
{
    DDLParserP->ProcessLoop();
}

void ProcessItemValueFromDDLParser()
{
    DDLParserP->ProcessItemValue();
}

void ProcessLsItemValueFromDDLParser()
{
    DDLParserP->ProcessLsItemValue();
}

void ProcessUnknownValueFromDDLParser()
{
    DDLParserP->ProcessUnknownValue();
}

void ProcessMissingValueFromDDLParser()
{
    DDLParserP->ProcessMissingValue();
}

void ProcessSaveBeginFromDDLParser()
{
    DDLParserP->ProcessSaveBegin();
}

void ProcessSaveEndFromDDLParser()
{
    DDLParserP->ProcessSaveEnd();
}

void ProcessDataBlockNameFromDDLParser()
{
    DDLParserP->ProcessDataBlockName();
}

