/*
FILE:     DICParserBase.C
*/
/*
VERSION:  2.201
*/
/*
DATE:     8/30/2004
*/
/*
  Comments and Questions to: sw-help@rcsb.rutgers.edu
*/
/*
COPYRIGHT 1999-2004 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 "DICScannerBase.h"
#include "DICParserInt.h"
#include "DICFileObj.h"
#include "DICParserBase.h"

/* class CifFileObj;
class CifFileObjBase;
*/

extern FILE* dicparser_in;
extern int dicparser_parse();

char Glob_tBufKeywordSaveDIC[MAX_CIF_NAME_LENGTH];
char* Glob_pBufValueDIC;
CifString* Glob_sBufValueDIC;
char* Glob_dataBlockNameDIC;

DICParser* DICParserP = NULL;

DICParser::DICParser(FILE* in, DICFileObj *fo, TblFileObj *ddl_in, ISTable *format2, int verbose)
{
    dicparser_in = in;
    _verbose=verbose;
    format=format2;
    Clear();
    errorLog.Clear();
    if (fo)
        _fobj = fo;
    else
        exit(-1);
    ddl=ddl_in;
    listcat.Clear(); listcat.Add("id");
    char * BlockName;
    BlockName = ddl->GetBlockName(0);
    cattbl=ddl->GetTablePtr(BlockName,"category");
    cattbl->SetFlags(ISTable::DT_STRING | ISTable::CASE_INSENSE,0);
    cattbl->SetFlags(ISTable::DT_STRING | ISTable::CASE_INSENSE,1);
    cattbl->SetFlags(ISTable::DT_STRING | ISTable::CASE_INSENSE,2);
    cattbl->SetFlags(ISTable::DT_STRING | ISTable::CASE_INSENSE,3);
    cattbl->CreateIndex("index0",listcat);
    catColIndex=cattbl->GetColumnIndex("id");
    listitem.Clear(); listitem.Add("category_id");
    listitem.Add("name");
    if (BlockName) delete[] BlockName;
    BlockName = ddl->GetBlockName(0);
    itemtbl=ddl->GetTablePtr(BlockName,"item");
    if (BlockName) delete[] BlockName;
    itemtbl->CreateIndex("index0",listitem);
    itemColIndex=itemtbl->GetColumnIndex("mandatory_code");
    listitem2.Clear(); listitem2.Add("category_id"); listitem2.Add("mandatory_code");
    itemtbl->CreateIndex("index2",listitem2);
                          OpenLog("DICParser.log",_verbose);

    DICParserP = this;
}

void DICParser::Parse()
{
    dicparser_parse();
}

void dicparser_error(const char *s)
{
    DICParserP->Error(s);
}

DICParser::~DICParser()
{
    Reset();
    DICParserP = NULL;
}



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

void DICParser::Reset()
{
  if (_tbl && (_curItemNo > 0)) { // write the current table / management of _tbl by _fobj
    CheckDDL();
    if (_nTablesInBlock) {
      _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
    } else {
      _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
    }
    _nTablesInBlock++;
    //    delete _tbl;
  }
  if (_rowBuf) delete _rowBuf;
  if (_fieldList) free(_fieldList);
  if (_colIndex) free(_colIndex);
  if (_rowBufSave) delete _rowBufSave;
  if (_fieldListSave) free(_fieldListSave);
  if (_prevtbl) delete _prevtbl;
  if (_tmpDataBlockNameSaveL) free(_tmpDataBlockNameSaveL);
}

void DICParser::Clear()
{
  _tmpDataBlockNameSaveL= (char*)calloc(1,sizeof(char));
  _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;
  _prevtbl=NULL;
  _fieldListAllocSave=100;
  _fieldListSave=(int *) malloc(_fieldListAllocSave*sizeof(int));
  _rowBufSave= new ReVarCifArray<CifString>;
  _uString.Copy(".");
  _mString.Copy("?");
  strcpy(_tBufKeyword,"");
  strcpy(_curCategoryName,"");
  strcpy(_curDataBlockName,"");
  strcpy(_prevDataBlockName,"");
  strcpy(_curCategoryNameSave,"");
  strcpy(_curDataBlockName,"MISSING_DIC");
  strcpy(_prevDataBlockName,"MISSING_DIC");
  // jdw xxx
  strcpy(_curDataBlockNameSave,"");
  strcpy(_tmpDataBlockNameSave,"");
  strcpy(_tmpDataBlockNameSaveL,"");
  ReVarCifArray<CifString> list;
  format->AddColumn("dbName");
  format->AddColumn("type");
  format->AddColumn("catName");
  list.Add("dbName");
  list.Add("type");
  list.Add("catName");
  format->CreateIndex("index0",list);
}


