/* Bbuuzzb main header file. Include only in binaries/executables
   that have the Bbuuzzb engine linked in.
   Rick Smereka, Copyright (C) 1995-2004.

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

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

   You should have received a copy of the GNU General Public License
   along with this program; if not, get a copy via the Internet at
   http://gnu.org/copyleft/gpl.html or write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   MA 02111-1307 USA

   You can contact the author via email at rsmereka@future-lab.com

   Original DOS version Dec/95, Rick Smereka

   Added table buffer size. Dec/96, Rick Smereka

   Modified for dynamic size of table name, alias (new)
   and record contents. Also added new functions
   'dbeng_is_active_rec' and 'dbeng_rec_resize'.
   Jan/97, Rick Smereka

   Ported to QNX V4.23a. Jan/97, Rick Smereka

   Added 'enforce_change_rec_flag' to 'dbeng_table' structure, removed
   'alias' and added 'tid' to table structure. Oct/97, Rick Smereka

   Added temporary path and 'is_table_locked' data item in 'dbeng_table'.
   Dec/97, Rick Smereka

   Added detection of C++ compiler with instructions not to
   mangle the function names. Also added new function
   'dbeng_set_pos'. Nov/98, Rick Smereka

   Ported to Windows 32bit under Code Warrior V4. Dec/98, Rick Smereka

   Ported to HP-UX under GNU C 2.8.1.
   Jan/99, Rick Smereka

   Removed defines for the temporary path and error log as
   these are managed by the configuration file management
   module ('dbengcfg.c'). Added prototypes
   'dbeng_get_tmp_path' and 'dbeng_get_error_log'.
   Added define 'DBENG_PATH_LIMIT'.
   Feb/99, Rick Smereka

   Ported to Red Hat Linux 5.2, Jul/99, Rick Smereka

   Added prototype 'dbeng_open_table_list'. Jun/2001,
   Rick Smereka

   Added new prototypes 'dbeng_ll_open' and 'dbeng_ll_close'.
   Oct/2001, Rick Smereka

   Added member 'current_position' to 'dbeng_table' structure.
   Nov/2001, Rick Smereka

   Changed defines 'DBENG_VM' to 'DBENG_SFM' and 'DBENG_SVM' to
   'DBENG_SSFM'. Added prototype of 'dbeng_nsubfields',
   'dbeng_subfield_size', 'dbeng_get_subfield',
   'dbeng_put_subfield', 'dbeng_nsubsubfields',
   'dbeng_subsubfield_size', 'dbeng_get_subsubfield'
   and 'dbeng_put_subsubfield'. Dec/2001, Rick Smereka

   Added prototype 'dbeng_record_header' and added new
   'dbeng_table' member 'write_mode'. Added new structure
   definition 'dbeng_rep' and define 'DBENG_ROT_NULL'. Added
   'dbeng_rep' pointer called 'rep_list' to 'dbeng_table'.
   Replication items and members are present only in
   'MULTIUSER' mode (database server). Jan/2002,
   Rick Smereka

   Added member 'is_systable' to 'dbeng_table' structure and
   added prototypes 'dbeng_open_systable', 'dbeng_new_systable'
   and 'dbeng_is_systable'. Deleted 'dbeng_table' member
   'write_mode'. Feb/2002, Rick Smereka

   Removed prototype 'dbeng_record_header' as this function is now
   private. Apr/2002, Rick Smereka

   Added prototype 'dbeng_exist'. May/2002, Rick Smereka

   Added prototype 'dbeng_clear_table'. Jun/2002, Rick Smereka

   Added an additional parameter to the prototype
   'dbeng_open_table_list'. Aug/2002, Rick Smereka

   Added prototype 'dbeng_tmp_systable'. Sep/2002, Rick Smereka

   Added members 'sort_max_open_bin' and 'sort_max_mem' to the
   'dbeng_table' structure. Nov/2002, Rick Smereka

   Added function prototypes 'dbeng_delete_field',
   'dbeng_delete_subfield' and 'dbeng_delete_subsubfield'. 
   Mar/2003, Rick Smereka

   Changed 'record_count' in 'dbeng_table' to 'active_record_count'
   and added 'deleted_record_count'. Added additional parameter to
   functions 'dbeng_count_rec' and 'dbeng_rec_count'. Dec/2003, Rick Smereka

   Added prototype 'dbeng_set_record_count'. Feb/2004, Rick Smereka

   Added 'autopack' member to 'dbeng_table'. Added define
   'DBENG_SYSTABLE_MAX_DELETED'. Added prototypes
   'dbeng_autopack' and 'dbeng_set_autopack'.
   Mar/2004, Rick Smereka */

