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
|
/************ Valblk C++ Functions Source Code File (.CPP) *************/
/* Name: VALBLK.CPP Version 1.7 */
/* */
/* (C) Copyright to the author Olivier BERTRAND 2005-2013 */
/* */
/* 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, and double. Shortly we should add more types. */
/***********************************************************************/
/***********************************************************************/
/* 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);
/***********************************************************************/
/* 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)
{
PVBLK blkp;
#ifdef DEBTRACE
htrc("AVB: mp=%p type=%d nval=%d len=%d check=%u blank=%u\n",
mp, type, nval, len, check, blank);
#endif
switch (type) {
case TYPE_STRING:
if (len)
blkp = new(g) CHRBLK(mp, nval, len, prec, blank);
else
blkp = new(g) STRBLK(g, mp, nval);
break;
case TYPE_SHORT:
blkp = new(g) TYPBLK<short>(mp, nval, type);
break;
case TYPE_INT:
blkp = new(g) TYPBLK<int>(mp, nval, type);
break;
case TYPE_DATE: // ?????
blkp = new(g) DATBLK(mp, nval);
break;
case TYPE_BIGINT:
blkp = new(g) TYPBLK<longlong>(mp, nval, type);
break;
case TYPE_FLOAT:
blkp = new(g) TYPBLK<double>(mp, nval, prec, type);
break;
case TYPE_TINY:
blkp = new(g) TYPBLK<char>(mp, nval, type);
break;
default:
sprintf(g->Message, MSG(BAD_VALBLK_TYPE), type);
return NULL;
} // endswitch Type
blkp->Init(g, check);
return blkp;
} // end of AllocValBlock
/* -------------------------- Class VALBLK --------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
VALBLK::VALBLK(void *mp, int type, int nval)
{
Blkp = mp;
To_Nulls = NULL;
Check = true;
Nullable = false;
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
/***********************************************************************/
/* 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()) {
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()) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(VALTYPE_NOMATCH));
longjmp(g->jumper[g->jump_level], Type);
} // endif Type
} // end of ChkTyp
/* -------------------------- Class TYPBLK --------------------------- */
/***********************************************************************/
/* Constructors. */
/***********************************************************************/
template <class TYPE>
TYPBLK<TYPE>::TYPBLK(void *mp, int nval, int type)
: VALBLK(mp, type, nval), Typp((TYPE*&)Blkp)
{
Fmt = GetFmt(Type);
} // end of TYPBLK constructor
template <class TYPE>
TYPBLK<TYPE>::TYPBLK(void *mp, int nval, int prec, int type)
: VALBLK(mp, type, nval), Typp((TYPE*&)Blkp)
{
DBUG_ASSERT(Type == TYPE_FLOAT);
Prec = prec;
Fmt = GetFmt(Type);
} // end of DBLBLK constructor
/***********************************************************************/
/* Initialization routine. */
/***********************************************************************/
template <class TYPE>
void TYPBLK<TYPE>::Init(PGLOBAL g, bool check)
{
if (!Blkp)
Blkp = PlugSubAlloc(g, NULL, Nval * sizeof(TYPE));
Check = check;
Global = g;
} // end of Init
/***********************************************************************/
/* 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() && Nullable))
Typp[n] = GetTypedValue(valp);
else
Reset(n);
SetNull(n, b);
} // end of SetValue
template <>
int TYPBLK<int>::GetTypedValue(PVAL valp)
{return valp->GetIntValue();}
template <>
short TYPBLK<short>::GetTypedValue(PVAL valp)
{return valp->GetShortValue();}
template <>
longlong TYPBLK<longlong>::GetTypedValue(PVAL valp)
{return valp->GetBigintValue();}
template <>
double TYPBLK<double>::GetTypedValue(PVAL valp)
{return valp->GetFloatValue();}
template <>
char TYPBLK<char>::GetTypedValue(PVAL valp)
{return valp->GetTinyValue();}
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
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
Typp[n] = GetTypedValue(p);
SetNull(n, false);
} // end of SetValue
template <>
int TYPBLK<int>::GetTypedValue(PSZ p) {return atol(p);}
template <>
short TYPBLK<short>::GetTypedValue(PSZ p) {return (short)atoi(p);}
template <>
longlong TYPBLK<longlong>::GetTypedValue(PSZ p) {return atoll(p);}
template <>
double TYPBLK<double>::GetTypedValue(PSZ p) {return atof(p);}
template <>
char TYPBLK<char>::GetTypedValue(PSZ p) {return (char)atoi(p);}
/***********************************************************************/
/* 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 <>
short TYPBLK<short>::GetTypedValue(PVBLK blk, int n)
{return blk->GetShortValue(n);}
template <>
longlong TYPBLK<longlong>::GetTypedValue(PVBLK blk, int n)
{return blk->GetBigintValue(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);}
#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[12];
int i, n;
for (i = n = 0; i < Nval; i++) {
sprintf(buf, Fmt, Typp[i]);
n = max(n, (signed)strlen(buf));
} // 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. */
/***********************************************************************/
void CHRBLK::Init(PGLOBAL g, bool check)
{
Valp = (char*)PlugSubAlloc(g, NULL, Long + 1);
Valp[Long] = '\0';
if (!Blkp)
Blkp = PlugSubAlloc(g, NULL, Nval * Long);
Check = check;
Global = g;
} // 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 short. */
/***********************************************************************/
short CHRBLK::GetShortValue(int n)
{
return (short)atoi((char *)GetValPtrEx(n));
} // end of GetShortValue
/***********************************************************************/
/* Return the value of the nth element converted to int. */
/***********************************************************************/
int CHRBLK::GetIntValue(int n)
{
return atol((char *)GetValPtrEx(n));
} // end of GetIntValue
/***********************************************************************/
/* Return the value of the nth element converted to big int. */
/***********************************************************************/
longlong CHRBLK::GetBigintValue(int n)
{
return atoll((char *)GetValPtrEx(n));
} // end of GetBigintValue
/***********************************************************************/
/* Return the value of the nth element converted to double. */
/***********************************************************************/
double CHRBLK::GetFloatValue(int n)
{
return atof((char *)GetValPtrEx(n));
} // end of GetFloatValue
/***********************************************************************/
/* Return the value of the nth element converted to tiny int. */
/***********************************************************************/
char CHRBLK::GetTinyValue(int n)
{
return (char)atoi((char *)GetValPtrEx(n));
} // end of GetTinyValue
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
void CHRBLK::SetValue(PVAL valp, int n)
{
bool b;
ChkIndx(n);
ChkTyp(valp);
if (!(b = valp->IsNull() && Nullable))
SetValue((PSZ)valp->GetCharValue(), n);
else
Reset(n);
SetNull(n, b);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
void CHRBLK::SetValue(PSZ sp, int n)
{
size_t len = (sp) ? strlen(sp) : 0;
char *p = Chrp + n * Long;
#if defined(_DEBUG) || defined(DEBTRACE)
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
if (sp)
strncpy(p, sp, Long);
else
*p = '\0';
if (Blanks)
// Suppress eventual ending zero and right fill with blanks
for (register int i = len; i < Long; i++)
p[i] = ' ';
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) && Nullable))
memcpy(Chrp + n1 * Long, ((CHRBLK*)pv)->Chrp + n2 * Long, Long);
else
Reset(n1);
SetNull(n1, b);
} // end of SetValue
#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) || defined(DEBTRACE)
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
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)
{
memcpy(Chrp + j * Long, Chrp + i * Long, Long);
MoveNull(i, j);
} // 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 == ' ' && p >= Valp; 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 = 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;
} // end of STRBLK constructor
/***********************************************************************/
/* Initialization routine. */
/***********************************************************************/
void STRBLK::Init(PGLOBAL g, bool check)
{
if (!Blkp)
Blkp = PlugSubAlloc(g, NULL, Nval * sizeof(PSZ));
Check = check;
Global = g;
} // end of Init
/***********************************************************************/
/* 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. */
/***********************************************************************/
void STRBLK::SetValue(PSZ p, int n)
{
Strp[n] = (PSZ)PlugSubAlloc(Global, NULL, strlen(p) + 1);
strcpy(Strp[n], p);
} // end of SetValue
/***********************************************************************/
/* 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 = 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, fmt)))
return true;
return false;
} // end of SetFormat
/***********************************************************************/
/* 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
/* ------------------------- End of Valblk --------------------------- */
|