void DICParser::ProcessLoopDeclaration(void)
/* ----------------------------------------------------------------------
     Purpose: DICParser::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;
/*
      errorLog += "Duplicate category name ";
      errorLog += categoryName;
      errorLog += " at line ";
      errorLog += NDBlineNo;
      errorLog += '\n';
*/
    }
  /*  else*/ {
      if (_tbl) { // write the current table / management of _tbl by _fobj
  CheckDDL();
        if (_nTablesInBlock) {
          _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
        } else {
          _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
        }
        _nTablesInBlock++;
        //      delete _tbl;
      }
      _tbl = _fobj->GetTablePtr(_curDataBlockName,categoryName);

      if (isSave ==0) {
        if (_prevtbl) delete _prevtbl;
        _prevtbl=new ISTable();
        _prevtbl->Copy(_tbl);
      }

      _curRow = _tbl->GetNumRows();
    ProcessItemNameList();
      strcpy(_curCategoryName,categoryName);
      strcpy(_tmpDataBlockNameSave,_curDataBlockNameSave);/***********/
      if (_tmpDataBlockNameSaveL) free(_tmpDataBlockNameSaveL);
      if (isSave == 1) {
      _tmpDataBlockNameSaveL = DICFileObj::lowercase(_tmpDataBlockNameSave);
      }
      else {
        _tmpDataBlockNameSaveL= (char*)calloc(sizeof(_tmpDataBlockNameSave)+1,sizeof(char));
        strcpy(_tmpDataBlockNameSaveL,_tmpDataBlockNameSave);
      }
    }

  }
else {
    if (_tbl) { // write the current table / management of _tbl by _fobj
  CheckDDL();
      if (_nTablesInBlock) {
        _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
        } else {
          _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
        }
      _nTablesInBlock++;
      //      delete _tbl;
    }
    _tbl = new ISTable(categoryName);

    if (isSave ==0) {
      if (_prevtbl) delete _prevtbl;
      _prevtbl=new ISTable();
      _prevtbl->Copy(_tbl);
    }

    _curRow = 0;
    strcpy(_curCategoryName,categoryName);
    strcpy(_tmpDataBlockNameSave,_curDataBlockNameSave);/*************/
      if (_tmpDataBlockNameSaveL) free(_tmpDataBlockNameSaveL);
    if (isSave == 1) {
      _tmpDataBlockNameSaveL = DICFileObj::lowercase(_tmpDataBlockNameSave);
    }
    else{
      _tmpDataBlockNameSaveL= (char*)calloc(sizeof(_tmpDataBlockNameSave)+1,sizeof(char));
      strcpy(_tmpDataBlockNameSaveL,_tmpDataBlockNameSave);
    }
    ProcessItemNameList();
  }
}