#ifndef DBENG_HXX
#define DBENG_HXX

/* maximum size of a record or field */

#ifdef OS_DOS
#define DBENG_MAXRECFIELD 32000 /* s/b the same as in 'dbmess.h' */
#define DBENG_MAXCOMLINE 32000
#else
#define DBENG_MAXRECFIELD 64000
#define DBENG_MAXCOMLINE 64000
#endif

#define DBENG_RM 255             /* ASCII record mark */
#define DBENG_FM 254             /* ASCII field mark */
#define DBENG_SFM 253            /* ASCII sub-field mark */
#define DBENG_SSFM 252           /* ASCII sub-sub-field mark */
#define DBENG_REC_HEAD_SIZE 12   /* size (in bytes) of record header */
#define DBENG_IO_BUF_SIZE 8192   /* table I/O buffer size */
#define DBENG_OT_NULL ((struct dbeng_table *)NULL)
#define DBENG_SYSTABLE_MAX_DELETED 25

#ifdef MULTIUSER
#define DBENG_ROT_NULL ((struct dbeng_rep *)NULL)

/* database table replication structure */

struct dbeng_rep
   {
   struct dbeng_rep *next;
   int port;
   char *tname;
   };
#endif

/* database table structure */

struct dbeng_table
   {
   struct dbeng_table *next;           /* pointer to next table */
   FILE *handle;                       /* file handle */
   char *rec;                          /* current record contents */
   char *name;                         /* physical file name */
   int tid;                            /* assigned table ID */
   int change_rec_flag;                /* has current record changed? */
   int enforce_change_rec_flag;        /* enforce 'change_rec_flag'? */
   int process_deleted;                /* retreive recs marked for del? */
   int is_table_locked;                /* is the table read/write locked? */
   int is_systable;                    /* is this a system table? */
   int sort_max_open_bin;              /* max allowable open bins in sort */
   long sort_max_mem;                  /* max alloc mem for sort records */
   long record_number;                 /* current record number */
   long active_record_count;           /* current active record count */
   long deleted_record_count;          /* current deleted record count */
   long orig_position;                 /* original (start) pos of record */
   long current_position;              /* current file position */
   int record_status;                  /* 1=active,0=deleted */
   int autopack;                       /* autopack threshold (0=off) */
#ifdef MULTIUSER
   struct dbeng_rep *rep_list;         /* link list of desired replication */
#endif
   };

