/*
FILE:     ndb_cifparse.h
*/
/*
VERSION:  3.101
*/
/*
DATE:     8/27/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:   Header file for cifparselib.a, the collection of routines 
             provides a simple interface to the DDL 2.1 compliant CIF files.
*/


#ifndef __NDB_CIFPARSE_H__
#define __NDB_CIFPARSE_H__
#ifdef __cplusplus
extern "C" {
#endif

/*
 *                     Prototypes ... 
 */

#include <strings.h>
#include <string.h>
#include "ndb_misclib.h"
#include "btree.h"

/*
 *   MxNameLen applies to block, category, item and keyword names...
 *   
 */
#define MxNameLen    		 80 
/*
 *   This is the size of the buffer length allocated to individual
 *   values.
 */

#define CifDefaultSpace           2

#undef   YYLMAX
#define  YYLMAX       256

#if 0
#undef   YYMAXDEPTH  
#define  YYMAXDEPTH   10000
#undef   YYINITDEPTH 
#define  YYINITDEPTH   1000
#endif

#define  YYPRINT(file, type, value) yyprint(file, type, value)
#define  STRCMP(str1, str2)         strcasecmp(str1,str2)
#define  STRCMPV(str1, str2)         strcmp(str1,str2)


#define TRUE                   1
#define FALSE                  0

typedef struct NdbCifRowFormat {
  char **columns;
} NdbCifRowFormat;

typedef struct NdbCifCategoryFormat {
  int numCol;
  int allCol;
  int curCol;
  int allRow;
  int numRow;
  int curRow;
  char categoryName[MxNameLen];
  char **colNames;
  NdbCifRowFormat *rows;
} NdbCifCategoryFormat;

typedef struct NdbCifDatablockFormat {
  int numCategory; /* Number of categories in this datablock */
  int allCategory; /* Allocated space */
  int curCategory; /* index of the current category */
  char datablockName[MxNameLen]; 
  NdbCifCategoryFormat *categories;
} NdbCifDatablockFormat;  

typedef struct CifHandle {
  int numDatablock; /* Number of datablocks in this structure */
  int curDatablock; /* index of the current datablock */
  int allDatablock; /* Allocated space */
  NdbCifDatablockFormat *datablocks;
} CifHandle;



/**PROTO*******************************************************************
 *       Prototypes for file:  ndb_cifparse_iolib.c
 */
int ndb_cif_read_file(CifHandle *cf,FILE *fp);
int ndb_cif_write_file(CifHandle *cf,FILE *fp);

int ndb_cif_read_ddl(CifHandle *cf,FILE *fp);
int ndb_cif_read_ddl_with_format(CifHandle *cf,FILE *fp,CifHandle *format);
int ndb_cif_write_ddl_by_format(CifHandle *cf,FILE *fp,CifHandle*format);

int ndb_cif_read_dic(CifHandle *cf,FILE *fp,CifHandle *ddlin,FILE *fp_ddl);
int ndb_cif_read_dic_with_format(CifHandle *cf,FILE *fp_cf,CifHandle *ddlin,FILE *fp_ddl,CifHandle *format);
int ndb_cif_write_dic_by_format(CifHandle *cf,FILE *fp,CifHandle *ddl,CifHandle*format,int implicit);

/**PROTO*******************************************************************
 *       Prototypes for file:  ndb_cifparse_util.c
 */
int ndb_cif_init(CifHandle *cf);
int ndb_cif_close(CifHandle *cf);
int ndb_cif_new_datablock(CifHandle *cf,const char *datablockName);
int ndb_cif_put_datablock_name(CifHandle *cf,const char *datablockName);
int ndb_cif_new_category(CifHandle *cf,const char *categoryName);
int ndb_cif_new_row(CifHandle *cf);
int ndb_cif_insert_new_row(CifHandle *cf,const int rowNo);
int ndb_cif_rewind_file(CifHandle *cf);
int ndb_cif_rewind_datablock(CifHandle *cf);
int ndb_cif_rewind_category(CifHandle *cf);
int ndb_cif_rewind_row(CifHandle *cf);
int ndb_cif_rewind_column(CifHandle *cf);
int ndb_cif_reset_datablocks(CifHandle *cf);
int ndb_cif_reset_datablock(CifHandle *cf);
int ndb_cif_reset_datablock_by_id(CifHandle *cf,const int datablockId);
int ndb_cif_reset_category(CifHandle *cf);
int ndb_cif_reset_category_by_id(CifHandle *cf,const int datablockId, const int categoryId);
int ndb_cif_remove_datablock(CifHandle *cf);
int ndb_cif_remove_datablock_by_id(CifHandle *cf,const int datablockId);
int ndb_cif_remove_datablock_by_name(CifHandle *cf,const char *datablockName);
int ndb_cif_remove_category(CifHandle *cf);
int ndb_cif_remove_category_by_id(CifHandle *cf,const int datablockId, const int categoryId);
int ndb_cif_remove_category_by_name(CifHandle *cf,const char *datablockName, const char *categoryName);
int ndb_cif_remove_row(CifHandle *cf);
int ndb_cif_remove_row_by_id(CifHandle *cf,const int datablockId, const int categoryId, const int rowId);
int ndb_cif_next_datablock(CifHandle *cf);
int ndb_cif_next_category(CifHandle *cf);
int ndb_cif_next_row(CifHandle *cf);
int ndb_cif_next_column(CifHandle *cf);
int ndb_cif_move_datablock(CifHandle *cf,const char *datablockName);
int ndb_cif_move_datablock_by_id(CifHandle *cf,const int datablockId);
int ndb_cif_move_category_by_name(CifHandle *cf,const char *datablockName, const char *categoryName);
int ndb_cif_move_category_by_id(CifHandle *cf,const int datablockId, const int categoryId);
int ndb_cif_move_category(CifHandle *cf,const int categoryId);
int ndb_cif_move_row_by_name(CifHandle *cf,const char *datablockName, const char *categoryName, const int rowId);
int ndb_cif_move_row_by_id(CifHandle *cf,const int datablockId, const int categoryId, const int rowId);
int ndb_cif_move_row(CifHandle *cf,const int rowId);
int ndb_cif_count_datablock(CifHandle *cf);
int ndb_cif_count_category(CifHandle *cf);
int ndb_cif_count_row(CifHandle *cf);
int ndb_cif_count_column(CifHandle *cf);
int ndb_cif_current_datablock(CifHandle *cf);
int ndb_cif_current_datablock_name(CifHandle *cf,char *datablockName);
int ndb_cif_current_category(CifHandle *cf);
int ndb_cif_current_category_name(CifHandle *cf,char *categoryName);
int ndb_cif_current_row(CifHandle *cf);
int ndb_cif_current_col(CifHandle *cf);
int ndb_cif_put_item_keyword(CifHandle *cf,const char *itemKeyword);
int ndb_cif_remove_item_keyword(CifHandle *cf,const char *itemKeyword);
int ndb_cif_get_item_shname(CifHandle *cf,const int colId, char *itemName);
int ndb_cif_get_item_name(CifHandle *cf,const int colId, char *itemName);
int ndb_cif_output_item(CifHandle *cf,FILE *fp, const int colId);
char *ndb_cif_copy_item_value(CifHandle *cf,const int colId);
int ndb_cif_get_item_value(CifHandle *cf,const int colId, char *fieldValue, const int maxFieldLen);
int ndb_cif_get_item_value_length(CifHandle *cf,const int colId);
int ndb_cif_item_value_strncmp(CifHandle *cf,const int catId, const int colId, 
			       const int rowId, const int startPos,
			       char *fieldValue, const int maxFieldLen);
int ndb_cif_item_values_strncmp(CifHandle *cf,const int catId1, const int colId1, 
				const int rowId1, const int startPos1, 
				const int catId2, const int colId2,  
				const int rowId2,  const int startPos2,
				const int maxFieldLen);
/*
   The following four functions return the row numbers where the given 
   strings are found (and sets the current row to this one), else return 0.
 */
int ndb_cif_item_row_1_key(CifHandle *cf,const int colId, char *fieldValue);
int ndb_cif_item_row_2_keys(CifHandle *cf,const int colId1, const char *fieldValue1,  
				    const int colId2, const char *fieldValue2);
int ndb_cif_item_row_3_keys(CifHandle *cf,const int colId1, const char *fieldValue1, 
			    const int colId2, const char *fieldValue2,
			    const int colId3, const char *fieldValue3);
int ndb_cif_item_row_4_keys(CifHandle *cf,const int colId1, const char *fieldValue1, 
			    const int colId2, const char *fieldValue2,
			    const int colId3, const char *fieldValue3,
			    const int colId4, const char *fieldValue4);
int ndb_cif_put_item_value(CifHandle *cf,const int colId, const char *fieldValue);
int ndb_cif_get_category_name_from_item_name(CifHandle *cf,char *categoryName, const char *itemName);
int ndb_cif_get_item_keyword_from_item_name(CifHandle *cf,char *itemKeyword, const char *itemName);
int ndb_cif_get_datablock_id(CifHandle *cf,const char *datablockName);
int ndb_cif_get_category_id(CifHandle *cf,const char *datablockName, const char *categoryName);
int ndb_cif_get_column_id(CifHandle *cf,const char *datablockName, const char *categoryName,
			  const char *itemKeyword);
void ndb_cif_print_datablock(CifHandle *cf,FILE *fp);
void ndb_cif_pretty_print_datablock(CifHandle *cf,FILE *fp);
void ndb_cif_print_datablocks(CifHandle *cf,FILE *fp);
void ndb_cif_write_category(CifHandle *cf,FILE *fp);
int ndb_cif_write_file_pr(CifHandle *cf,FILE *fp);
int ndb_cif_write_file_pr1(CifHandle *cf,FILE *fp);
int ndb_cif_compare(CifHandle cif1,char*name1,CifHandle cif2,char*name2,FILE*msg);
void ndb_cif_compress_file(CifHandle *cif,CifHandle *ddl);
void ndb_cif_compress_datablock(CifHandle *cif,CifHandle *ddl);
void ndb_cif_compress_category(CifHandle *cif,CifHandle *ddl);
void ndb_cif_encapsulate(char **a);
void ndb_cif_de_encapsulate(char ** a);

/**************************************************************************
*   The following functions are only local functions
*/
void ndb_cif_print_item_value(FILE *fp, const char *itemValue, int *linePos);
int ndb_cif_print_item_value_tab(FILE *fp, const char *itemValue, int *linePos);
void ndb_cif_print_item_name(FILE *fp, const char *itemName, int *linePos);
void ndb_cif_print_formatted_item_value(FILE *outfile, int *offset, 
					char *value, int newline_end,
					int fieldLength);
char ndb_cif_set_null_char(char new_null);
int ndb_cif_print_item_value_pr(FILE *fp, const char *itemName, int *linePos);

int ndb_cif_item_row_1_keycase(CifHandle *cf,const int colId, char *fieldValue);

void ndb_cif_build_category_index(CifHandle * Cif,tree * BTree,char itemName[MxNameLen],int csn);
int ndb_cif_put_item_value_index(CifHandle *cf,const int colId, char *fieldValue,tree *btree,int csn);
int ndb_cif_go_first(CifHandle *cf,tree* btree);
int ndb_cif_remove_row_index(CifHandle *cf,tree * btree);
int ndb_cif_next_row_index(CifHandle *cf,tree *btree);
int ndb_cif_prev_row_index(CifHandle *cf,tree *btree);
int ndb_cif_item_row_1_key_index(CifHandle *cf,tree *btree, char *fieldValue, int csn);
void ndb_cif_print_category_index(CifHandle *cf, tree *btree,FILE *fp);

#ifdef __cplusplus
}
#endif

#endif