void DICParser::ProcessItemNameList(void)
/* ----------------------------------------------------------------------
   Purpose: DICParser::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, target2;
  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  << " line " << NDBlineNo << endl;
/*
      errorLog += "Duplicate item name ";
      errorLog += _tBufKeyword;
      errorLog += " at line ";
      errorLog += NDBlineNo;
      errorLog += '\n';
      return;
*/
    }
 /*   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 DICParser::ProcessValueList(void)
/* ----------------------------------------------------------------------
     Purpose:  DICParser::ProcessValueList(void)

               Add the current value to the appropriate column in the
               the current row.  Start a new row if necessary.
 * ----------------------------------------------------------------------*/
{
  CifString cs;
  CifString dotString(".");
  CifString* emptyStringP = new CifString("");
#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,_curValueNo , _curRow-1);
         _tbl->UpdateCell(cs,_colIndex[_curValueNo] , _curRow-1);
      } else {
         _tbl->UpdateCell(_mString,_colIndex[_curValueNo] , _curRow-1);
      }
    } else {
      if (_sBufValue->Length() > 0)
      {
         if ((*_sBufValue) == dotString)
         {
             // If a dot is specified as a value, store an empty string
             // in the table
             _tbl->UpdateCell(*emptyStringP,_colIndex[_curValueNo] , _curRow-1);
         }
         else
         {
             // Otherwise store the original value
             _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;

  }
  delete emptyStringP;
}





void DICParser::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;
  CifString dotString(".");
  CifString* emptyStringP = new CifString("");
  _curItemNo  = 1;
  _curValueNo = 0;
//  _itemIndex  = 1;
#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';
    delete emptyStringP;
    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;
/*
      errorLog += "Duplicate category name ";
      errorLog += categoryName;
      errorLog += " at line ";
      errorLog += NDBlineNo;
      errorLog += '\n';
      delete emptyStringP;
      return;
*/
    }
   /* else*/ {
  if (strcmp(categoryName,_curCategoryName)|| _afterLoop) {
    if (_tbl) { // write the current table / management of _tbl by _fobj
  CheckDDL();
      if (_nTablesInBlock) {
        _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
      } else {
        _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
      }
      _nTablesInBlock++;
      //      delete _tbl;
    }
/*
  if (!strcmp(categoryName,_curCategoryName)&&_afterLoop) { //  duplicates a persistent table?
    log << "Duplicate category name " << categoryName << " at line " << NDBlineNo << endl;
    errorLog += "Duplicate category name ";
    errorLog += categoryName;
    errorLog += " at line ";
    errorLog += NDBlineNo;
    errorLog += '\n';
    delete emptyStringP;
    return;
  }
*/
      _tbl = _fobj->GetTablePtr(_curDataBlockName,categoryName);

      if (isSave ==0) {
        if (_prevtbl) delete _prevtbl;
        _prevtbl=new ISTable();
        _prevtbl->Copy(_tbl);
      }

      _tbl->AddRow();
      _curCol = 1;
      strcpy(_curCategoryName,categoryName);
      strcpy(_tmpDataBlockNameSave,_curDataBlockNameSave);/************/
      if (_tmpDataBlockNameSaveL) free(_tmpDataBlockNameSaveL);
      if (isSave == 1) {
      _tmpDataBlockNameSaveL = DICFileObj::lowercase(_tmpDataBlockNameSave);
      }
      else{
        _tmpDataBlockNameSaveL= (char*)calloc(sizeof(_tmpDataBlockNameSave)+1,sizeof(char));
        strcpy(_tmpDataBlockNameSaveL,_tmpDataBlockNameSave);
      }
  }
  _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->AddElementToColumn( cs, _curCol-1);
      _tbl->UpdateCell( cs, _curCol-1 , _curRow-1);
    } else {
//      _tbl->AddElementToColumn( _mString, _curCol-1);
      _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->AddElementToColumn( *_sBufValue, _curCol-1);
      _tbl->UpdateCell(*_sBufValue , _curCol-1 , _curRow-1);
    }
    else {
//      _tbl->AddElementToColumn( _mString, _curCol-1);
      _tbl->UpdateCell( _mString, _curCol-1 , _curRow-1);
    }
  }
  _curCol++;
    }
  }
  else {
  if (strcmp(categoryName,_curCategoryName)) {
    if (_tbl) { // write the current table / management of _tbl by _fobj
  CheckDDL();
      if (_nTablesInBlock) {
        _fobj->WriteTable(_tbl,_curDataBlockName,_curCategoryName);
      } else {
        _fobj->WriteTable(_tbl,_prevDataBlockName,_curCategoryName);
      }
      _nTablesInBlock++;
      //      delete _tbl;
    }
    _tbl = new ISTable(categoryName);

    if (isSave ==0) {
      if (_prevtbl) delete _prevtbl;
      _prevtbl=new ISTable();
      _prevtbl->Copy(_tbl);
    }

    _curCol = 0;
    strcpy(_curCategoryName,categoryName);
    strcpy(_tmpDataBlockNameSave,_curDataBlockNameSave);/**************/
      if (_tmpDataBlockNameSaveL) free(_tmpDataBlockNameSaveL);
    if (isSave == 1) {
      _tmpDataBlockNameSaveL = DICFileObj::lowercase(_tmpDataBlockNameSave);
    }
    else {
      _tmpDataBlockNameSaveL= (char*)calloc(sizeof(_tmpDataBlockNameSave)+1,sizeof(char));
      strcpy(_tmpDataBlockNameSaveL,_tmpDataBlockNameSave);
    }
  }

  if ( CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword) ) {
#if DEBUG
    if (_verbose) log << "Line " << NDBlineNo << " keyword is " << keywordName << endl;
#endif
    _tbl->AddColumn(keywordName);
    _curCol++;
  }
  else {
    log << "Syntax2 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)
      {
         if ((*_sBufValue) == dotString)
         {
             // If a dot is specified as a value, store an empty string
             // in the table
             _tbl->AddElementToColumn( *emptyStringP, _curCol-1);
         }
         else
         {
             // Otherwise store the original value
             _tbl->AddElementToColumn( *_sBufValue, _curCol-1);
         }
      }
      else
        _tbl->AddElementToColumn( _mString, _curCol-1);
  }
  }
  delete emptyStringP;
}


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

