/* ---------------------------------------------------- */
/* Header-file DBSYS 1.0, (C) Wabnig Harald, 15.12.2002 */
/* ---------------------------------------------------- */

#ifndef DBSYS_INCLUDE
#define DBSYS_INCLUDE

#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <msglog.h>
#include "mymemory.h"

#include "dbarray.h"
#include "dbcache.h"
#include "dbtypes.h"
#include "dbio.h"
#include "dbapi.h"

#define MAXNAME		10000

/* ---------------------------------- */

extern ATIME _Def_Time;
extern ATIME _Max_Time;
extern ADATE _Def_Date;
extern ADATE _Max_Date;
extern ADATI _Def_Dati;
extern ADATI _Max_Dati;
extern char _Def_Binary[sizeof(short)];

/* ---------------------------------- */

extern void Make_Surrogat(TFSurrogat *surr);
extern void Get_Timestamp(TFTimestamp *timestamp);
/* accurate system time */
extern void Get_SysTimestamp(TFTimestamp *timestamp);

/* ---------------------------------- */

/* returns TRUE, if search_strg (incl. '*') is contained in strg */
extern int match_search_strg(char * strg, char * search_strg);

#define MAX_VALLEN	10000
#define MAX_VALSTR	10000

extern int Hold_ValueStrings();
extern int Release_ValueStrings();
extern char * Make_ValueString(char *strg, int size);
extern void Set_DigitsAfterComma(int comma);

extern char * Make_DbValueString(char *strg, int size);

extern char * Make_String(char * strg);
extern char * Make_BinaryString(char * strg, int size);
extern char * Make_TimeString(ATIME * time);
extern char * Make_DateString(ADATE * dat);
extern char * Make_DatiString(ADATI * dati);
extern char * Make_FETimeString(ATIME * time);
extern char * Make_FEDateString(ADATE * dat);
extern char * Make_FETimestampString(ADATI * dati);
extern char * Make_CharString(char ch);
extern char * Make_LogicalString(char ch);
extern char * Make_AmountString(double btrg);
extern char * Make_ShortString(short int sval);
extern char * Make_LongString(long int lval);
extern char * Make_DoubleString(double dval);
extern char * Make_DoubleIntString(double dval);
extern char * Make_Reduce_String(char * strg);
extern char * Make_Reduce_NumString(char * strg);
extern char * Make_Reduce_HTML_String(char * strg);
extern char * Make_Reduce_HTML_NumString(char * strg);
extern char * Make_Reduce_HTML_Leading_Zeros(char * strg);
extern char * Make_HTML_DateString(ADATE * dat);
extern char * Make_HTML_TimestampString(ADATI * dati);

extern char Make_CharValue(char ch);
extern void Free_ValueStrings();

extern char * Fillup_String(char *strg, int size);
extern char * Reduce_String(char * strg);
extern char * Reduce_NumString(char * strg);
extern char * Reduce_Leading_Zeros(char * strg);
extern char * Reduce_HTML_String(char * strg);
extern char * Reduce_HTML_NumString(char * strg);
extern char * Reduce_HTML_Leading_Zeros(char * strg);

/* ----------------- */

/* replaces '*' by '%' */
extern void Make_SearchString(char * strg);
/* replaces '*' by '%' and adds a '%' at the end of the string
   if strg-size allows */
extern void Make_SearchString_Size(char * strg, int size);

extern int Is_EmptyString(const char * strg);

/* ----------------- */

/* Backend-string-compare for CMP-functions; returns TRUE / FALSE */
extern int my_strcmp(const char * s1, const char * s2);
extern void my_strncpy(char * s1, char * s2, int n);

/* Backend-string-compare for XStrcmp ("   " = "") */
extern int xstrcmp(char * str1, char * str2);

extern int my_chkstrg(char * strg, int size);
extern int my_chkshort(int * a);

/* functions for binary-datatypes */
extern void bin_strinit(char * s1, int size);
extern void bin_strcpy(char * s1, char * s2);
extern int bin_strcmp(const char * s1, const char * s2);
extern int bin_cmp(const char * s1, const char * s2);

/* ----------------- */

