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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
|
/************ Valblk C++ Functions Source Code File (.CPP) *************/
/* Name: VALBLK.CPP Version 2.1 */
/* */
/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
/* */
/* This file contains the VALBLK and derived classes functions. */
/* Second family is VALBLK, representing simple suballocated arrays */
/* of values treated sequentially by FIX, BIN and VCT tables and */
/* columns, as well for min/max blocks as for VCT column blocks. */
/* Q&A: why not using only one family ? Simple values are arrays that */
/* have only one element and arrays could have functions for all kind */
/* of processing. The answer is a-because historically it was simpler */
/* to do that way, b-because of performance on single values, and c- */
/* to avoid too complicated classes and unuseful duplication of many */
/* functions used on one family only. The drawback is that for new */
/* types of objects, we shall have more classes to update. */
/* This is why we are now using a template class for many types. */
/* Currently the only implemented types are PSZ, chars, int, short, */
/* DATE, longlong, double and tiny. Fix numeric ones can be unsigned. */
/***********************************************************************/
/***********************************************************************/
/* Include relevant MariaDB header file. */
/***********************************************************************/
#include "my_global.h"
#if defined(WIN32)
//#include <windows.h>
#else
#include "osutil.h"
#include "string.h"
#endif
/***********************************************************************/
/* Include required application header files */
/* global.h is header containing all global Plug declarations. */
/* plgdbsem.h is header containing the DB applic. declarations. */
/* valblk.h is header containing VALBLK derived classes declares. */
/***********************************************************************/
#include "global.h"
#include "plgdbsem.h"
#include "valblk.h"
#define CheckBlanks assert(!Blanks);
#define CheckParms(V, N) ChkIndx(N); ChkTyp(V);
extern MBLOCK Nmblk; /* Used to initialize MBLOCK's */
/***********************************************************************/
/* AllocValBlock: allocate a VALBLK according to type. */
/***********************************************************************/
PVBLK AllocValBlock(PGLOBAL g, void *mp, int type, int nval, int len,
int prec, bool check, bool blank, bool un)
{
PVBLK blkp;
if (trace)
htrc("AVB: mp=%p type=%d nval=%d len=%d check=%u blank=%u\n",
mp, type, nval, len, check, blank);
switch (type) {
case TYPE_STRING:
case TYPE_DECIM:
if (len)
blkp = new(g) CHRBLK(mp, nval, len, prec, blank);
else
blkp = new(g) STRBLK(g, mp, nval);
break;
case TYPE_SHORT:
if (un)
blkp = new(g) TYPBLK<ushort>(mp, nval, type, 0, true);
else
blkp = new(g) TYPBLK<short>(mp, nval, type);
break;
case TYPE_INT:
if (un)
blkp = new(g) TYPBLK<uint>(mp, nval, type, 0, true);
else
blkp = new(g) TYPBLK<int>(mp, nval, type);
break;
case TYPE_DATE: // ?????
blkp = new(g) DATBLK(mp, nval);
break;
case TYPE_BIGINT:
if (un)
blkp = new(g) TYPBLK<ulonglong>(mp, nval, type, 0, true);
else
blkp = new(g) TYPBLK<longlong>(mp, nval, type);
break;
case TYPE_DOUBLE:
blkp = new(g) TYPBLK<double>(mp, nval, type, prec);
break;
case TYPE_TINY:
if (un)
blkp = new(g) TYPBLK<uchar>(mp, nval, type, 0, true);
else
blkp = new(g) TYPBLK<char>(mp, nval, type);
break;
case TYPE_PCHAR:
blkp = new(g) PTRBLK(g, mp, nval);
break;
default:
sprintf(g->Message, MSG(BAD_VALBLK_TYPE), type);
return NULL;
} // endswitch Type
return (blkp->Init(g, check)) ? NULL : blkp;
} // end of AllocValBlock
/* -------------------------- Class VALBLK --------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
VALBLK::VALBLK(void *mp, int type, int nval, bool un)
{
Mblk = Nmblk;
Blkp = mp;
To_Nulls = NULL;
Check = true;
Nullable = false;
Unsigned = un;
Type = type;
Nval = nval;
Prec = 0;
} // end of VALBLK constructor
/***********************************************************************/
/* Raise error for numeric types. */
/***********************************************************************/
PSZ VALBLK::GetCharValue(int n)
{
PGLOBAL& g = Global;
assert(g);
sprintf(g->Message, MSG(NO_CHAR_FROM), Type);
longjmp(g->jumper[g->jump_level], Type);
return NULL;
} // end of GetCharValue
/***********************************************************************/
/* Set format so formatted dates can be converted on input. */
/***********************************************************************/
bool VALBLK::SetFormat(PGLOBAL g, PSZ fmt, int len, int year)
{
sprintf(g->Message, MSG(NO_DATE_FMT), Type);
return true;
} // end of SetFormat
/***********************************************************************/
/* Set the index of the location of value and return true if found. */
/* To be used on ascending sorted arrays only. */
/* Currently used by some BLKFIL classes only. */
/***********************************************************************/
bool VALBLK::Locate(PVAL vp, int& i)
{
ChkTyp(vp);
int n = 1;
for (i = 0; i < Nval; i++)
if ((n = CompVal(vp, i)) <= 0)
break;
return (!n);
} // end of Locate
/***********************************************************************/
/* Set Nullable and allocate the Null array. */
/***********************************************************************/
void VALBLK::SetNullable(bool b)
{
if ((Nullable = b)) {
To_Nulls = (char*)PlugSubAlloc(Global, NULL, Nval);
memset(To_Nulls, 0, Nval);
} else
To_Nulls = NULL;
} // end of SetNullable
/***********************************************************************/
/* Buffer allocation routine. */
/***********************************************************************/
bool VALBLK::AllocBuff(PGLOBAL g, size_t size)
{
Mblk.Size = size;
if (!(Blkp = PlgDBalloc(g, NULL, Mblk))) {
sprintf(g->Message, MSG(MEM_ALLOC_ERR), "Blkp", (int) Mblk.Size);
fprintf(stderr, "%s\n", g->Message);
return true;
} // endif Blkp
return false;
} // end of AllocBuff
/***********************************************************************/
/* Check functions. */
/***********************************************************************/
void VALBLK::ChkIndx(int n)
{
if (n < 0 || n >= Nval) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(BAD_VALBLK_INDX));
longjmp(g->jumper[g->jump_level], Type);
} // endif n
} // end of ChkIndx
void VALBLK::ChkTyp(PVAL v)
{
if (Check && (Type != v->GetType() || Unsigned != v->IsUnsigned())) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(VALTYPE_NOMATCH));
longjmp(g->jumper[g->jump_level], Type);
} // endif Type
} // end of ChkTyp
void VALBLK::ChkTyp(PVBLK vb)
{
if (Check && (Type != vb->GetType() || Unsigned != vb->IsUnsigned())) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(VALTYPE_NOMATCH));
longjmp(g->jumper[g->jump_level], Type);
} // endif Type
} // end of ChkTyp
/* -------------------------- Class TYPBLK --------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
template <class TYPE>
TYPBLK<TYPE>::TYPBLK(void *mp, int nval, int type, int prec, bool un)
: VALBLK(mp, type, nval, un), Typp((TYPE*&)Blkp)
{
Prec = prec;
Fmt = GetFmt(Type);
} // end of TYPBLK constructor
/***********************************************************************/
/* Initialization routine. */
/***********************************************************************/
template <class TYPE>
bool TYPBLK<TYPE>::Init(PGLOBAL g, bool check)
{
if (!Blkp)
if (AllocBuff(g, Nval * sizeof(TYPE)))
return true;
Check = check;
Global = g;
return false;
} // end of Init
/***********************************************************************/
/* TYPVAL GetCharString: get string representation of a typed value. */
/***********************************************************************/
template <class TYPE>
char *TYPBLK<TYPE>::GetCharString(char *p, int n)
{
sprintf(p, Fmt, Typp[n]);
return p;
} // end of GetCharString
template <>
char *TYPBLK<double>::GetCharString(char *p, int n)
{
sprintf(p, Fmt, Prec, Typp[n]);
return p;
} // end of GetCharString
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::SetValue(PVAL valp, int n)
{
bool b;
ChkIndx(n);
ChkTyp(valp);
if (!(b = valp->IsNull()))
Typp[n] = GetTypedValue(valp);
else
Reset(n);
SetNull(n, b && Nullable);
} // end of SetValue
template <>
int TYPBLK<int>::GetTypedValue(PVAL valp)
{return valp->GetIntValue();}
template <>
uint TYPBLK<uint>::GetTypedValue(PVAL valp)
{return valp->GetUIntValue();}
template <>
short TYPBLK<short>::GetTypedValue(PVAL valp)
{return valp->GetShortValue();}
template <>
ushort TYPBLK<ushort>::GetTypedValue(PVAL valp)
{return valp->GetUShortValue();}
template <>
longlong TYPBLK<longlong>::GetTypedValue(PVAL valp)
{return valp->GetBigintValue();}
template <>
ulonglong TYPBLK<ulonglong>::GetTypedValue(PVAL valp)
{return valp->GetUBigintValue();}
template <>
double TYPBLK<double>::GetTypedValue(PVAL valp)
{return valp->GetFloatValue();}
template <>
char TYPBLK<char>::GetTypedValue(PVAL valp)
{return valp->GetTinyValue();}
template <>
uchar TYPBLK<uchar>::GetTypedValue(PVAL valp)
{return valp->GetUTinyValue();}
/***********************************************************************/
/* Set one value in a block from a zero terminated string. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::SetValue(PSZ p, int n)
{
ChkIndx(n);
if (Check) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(BAD_SET_STRING));
longjmp(g->jumper[g->jump_level], Type);
} // endif Check
bool minus;
ulonglong maxval = MaxVal();
ulonglong val = CharToNumber(p, strlen(p), maxval, Unsigned, &minus);
if (minus && val < maxval)
Typp[n] = (TYPE)(-(signed)val);
else
Typp[n] = (TYPE)val;
SetNull(n, false);
} // end of SetValue
template <class TYPE>
ulonglong TYPBLK<TYPE>::MaxVal(void) {DBUG_ASSERT(false); return 0;}
template <>
ulonglong TYPBLK<short>::MaxVal(void) {return INT_MAX16;}
template <>
ulonglong TYPBLK<ushort>::MaxVal(void) {return UINT_MAX16;}
template <>
ulonglong TYPBLK<int>::MaxVal(void) {return INT_MAX32;}
template <>
ulonglong TYPBLK<uint>::MaxVal(void) {return UINT_MAX32;}
template <>
ulonglong TYPBLK<char>::MaxVal(void) {return INT_MAX8;}
template <>
ulonglong TYPBLK<uchar>::MaxVal(void) {return UINT_MAX8;}
template <>
ulonglong TYPBLK<longlong>::MaxVal(void) {return INT_MAX64;}
template <>
ulonglong TYPBLK<ulonglong>::MaxVal(void) {return ULONGLONG_MAX;}
template <>
void TYPBLK<double>::SetValue(PSZ p, int n)
{
ChkIndx(n);
if (Check) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(BAD_SET_STRING));
longjmp(g->jumper[g->jump_level], Type);
} // endif Check
Typp[n] = atof(p);
SetNull(n, false);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block from an array of characters. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::SetValue(char *sp, uint len, int n)
{
PGLOBAL& g = Global;
PSZ spz = (PSZ)PlugSubAlloc(g, NULL, 0); // Temporary
if (sp)
memcpy(spz, sp, len);
spz[len] = 0;
SetValue(spz, n);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block from a value in another block. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::SetValue(PVBLK pv, int n1, int n2)
{
bool b;
ChkIndx(n1);
ChkTyp(pv);
if (!(b = pv->IsNull(n2) && Nullable))
Typp[n1] = GetTypedValue(pv, n2);
else
Reset(n1);
SetNull(n1, b);
} // end of SetValue
template <>
int TYPBLK<int>::GetTypedValue(PVBLK blk, int n)
{return blk->GetIntValue(n);}
template <>
uint TYPBLK<uint>::GetTypedValue(PVBLK blk, int n)
{return blk->GetUIntValue(n);}
template <>
short TYPBLK<short>::GetTypedValue(PVBLK blk, int n)
{return blk->GetShortValue(n);}
template <>
ushort TYPBLK<ushort>::GetTypedValue(PVBLK blk, int n)
{return blk->GetUShortValue(n);}
template <>
longlong TYPBLK<longlong>::GetTypedValue(PVBLK blk, int n)
{return blk->GetBigintValue(n);}
template <>
ulonglong TYPBLK<ulonglong>::GetTypedValue(PVBLK blk, int n)
{return blk->GetUBigintValue(n);}
template <>
double TYPBLK<double>::GetTypedValue(PVBLK blk, int n)
{return blk->GetFloatValue(n);}
template <>
char TYPBLK<char>::GetTypedValue(PVBLK blk, int n)
{return blk->GetTinyValue(n);}
template <>
uchar TYPBLK<uchar>::GetTypedValue(PVBLK blk, int n)
{return blk->GetUTinyValue(n);}
/***********************************************************************/
/* Set one value in a block if val is less than the current value. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::SetMin(PVAL valp, int n)
{
CheckParms(valp, n)
TYPE tval = GetTypedValue(valp);
TYPE& tmin = Typp[n];
if (tval < tmin)
tmin = tval;
} // end of SetMin
/***********************************************************************/
/* Set one value in a block if val is greater than the current value. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::SetMax(PVAL valp, int n)
{
CheckParms(valp, n)
TYPE tval = GetTypedValue(valp);
TYPE& tmin = Typp[n];
if (tval > tmin)
tmin = tval;
} // end of SetMax
#if 0
/***********************************************************************/
/* Set many values in a block from values in another block. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::SetValues(PVBLK pv, int k, int n)
{
CheckType(pv)
TYPE *lp = ((TYPBLK*)pv)->Typp;
for (register int i = k; i < n; i++) // TODO
Typp[i] = lp[i];
} // end of SetValues
#endif // 0
/***********************************************************************/
/* Move one value from i to j. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::Move(int i, int j)
{
Typp[j] = Typp[i];
MoveNull(i, j);
} // end of Move
/***********************************************************************/
/* Compare a Value object with the nth value of the block. */
/***********************************************************************/
template <class TYPE>
int TYPBLK<TYPE>::CompVal(PVAL vp, int n)
{
#if defined(_DEBUG)
ChkIndx(n);
ChkTyp(vp);
#endif // _DEBUG
TYPE mlv = Typp[n];
TYPE vlv = GetTypedValue(vp);
return (vlv > mlv) ? 1 : (vlv < mlv) ? (-1) : 0;
} // end of CompVal
/***********************************************************************/
/* Compare two values of the block. */
/***********************************************************************/
template <class TYPE>
int TYPBLK<TYPE>::CompVal(int i1, int i2)
{
TYPE lv1 = Typp[i1];
TYPE lv2 = Typp[i2];
return (lv1 > lv2) ? 1 : (lv1 < lv2) ? (-1) : 0;
} // end of CompVal
/***********************************************************************/
/* Get a pointer on the nth value of the block. */
/***********************************************************************/
template <class TYPE>
void *TYPBLK<TYPE>::GetValPtr(int n)
{
ChkIndx(n);
return Typp + n;
} // end of GetValPtr
/***********************************************************************/
/* Get a pointer on the nth value of the block. */
/***********************************************************************/
template <class TYPE>
void *TYPBLK<TYPE>::GetValPtrEx(int n)
{
ChkIndx(n);
return Typp + n;
} // end of GetValPtrEx
/***********************************************************************/
/* Returns index of matching value in block or -1. */
/***********************************************************************/
template <class TYPE>
int TYPBLK<TYPE>::Find(PVAL vp)
{
ChkTyp(vp);
int i;
TYPE n = GetTypedValue(vp);
for (i = 0; i < Nval; i++)
if (n == Typp[i])
break;
return (i < Nval) ? i : (-1);
} // end of Find
/***********************************************************************/
/* Returns the length of the longest string in the block. */
/***********************************************************************/
template <class TYPE>
int TYPBLK<TYPE>::GetMaxLength(void)
{
char buf[64];
int i, n, m;
for (i = n = 0; i < Nval; i++) {
m = sprintf(buf, Fmt, Typp[i]);
n = MY_MAX(n, m);
} // endfor i
return n;
} // end of GetMaxLength
/* -------------------------- Class CHRBLK --------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
CHRBLK::CHRBLK(void *mp, int nval, int len, int prec, bool blank)
: VALBLK(mp, TYPE_STRING, nval), Chrp((char*&)Blkp)
{
Valp = NULL;
Blanks = blank;
Ci = (prec != 0);
Long = len;
} // end of CHRBLK constructor
/***********************************************************************/
/* Initialization routine. */
/***********************************************************************/
bool CHRBLK::Init(PGLOBAL g, bool check)
{
Valp = (char*)PlugSubAlloc(g, NULL, Long + 1);
Valp[Long] = '\0';
if (!Blkp)
if (AllocBuff(g, Nval * Long))
return true;
Check = check;
Global = g;
return false;
} // end of Init
/***********************************************************************/
/* Reset nth element to a null string. */
/***********************************************************************/
void CHRBLK::Reset(int n)
{
if (Blanks)
memset(Chrp + n * Long, ' ', Long);
else
*(Chrp + n * Long) = '\0';
} // end of Reset
/***********************************************************************/
/* Return the zero ending value of the nth element. */
/***********************************************************************/
char *CHRBLK::GetCharValue(int n)
{
return (char *)GetValPtrEx(n);
} // end of GetCharValue
/***********************************************************************/
/* Return the value of the nth element converted to tiny int. */
/***********************************************************************/
char CHRBLK::GetTinyValue(int n)
{
bool m;
ulonglong val = CharToNumber((char*)GetValPtr(n), Long, INT_MAX8,
false, &m);
return (m && val < INT_MAX8) ? (char)(-(signed)val) : (char)val;
} // end of GetTinyValue
/***********************************************************************/
/* Return the value of the nth element converted to unsigned tiny int.*/
/***********************************************************************/
uchar CHRBLK::GetUTinyValue(int n)
{
return (uchar)CharToNumber((char*)GetValPtr(n), Long, UINT_MAX8, true);
} // end of GetTinyValue
/***********************************************************************/
/* Return the value of the nth element converted to short. */
/***********************************************************************/
short CHRBLK::GetShortValue(int n)
{
bool m;
ulonglong val = CharToNumber((char*)GetValPtr(n), Long, INT_MAX16,
false, &m);
return (m && val < INT_MAX16) ? (short)(-(signed)val) : (short)val;
} // end of GetShortValue
/***********************************************************************/
/* Return the value of the nth element converted to ushort. */
/***********************************************************************/
ushort CHRBLK::GetUShortValue(int n)
{
return (ushort)CharToNumber((char*)GetValPtr(n), Long, UINT_MAX16, true);
} // end of GetShortValue
/***********************************************************************/
/* Return the value of the nth element converted to int. */
/***********************************************************************/
int CHRBLK::GetIntValue(int n)
{
bool m;
ulonglong val = CharToNumber((char*)GetValPtr(n), Long, INT_MAX32,
false, &m);
return (m && val < INT_MAX32) ? (int)(-(signed)val) : (int)val;
} // end of GetIntValue
/***********************************************************************/
/* Return the value of the nth element converted to uint. */
/***********************************************************************/
uint CHRBLK::GetUIntValue(int n)
{
return (uint)CharToNumber((char*)GetValPtr(n), Long, UINT_MAX32, true);
} // end of GetIntValue
/***********************************************************************/
/* Return the value of the nth element converted to big int. */
/***********************************************************************/
longlong CHRBLK::GetBigintValue(int n)
{
bool m;
ulonglong val = CharToNumber((char*)GetValPtr(n), Long, INT_MAX64,
false, &m);
return (m && val < INT_MAX64) ? (longlong)(-(signed)val) : (longlong)val;
} // end of GetBigintValue
/***********************************************************************/
/* Return the value of the nth element converted to unsigned big int. */
/***********************************************************************/
ulonglong CHRBLK::GetUBigintValue(int n)
{
return CharToNumber((char*)GetValPtr(n), Long, ULONGLONG_MAX, true);
} // end of GetUBigintValue
/***********************************************************************/
/* Return the value of the nth element converted to double. */
/***********************************************************************/
double CHRBLK::GetFloatValue(int n)
{
return atof((char *)GetValPtrEx(n));
} // end of GetFloatValue
/***********************************************************************/
/* STRING GetCharString: get string representation of a char value. */
/***********************************************************************/
char *CHRBLK::GetCharString(char *p, int n)
{
return (char *)GetValPtrEx(n);
} // end of GetCharString
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
void CHRBLK::SetValue(PVAL valp, int n)
{
bool b;
ChkIndx(n);
ChkTyp(valp);
if (!(b = valp->IsNull()))
SetValue((PSZ)valp->GetCharValue(), n);
else
Reset(n);
SetNull(n, b && Nullable);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block from a zero terminated string. */
/***********************************************************************/
void CHRBLK::SetValue(PSZ sp, int n)
{
uint len = (sp) ? strlen(sp) : 0;
SetValue(sp, len, n);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block from an array of characters. */
/***********************************************************************/
void CHRBLK::SetValue(char *sp, uint len, int n)
{
char *p = Chrp + n * Long;
#if defined(_DEBUG)
if (Check && (signed)len > Long) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(SET_STR_TRUNC));
longjmp(g->jumper[g->jump_level], Type);
} // endif Check
#endif // _DEBUG
if (sp)
memcpy(p, sp, MY_MIN((unsigned)Long, len));
if (Blanks) {
// Suppress eventual ending zero and right fill with blanks
for (register int i = len; i < Long; i++)
p[i] = ' ';
} else if ((signed)len < Long)
p[len] = 0;
SetNull(n, false);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block from a value in another block. */
/***********************************************************************/
void CHRBLK::SetValue(PVBLK pv, int n1, int n2)
{
bool b;
if (Type != pv->GetType() || Long != ((CHRBLK*)pv)->Long) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(BLKTYPLEN_MISM));
longjmp(g->jumper[g->jump_level], Type);
} // endif Type
if (!(b = pv->IsNull(n2)))
memcpy(Chrp + n1 * Long, ((CHRBLK*)pv)->Chrp + n2 * Long, Long);
else
Reset(n1);
SetNull(n1, b && Nullable);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block if val is less than the current value. */
/***********************************************************************/
void CHRBLK::SetMin(PVAL valp, int n)
{
CheckParms(valp, n)
CheckBlanks
char *vp = valp->GetCharValue();
char *bp = Chrp + n * Long;
if (((Ci) ? strnicmp(vp, bp, Long) : strncmp(vp, bp, Long)) < 0)
memcpy(bp, vp, Long);
} // end of SetMin
/***********************************************************************/
/* Set one value in a block if val is greater than the current value. */
/***********************************************************************/
void CHRBLK::SetMax(PVAL valp, int n)
{
CheckParms(valp, n)
CheckBlanks
char *vp = valp->GetCharValue();
char *bp = Chrp + n * Long;
if (((Ci) ? strnicmp(vp, bp, Long) : strncmp(vp, bp, Long)) > 0)
memcpy(bp, vp, Long);
} // end of SetMax
#if 0
/***********************************************************************/
/* Set many values in a block from values in another block. */
/***********************************************************************/
void CHRBLK::SetValues(PVBLK pv, int k, int n)
{
#if defined(_DEBUG)
if (Type != pv->GetType() || Long != ((CHRBLK*)pv)->Long) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(BLKTYPLEN_MISM));
longjmp(g->jumper[g->jump_level], Type);
} // endif Type
#endif // _DEBUG
char *p = ((CHRBLK*)pv)->Chrp;
if (!k)
memcpy(Chrp, p, Long * n);
else
memcpy(Chrp + k * Long, p + k * Long, Long * (n - k));
} // end of SetValues
#endif // 0
/***********************************************************************/
/* Move one value from i to j. */
/***********************************************************************/
void CHRBLK::Move(int i, int j)
{
if (i != j) {
memcpy(Chrp + j * Long, Chrp + i * Long, Long);
MoveNull(i, j);
} // endif i
} // end of Move
/***********************************************************************/
/* Compare a Value object with the nth value of the block. */
/***********************************************************************/
int CHRBLK::CompVal(PVAL vp, int n)
{
ChkIndx(n);
ChkTyp(vp);
char *xvp = vp->GetCharValue(); // Get Value zero ended string
bool ci = Ci || vp->IsCi(); // true if is case insensitive
GetValPtrEx(n); // Get a zero ended string in Valp
return (ci) ? stricmp(xvp, Valp) : strcmp(xvp, Valp);
} // end of CompVal
/***********************************************************************/
/* Compare two values of the block. */
/***********************************************************************/
int CHRBLK::CompVal(int i1, int i2)
{
return (Ci) ? strnicmp(Chrp + i1 * Long, Chrp + i2 * Long, Long)
: strncmp(Chrp + i1 * Long, Chrp + i2 * Long, Long);
} // end of CompVal
/***********************************************************************/
/* Get a pointer on the nth value of the block. */
/***********************************************************************/
void *CHRBLK::GetValPtr(int n)
{
ChkIndx(n);
return Chrp + n * Long;
} // end of GetValPtr
/***********************************************************************/
/* Get a pointer on a zero ended string equal to nth value. */
/***********************************************************************/
void *CHRBLK::GetValPtrEx(int n)
{
ChkIndx(n);
memcpy(Valp, Chrp + n * Long, Long);
if (IsNull(n))
return const_cast<char *>("");
if (Blanks) {
// The (fast) way this is done works only for blocks such
// as Min and Max where strings are stored with the ending 0
// except for those whose length is equal to Len.
// For VCT blocks we must remove rightmost blanks.
char *p = Valp + Long;
for (p--; p >= Valp && *p == ' '; p--) ;
*(++p) = '\0';
} // endif Blanks
return Valp;
} // end of GetValPtrEx
/***********************************************************************/
/* Returns index of matching value in block or -1. */
/***********************************************************************/
int CHRBLK::Find(PVAL vp)
{
ChkTyp(vp);
int i;
bool ci = Ci || vp->IsCi();
PSZ s = vp->GetCharValue();
if (vp->IsNull())
return -1;
for (i = 0; i < Nval; i++) {
if (IsNull(i))
continue;
GetValPtrEx(i); // Get a zero ended string in Valp
if (!((ci) ? strnicmp(s, Valp, Long) : strncmp(s, Valp, Long)))
break;
} // endfor i
return (i < Nval) ? i : (-1);
} // end of Find
/***********************************************************************/
/* Returns the length of the longest string in the block. */
/***********************************************************************/
int CHRBLK::GetMaxLength(void)
{
int i, n;
for (i = n = 0; i < Nval; i++)
if (!IsNull(i)) {
GetValPtrEx(i);
n = MY_MAX(n, (signed)strlen(Valp));
} // endif null
return n;
} // end of GetMaxLength
/* -------------------------- Class STRBLK --------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
STRBLK::STRBLK(PGLOBAL g, void *mp, int nval)
: VALBLK(mp, TYPE_STRING, nval), Strp((PSZ*&)Blkp)
{
Global = g;
Nullable = true;
Sorted = false;
} // end of STRBLK constructor
/***********************************************************************/
/* Initialization routine. */
/***********************************************************************/
bool STRBLK::Init(PGLOBAL g, bool check)
{
if (!Blkp)
if (AllocBuff(g, Nval * sizeof(PSZ)))
return true;
Check = check;
Global = g;
return false;
} // end of Init
/***********************************************************************/
/* Get the tiny value represented by the Strp string. */
/***********************************************************************/
char STRBLK::GetTinyValue(int n)
{
bool m;
ulonglong val = CharToNumber(Strp[n], strlen(Strp[n]), INT_MAX8,
false, &m);
return (m && val < INT_MAX8) ? (char)(-(signed)val) : (char)val;
} // end of GetTinyValue
/***********************************************************************/
/* Get the unsigned tiny value represented by the Strp string. */
/***********************************************************************/
uchar STRBLK::GetUTinyValue(int n)
{
return (uchar)CharToNumber(Strp[n], strlen(Strp[n]), UINT_MAX8, true);
} // end of GetUTinyValue
/***********************************************************************/
/* Get the short value represented by the Strp string. */
/***********************************************************************/
short STRBLK::GetShortValue(int n)
{
bool m;
ulonglong val = CharToNumber(Strp[n], strlen(Strp[n]), INT_MAX16,
false, &m);
return (m && val < INT_MAX16) ? (short)(-(signed)val) : (short)val;
} // end of GetShortValue
/***********************************************************************/
/* Get the unsigned short value represented by the Strp string. */
/***********************************************************************/
ushort STRBLK::GetUShortValue(int n)
{
return (ushort)CharToNumber(Strp[n], strlen(Strp[n]), UINT_MAX16, true);
} // end of GetUshortValue
/***********************************************************************/
/* Get the integer value represented by the Strp string. */
/***********************************************************************/
int STRBLK::GetIntValue(int n)
{
bool m;
ulonglong val = CharToNumber(Strp[n], strlen(Strp[n]), INT_MAX32,
false, &m);
return (m && val < INT_MAX32) ? (int)(-(signed)val) : (int)val;
} // end of GetIntValue
/***********************************************************************/
/* Get the unsigned integer value represented by the Strp string. */
/***********************************************************************/
uint STRBLK::GetUIntValue(int n)
{
return (uint)CharToNumber(Strp[n], strlen(Strp[n]), UINT_MAX32, true);
} // end of GetUintValue
/***********************************************************************/
/* Get the big integer value represented by the Strp string. */
/***********************************************************************/
longlong STRBLK::GetBigintValue(int n)
{
bool m;
ulonglong val = CharToNumber(Strp[n], strlen(Strp[n]), INT_MAX64,
false, &m);
return (m && val < INT_MAX64) ? (-(signed)val) : (longlong)val;
} // end of GetBigintValue
/***********************************************************************/
/* Get the unsigned big integer value represented by the Strp string. */
/***********************************************************************/
ulonglong STRBLK::GetUBigintValue(int n)
{
return CharToNumber(Strp[n], strlen(Strp[n]), ULONGLONG_MAX, true);
} // end of GetUBigintValue
/***********************************************************************/
/* Set one value in a block from a value in another block. */
/***********************************************************************/
void STRBLK::SetValue(PVBLK pv, int n1, int n2)
{
ChkTyp(pv);
Strp[n1] = (!pv->IsNull(n2)) ? ((STRBLK*)pv)->Strp[n2] : NULL;
} // end of SetValue
#if 0
/***********************************************************************/
/* Set many values in a block from values in another block. */
/***********************************************************************/
void STRBLK::SetValues(PVBLK pv, int k, int n)
{
CheckType(pv)
PSZ *sp = ((STRBLK*)pv)->Strp;
for (register int i = k; i < n; i++)
Strp[i] = (!pv->IsNull(i)) ? sp[i] : NULL;
} // end of SetValues
#endif // 0
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
void STRBLK::SetValue(PVAL valp, int n)
{
ChkIndx(n);
ChkTyp(valp);
if (!valp->IsNull())
SetValue((PSZ)valp->GetCharValue(), n);
else
Strp[n] = NULL;
} // end of SetValue
/***********************************************************************/
/* Set one value in a block from a zero terminated string. */
/***********************************************************************/
void STRBLK::SetValue(PSZ p, int n)
{
if (p) {
if (!Sorted || !n || !Strp[n-1] || strcmp(p, Strp[n-1])) {
Strp[n] = (PSZ)PlugSubAlloc(Global, NULL, strlen(p) + 1);
strcpy(Strp[n], p);
} else
Strp[n] = Strp[n-1];
} else
Strp[n] = NULL;
} // end of SetValue
/***********************************************************************/
/* Set one value in a block from an array of characters. */
/***********************************************************************/
void STRBLK::SetValue(char *sp, uint len, int n)
{
PSZ p;
if (sp) {
if (!Sorted || !n || !Strp[n-1] || strlen(Strp[n-1]) != len ||
strncmp(sp, Strp[n-1], len)) {
p = (PSZ)PlugSubAlloc(Global, NULL, len + 1);
memcpy(p, sp, len);
p[len] = 0;
} else
p = Strp[n-1];
} else
p = NULL;
Strp[n] = p;
} // end of SetValue
/***********************************************************************/
/* Set one value in a block if val is less than the current value. */
/***********************************************************************/
void STRBLK::SetMin(PVAL valp, int n)
{
CheckParms(valp, n)
char *vp = valp->GetCharValue();
char *bp = Strp[n];
if (strcmp(vp, bp) < 0)
SetValue(valp, n);
} // end of SetMin
/***********************************************************************/
/* Set one value in a block if val is greater than the current value. */
/***********************************************************************/
void STRBLK::SetMax(PVAL valp, int n)
{
CheckParms(valp, n)
char *vp = valp->GetCharValue();
char *bp = Strp[n];
if (strcmp(vp, bp) > 0)
SetValue(valp, n);
} // end of SetMax
/***********************************************************************/
/* Move one value from i to j. */
/***********************************************************************/
void STRBLK::Move(int i, int j)
{
Strp[j] = Strp[i];
} // end of Move
/***********************************************************************/
/* Compare a Value object with the nth value of the block. */
/***********************************************************************/
int STRBLK::CompVal(PVAL vp, int n)
{
ChkIndx(n);
ChkTyp(vp);
if (vp->IsNull() || !Strp[n])
DBUG_ASSERT(false);
return strcmp(vp->GetCharValue(), Strp[n]);
} // end of CompVal
/***********************************************************************/
/* Compare two values of the block. */
/***********************************************************************/
int STRBLK::CompVal(int i1, int i2)
{
if (!Strp[i1] || !Strp[i2])
DBUG_ASSERT(false);
return (strcmp(Strp[i1], Strp[i2]));
} // end of CompVal
/***********************************************************************/
/* Get a pointer on the nth value of the block. */
/***********************************************************************/
void *STRBLK::GetValPtr(int n)
{
ChkIndx(n);
return Strp + n;
} // end of GetValPtr
/***********************************************************************/
/* Get a pointer on a zero ended string equal to nth value. */
/***********************************************************************/
void *STRBLK::GetValPtrEx(int n)
{
ChkIndx(n);
return (Strp[n]) ? Strp[n] : const_cast<char*>("");
} // end of GetValPtrEx
/***********************************************************************/
/* Returns index of matching value in block or -1. */
/***********************************************************************/
int STRBLK::Find(PVAL vp)
{
int i;
PSZ s;
ChkTyp(vp);
if (vp->IsNull())
return -1;
else
s = vp->GetCharValue();
for (i = 0; i < Nval; i++)
if (Strp[i] && !strcmp(s, Strp[i]))
break;
return (i < Nval) ? i : (-1);
} // end of Find
/***********************************************************************/
/* Returns the length of the longest string in the block. */
/***********************************************************************/
int STRBLK::GetMaxLength(void)
{
int i, n;
for (i = n = 0; i < Nval; i++)
if (Strp[i])
n = MY_MAX(n, (signed)strlen(Strp[i]));
return n;
} // end of GetMaxLength
/* -------------------------- Class DATBLK --------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
DATBLK::DATBLK(void *mp, int nval) : TYPBLK<int>(mp, nval, TYPE_INT)
{
Type = TYPE_DATE;
Dvalp = NULL;
} // end of DATBLK constructor
/***********************************************************************/
/* Set format so formatted dates can be converted on input. */
/***********************************************************************/
bool DATBLK::SetFormat(PGLOBAL g, PSZ fmt, int len, int year)
{
if (!(Dvalp = AllocateValue(g, TYPE_DATE, len, year, false, fmt)))
return true;
return false;
} // end of SetFormat
/***********************************************************************/
/* DTVAL GetCharString: get string representation of a date value. */
/***********************************************************************/
char *DATBLK::GetCharString(char *p, int n)
{
char *vp;
if (Dvalp) {
Dvalp->SetValue(Typp[n]);
vp = Dvalp->GetCharString(p);
} else
vp = TYPBLK<int>::GetCharString(p, n);
return vp;
} // end of GetCharString
/***********************************************************************/
/* Set one value in a block from a char string. */
/***********************************************************************/
void DATBLK::SetValue(PSZ p, int n)
{
if (Dvalp) {
// Decode the string according to format
Dvalp->SetValue_psz(p);
Typp[n] = Dvalp->GetIntValue();
} else
TYPBLK<int>::SetValue(p, n);
} // end of SetValue
/* -------------------------- Class PTRBLK --------------------------- */
/***********************************************************************/
/* Compare two values of the block. */
/***********************************************************************/
int PTRBLK::CompVal(int i1, int i2)
{
return (Strp[i1] > Strp[i2]) ? 1 : (Strp[i1] < Strp[i2]) ? (-1) : 0;
} // end of CompVal
/* -------------------------- Class MBVALS --------------------------- */
/***********************************************************************/
/* Allocate a value block according to type,len, and nb of values. */
/***********************************************************************/
PVBLK MBVALS::Allocate(PGLOBAL g, int type, int len, int prec,
int n, bool sub)
{
Mblk.Sub = sub;
Mblk.Size = n * GetTypeSize(type, len);
if (!PlgDBalloc(g, NULL, Mblk)) {
sprintf(g->Message, MSG(ALLOC_ERROR), "MBVALS::Allocate");
return NULL;
} else
Vblk = AllocValBlock(g, Mblk.Memp, type, n, len, prec,
TRUE, TRUE, FALSE);
return Vblk;
} // end of Allocate
/***********************************************************************/
/* Reallocate the value block according to the new size. */
/***********************************************************************/
bool MBVALS::ReAllocate(PGLOBAL g, int n)
{
if (!PlgDBrealloc(g, NULL, Mblk, n * Vblk->GetVlen())) {
sprintf(g->Message, MSG(ALLOC_ERROR), "MBVALS::ReAllocate");
return TRUE;
} else
Vblk->ReAlloc(Mblk.Memp, n);
return FALSE;
} // end of ReAllocate
/***********************************************************************/
/* Free the value block. */
/***********************************************************************/
void MBVALS::Free(void)
{
PlgDBfree(Mblk);
Vblk = NULL;
} // end of Free
/* ------------------------- End of Valblk --------------------------- */
|