void DICParser::ProcessLoopDeclarationSave(void)
/* ----------------------------------------------------------------------
     Purpose: DICParser::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++;
      //      delete _savetbl;
      if (_prevtbl) delete _prevtbl;
      _prevtbl=new ISTable();
      _prevtbl->Copy(_savetbl);
    }
    _savetbl = new ISTable(categoryName);
    _curRowSave = 0;
    strcpy(_curCategoryNameSave,categoryName);
    ProcessItemNameListSave();
  }
}


void DICParser::ProcessItemNameListSave(void)
/* ----------------------------------------------------------------------
   Purpose: DICParser::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 DICParser::ProcessValueListSave(void)
/* ----------------------------------------------------------------------
     Purpose:  DICParser::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 DICParser::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;
//  _itemIndexSave  = 1;

  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++;
      //      delete _savetbl;
      if (_prevtbl) delete _prevtbl;
      _prevtbl=new ISTable();
      _prevtbl->Copy(_savetbl);
    }
    _savetbl = new ISTable(categoryName);
    _curColSave = 0;
    strcpy(_curCategoryNameSave,categoryName);
  }
  if ( CifFileObjBase::GetKeywordFromItemName(keywordName, _tBufKeyword) ) {
//    cout<<categoryName<<"   "<<_curCategoryNameSave<<"   "<<endl;
    if (_savetbl==NULL)
      _savetbl = new ISTable(categoryName);
    _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  DICParser::CheckDDL(void) {
  char categoryName[MAX_CIF_NAME_LENGTH], keywordName[MAX_CIF_NAME_LENGTH];
  ReVarCifArray<CifString> target, target2;
  int errCode;
  ReVarCifArray<CifString> *ColumnNames=NULL;
  ReVarPCifArray<int> * listOut=NULL;
  ReVarCifArray<CifString> listitem1;
  CifString itemName;
  int numCols, numRows;
  int  numCols2, numRows2;
  int implicitFound = 0;
  int i, ret;
  CifString elem1;
  CifString elem;
  ISTable * tmptbl=NULL;
  char* tmp = NULL;
  ReVarCifArray<CifString> * row;

  listitem1.Clear(); listitem1.Add("name");

  target.Add(_curCategoryName);
  listOut = cattbl->Search(target,listcat,errCode);
  if (errCode < 0){
    log << "Category " << _curCategoryName << " isn't defined; line " << NDBlineNo<< endl;
    errorLog += "Category ";
    errorLog += _curCategoryName;
    errorLog += " isn't defined; line ";
    errorLog += NDBlineNo;
    errorLog += '\n';
  }
  else {
  cattbl->GetCell(itemName,catColIndex,(*listOut)[0]);
  if(strcmp(itemName.Text(),_tbl->GetName())!=0) {
    // example: item_examples & Item_examples
    tmptbl=_tbl;
    _tbl = _fobj->GetTablePtr(_curDataBlockName,itemName.Text());
    for (int l=0; l<tmptbl->GetNumRows(); l++){
      row = tmptbl->GetRow(l);
      _tbl->AddRow();
      _tbl->FillRow(*row,_tbl->GetLastRowIndex());
      if (row) delete row;
    }
    _fobj->DeleteTable(_curDataBlockName,tmptbl->GetName());
    delete tmptbl;
    strcpy(_curCategoryName,itemName.Text());
    _prevtbl->Rename(itemName.Text());
  }

  numRows2 = _tbl->GetNumRows();
  numCols2 = _tbl->GetNumColumns();
  ColumnNames = _prevtbl->GetColumnNames();
  numCols = _prevtbl->GetNumColumns();
  numRows = _prevtbl->GetNumRows();
  for (i=0; i<numCols; i++) {
    elem1.Copy("_");
    elem1+=_prevtbl->GetName();
    elem1+=".";
    elem1+=(*ColumnNames)[i];
    target2.Clear();
    target2.Add(elem1);
    ret = itemtbl->FindFirst(target2,listitem1,errCode);
    if (errCode<0) {
      log << "Item " << elem1.Text() << " isn't defined; line " << NDBlineNo<< endl;
      errorLog += "Item ";
      errorLog += elem1.Text();
      errorLog += " isn't defined; line ";
      errorLog += NDBlineNo;
      errorLog += '\n';
    }
    itemtbl->GetCell(itemName,itemColIndex,ret);
    if (strcasecmp(itemName.Text(),"implicit")==0) {
      implicitFound=1;
    }
  }

  if (strcasecmp(_curCategoryName,"item")==0) {

    colIndex2=_prevtbl->GetColumnIndex("name");
    if (colIndex2 <0) {
      colIndex2=_tbl->GetColumnIndex("name");
      elem.Clear();
      elem.Copy(_curDataBlockNameSave);

      if (numRows == numRows2) {
        _tbl->AddColumn("name");
        colIndex2=_tbl->GetColumnIndex("name");
        for (i=0; i<numRows; i++)
          _tbl->AddElementToColumn(elem,colIndex2);
      }
      else {

        for (i=numRows; i>0; i--)
          _tbl->UpdateCell(elem,colIndex2,numRows2-i);
      }
    }
    colIndex2=_prevtbl->GetColumnIndex("category_id");
    if (colIndex2 <0) {
      colIndex2=_tbl->GetColumnIndex("category_id");
                if (isSave==2)
      CifFileObjBase::GetCategoryNameFromItemName(categoryName,_curDataBlockNameSave);
                if (isSave==1)
      CifFileObjBase::GetCategoryNameFromItemName(categoryName,_prevDataBlockNameSave);
      elem.Clear();
      elem.Copy(categoryName);

      if (numRows == numRows2) {
        _tbl->AddColumn("category_id");
        colIndex2=_tbl->GetColumnIndex("category_id");
        for (i=0; i<numRows; i++)
          _tbl->AddElementToColumn(elem,colIndex2);
    }
      else {

        for (i=numRows; i>0; i--)
          _tbl->UpdateCell(elem,colIndex2,numRows2-i);

      }
    }
  }
  else {

    if (implicitFound == 0) {
      target2.Clear();
      target2.Add(_curCategoryName);
      target2.Add("implicit");
      if (listOut) delete listOut;
      listOut = itemtbl->Search(target2,listitem2,errCode);
      if (errCode >=0) {
        colIndex2=itemtbl->GetColumnIndex("name");
        itemtbl->GetCell(itemName,colIndex2,(*listOut)[0]);
        CifFileObjBase::GetKeywordFromItemName(keywordName,itemName.Text());
        //        _tbl->AddColumn(keywordName);


        if (strcasecmp(_curCategoryName,"dictionary")==0 ||
            strcasecmp(_curCategoryName,"category")==0) {
          if (isSave == 1)
            tmp = DICFileObj::lowercase(_curDataBlockName);
          elem.Clear();
          elem.Copy(_curDataBlockName);
        }
        else {
          elem.Clear();
          elem.Copy(_tmpDataBlockNameSave);
        }

        if (numCols == numCols2) {
          _tbl->AddColumn(keywordName);
          colIndex2=_tbl->GetColumnIndex(keywordName);
          for (i=0; i<numRows; i++)
            _tbl->AddElementToColumn(elem,colIndex2);
        }
        else {

        colIndex2=_tbl->GetColumnIndex(keywordName);
        for (i=numRows; i>0; i--){
          _tbl->UpdateCell(elem,colIndex2,numRows2-i);
        }
        }
      }
    }
  }
  //*******
   /*
  target.Add("yes");
  colIndex2=itemtbl->GetColumnIndex("name");
  if (listOut) delete listOut;
  listOut = itemtbl->Search(target,listitem2,errCode);
  for (j=0; j<listOut->Length(); j++) {
    itemtbl->GetCell(itemName,colIndex2,(*listOut)[j]);
    CifFileObjBase::GetKeywordFromItemName(keywordName,itemName.Text());
    k=0;
    while (k<numCols && strcasecmp(keywordName,(*ColumnNames)[k].Text())!=0) {
      k++;
    }
    if(k==numCols) {
      log << "Mandatory column "<< itemName.Text()  <<" is missing; near line " << NDBlineNo<< endl;
      errorLog += "Mandatory column ";
      errorLog += itemName.Text();
      errorLog += " is missing; near line ";
      errorLog += NDBlineNo;
      errorLog += '\n';
    }
  }
*/
  }
  if (listOut) delete listOut;
  if (ColumnNames) delete ColumnNames;
  if (tmp) free(tmp);
}

