1 /**************** Value H Declares Source Code File (.H) ***************/ 2 /* Name: VALUE.H Version 2.4 */ 3 /* */ 4 /* (C) Copyright to the author Olivier BERTRAND 2001-2019 */ 5 /* */ 6 /* This file contains the VALUE and derived classes declares. */ 7 /***********************************************************************/ 8 #ifndef __VALUE__H__ 9 #define __VALUE__H__ 10 11 /***********************************************************************/ 12 /* Include required application header files */ 13 /* assert.h is header required when using the assert function. */ 14 /* block.h is header containing Block global declarations. */ 15 /***********************************************************************/ 16 #include "assert.h" 17 #include "block.h" 18 19 /***********************************************************************/ 20 /* This should list the processors accepting unaligned numeral values.*/ 21 /***********************************************************************/ 22 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || defined(_M_IA64) 23 #define UNALIGNED_OK 24 #endif 25 26 /***********************************************************************/ 27 /* Types used in some class definitions. */ 28 /***********************************************************************/ 29 enum CONV {CNV_ANY = 0, /* Convert to any type */ 30 CNV_CHAR = 1, /* Convert to character type */ 31 CNV_NUM = 2}; /* Convert to numeric type */ 32 33 /***********************************************************************/ 34 /* Types used in some class definitions. */ 35 /***********************************************************************/ 36 class CONSTANT; // For friend setting 37 typedef struct _datpar *PDTP; // For DTVAL 38 39 /***********************************************************************/ 40 /* Utilities used to test types and to allocated values. */ 41 /***********************************************************************/ 42 // Exported functions 43 DllExport PCSZ GetTypeName(int); 44 DllExport int GetTypeSize(int, int); 45 #ifdef ODBC_SUPPORT 46 /* This function is exported for use in OEM table type DLLs */ 47 DllExport int TranslateSQLType(int stp, int prec, 48 int& len, char& v, bool& w); 49 #endif 50 DllExport const char *GetFormatType(int); 51 DllExport int GetFormatType(char); 52 DllExport bool IsTypeChar(int type); 53 DllExport bool IsTypeNum(int type); 54 DllExport int ConvertType(int, int, CONV, bool match = false); 55 DllExport PVAL AllocateValue(PGLOBAL, void *, short, short = 2); 56 DllExport PVAL AllocateValue(PGLOBAL, PVAL, int = TYPE_VOID, int = 0); 57 DllExport PVAL AllocateValue(PGLOBAL, int, int len = 0, int prec = 0, 58 bool uns = false, PCSZ fmt = NULL); 59 DllExport ulonglong CharToNumber(PCSZ, int, ulonglong, bool, 60 bool *minus = NULL, bool *rc = NULL); 61 DllExport BYTE OpBmp(PGLOBAL g, OPVAL opc); 62 63 /***********************************************************************/ 64 /* Class VALUE represents a constant or variable of any valid type. */ 65 /***********************************************************************/ 66 class DllExport VALUE : public BLOCK { 67 friend class CONSTANT; // The only object allowed to use SetConstFormat 68 friend class SWAP; // The only class allowed to access protected 69 public: 70 // Constructors 71 72 // Implementation 73 virtual bool IsTypeNum(void) = 0; 74 virtual bool IsZero(void) = 0; IsCi(void)75 virtual bool IsCi(void) {return false;} IsUnsigned(void)76 virtual bool IsUnsigned(void) {return Unsigned;} 77 virtual void Reset(void) = 0; 78 virtual int GetSize(void) = 0; 79 virtual int GetValLen(void) = 0; 80 virtual int GetValPrec(void) = 0; GetLength(void)81 virtual int GetLength(void) {return 1;} GetCharValue(void)82 virtual PSZ GetCharValue(void) {assert(false); return NULL;} GetTinyValue(void)83 virtual char GetTinyValue(void) {assert(false); return 0;} GetUTinyValue(void)84 virtual uchar GetUTinyValue(void) {assert(false); return 0;} GetShortValue(void)85 virtual short GetShortValue(void) {assert(false); return 0;} GetUShortValue(void)86 virtual ushort GetUShortValue(void) {assert(false); return 0;} 87 virtual int GetIntValue(void) = 0; 88 virtual uint GetUIntValue(void) = 0; 89 virtual longlong GetBigintValue(void) = 0; 90 virtual ulonglong GetUBigintValue(void) = 0; 91 virtual double GetFloatValue(void) = 0; 92 virtual void *GetTo_Val(void) = 0; SetPrec(int prec)93 virtual void SetPrec(int prec) {Prec = prec;} IsNull(void)94 bool IsNull(void) {return (Nullable && Null);} SetNull(bool b)95 void SetNull(bool b) {Null = (Nullable ? b : false);} GetNullable(void)96 bool GetNullable(void) {return Nullable;} SetNullable(bool b)97 void SetNullable(bool b) {Nullable = b;} GetType(void)98 int GetType(void) {return Type;} GetClen(void)99 int GetClen(void) {return Clen;} SetGlobal(PGLOBAL g)100 void SetGlobal(PGLOBAL g) {Global = g;} 101 102 // Methods 103 virtual bool SetValue_pval(PVAL valp, bool chktype = false) = 0; 104 virtual bool SetValue_char(const char *p, int n) = 0; 105 virtual void SetValue_psz(PCSZ s) = 0; SetValue_bool(bool)106 virtual void SetValue_bool(bool) {assert(false);} 107 virtual int CompareValue(PVAL vp) = 0; 108 virtual BYTE TestValue(PVAL vp); SetValue(char)109 virtual void SetValue(char) {assert(false);} SetValue(uchar)110 virtual void SetValue(uchar) {assert(false);} SetValue(short)111 virtual void SetValue(short) {assert(false);} SetValue(ushort)112 virtual void SetValue(ushort) {assert(false);} SetValue(int)113 virtual void SetValue(int) {assert(false);} SetValue(uint)114 virtual void SetValue(uint) {assert(false);} SetValue(longlong)115 virtual void SetValue(longlong) {assert(false);} SetValue(ulonglong)116 virtual void SetValue(ulonglong) {assert(false);} SetValue(double)117 virtual void SetValue(double) {assert(false);} 118 virtual void SetValue_pvblk(PVBLK blk, int n) = 0; 119 virtual void SetBinValue(void* p) = 0; 120 virtual bool GetBinValue(void *buf, int buflen, bool go) = 0; 121 virtual int ShowValue(char *buf, int len) = 0; 122 virtual char *GetCharString(char *p) = 0; 123 virtual bool IsEqual(PVAL vp, bool chktype) = 0; 124 virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op); 125 virtual bool FormatValue(PVAL vp, PCSZ fmt) = 0; 126 virtual void Printf(PGLOBAL g, FILE *, uint); 127 virtual void Prints(PGLOBAL g, char *ps, uint z); 128 129 /** 130 Set value from a non-aligned in-memory value in the machine byte order. 131 TYPE can be either of: 132 - int, short, longlong 133 - uint, ushort, ulonglong 134 - float, double 135 @param - a pointer to a non-aligned value of type TYPE. 136 */ 137 template<typename TYPE> SetValueNonAligned(const char * p)138 void SetValueNonAligned(const char *p) 139 { 140 #if defined(UNALIGNED_OK) 141 SetValue(*((TYPE*)p)); // x86 can cast non-aligned memory directly 142 #else 143 TYPE tmp; // a slower version for non-x86 platforms 144 memcpy(&tmp, p, sizeof(tmp)); 145 SetValue(tmp); 146 #endif 147 } // end of SetValueNonAligned 148 149 /** 150 Get value from a non-aligned in-memory value in the machine byte order. 151 TYPE can be either of: 152 - int, short, longlong 153 - uint, ushort, ulonglong 154 - float, double 155 @params - a pointer to a non-aligned value of type TYPE, the TYPE value. 156 */ 157 template<typename TYPE> GetValueNonAligned(char * p,TYPE n)158 void GetValueNonAligned(char *p, TYPE n) 159 { 160 #if defined(UNALIGNED_OK) 161 *(TYPE *)p = n; // x86 can cast non-aligned memory directly 162 #else 163 TYPE tmp = n; // a slower version for non-x86 platforms 164 memcpy(p, &tmp, sizeof(tmp)); 165 #endif 166 } // end of SetValueNonAligned 167 168 protected: 169 virtual bool SetConstFormat(PGLOBAL, FORMAT&) = 0; 170 const char *GetXfmt(void); 171 172 // Constructor used by derived classes 173 VALUE(int type, bool un = false); 174 175 // Members 176 PGLOBAL Global; // To reduce arglist 177 const char *Fmt; 178 const char *Xfmt; 179 bool Nullable; // True if value can be null 180 bool Null; // True if value is null 181 bool Unsigned; // True if unsigned 182 int Type; // The value type 183 int Clen; // Internal value length 184 int Prec; 185 }; // end of class VALUE 186 187 /***********************************************************************/ 188 /* Class TYPVAL: represents a typed value. */ 189 /***********************************************************************/ 190 template <class TYPE> 191 class DllExport TYPVAL : public VALUE { 192 public: 193 // Constructor 194 TYPVAL(TYPE n, int type, int prec = 0, bool un = false); 195 196 // Implementation IsTypeNum(void)197 virtual bool IsTypeNum(void) {return true;} IsZero(void)198 virtual bool IsZero(void) {return Tval == 0;} Reset(void)199 virtual void Reset(void) {Tval = 0;} 200 virtual int GetValLen(void); GetValPrec()201 virtual int GetValPrec() {return Prec;} GetSize(void)202 virtual int GetSize(void) {return sizeof(TYPE);} 203 //virtual PSZ GetCharValue(void) {return VALUE::GetCharValue();} GetTinyValue(void)204 virtual char GetTinyValue(void) {return (char)Tval;} GetUTinyValue(void)205 virtual uchar GetUTinyValue(void) {return (uchar)Tval;} GetShortValue(void)206 virtual short GetShortValue(void) {return (short)Tval;} GetUShortValue(void)207 virtual ushort GetUShortValue(void) {return (ushort)Tval;} GetIntValue(void)208 virtual int GetIntValue(void) {return (int)Tval;} GetUIntValue(void)209 virtual uint GetUIntValue(void) {return (uint)Tval;} GetBigintValue(void)210 virtual longlong GetBigintValue(void) {return (longlong)Tval;} GetUBigintValue(void)211 virtual ulonglong GetUBigintValue(void) {return (ulonglong)Tval;} GetFloatValue(void)212 virtual double GetFloatValue(void) {return (double)Tval;} GetTo_Val(void)213 virtual void *GetTo_Val(void) {return &Tval;} 214 215 // Methods 216 virtual bool SetValue_pval(PVAL valp, bool chktype); 217 virtual bool SetValue_char(const char *p, int n); 218 virtual void SetValue_psz(PCSZ s); SetValue_bool(bool b)219 virtual void SetValue_bool(bool b) {Tval = (b) ? 1 : 0;} 220 virtual int CompareValue(PVAL vp); SetValue(char c)221 virtual void SetValue(char c) {Tval = (TYPE)c; Null = false;} SetValue(uchar c)222 virtual void SetValue(uchar c) {Tval = (TYPE)c; Null = false;} SetValue(short i)223 virtual void SetValue(short i) {Tval = (TYPE)i; Null = false;} SetValue(ushort i)224 virtual void SetValue(ushort i) {Tval = (TYPE)i; Null = false;} SetValue(int n)225 virtual void SetValue(int n) {Tval = (TYPE)n; Null = false;} SetValue(uint n)226 virtual void SetValue(uint n) {Tval = (TYPE)n; Null = false;} SetValue(longlong n)227 virtual void SetValue(longlong n) {Tval = (TYPE)n; Null = false;} SetValue(ulonglong n)228 virtual void SetValue(ulonglong n) {Tval = (TYPE)n; Null = false;} SetValue(double f)229 virtual void SetValue(double f) {Tval = (TYPE)f; Null = false;} 230 virtual void SetValue_pvblk(PVBLK blk, int n); 231 virtual void SetBinValue(void *p); 232 virtual bool GetBinValue(void *buf, int buflen, bool go); 233 virtual int ShowValue(char *buf, int len); 234 virtual char *GetCharString(char *p); 235 virtual bool IsEqual(PVAL vp, bool chktype); 236 virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op); 237 virtual bool SetConstFormat(PGLOBAL, FORMAT&); 238 virtual bool FormatValue(PVAL vp, PCSZ fmt); 239 240 protected: 241 static TYPE MinMaxVal(bool b); 242 TYPE SafeAdd(TYPE n1, TYPE n2); 243 TYPE SafeMult(TYPE n1, TYPE n2); 244 bool Compall(PGLOBAL g, PVAL *vp, int np, OPVAL op); 245 246 // Default constructor not to be used TYPVAL(void)247 TYPVAL(void) : VALUE(TYPE_ERROR) {} 248 249 // Specialized functions 250 static ulonglong MaxVal(void); 251 TYPE GetTypedValue(PVAL vp); 252 TYPE GetTypedValue(PVBLK blk, int n); 253 // TYPE GetTypedValue(PSZ s); 254 255 // Members 256 TYPE Tval; 257 }; // end of class TYPVAL 258 259 /***********************************************************************/ 260 /* Specific STRING class. */ 261 /***********************************************************************/ 262 template <> 263 class DllExport TYPVAL<PSZ>: public VALUE { 264 friend class SWAP; // The only class allowed to offsets Strg 265 public: 266 // Constructors 267 TYPVAL(PSZ s, short c = 0); 268 TYPVAL(PGLOBAL g, PSZ s, int n, int c); 269 270 // Implementation IsTypeNum(void)271 virtual bool IsTypeNum(void) {return false;} IsZero(void)272 virtual bool IsZero(void) {return *Strp == 0;} Reset(void)273 virtual void Reset(void) {*Strp = 0;} GetValLen(void)274 virtual int GetValLen(void) {return Len;}; GetValPrec()275 virtual int GetValPrec() {return (Ci) ? 1 : 0;} GetSize(void)276 virtual int GetSize(void) {return (Strp) ? (int)strlen(Strp) : 0;} GetCharValue(void)277 virtual PSZ GetCharValue(void) {return Strp;} 278 virtual char GetTinyValue(void); 279 virtual uchar GetUTinyValue(void); 280 virtual short GetShortValue(void); 281 virtual ushort GetUShortValue(void); 282 virtual int GetIntValue(void); 283 virtual uint GetUIntValue(void); 284 virtual longlong GetBigintValue(void); 285 virtual ulonglong GetUBigintValue(void); GetFloatValue(void)286 virtual double GetFloatValue(void) {return atof(Strp);} GetTo_Val(void)287 virtual void *GetTo_Val(void) {return Strp;} SetPrec(int prec)288 virtual void SetPrec(int prec) {Ci = prec != 0;} 289 290 // Methods 291 virtual bool SetValue_pval(PVAL valp, bool chktype); 292 virtual bool SetValue_char(const char *p, int n); 293 virtual void SetValue_psz(PCSZ s); 294 virtual void SetValue_pvblk(PVBLK blk, int n); 295 virtual void SetValue(char c); 296 virtual void SetValue(uchar c); 297 virtual void SetValue(short i); 298 virtual void SetValue(ushort i); 299 virtual void SetValue(int n); 300 virtual void SetValue(uint n); 301 virtual void SetValue(longlong n); 302 virtual void SetValue(ulonglong n); 303 virtual void SetValue(double f); 304 virtual void SetBinValue(void *p); 305 virtual int CompareValue(PVAL vp); 306 virtual bool GetBinValue(void *buf, int buflen, bool go); 307 virtual int ShowValue(char *buf, int len); 308 virtual char *GetCharString(char *p); 309 virtual bool IsEqual(PVAL vp, bool chktype); 310 virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op); 311 virtual bool FormatValue(PVAL vp, PCSZ fmt); 312 virtual bool SetConstFormat(PGLOBAL, FORMAT&); 313 virtual void Prints(PGLOBAL g, char *ps, uint z); 314 315 protected: 316 // Members 317 PSZ Strp; 318 bool Ci; // true if case insensitive 319 int Len; 320 }; // end of class TYPVAL<PSZ> 321 322 /***********************************************************************/ 323 /* Specific DECIMAL class. */ 324 /***********************************************************************/ 325 class DllExport DECVAL: public TYPVAL<PSZ> { 326 public: 327 // Constructors 328 DECVAL(PSZ s); 329 DECVAL(PGLOBAL g, PSZ s, int n, int prec, bool uns); 330 331 // Implementation IsTypeNum(void)332 virtual bool IsTypeNum(void) {return true;} 333 virtual bool IsZero(void); 334 virtual void Reset(void); GetValPrec()335 virtual int GetValPrec() {return Prec;} 336 337 // Methods 338 virtual bool GetBinValue(void *buf, int buflen, bool go); 339 virtual int ShowValue(char *buf, int len); 340 virtual bool IsEqual(PVAL vp, bool chktype); 341 virtual int CompareValue(PVAL vp); 342 343 protected: 344 // Members 345 }; // end of class DECVAL 346 347 /***********************************************************************/ 348 /* Specific BINARY class. */ 349 /***********************************************************************/ 350 class DllExport BINVAL: public VALUE { 351 friend class SWAP; // The only class allowed to offsets pointers 352 public: 353 // Constructors 354 //BINVAL(void *p); 355 BINVAL(PGLOBAL g, void *p, int cl, int n); 356 357 // Implementation IsTypeNum(void)358 virtual bool IsTypeNum(void) {return false;} 359 virtual bool IsZero(void); 360 virtual void Reset(void); GetValLen(void)361 virtual int GetValLen(void) {return Clen;}; GetValPrec()362 virtual int GetValPrec() {return 0;} GetSize(void)363 virtual int GetSize(void) {return Len;} GetCharValue(void)364 virtual PSZ GetCharValue(void) {return (PSZ)Binp;} 365 virtual char GetTinyValue(void); 366 virtual uchar GetUTinyValue(void); 367 virtual short GetShortValue(void); 368 virtual ushort GetUShortValue(void); 369 virtual int GetIntValue(void); 370 virtual uint GetUIntValue(void); 371 virtual longlong GetBigintValue(void); 372 virtual ulonglong GetUBigintValue(void); 373 virtual double GetFloatValue(void); GetTo_Val(void)374 virtual void *GetTo_Val(void) {return Binp;} 375 376 // Methods 377 virtual bool SetValue_pval(PVAL valp, bool chktype); 378 virtual bool SetValue_char(const char *p, int n); 379 virtual void SetValue_psz(PCSZ s); 380 virtual void SetValue_pvblk(PVBLK blk, int n); 381 virtual void SetValue(char c); 382 virtual void SetValue(uchar c); 383 virtual void SetValue(short i); 384 virtual void SetValue(ushort i); 385 virtual void SetValue(int n); 386 virtual void SetValue(uint n); 387 virtual void SetValue(longlong n); 388 virtual void SetValue(ulonglong n); 389 virtual void SetValue(double f); 390 virtual void SetBinValue(void *p); 391 virtual void SetBinValue(void* p, ulong len); 392 virtual bool GetBinValue(void *buf, int buflen, bool go); CompareValue(PVAL)393 virtual int CompareValue(PVAL) {assert(false); return 0;} 394 virtual int ShowValue(char *buf, int len); 395 virtual char *GetCharString(char *p); 396 virtual bool IsEqual(PVAL vp, bool chktype); 397 virtual bool FormatValue(PVAL vp, PCSZ fmt); 398 virtual bool SetConstFormat(PGLOBAL, FORMAT&); 399 400 protected: 401 // Members 402 void *Binp; 403 char *Chrp; 404 int Len; 405 }; // end of class BINVAL 406 407 /***********************************************************************/ 408 /* Class DTVAL: represents a time stamp value. */ 409 /***********************************************************************/ 410 class DllExport DTVAL : public TYPVAL<int> { 411 public: 412 // Constructors 413 DTVAL(PGLOBAL g, int n, int p, PCSZ fmt); 414 DTVAL(int n); 415 using TYPVAL<int>::SetValue; 416 417 // Implementation 418 virtual bool SetValue_pval(PVAL valp, bool chktype); 419 virtual bool SetValue_char(const char *p, int n); 420 virtual void SetValue_psz(PCSZ s); 421 virtual void SetValue_pvblk(PVBLK blk, int n); 422 virtual void SetValue(int n); GetCharValue(void)423 virtual PSZ GetCharValue(void) { return Sdate; } 424 virtual char *GetCharString(char *p); 425 virtual int ShowValue(char *buf, int len); 426 virtual bool FormatValue(PVAL vp, PCSZ fmt); 427 bool SetFormat(PGLOBAL g, PCSZ fmt, int len, int year = 0); 428 bool SetFormat(PGLOBAL g, PVAL valp); IsFormatted(void)429 bool IsFormatted(void) {return Pdtp != NULL;} 430 bool MakeTime(struct tm *ptm); 431 static void SetTimeShift(void); GetShift(void)432 static int GetShift(void) {return Shift;} 433 434 // Methods 435 bool MakeDate(PGLOBAL g, int *val, int nval); 436 437 struct tm *GetGmTime(struct tm *); 438 439 protected: 440 // Default constructor not to be used DTVAL(void)441 DTVAL(void) : TYPVAL<int>() {} 442 443 // Members 444 static int Shift; // Time zone shift in seconds 445 PDTP Pdtp; // To the DATPAR structure 446 char *Sdate; // Utility char buffer 447 int DefYear; // Used by ExtractDate 448 int Len; // Used by CHAR scalar function 449 }; // end of class DTVAL 450 451 #endif // __VALUE__H__ 452