extern int Matching_String(char * line, char * str1);
extern int Find_String(char * line, char * str1);
extern int Plain_String_Replace(char * line, char * str1, char * str2);
extern int String_Replace(char * line, char * str1, char * str2);

/* ---------------------------------- */

extern void ARGV_GetDB_Name(char ** argv, int argc, char * dbname);
extern const char * Get_DBname(DBHANDLE * pDBHANDLE);
extern int Is_DBSQL();

extern void DB_Print_Error_String(void);

extern void DB_Set_Error_Log(void);
extern void DB_Set_Error_String(void);

extern int DBHANDLE_Init(DBHANDLE **pDBHANDLE, char * dbname);
extern void DBHANDLE_Free(DBHANDLE *pDBHANDLE);
extern void DB_Reopen(DBHANDLE *pDBHANDLE);

extern int DB_Exists_Table(DBHANDLE *pDBHANDLE, char * tablename);
extern int DB_Create_Table(DBHANDLE *pDBHANDLE, char * sql);
extern int DB_Drop_Table(DBHANDLE *pDBHANDLE, char * tablename);
extern int DB_Exists_Index(DBHANDLE *pDBHANDLE, char * indexname);
extern int DB_Create_Index(DBHANDLE *pDBHANDLE, char * sql);
extern int DB_Drop_Index(DBHANDLE *pDBHANDLE, char * indexname);

extern int DB_Begin(DBHANDLE *pDBHANDLE);
extern int DB_Commit(DBHANDLE *pDBHANDLE);
extern int DB_RollBack(DBHANDLE *pDBHANDLE);

extern int DB_Exec_SQL(DBHANDLE *pDBHANDLE, char * sql, int * cnt, int print);

extern void DATA_Assign(void *lptr, int ltyp, void *rptr, int rtyp);
extern int DATA_Compare(void *lptr, int ltyp, void *rptr, int rtyp);

extern void STRUCT_Assign(void *lptr, void *rptr);

typedef struct {
  int _iSize;
  int iDB;
  int _iTablenr;
  PFNCMP _pfnCmpPk;
  PFNCMP _pfnCmpLk;
} _TABLEHDR;

extern int TBL_Id (void * ptable);

/* ----------------- */

extern void DATA_GetNumber(DBHANDLE * pDBHANDLE, long * num, int len);
extern void DATA_GetSurrogat (DBHANDLE * pDBHANDLE, void * ptr, void * fszId);

/* compares arg1 and arg2 using arg3 accuracy */
extern int DATA_CmpDouble (double, double, double);
/* rounds arg1 by base arg2 using the given rounding convention */
extern void DATA_RndDouble (double *, double, RndType);

/* ----------------- */

/* sets the virtual date */
extern void DATE_Set(void *);
/* inactivates the virtual date */
extern void DATE_Clear();

/* sets the virtual time */
extern void TIME_Set(void *);
/* inactivates the virtual time */
extern void TIME_Clear();

/* sets the virtual date and time */
extern void Set_AktDateTime(void *, void *);
/* sets the virtual timestamp */
extern void Set_AktTimestamp(void *);

/* ----------------- */

extern void TIME_Now(void *);
extern void TIME_Copy(void *, void *);
extern int TIME_IsValid(void *);
extern int TIME_IsValidString(void *);
extern int TIME_Compare(void *, void *);
extern int TIME_FromString(void *, char *);

/* ----------------- */

extern void DATE_Now(void *);
extern void DATE_Copy(void *, void *);
extern int DATE_IsDefault(void *);
extern int DATE_IsValid(void *);
extern int DATE_Compare(void *, void *);
extern int DATE_ToString(void *, char *, DateType);
extern int DATE_FromString(void *, char *, DateType);
/* actual number of the day in the year */
extern int DATE_DayNumber(void *);
/* add given number of days to the given date */
extern void DATE_AddDays(void *, int);
/* add given number of months to the given date */
extern void DATE_AddMonths(void *, int);
extern int DATE_MonthDays(void *);
extern void DATE_GetUltimo(void *);
extern int DATE_LeapYear(void *);
extern int DATE_AddMonths_Ultimo (ADATE *, short);
/* number of leap days within the given time period */
extern int DATE_Diff_LeapDays (ADATE *, ADATE *);
extern int DATE_Diff(void *, void *);
extern int DATE_Diff_Mode(void * dat1, void * dat2, ModeType mode);
extern char * DATE_GetWeekdayStr(int day);
extern int DATE_GetWeekday(ADATE * dat);