void DICParser::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 DICParser::ProcessOneAssignment(void)
{
  if (isSave)
    ProcessItemValuePairSave();
  ProcessItemValuePair();
}

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

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

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

void DICParser::ProcessItemName(void)
{
    strcpy(_tBufKeyword,Glob_tBufKeywordSaveDIC);
}

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

void DICParser::ProcessItemValue(void)
{
   _pBufValue = Glob_pBufValueDIC;
   _sBufValue = NULL;
}

void DICParser::ProcessLsItemValue(void)
{
   _pBufValue = NULL;
   _sBufValue = Glob_sBufValueDIC;
}

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

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

void DICParser::ProcessSaveBegin(void)
{
  _nTablesInBlockSave=1;
  _saveobj = new TblFileObj((char*)("./test/pera"), WRITE_MODE);
  _saveobj->SetDataBlock(&(Glob_dataBlockNameDIC)[5]);
  strcpy(_prevDataBlockNameSave,_curDataBlockNameSave);
  strcpy(_curDataBlockNameSave,&(Glob_dataBlockNameDIC)[5]);
  _savetbl=NULL;
  _pBufValue = Glob_dataBlockNameDIC;
}

void DICParser::ProcessSaveEnd(void)
{
  if (_prevtbl) delete _prevtbl;
  _prevtbl=new ISTable();
  _prevtbl->Copy(_savetbl);
  delete _saveobj;
  delete _savetbl;
  _savetbl=NULL;
  _pBufValue = Glob_pBufValueDIC; 
}

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

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

  if (strcmp(_tmpDataBlockNameSave,_curDataBlockNameSave)!=0) {
    strcpy(_tmpDataBlockNameSave,"");
    strcpy(_tmpDataBlockNameSaveL,"");
  }

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



