1 /************** FilAMTxt H Declares Source Code File (.H) **************/
2 /*  Name: FILAMTXT.H    Version 1.4                                    */
3 /*                                                                     */
4 /*  (C) Copyright to the author Olivier BERTRAND          2005-2020    */
5 /*                                                                     */
6 /*  This file contains the file access method classes declares.        */
7 /***********************************************************************/
8 
9 #ifndef __FILAMTXT_H
10 #define __FILAMTXT_H
11 
12 #include "block.h"
13 #include "array.h"
14 
15 typedef class TXTFAM *PTXF;
16 typedef class DOSFAM *PDOSFAM;
17 typedef class BLKFAM *PBLKFAM;
18 typedef class BINFAM *PBINFAM;
19 typedef class DOSDEF *PDOSDEF;
20 typedef class TDBDOS *PTDBDOS;
21 
22 /***********************************************************************/
23 /*  This is the base class for all file access method classes.         */
24 /***********************************************************************/
25 class DllExport TXTFAM : public BLOCK {
26   friend class TDBDOS;
27   friend class TDBCSV;
28   friend class TDBFIX;
29   friend class TDBVCT;
30   friend class TDBJSON;
31   friend class DOSCOL;
32   friend class BINCOL;
33   friend class VCTCOL;
34  public:
35   // Constructor
36   TXTFAM(PDOSDEF tdp);
37   TXTFAM(PTXF txfp);
38 
39   // Implementation
40   virtual AMT   GetAmType(void) = 0;
41   virtual int   GetPos(void) = 0;
42   virtual int   GetNextPos(void) = 0;
43   virtual PTXF  Duplicate(PGLOBAL g) = 0;
GetUseTemp(void)44   virtual bool  GetUseTemp(void) {return false;}
GetDelRows(void)45   virtual int   GetDelRows(void) {return DelRows;}
GetTo_Fb(void)46         PFBLOCK GetTo_Fb(void) {return To_Fb;}
GetCurBlk(void)47           int   GetCurBlk(void) {return CurBlk;}
SetTdbp(PTDBDOS tdbp)48           void  SetTdbp(PTDBDOS tdbp) {Tdbp = tdbp;}
GetBlock(void)49           int   GetBlock(void) {return Block;}
SetBlkPos(int * bkp)50           void  SetBlkPos(int *bkp) {BlkPos = bkp;}
SetNrec(int n)51           void  SetNrec(int n) {Nrec = n;}
GetBuf(void)52           char *GetBuf(void) {return To_Buf;}
GetRows(void)53           int   GetRows(void) {return Rows;}
IsBlocked(void)54           bool  IsBlocked(void) {return Blocked;}
55 
56   // Methods
57   virtual void  Reset(void);
58   virtual int   GetFileLength(PGLOBAL g);
59   virtual int   Cardinality(PGLOBAL g);
60   virtual int   MaxBlkSize(PGLOBAL g, int s);
AllocateBuffer(PGLOBAL)61   virtual bool  AllocateBuffer(PGLOBAL) {return false;}
ResetBuffer(PGLOBAL)62   virtual void  ResetBuffer(PGLOBAL) {}
GetNerr(void)63   virtual int   GetNerr(void) {return 0;}
64   virtual int   GetRowID(void) = 0;
65   virtual bool  RecordPos(PGLOBAL g) = 0;
66   virtual bool  SetPos(PGLOBAL g, int recpos) = 0;
67   virtual int   SkipRecord(PGLOBAL g, bool header) = 0;
68   virtual bool  OpenTableFile(PGLOBAL g) = 0;
DeferReading(void)69   virtual bool  DeferReading(void) {IsRead = false; return true;}
70   virtual int   ReadBuffer(PGLOBAL g) = 0;
71   virtual int   WriteBuffer(PGLOBAL g) = 0;
72   virtual int   DeleteRecords(PGLOBAL g, int irc) = 0;
73   virtual void  CloseTableFile(PGLOBAL g, bool abort) = 0;
74   virtual void  Rewind(void) = 0;
75   virtual int   InitDelete(PGLOBAL g, int fpos, int spos);
76           bool  AddListValue(PGLOBAL g, int type, void *val, PPARM *top);
77           int   StoreValues(PGLOBAL g, bool upd);
78           int   UpdateSortedRows(PGLOBAL g);
79           int   DeleteSortedRows(PGLOBAL g);
80 
81  protected:
82   // Members
83   PTDBDOS Tdbp;              // To table class
84   PCSZ    To_File;           // Points to table file name
85   PFBLOCK To_Fb;             // Pointer to file block
86   PPARM   To_Pos;            // Pointer to position list
87   PPARM   To_Sos;            // Pointer to start position list
88   PPARM   To_Upd;            // Pointer to udated line list
89   PARRAY  Posar;             // Pointer to position array
90   PARRAY  Sosar;             // Pointer to start position array
91   PARRAY  Updar;             // Pointer to udated lines array
92   bool    Placed;            // true if Recpos was externally set
93   bool    IsRead;            // false for deferred reading
94   bool    Blocked;           // true if using blocked I/O
95   char   *To_Buf;            // Points to I/O buffer
96   void   *DelBuf;            // Buffer used to move lines in Delete
97   int    *BlkPos;            // To array of block positions
98   int     BlkLen;            // Current block length
99   int     Buflen;            // Buffer length
100   int     Dbflen;            // Delete buffer length
101   int     Rows;              // Number of rows read so far
102   int     DelRows;           // Number of deleted rows
103   int     Headlen;           // Number of bytes in header
104   int     Lrecl;             // Logical Record Length
105   int     Block;             // Number of blocks in table
106   int     Last;              // Number of elements of last block
107   int     Nrec;              // Number of records in buffer
108   int     OldBlk;            // Index of last read block
109   int     CurBlk;            // Index of current block
110   int     CurNum;            // Current buffer line number
111   int     ReadBlks;          // Number of blocks read (selected)
112   int     Rbuf;              // Number of lines read in buffer
113   int     Modif;             // Number of modified lines in block
114   int     Blksize;           // Size of padded blocks
115   int     Ending;            // Length of line end
116   int     Fpos;              // Position of last read record
117   int     Spos;              // Start position for update/delete move
118   int     Tpos;              // Target Position for delete move
119   bool    Padded;            // true if fixed size blocks are padded
120   bool    Eof;               // true if an EOF (0xA) character exists
121   bool    Abort;             // To abort on error
122   char   *CrLf;              // End of line character(s)
123   }; // end of class TXTFAM
124 
125 /***********************************************************************/
126 /*  This is the DOS/UNIX Access Method class declaration for standard  */
127 /*  text files with variable record format (DOS, CSV, FMT)             */
128 /***********************************************************************/
129 class DllExport DOSFAM : public TXTFAM {
130  public:
131   // Constructor
132   DOSFAM(PDOSDEF tdp);
133   DOSFAM(PDOSFAM txfp);
134   DOSFAM(PBLKFAM tdfp, PDOSDEF tdp);
135 
136   // Implementation
GetAmType(void)137   virtual AMT   GetAmType(void) {return TYPE_AM_DOS;}
GetUseTemp(void)138   virtual bool  GetUseTemp(void) {return UseTemp;}
139   virtual int   GetPos(void);
140   virtual int   GetNextPos(void);
Duplicate(PGLOBAL g)141   virtual PTXF  Duplicate(PGLOBAL g)
142                   {return (PTXF)new(g) DOSFAM(this);}
143 
144   // Methods
145   virtual void  Reset(void);
146   virtual int   GetFileLength(PGLOBAL g);
147   virtual int   Cardinality(PGLOBAL g);
148   virtual int   MaxBlkSize(PGLOBAL g, int s);
149   virtual bool  AllocateBuffer(PGLOBAL g);
150   virtual int   GetRowID(void);
151   virtual bool  RecordPos(PGLOBAL g);
152   virtual bool  SetPos(PGLOBAL g, int recpos);
153   virtual int   SkipRecord(PGLOBAL g, bool header);
154   virtual bool  OpenTableFile(PGLOBAL g);
155   virtual int   ReadBuffer(PGLOBAL g);
156   virtual int   WriteBuffer(PGLOBAL g);
157   virtual int   DeleteRecords(PGLOBAL g, int irc);
158   virtual void  CloseTableFile(PGLOBAL g, bool abort);
159   virtual void  Rewind(void);
160 
161  protected:
162   virtual bool  OpenTempFile(PGLOBAL g);
163   virtual bool  MoveIntermediateLines(PGLOBAL g, bool *b);
164   virtual int   RenameTempFile(PGLOBAL g);
165   virtual int   InitDelete(PGLOBAL g, int fpos, int spos);
166 
167   // Members
168   FILE   *Stream;             // Points to Dos file structure
169   FILE   *T_Stream;           // Points to temporary file structure
170   PFBLOCK To_Fbt;             // Pointer to temp file block
171   bool    UseTemp;            // True to use a temporary file in Upd/Del
172   bool    Bin;                // True to force binary mode
173   }; // end of class DOSFAM
174 
175 /***********************************************************************/
176 /*  This is the DOS/UNIX Access Method class declaration for standard  */
177 /*  text files with variable record format (DOS, CSV, FMT)             */
178 /***********************************************************************/
179 class DllExport BLKFAM : public DOSFAM {
180  public:
181   // Constructor
182   BLKFAM(PDOSDEF tdp);
183   BLKFAM(PBLKFAM txfp);
184 
185   // Implementation
GetAmType(void)186   virtual AMT   GetAmType(void) {return TYPE_AM_BLK;}
187   virtual int   GetPos(void);
188   virtual int   GetNextPos(void);
Duplicate(PGLOBAL g)189   virtual PTXF  Duplicate(PGLOBAL g)
190                   {return (PTXF)new(g) BLKFAM(this);}
191 
192   // Methods
193   virtual void  Reset(void);
194   virtual int   Cardinality(PGLOBAL g);
195   virtual int   MaxBlkSize(PGLOBAL g, int s);
196   virtual bool  AllocateBuffer(PGLOBAL g);
197   virtual int   GetRowID(void);
198   virtual bool  RecordPos(PGLOBAL g);
199   virtual bool  SetPos(PGLOBAL g, int recpos);
200   virtual int   SkipRecord(PGLOBAL g, bool header);
201   virtual int   ReadBuffer(PGLOBAL g);
202   virtual int   WriteBuffer(PGLOBAL g);
203   virtual void  CloseTableFile(PGLOBAL g, bool abort);
204   virtual void  Rewind(void);
205 
206  protected:
207   // Members
208   char *CurLine;              // Position of current line in buffer
209   char *NxtLine;              // Position of Next    line in buffer
210   char *OutBuf;               // Buffer to write in temporary file
211   bool  Closing;              // True when closing on Update
212   }; // end of class BLKFAM
213 
214 /***********************************************************************/
215 /*  This is the DOS/UNIX Access Method class declaration for binary    */
216 /*  files with variable record format (BJSON)                          */
217 /***********************************************************************/
218 class DllExport BINFAM : public DOSFAM {
219 public:
220 	// Constructor
BINFAM(PDOSDEF tdp)221 	BINFAM(PDOSDEF tdp) : DOSFAM(tdp) {Recsize = 0;}
BINFAM(PBINFAM txfp)222 	BINFAM(PBINFAM txfp) : DOSFAM(txfp) {Recsize = txfp->Recsize;}
223 
224 	// Implementation
GetAmType(void)225 	virtual AMT   GetAmType(void) {return TYPE_AM_BIN;}
226 //virtual int   GetPos(void);
227 //virtual int   GetNextPos(void);
Duplicate(PGLOBAL g)228 	virtual PTXF  Duplicate(PGLOBAL g) { return (PTXF)new(g) BINFAM(this); }
229 
230 	// Methods
231 //virtual void  Reset(void) {TXTFAM::Reset();}
232 //virtual int   GetFileLength(PGLOBAL g);
233 //virtual int   Cardinality(PGLOBAL g);
MaxBlkSize(PGLOBAL g,int s)234 	virtual int   MaxBlkSize(PGLOBAL g, int s) {return s;}
235 	virtual bool  AllocateBuffer(PGLOBAL g);
236 //virtual int   GetRowID(void);
237 //virtual bool  RecordPos(PGLOBAL g);
238 //virtual bool  SetPos(PGLOBAL g, int recpos);
SkipRecord(PGLOBAL g,bool header)239 	virtual int   SkipRecord(PGLOBAL g, bool header) {return RC_OK;}
240 //virtual bool  OpenTableFile(PGLOBAL g);
241 	virtual int   ReadBuffer(PGLOBAL g);
242 	virtual int   WriteBuffer(PGLOBAL g);
243 //virtual int   DeleteRecords(PGLOBAL g, int irc);
244 //virtual void  CloseTableFile(PGLOBAL g, bool abort);
245 //virtual void  Rewind(void);
246 
247 //protected:
248 //virtual int   InitDelete(PGLOBAL g, int fpos, int spos);
249 
250 	// Members
251 	size_t Recsize;		// Length of last read or next written record
252 }; // end of class BINFAM
253 
254 #endif // __FILAMTXT_H
255