#ifdef __cplusplus
extern "C"
{
#endif

int dbeng_pack_table(struct dbeng_table *);
int dbeng_open_systable(char *, int *);
int dbeng_open_table(char *, int *);
int dbeng_close_table(struct dbeng_table *);
int dbeng_put_field(struct dbeng_table *, char *, int);
int dbeng_put_subfield(struct dbeng_table *, char *, int, int);
int dbeng_put_subsubfield(struct dbeng_table *, char *, int, int, int);
int dbeng_find(struct dbeng_table *, int, const char *, int *);
int dbeng_find_field(struct dbeng_table *, int, const char *, int);
int dbeng_find_part(struct dbeng_table *, int, const char *, int *);
int dbeng_find_field_part(struct dbeng_table *, int, const char *, int);
int dbeng_get_rec(struct dbeng_table *, char *);
int dbeng_get_field(struct dbeng_table *, char *, int);
int dbeng_get_subfield(struct dbeng_table *, char *, int, int);
int dbeng_get_subsubfield(struct dbeng_table *, char *, int, int, int);
int dbeng_goto_record(struct dbeng_table *, long);
int dbeng_count_rec(struct dbeng_table *, long *, long *);
int dbeng_rec_count(struct dbeng_table *, long *, long *);
int dbeng_goto_top(struct dbeng_table *);
int dbeng_rewrite_recd(struct dbeng_table *);
int dbeng_write_new_recd(struct dbeng_table *);
int dbeng_write_string_recd(struct dbeng_table *, char *);
int dbeng_write_recd(struct dbeng_table *);
int dbeng_new(struct dbeng_table *);
int dbeng_delete_recd(struct dbeng_table *);
int dbeng_get_recd(struct dbeng_table *);
int dbeng_enf_change_rec_flag(struct dbeng_table *, int *);
int dbeng_set_enf_change_rec_flag(struct dbeng_table *, int);
int dbeng_autopack(struct dbeng_table *, int *);
int dbeng_set_autopack(struct dbeng_table *, int);
int dbeng_is_table_locked(struct dbeng_table *, int *);
int dbeng_set_is_table_locked(struct dbeng_table *, int);
int dbeng_lock_table(struct dbeng_table *);
int dbeng_unlock_table(struct dbeng_table *);
int dbeng_tmp_systable(int *);
int dbeng_new_systable(char *, int *);
int dbeng_new_table(char *, int *);
int dbeng_delete_table(char *);
void dbeng_terminate(void);
int dbeng_change_rec_flag(struct dbeng_table *, int *);
int dbeng_set_change_rec_flag(struct dbeng_table *, int);
int dbeng_delete_flag(struct dbeng_table *, int *);
int dbeng_set_delete_flag(struct dbeng_table *, int);
int dbeng_max_combine_len(const char *, const char *);
int dbeng_initialize(void);
int dbeng_get_tid(void);
struct dbeng_table *dbeng_atid_lookup(int);
int dbeng_find_delete(struct dbeng_table *, const char *, const int);
int dbeng_goto_position(struct dbeng_table *, const long, const long);
int dbeng_is_active_rec(struct dbeng_table *);
int dbeng_rec_resize(struct dbeng_table *, const int);
int dbeng_rec_size(struct dbeng_table *, int *);
int dbeng_field_size(struct dbeng_table *, const int, int *);
int dbeng_subfield_size(struct dbeng_table *, int, int, int *);
int dbeng_subsubfield_size(struct dbeng_table *, int, int, int, int *);
int dbeng_rec_num(struct dbeng_table *, long *);
int dbeng_pos(struct dbeng_table *, long *);
int dbeng_set_pos(struct dbeng_table *, long);
int dbeng_nfields(struct dbeng_table *, int *);
int dbeng_nsubfields(struct dbeng_table *, int, int *);
int dbeng_nsubsubfields(struct dbeng_table *, int, int, int *);
int dbeng_delete_field(struct dbeng_table *, int);
int dbeng_delete_subfield(struct dbeng_table *, int, int);
int dbeng_delete_subsubfield(struct dbeng_table *, int, int, int);
void dbeng_ferror(char *);
int dbeng_open_table_list(char *, char *);
int dbeng_exist(char *, int *);
int dbeng_clear_table(char *);
int dbeng_copy_table(struct dbeng_table *, struct dbeng_table *);
int dbeng_ll_open(struct dbeng_table *);
int dbeng_ll_close(struct dbeng_table *);
int dbeng_is_systable(struct dbeng_table *, int *);
int dbeng_set_record_count(struct dbeng_table *);

#ifdef __cplusplus
}
#endif
#endif