/* ----------------- */

extern void DATI_Now(void *);
extern int DATI_IsValid(void *);
extern int DATI_IsValidString(void *);
extern int DATI_Compare(void *, void *);
extern int DATI_FromString(void *, char *);
extern int DATI_ToString(void *, char *, DatiType);
extern int DATI_Diff(void *, void *, double *);

/* ----------------- */

#define MAX_TBL_ATTRIBS		500
#define MIN_MEM_SIZE		100000
#define MORE_MEM_SIZE		1000000

extern int TABLE_File_Save(DBHANDLE * pDBHANDLE, FILE * ofp,
                           char * pDefinition, char * pTable,
                           void * TABLE_Select, void * TABLE_Fetch);

#define _COMMIT_CNT	20

extern int TABLE_File_Load(DBHANDLE * pDBHANDLE, FILE * ifp, char * pDefinition,
                           void * TABLE_Init, void * TABLE_Insert);

extern int TABLE_File_Skip(FILE * ifp, char * pDefinition);

extern int Get_TABLE_Attrib_Type(char * pDefinition, int idx, int * size);
extern int Set_TABLE_Attrib_Value(char * pDefinition, int idx,
                                  char * info, char * data);
extern char * Get_TABLE_Attrib_Value(char * pDefinition, int idx, char * info);

/* ----------------- */

#define COMPR_FmtBin	1
#define COMPR_FmtComp	2

/* initial size of internal COMPR-buffer */
#define CPROW_MIN  10000
/* size increment of COMPR-buffer if needed */
#define CPROW_MORE 100000

#define COMPR_MOREMEM     MAXSTRG

#define COMP_WRITE_BUFFER_MIN     10000
#define COMP_WRITE_BUFFER_MORE    100000

/* maximum length of compr-data-item */
#define COMPR_MAX_BUFFER	2000000

/* cprow_min/cprow_more = -1 uses default values CPROW_MIN/CPROW_MORE 
   cprow_write_min/cprow_write_more uses default COMP_WRITE_BUFFER_MIN/MORE */ 
extern void   COMPR_Init       (COMPR * compr, int size, 
                                int cprow_min, int cprow_more,
                                int cprow_write_min, int cprow_write_more);
extern void   COMPR_fnInit     (COMPR * compr, void * fnInit);
extern void   COMPR_FmtBinFix  (COMPR * compr);
extern void   COMPR_FmtCompVar (COMPR * compr); 

extern void   COMPR_Free       (COMPR * compr);
extern void   COMPR_Reset      (COMPR * compr);
extern void   COMPR_AddBin     (COMPR * compr, void * abin);
extern int    COMPR_GetBin     (COMPR * compr, void * abin);
extern void * COMPR_GetBinPtr  (COMPR * compr);
extern int    COMPR_Write      (COMPR * compr, void * abin);
extern int    COMPR_Read       (COMPR * compr, void * abin);
extern short  COMPR_GetId      (const COMPR * compr);
/* Set_Length ... redefines length of data-items */
extern void   COMPR_Set_Length (COMPR * compr, int size);
/* SkipRead ... skips the next data-item */
extern int    COMPR_SkipRead   (COMPR * compr);

/* ---------- */

/* add data to a string a buffer; first stack memory (data) is used if available
   i.e. max != -1; then dynamic data is used (pdata); pmax defines size of dynamic data;
   if more memory is required data_min/more defines the allocated size of the memory;
   malloc/realloc-functions can be given (if they are not given malloc/realloc are used) */
extern int DATA_Add(int size, char * bdata, int bsize, 
                    char * data, int max, char ** pdata, int * pmax,
                    int data_min, int data_more,
                    void * malloc_func(), void * realloc_func());

#endif