void ProcessAssignmentsFromDICParser()
{
    DICParserP->ProcessAssignments();
}

void ProcessOneAssignmentFromDICParser()
{
    DICParserP->ProcessOneAssignment();
}

void ProcessItemNameListLoopFromDICParser()
{
    DICParserP->ProcessItemNameListLoop();
}

void ProcessItemNameListNameFromDICParser()
{
    DICParserP->ProcessItemNameListName();
}

void ProcessValueListFromDICParser()
{
    DICParserP->ProcessValueListItem();
}

void ProcessItemNameFromDICParser()
{
    DICParserP->ProcessItemName();
}

void ProcessLoopFromDICParser()
{
    DICParserP->ProcessLoop();
}

void ProcessItemValueFromDICParser()
{
    DICParserP->ProcessItemValue();
}

void ProcessLsItemValueFromDICParser()
{
    DICParserP->ProcessLsItemValue();
}

void ProcessUnknownValueFromDICParser()
{
    DICParserP->ProcessUnknownValue();
}

void ProcessMissingValueFromDICParser()
{
    DICParserP->ProcessMissingValue();
}

void ProcessSaveBeginFromDICParser()
{
    DICParserP->ProcessSaveBegin();
}

void ProcessSaveEndFromDICParser()
{
    DICParserP->ProcessSaveEnd();
}

void ProcessDataBlockNameFromDICParser()
{
    DICParserP->ProcessDataBlockName();
}

