| 1 | /*************** TabFmt H Declares Source Code File (.H) ***************/ |
| 2 | /* Name: TABFMT.H Version 2.5 */ |
| 3 | /* */ |
| 4 | /* (C) Copyright to the author Olivier BERTRAND 2001-2016 */ |
| 5 | /* */ |
| 6 | /* This file contains the CSV and FMT classes declares. */ |
| 7 | /***********************************************************************/ |
| 8 | #include "xtable.h" // Base class declares |
| 9 | #include "tabdos.h" |
| 10 | |
| 11 | typedef class TDBFMT *PTDBFMT; |
| 12 | |
| 13 | /***********************************************************************/ |
| 14 | /* Functions used externally. */ |
| 15 | /***********************************************************************/ |
| 16 | PQRYRES CSVColumns(PGLOBAL g, PCSZ dp, PTOS topt, bool info); |
| 17 | |
| 18 | /***********************************************************************/ |
| 19 | /* CSV table. */ |
| 20 | /***********************************************************************/ |
| 21 | class DllExport CSVDEF : public DOSDEF { /* Logical table description */ |
| 22 | friend class TDBCSV; |
| 23 | friend class TDBCCL; |
| 24 | friend PQRYRES CSVColumns(PGLOBAL, PCSZ, PTOS, bool); |
| 25 | public: |
| 26 | // Constructor |
| 27 | CSVDEF(void); |
| 28 | |
| 29 | // Implementation |
| 30 | virtual const char *GetType(void) {return "CSV" ;} |
| 31 | char GetSep(void) {return Sep;} |
| 32 | char GetQot(void) {return Qot;} |
| 33 | |
| 34 | // Methods |
| 35 | virtual bool DefineAM(PGLOBAL g, LPCSTR am, int poff); |
| 36 | virtual PTDB GetTable(PGLOBAL g, MODE mode); |
| 37 | |
| 38 | protected: |
| 39 | // Members |
| 40 | bool Fmtd; /* true for formatted files */ |
| 41 | //bool Accept; /* true if wrong lines are accepted */ |
| 42 | bool ; /* true if first line contains headers */ |
| 43 | //int Maxerr; /* Maximum number of bad records */ |
| 44 | int Quoted; /* Quoting level for quoted fields */ |
| 45 | char Sep; /* Separator for standard CSV files */ |
| 46 | char Qot; /* Character for quoted strings */ |
| 47 | }; // end of CSVDEF |
| 48 | |
| 49 | /***********************************************************************/ |
| 50 | /* This is the DOS/UNIX Access Method class declaration for files */ |
| 51 | /* that are CSV files with columns separated by the Sep character. */ |
| 52 | /***********************************************************************/ |
| 53 | class DllExport TDBCSV : public TDBDOS { |
| 54 | friend class CSVCOL; |
| 55 | friend class MAPFAM; |
| 56 | friend PQRYRES CSVColumns(PGLOBAL, PCSZ, PTOS, bool); |
| 57 | public: |
| 58 | // Constructor |
| 59 | TDBCSV(PCSVDEF tdp, PTXF txfp); |
| 60 | TDBCSV(PGLOBAL g, PTDBCSV tdbp); |
| 61 | |
| 62 | // Implementation |
| 63 | virtual AMT GetAmType(void) {return TYPE_AM_CSV;} |
| 64 | virtual PTDB Duplicate(PGLOBAL g) |
| 65 | {return (PTDB)new(g) TDBCSV(g, this);} |
| 66 | |
| 67 | // Methods |
| 68 | virtual PTDB Clone(PTABS t); |
| 69 | //virtual bool IsUsingTemp(PGLOBAL g); |
| 70 | virtual int GetBadLines(void) {return (int)Nerr;} |
| 71 | |
| 72 | // Database routines |
| 73 | virtual PCOL MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n); |
| 74 | virtual bool OpenDB(PGLOBAL g); |
| 75 | virtual int WriteDB(PGLOBAL g); |
| 76 | virtual int CheckWrite(PGLOBAL g); |
| 77 | virtual int ReadBuffer(PGLOBAL g); // Physical file read |
| 78 | |
| 79 | // Specific routines |
| 80 | virtual int EstimatedLength(void); |
| 81 | virtual bool (PGLOBAL g); |
| 82 | virtual bool CheckErr(void); |
| 83 | |
| 84 | protected: |
| 85 | virtual bool PrepareWriting(PGLOBAL g); |
| 86 | |
| 87 | // Members |
| 88 | PSZ *Field; // Field to write to current line |
| 89 | int *Offset; // Column offsets for current record |
| 90 | int *Fldlen; // Column field length for current record |
| 91 | bool *Fldtyp; // true for numeric fields |
| 92 | int Fields; // Number of fields to handle |
| 93 | int Nerr; // Number of bad records |
| 94 | int Maxerr; // Maximum number of bad records |
| 95 | int Quoted; // Quoting level for quoted fields |
| 96 | bool Accept; // true if bad lines are accepted |
| 97 | bool ; // true if first line contains column headers |
| 98 | char Sep; // Separator |
| 99 | char Qot; // Quoting character |
| 100 | }; // end of class TDBCSV |
| 101 | |
| 102 | /***********************************************************************/ |
| 103 | /* Class CSVCOL: CSV access method column descriptor. */ |
| 104 | /* This A.M. is used for Comma Separated V(?) files. */ |
| 105 | /***********************************************************************/ |
| 106 | class DllExport CSVCOL : public DOSCOL { |
| 107 | friend class TDBCSV; |
| 108 | friend class TDBFMT; |
| 109 | public: |
| 110 | // Constructors |
| 111 | CSVCOL(PGLOBAL g, PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i); |
| 112 | CSVCOL(CSVCOL *colp, PTDB tdbp); // Constructor used in copy process |
| 113 | |
| 114 | // Implementation |
| 115 | virtual int GetAmType() {return TYPE_AM_CSV;} |
| 116 | |
| 117 | // Methods |
| 118 | virtual bool VarSize(void); |
| 119 | virtual void ReadColumn(PGLOBAL g); |
| 120 | virtual void WriteColumn(PGLOBAL g); |
| 121 | |
| 122 | protected: |
| 123 | // Default constructor not to be used |
| 124 | CSVCOL(void) {} |
| 125 | |
| 126 | // Members |
| 127 | int Fldnum; // Field ordinal number (0 based) |
| 128 | }; // end of class CSVCOL |
| 129 | |
| 130 | /***********************************************************************/ |
| 131 | /* This is the DOS/UNIX Access Method class declaration for files */ |
| 132 | /* whose record format is described by a Format keyword. */ |
| 133 | /***********************************************************************/ |
| 134 | class DllExport TDBFMT : public TDBCSV { |
| 135 | friend class CSVCOL; |
| 136 | //friend class FMTCOL; |
| 137 | public: |
| 138 | // Standard constructor |
| 139 | TDBFMT(PCSVDEF tdp, PTXF txfp) : TDBCSV(tdp, txfp) |
| 140 | {FldFormat = NULL; To_Fld = NULL; FmtTest = NULL; Linenum = 0;} |
| 141 | |
| 142 | // Copy constructor |
| 143 | TDBFMT(PGLOBAL g, PTDBFMT tdbp); |
| 144 | |
| 145 | // Implementation |
| 146 | virtual AMT GetAmType(void) {return TYPE_AM_FMT;} |
| 147 | virtual PTDB Duplicate(PGLOBAL g) |
| 148 | {return (PTDB)new(g) TDBFMT(g, this);} |
| 149 | |
| 150 | // Methods |
| 151 | virtual PTDB Clone(PTABS t); |
| 152 | |
| 153 | // Database routines |
| 154 | virtual PCOL MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n); |
| 155 | //virtual int GetMaxSize(PGLOBAL g); |
| 156 | virtual bool OpenDB(PGLOBAL g); |
| 157 | virtual int WriteDB(PGLOBAL g); |
| 158 | //virtual int CheckWrite(PGLOBAL g); |
| 159 | virtual int ReadBuffer(PGLOBAL g); // Physical file read |
| 160 | |
| 161 | // Specific routines |
| 162 | virtual int EstimatedLength(void); |
| 163 | |
| 164 | protected: |
| 165 | virtual bool PrepareWriting(PGLOBAL g) |
| 166 | {sprintf(g->Message, MSG(TABLE_READ_ONLY), "FMT" ); return true;} |
| 167 | |
| 168 | // Members |
| 169 | PSZ *FldFormat; // Field read format |
| 170 | void *To_Fld; // To field test buffer |
| 171 | int *FmtTest; // Test on ending by %n or %m |
| 172 | int Linenum; // Last read line |
| 173 | }; // end of class TDBFMT |
| 174 | |
| 175 | /***********************************************************************/ |
| 176 | /* This is the class declaration for the CSV catalog table. */ |
| 177 | /***********************************************************************/ |
| 178 | class DllExport TDBCCL : public TDBCAT { |
| 179 | public: |
| 180 | // Constructor |
| 181 | TDBCCL(PCSVDEF tdp); |
| 182 | |
| 183 | protected: |
| 184 | // Specific routines |
| 185 | virtual PQRYRES GetResult(PGLOBAL g); |
| 186 | |
| 187 | // Members |
| 188 | PTOS Topt; |
| 189 | }; // end of class TDBCCL |
| 190 | |
| 191 | /* ------------------------- End of TabFmt.H ------------------------- */ |
| 192 | |