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