-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdwnode.cpp
executable file
·1994 lines (1684 loc) · 57.4 KB
/
fdwnode.cpp
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
// A 'C++' class implementation for creating FrontDoor nodelist indices.
// Written by Colin Turner; 2:443/13@fidonet
// FrontDoor is a registered trademark of Joaquim Homrighausen
#include "fdnode.h"
/****************************************************************************/
/* */
/* Class: FrontDoorWNode */
/* Purpose: Writes FrontDoor style index files */
/* */
/* Related Classes: FDNWFile */
/* */
/****************************************************************************/
/****************************************************************************/
/* */
/* P U B L I C F U N C T I O N S */
/* */
/****************************************************************************/
/*
** SetNLDir
**
** Sets internal nodelist directory to specified path if the class is
** frozen.
**
*/
FDNPREF void FDNFUNC FrontDoorWNode::SetNLDir(const char FDNDATA *DirName)
{
if(IsFrozen()){
strcpy(NodelistDir, DirName);
AddTrail(NodelistDir);
}
}
/*
** SetNLExt
**
** Used to set the extension of the official nodelist file NODELIST.???
** if the class is frozen.
**
** Parameters
**
** Specifically a three character 'C' string, Zero padded at left.
** Two special cases exist
**
** PVT - Used to indicate the absence of the official nodelist
** CUR - Use current extenion (not in OverWrite mode).
*/
FDNPREF void FDNFUNC FrontDoorWNode::SetNLExt(const char FDNDATA *nlExt)
{
if(IsFrozen()){
if(strlen(nlExt) != 3) return;
else strcpy(NodeExt, nlExt);
}
}
/*
** Constructors
**
** All call the private function detailed below. See it for more
** more details.
**
*/
FrontDoorWNode::FrontDoorWNode()
{
Constructor("", "", 0, WFDNodeCreateFrozen);
}
FrontDoorWNode::FrontDoorWNode(const char FDNDATA *nldir, const char FDNDATA *nlext, unsigned short cc)
{
Constructor(nldir, nlext, cc, 0);
}
FrontDoorWNode::FrontDoorWNode(const char FDNDATA *nldir, const char FDNDATA *nlext, unsigned short cc, long flags)
{
Constructor(nldir, nlext, cc, flags);
}
/*
** Destructor
**
** Simply calls the Freeze function to tidy up.
**
*/
FrontDoorWNode::~FrontDoorWNode()
{
Freeze();
}
/*
** Thaw
**
** Attempts to initialise the class for use
**
** Returns
**
** 0 on failure; 1 on success
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::Thaw()
{
int success;
if(IsFrozen()){
success = InitClass();
if(success) OnThaw();
return(success);
}
return(1);
}
/*
** Freeze
**
** Renders class inactive. Writes current Stub information
** and closes relevant files.
**
*/
FDNPREF void FDNFUNC FrontDoorWNode::Freeze()
{
if(IsFrozen()) return;
OnFreeze();
WriteNFDXStub();
WriteUFDXStub();
WritePFDXStub();
NFDX.Close();
UFDX.Close();
PFDX.Close();
PFDA.Close();
Frozen = 1;
return;
}
/****************************************************************************/
/* */
/* P R I V A T E F U N C T I O N S */
/* */
/****************************************************************************/
/*
** Constructor
**
** Generic constuction function called by all overloaded constructors.
**
** Parameters
**
** nldir Path to nodelist files
** nlext Extension of nodelist. See SetNLExt for more details
** cc Country code (1 for USA, 44 for UK, 46 for Sweden etc)
** flags Flags with which to create class
**
*/
FDNPREF void FDNFUNC FrontDoorWNode::Constructor(const char FDNDATA *nldir, const char FDNDATA *nlext, unsigned short cc, long flags)
{
Frozen = 1;
strcpy(NodelistDir, nldir);
AddTrail(NodelistDir);
Flags = flags;
CountryCode = cc;
if(strlen(nlext) != 3) *NodeExt=0;
else strcpy(NodeExt, nlext);
// Initialise - unless we're told not to
if(!(Flags & WFDNodeCreateFrozen)) Thaw();
}
/*
** InitClass
**
** Called by Thaw or construction when not created in a frozen
** state. Initialises key data, opens files and reads in
** statistics concerning the current index set.
**
** Returns
**
** 1 on success; 0 on failure
*/
FDNPREF int FDNFUNC FrontDoorWNode::InitClass()
{
int FatalError = 0;
char filename[PATHLENGTH];
if(!CountryCode || *NodeExt==0){
// Insufficient Data
SignalError(15);
return(0);
}
strcpy(filename, NodelistDir);
strcat(filename, "NODELIST.FDX");
NFDX.SetName(filename);
strcpy(filename, NodelistDir);
strcat(filename, "USERLIST.FDX");
UFDX.SetName(filename);
strcpy(filename, NodelistDir);
strcat(filename, "PHONE.FDX");
PFDX.SetName(filename);
strcpy(filename, NodelistDir);
strcat(filename, "PHONE.FDA");
PFDA.SetName(filename);
// Examine our Nodelist Extent
if((!stricmp(NodeExt, "CUR")) && (Flags & WFDNodeOverWrite)){
// We can't use the "Current" extent when we're in overwrite mode!
SignalError(100);
Frozen = 1;
return(0);
}
if(Flags & WFDNodeOverWrite){
NFDX.SetFlags(FDNFileDestroy);
UFDX.SetFlags(FDNFileDestroy);
PFDX.SetFlags(FDNFileDestroy);
PFDA.SetFlags(FDNFileDestroy);
}
else {
NFDX.SetFlags(FDNFileUpdate);
UFDX.SetFlags(FDNFileUpdate);
PFDX.SetFlags(FDNFileUpdate);
PFDA.SetFlags(FDNFileUpdate);
}
// Open our file objects
NFDX.Open();
if(!NFDX.GetStatus()) FatalError=1;
UFDX.Open();
if(!UFDX.GetStatus()) FatalError=2;
PFDX.Open();
if(!PFDX.GetStatus()) FatalError=14;
PFDA.Open();
if(!PFDA.GetStatus()) FatalError=12;
if(FatalError){
SignalError(FatalError);
Frozen = 1;
NFDX.Close();
UFDX.Close();
PFDX.Close();
PFDA.Close();
return(0);
}
memset(&NFirst, 0, sizeof(FirstPage));
memset(&UFirst, 0, sizeof(FirstPage));
memset(&PFirst, 0, sizeof(FirstPage));
memset(&DefaultInfo, 0, sizeof(StubInfo));
// Find the lengths of the files - Calculate the Info block details
PFDARecords = (PFDA.Size() / (long) sizeof(FDNPhoneRec) - 1L);
NInfo.Pages = (NFDX.Size() / (long) sizeof(NFDXPage) - 1L);
UInfo.Pages = (UFDX.Size() / (long) sizeof(UFDXPage) - 1L);
PInfo.Pages = (PFDX.Size() / (long) sizeof(PFDXPage) - 1L);
if(stricmp(NodeExt, "CUR")){
DefaultInfo.NodeExt[0]=3;
strcpy(DefaultInfo.NodeExt+1, NodeExt);
// This puts a zero beyond the structure, but this is corrected now
}
DefaultInfo.Swedish = (char) (Flags & WFDNodeSwedish);
DefaultInfo.RevisionMaj = 2;
DefaultInfo.RevisionMin = 3;
DefaultInfo.ZeroWord = 0;
DefaultInfo.CountryCode = CountryCode;
if(PFDARecords == -1){
WritePFDAStub();
PFDARecords++;
}
if(NInfo.Pages == -1){
WriteNFDXStub();
NInfo.Pages++;
}
else{
if(!ReadNFDXStub()){
SignalError(26);
return(0);
}
}
if(UInfo.Pages == -1){
WriteUFDXStub();
UInfo.Pages++;
}
else{
if(!ReadUFDXStub()){
SignalError(27);
return(0);
}
}
if(PInfo.Pages == -1){
WritePFDXStub();
PInfo.Pages++;
}
else{
if(!ReadPFDXStub()){
SignalError(28);
return(0);
}
}
// Load in root pages - so that we can use ReadPage, we zero the records
if(NFirst.index) RawReadPage(NRoot, NFirst.index);
if(UFirst.index) RawReadPage(URoot, UFirst.index);
if(PFirst.index) RawReadPage(PRoot, PFirst.index);
Frozen = 0;
return(1);
}
/*
** FormUserName
**
** Forms a key suitable for USERLIST.FDX and places it in the buffer
** indicated by Out. The string passed into In should be one of
**
** A Pascal string from FDNODE.FDA
** A Comma terminated 'C' string from a Nodelist File
**
** Underscores are converted to spaces, the last name is placed first
** and all characters are uppercased.
**
*/
FDNPREF void FDNFUNC FrontDoorWNode::FormUserName(const char * In, char * Out)
{
int loop, count = 1, StrLen = 0, LastSpace = -1;
const char * Start = In;
if((*In <= 31) && !strchr(In, ',')){
// Looks like a Pascal string
Start++;
StrLen = *In;
}
else
{
StrLen = strcspn(Start, ",");
}
// Begin processing the string
for(loop = 0; loop < StrLen; loop++){
if(Start[loop]=='_' || Start[loop]==' ') LastSpace = loop;
}
for(loop = LastSpace + 1; (loop < StrLen) && (count < 16); loop++) Out[count++] = ToUpper(Start[loop]);
if(count < 16) Out[count++] = ' ';
for(loop = 0; (loop < LastSpace) && (count < 16); loop++){
if(Start[loop]=='_') Out[(count++)] = ' ';
else Out[(count++)] = ToUpper(Start[loop]);
}
for(loop = count; loop < 17; loop++) Out[loop] = 0;
*Out = 24;
}
/*
** ToUpper
**
** A more general replacement of the toupper() standard
** library function.
*/
FDNPREF char FDNFUNC FrontDoorWNode::ToUpper(char c)
{
if (((unsigned char) c) >= 'a' && ((unsigned char) c) <= 'z')
c-=32;
else if (((unsigned char) c) > 127){
#ifdef FDN_WINDOWS
c=LOBYTE(LOWORD(AnsiUpper((char __far *)MAKELONG((unsigned char) c, 0))));
#elif defined(FDN_DOS)
static int First=1;
static unsigned char uprbuf[256];
if(First){
First=0;
for(int i=0;i<sizeof(uprbuf);i++)
uprbuf[i]=(unsigned char)i;
for(i='a';i<='z';i++)
uprbuf[i]=(unsigned char)(i-32);
#ifdef __WATCOMC__
#pragma off (unreferenced);
#endif
union REGS r;
SREGS s;
struct cbuf_t {
unsigned char id;
void __far * ptr;
} cbuf;
#ifdef __WATCOMC__
#pragma on (unreferenced);
#endif
r.x.ax=0x3000;
intdos(&r, &r);
if(r.h.al>3 || (r.h.al==3 && r.h.ah>=30)){
r.x.ax=0x6502;
r.x.bx=0xFFFF;
r.x.dx=0xFFFF;
r.x.cx=5;
r.x.di=FP_OFF((void __far *)&cbuf);
s.es=FP_SEG((void __far *)&cbuf);
intdosx(&r, &r, &s);
i=(int) *((unsigned short __far *)cbuf.ptr);
for(i=0; i<*((unsigned short __far *)cbuf.ptr); i++)
uprbuf[128+i]=*((unsigned char __far *)cbuf.ptr+2+i);
}
}
c=(char)uprbuf[(unsigned char)c];
#elif defined(FDN_OS2)
COUNTRYCODE cc;
cc.country=0;
cc.codepage=0;
DosCaseMap(1, &cc, &c);
#else
static char * pLower = "\x87" "\x84" "\x86" "\x82" "\x91" "\x94" "\x81" "\xA4",
* pUpper = "\x80" "\x8E" "\x8F" "\x90" "\x92" "\x99" "\x9A" "\xA5";
char * p;
if((p=strchr(pLower, c))!=NULL)
c = (char) (((unsigned char *) pUpper)[p-pLower]);
#endif
}
return(c);
}
/*
** CompareKey
**
** This function compares two Pascal style strings up to a MaxLength
** specified.
**
** Returns
**
** -1 if string key1 lies alphabetically before key2
** 0 if string key1 and key2 are identical
** +1 if string key1 lies alphabetically after key2
*/
FDNPREF int FDNFUNC FrontDoorWNode::CompareKey(const char * key1, const char * key2, int MaxLen)
{
register int loop;
for(loop = 1; loop < MaxLen && loop <= key1[0] && loop <= key2[0]; loop++){
if((unsigned char) key1[loop] > (unsigned char) key2[loop]) return(1);
if((unsigned char) key1[loop] < (unsigned char) key2[loop]) return(-1);
}
if((loop == key1[0] + 1) || (loop == key2[0] + 1)){
if(key1[0] > key2[0]) return(1);
if(key1[0] < key2[0]) return(-1);
return(0);
}
return(0);
}
/****************************************************************************/
/* */
/* P U B L I C F U N C T I O N S */
/* */
/****************************************************************************/
/*
** CreateRecord (NODELIST.FDX variant)
**
** Formats an NFDXRecord according to relevant parameters for it.
**
** Parameters
**
** ToFill An NFDXRecord (not pointer) in which to place the information
** See the similar AddRecord for information on the other parameters.
**
**/
FDNPREF void FDNFUNC FrontDoorWNode::CreateRecord(NFDXRecord & ToFill, unsigned short Zone, unsigned short Net, unsigned short Node, unsigned short Point, unsigned short RNet, unsigned short RNode, char Status, long int Whence, long int Offset)
{
ToFill.zone = SwapBytes(Zone);
ToFill.net = SwapBytes(Net);
ToFill.node = SwapBytes(Node);
ToFill.point = SwapBytes(Point);
ToFill.rnet = RNet;
ToFill.rnode = RNode;
ToFill.nodetype = Status;
ToFill.esmark = 0;
ToFill.key[0] = 14;
ToFill.offset.loff = Whence + Offset;
ToFill.link = 0;
}
/*
** CreateRecord (USERLIST.FDX variant)
**
** Formats an UFDXRecord according to relevant parameters for it.
**
** Parameters
**
** ToFill A UFDXRecord (not pointer) in which to place the information
** See the similar AddRecord for information on the other parameters.
**
**/
FDNPREF void FDNFUNC FrontDoorWNode::CreateRecord(UFDXRecord & ToFill, unsigned short Zone, unsigned short Net, unsigned short Node, unsigned short Point, const char * UserName, char Status, long int Whence, long int Offset)
{
FormUserName(UserName, ToFill.key);
ToFill.zone = SwapBytes(Zone);
ToFill.net = SwapBytes(Net);
ToFill.node = SwapBytes(Node);
ToFill.point = SwapBytes(Point);
ToFill.nodetype = Status;
ToFill.offset = Whence + Offset;
ToFill.link = 0;
}
/*
** AddRecord (NODELIST.FDX variant)
**
** Adds a record into the BTree index. This function forms the record and passes
** it to the generic AddRecord(Record) function class detailed below.
**
** Parameters
**
** Zone, Net, Node, Point as obvious
** RNet, RNode are routing targets.
** ie. Host for non hubbed nodes, Hub for hubbed notes, Region/0 for Hosts.
** Status is the status as defined in the development toolkit.
** Whence is the code which denotes the file which the referenced entry came from.
** ie. one of
** WFDNOfficial, WFDNPrivate, WFDNPoint, WFDNInternal
** Offset is the offset to the above file.
** ie. in FDNODE.FDA, the record number, in textual nodelist fields
** use the offset to the first character in the system name
**
** Returns
**
** 0 on failure
** 1 on success (new key added)
** 2 on success (key replaced)
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::AddRecord(unsigned short Zone, unsigned short Net, unsigned short Node, unsigned short Point, unsigned short RNet, unsigned short RNode, char Status, long int Whence, long int Offset)
{
int success;
NFDXRecord NewData;
if(IsFrozen()){
SignalError(29);
return(0);
}
CreateRecord(NewData, Zone, Net, Node, Point, RNet, RNode, Status, Whence, Offset);
success = AddRecord(NewData);
return(success);
}
/*
** AddRecord (USERLIST.FDX variant)
**
** Adds a record into the BTree index. This function forms the record and passes
** it to the generic AddRecord(Record) function class detailed below.
**
** Parameters
**
** Zone, Net, Node, Point as obvious
** UserName is a the SysOp name in one of two formats
** Pascal style string from FDNODE.FDA;
** 'C' style string, but ',' terminated. (Raw nodelist field)
** underscores are treated as spaces, case irrelevant.
** Status is the status as defined in the development toolkit.
** Whence is the code which denotes the file which the referenced entry came from.
** ie. one of
** WFDNOfficial, WFDNPrivate, WFDNPoint, WFDNInternal
** Offset is the offset to the above file.
** ie. in FDNODE.FDA, the record number, in textual nodelist fields
** use the offset to the first character in the system name
**
** Returns
**
** 0 on failure
** 1 on success (new key added)
** 2 on success (key replaced)
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::AddRecord(unsigned short Zone, unsigned short Net, unsigned short Node, unsigned short Point, const char * UserName, char Status, long int Whence, long int Offset)
{
int success;
UFDXRecord NewData;
if(IsFrozen()){
SignalError(29);
return(0);
}
CreateRecord(NewData, Zone, Net, Node, Point, UserName, Status, Whence, Offset);
success = AddRecord(NewData);
return(success);
}
/*
** AddRecord (PHONE.FDX variant)
**
** Adds a record into the BTree index AND in this case PHONE.FDA
** Offsets are handled automatically and hence not passed.
**
** Parameters
**
** ToMatch The string to look for in the phone number.
** XLT The translation string (format defined in FDNODE.CTL).
** Cost The cost for the system.
**
** Set XLT = "=" for Cost only entries.
** You must add two entries, one with ToMatch = "DOM", the other "INTL"
** which specify default Domestic and International handling respectively.
**
** Returns
**
** 0 on failure
** 1 on success (new key added)
** 2 on success (key replaced)
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::AddRecord(const char * ToMatch, const char * XLT, unsigned short Cost)
{
int success = 1;
PFDXRecord IDXData;
FDNPhoneRec PFDAData;
if(IsFrozen()){
SignalError(29);
return(0);
}
if((strlen(ToMatch) > 20) || (strlen(XLT) > 40)){
SignalError(101);
return(0);
} // String is too long too record, probably safer to discard
IDXData.key[0] = (char) strlen(ToMatch);
strncpy(IDXData.key + 1, ToMatch, 20);
PFDAData.Telephone[0] = (char) strlen(XLT);
strncpy(PFDAData.Telephone + 1, XLT, 40);
PFDAData.Erased = 0;
PFDAData.Cost = Cost;
PFDAData.Baudrate = 0;
PFDA.Write(&PFDAData, sizeof(FDNPhoneRec), 1, 1);
IDXData.offset = ++PFDARecords;
AddRecord(IDXData);
return(success);
}
/*
** AddRecord
**
** A set of functions for adding fully formed records into existing
** trees. It is recommended that the above functions are used in
** preference. Note that the link value in the item is irrelevant.
**
** Returns
**
** 0 on failure
** 1 on success (new key added)
** 2 on success (key replaced)
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::AddRecord(NFDXRecord & NData)
{
int success;
if(IsFrozen()){
SignalError(29);
return(0);
}
if(GetInsertPoint(NData)){
if(InsertPoint.Status && !(NInfo.Flags & WFDNodeUseDupes)) return(0); // Duplicate, simply kill
success = AddRecord(NData, 0, 0);
if(success) NInfo.Records++;
return(success);
}
return(0);
}
/*
** See overloaded variant above for details.
*/
FDNPREF int FDNFUNC FrontDoorWNode::AddRecord(UFDXRecord & UData)
{
int success;
if(IsFrozen()){
SignalError(29);
return(0);
}
if(GetInsertPoint(UData)){
if(InsertPoint.Status && !(UInfo.Flags & WFDNodeUseDupes)) return(0); // Duplicate, simply kill
success = AddRecord(UData, 0, 0);
if(success) UInfo.Records++;
return(success);
}
return(0);
}
/*
** See overloaded variant above for details.
**
** Note that this function does NOT handle PHONE.FDA at ALL.
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::AddRecord(PFDXRecord & PData)
{
int success;
if(IsFrozen()){
SignalError(29);
return(0);
}
if(GetInsertPoint(PData)){
if(InsertPoint.Status && !(PInfo.Flags & WFDNodeUseDupes)) return(0); // Duplicate, simply kill
success = AddRecord(PData, 0, 0);
if(success) PInfo.Records++;
return(success);
}
return(0);
}
/****************************************************************************/
/* */
/* P R I V A T E F U N C T I O N S */
/* */
/****************************************************************************/
/*
** GetInsertPoint
**
** A set of functions, one for each tree, which determines the insertion
** point for a new record in an existing index.
**
** The insertion point will be such that the current key in that point
** should be moved to the right.
**
** Parameters
**
** The relevant data item. The link field is ignored.
**
** Returns
**
** The InsertPoint object is filled in. Note that
** InsertPoint.Level - 1 is the greatest subscript of correct data.
** InsertPoint.Status = 0 for simple insertions, 1 denotes a duplicate
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::GetInsertPoint(NFDXRecord & NData)
{
InsertPoint.Level = 0;
InsertPoint.Status = 0;
long Page = NFirst.index;
long NextPage = 0;
int Test;
int loop;
int quit = 0;
char * SearchKey = NData.key;
NFDXPage * PageData;
if(!Page) return(1); // There isn't an index yet!
PageData = new NFDXPage;
if(!PageData){
SignalError(0);
return(0);
}
while(!quit){
NextPage = 0;
InsertPoint.Level++;
ReadPage(*PageData, Page);
InsertPoint.Page[InsertPoint.Level - 1] = Page;
InsertPoint.MaxRecord[InsertPoint.Level - 1] = PageData->records;
for(loop = PageData->records - 1; (loop >= 0) && !NextPage && !quit; loop--){
Test = CompareKey(SearchKey, PageData->nodes[loop].key, SearchKey[0]);
if(Test == 0){
// Duplicate key : Match found, mark for replacement and quit
InsertPoint.Status = 1;
InsertPoint.Record[InsertPoint.Level - 1] = loop;
quit = 1;
}
else InsertPoint.Status = 0;
if(Test > 0){
InsertPoint.Record[InsertPoint.Level - 1] = loop + 1;
if(!PageData->backref) quit = 1; // Not a link page, nowhere to go to refine search
else NextPage = PageData->nodes[loop].link; // Look further
}
if(Test < 0){
if(!loop){
InsertPoint.Record[InsertPoint.Level - 1] = 0; // Less than all items on the page
if(!PageData->backref) quit = 1;
else NextPage = PageData->backref;
}
}
}
if(!quit) Page = NextPage;
}
delete PageData;
return(1);
}
/*
** See notes for overloaded variant above
*/
FDNPREF int FDNFUNC FrontDoorWNode::GetInsertPoint(UFDXRecord & UData)
{
InsertPoint.Level = 0;
InsertPoint.Status = 0;
long Page = UFirst.index;
long NextPage = 0;
int Test;
int loop;
int quit = 0;
char * SearchKey = UData.key;
UFDXPage * PageData;
if(!Page) return(1); // There isn't an index yet!
PageData = new UFDXPage;
if(!PageData){
SignalError(0);
return(0);
}
while(!quit){
NextPage = 0;
InsertPoint.Level++;
ReadPage(*PageData, Page);
InsertPoint.Page[InsertPoint.Level - 1] = Page;
InsertPoint.MaxRecord[InsertPoint.Level - 1] = PageData->records;
for(loop = PageData->records - 1; (loop >= 0) && !NextPage && !quit; loop--){
Test = CompareKey(SearchKey, PageData->names[loop].key, 24);
if(Test == 0){
// Duplicate key : Match found, mark for replacement and quit
InsertPoint.Status = 1;
InsertPoint.Record[InsertPoint.Level - 1] = loop;
quit = 1;
}
else InsertPoint.Status = 0;
if(Test > 0){
InsertPoint.Record[InsertPoint.Level - 1] = loop + 1;
if(!PageData->backref) quit = 1; // Not a link page, nowhere to go to refine search
else NextPage = PageData->names[loop].link; // Look further
}
if(Test < 0){
if(!loop){
InsertPoint.Record[InsertPoint.Level - 1] = 0; // Less than all items on the page
if(!PageData->backref) quit = 1;
else NextPage = PageData->backref;
}
}
}
if(!quit) Page = NextPage;
}
delete PageData;
return(1);
}
/*
** See notes for overloaded variant above
*/
FDNPREF int FDNFUNC FrontDoorWNode::GetInsertPoint(PFDXRecord & PData)
{
InsertPoint.Level = 0;
InsertPoint.Status = 0;
long Page = PFirst.index;
long NextPage = 0;
int Test;
int loop;
int quit = 0;
char * SearchKey = PData.key;
PFDXPage * PageData;
if(!Page) return(1); // There isn't an index yet!
PageData = new PFDXPage;
if(!PageData){
SignalError(0);
return(0);
}
while(!quit){
NextPage = 0;
InsertPoint.Level++;
ReadPage(*PageData, Page);
InsertPoint.Page[InsertPoint.Level - 1] = Page;
InsertPoint.MaxRecord[InsertPoint.Level - 1] = PageData->records;
for(loop = PageData->records - 1; (loop >= 0) && !NextPage && !quit; loop--){
Test = CompareKey(SearchKey, PageData->phones[loop].key, 21);
if(Test == 0){
// Duplicate key : Match found, mark for replacement and quit
InsertPoint.Status = 1;
InsertPoint.Record[InsertPoint.Level - 1] = loop;
quit = 1;
}
else InsertPoint.Status = 0;
if(Test > 0){
InsertPoint.Record[InsertPoint.Level - 1] = loop + 1;
if(!PageData->backref) quit = 1; // Not a link page, nowhere to go to refine search
else NextPage = PageData->phones[loop].link; // Look further
}
if(Test < 0){
if(!loop){
InsertPoint.Record[InsertPoint.Level - 1] = 0; // Less than all items on the page
if(!PageData->backref) quit = 1;
else NextPage = PageData->backref;
}
}
}
if(!quit) Page = NextPage;
}
delete PageData;
return(1);
}
/*
** AddRecord (Private)
**
** This set of functions does the work of inserting records once
** an insertion point has been established. It is a recursive
** function that may split pages and force insertions into higher
** levels of the tree
**
** Returns
**
** 0 on failure
** 1 on success (new key added)
** 2 on success (key replaced)
**
*/
FDNPREF int FDNFUNC FrontDoorWNode::AddRecord(NFDXRecord & NData, long LeftChild, long RightChild)
{
int loop;
int InsertRoom;
int InsertRecord = InsertPoint.Record[InsertPoint.Level - 1];
if(!(InsertPoint.Level)){
// No Insert point, create new root
NFDXPage New;
// Write root data
memcpy(&(New.nodes[0]), &NData, sizeof(NData));
New.backref = LeftChild;
New.nodes[0].link = RightChild;
New.records = 1;
// Write and cache new root, and update first page data
WritePage(New, ++NInfo.Pages);
memcpy(&NRoot, &New, sizeof(NFDXPage));
NFirst.index = NInfo.Pages;
NInfo.Level++;