|
» |
|
|
|
The example shows a new KSAM/3000 file built programmatically.
This new KSAM/3000 file is built with a language attribute.
This means the keys are sorted according to the collating sequence
of this language. After building the file, it is filled with 15
hard-coded data records.
Perform a generic FFINDBYKEY with a partial key
of length1 containing "E". This should position the
KSAM/3000 file pointer to the very first record whose key starts with
any kind of "E".
After locating this record, read all subsequent records in the file
sequentially and call NLKEYCOMPARE to check whether the key
found is what was requested. If the result returned
by NLKEYCOMPARE is 3, there are no more records starting with
any kind of "E".
1 $CONTROL USLINIT
2 BEGIN
3 LOGICAL ARRAY
4 L'ERROR (0:1),
5 L'KSAM'PARAM (0:79),
6 L'PRINT (0:39),
7 L'RECORD (0:4),
8 COLL'TABLE (0:399);
9
10 BYTE ARRAY
11 FILENAME (0:7),
12 GEN'KEY (0:4),
13 KEY (0:4),
14 B'KSAM'PARAM(*) = L'KSAM'PARAM,
15 B'PRINT(*) = L'PRINT,
16 B'RECORD(*) = L'RECORD;
17
18 DOUBLE ARRAY
19 D'KSAM'PARAM(*) = L'KSAM'PARAM;
20
21 BYTE POINTER
22 BP'PRINT;
23
24 INTEGER
25 I,
26 LGTH,
27 FNUM,
28 RESULT,
29 LANGNUM;
30
31 LOGICAL
32 FOPTIONS,
33 AOPTIONS;
34
|
35 LOGICAL ARRAY
36 L'DATA(0:74) :=
37
38 << |key | >>
39 "014hBBeZZZ",
40 "011EZqrzyx",
41 "001ABCDXXX", << This is the data, to>>
42 "007EdCDxyx", << be written to the >>
43 "012IzzAzzz", << KSAM file. >>
44 "015iABDYZY", << The key starts in >>
45 "005eLDFyxy", << column 4 and is 4 >>
46 "002BBCdxxx", << characters long. >>
47 "003EaBCXXX",
48 "008\\aaYZZ",
49 "004eABCYYY",
50 "006EabcYYY",
51 "009Ayzz",
52 "010eaxfxyz",
53 "013FGHIzqs";
54
55 << The following DEFINE statement defines the layout of
56 the KSAM parameter array; necessary to build a KSAM
57 file programmatically.>>
58
59 DEFINE
60 KEY'FILE = L'KSAM'PARAM#,
61 KEY'FILE'SIZ = D'KSAM'PARAM(2)#,
62 KEY'DEV = L'KSAM'PARAM(6)#,
63 LANGUAGE = L'KSAM'PARAM(10)#,
64 FLAGWORD = L'KSAM'PARAM(15)#,
65 NUM'OF'KEYS = L'KSAM'PARAM(16)#,
66 KEY'TYPE = L'KSAM'PARAM(17).(0:4)#,
67 KEY'LENGTH = L'KSAM'PARAM(17).(4:12)#,
68 KEY'LOCATION = L'KSAM'PARAM(18)#,
69 DUP'FLAG = L'KSAM'PARAM(19).(0:1)#,
70 KEY'BLOCK = L'KSAM'PARAM(19).(1:15)#,
71 RANDOM'FLAG = L'KSAM'PARAM(20).(8:1)#;
72
73 DEFINE
74
75 RECORD = L'DATA (I * 5)#,
76
77 ERROR'CHECK = IF L'ERROR(0) <> 0 THEN
78 QUIT #,
79
80 CCNE = IF <> THEN
81 QUIT #,
82
83 DISPLAY = MOVE B'PRINT := #,
84
|
85 ON'STDLIST = ,2;
86 @BP'PRINT := TOS;
87 LGTH := LOGICAL(@BP'PRINT) -
88 LOGICAL(@B'PRINT);
89 PRINT(L'PRINT, -LGTH, 0) #;
90
91 INTRINSIC
92 FOPEN,
93 FREAD,
94 FWRITE,
95 FCLOSE,
96 FFINDBYKEY,
97 FGETKEYINFO,
98 PRINTFILEINFO,
99 NLINFO,
100 NLKEYCOMPARE,
101 FCLOSE,
102 PRINT,
103 QUIT,
104 READ;
105
106 << Initializing the arrays.>>
107
108 MOVE L'KSAM'PARAM := " ";
109 MOVE L'KSAM'PARAM(1) := L'KSAM'PARAM(0),(79);
110
111 MOVE GEN'KEY := " ";
112
113 MOVE KEY := " ";
114
115 << Hard-code the language used to 8 (GERMAN).>>
116
117 LANGNUM := 8;
118
119 << Call in the collating sequence table.
120 This is done by calling NLINFO ITEM 11.>>
121
122 NLINFO (11, COLL'TABLE, LANGNUM, L'ERROR);
123 IF L'ERROR(0) THEN
124 QUIT(1000 + L'ERROR(0));
125
126 << Build a new KSAM file with the data file name
127 KD008. The key file has the name KK008.>>
128
129 << Set the values for KSAM parameter array.>>
130
131 MOVE FILENAME := "KD008 "; << KSAM data file>>
132 MOVE KEY'FILE := "KK008 "; << KSAM key file>>
133
|
134 NUM'OF'KEYS := 1; << Num of keys = 0 >>
135 LANGUAGE := LANGNUM; << Set the language >>
136 FLAGWORD.(11:1) := 1; << Indicates that >>
137 << language is set >>
138 KEY'FILE'SIZ := 200D; << Max. 200 entries >>
139 KEY'TYPE := 1; << Byte key >>
140 KEY'LENGTH := 4; << 4 byte length >>
141 KEY'LOCATION := 4; << Key start at col.4>>
142 DUP'FLAG := 1; << Allow dupl. keys >>
143 KEY'BLOCK := 10; << Keys per block 10 >>
144
145 FOPTIONS := %4000; << KSAM file >>
146 AOPTIONS := %5; << Update >>
147
148 FNUM := FOPEN(FILENAME,FOPTIONS,AOPTIONS,-10,,
149 B'KSAM'PARAM);
150 IF <> THEN
151 BEGIN
152 PRINTFILEINFO(FNUM);
153 QUIT(2000);
154 END;
155
156 << Copy the hard-coded data into the KSAM file.>>
157 I := -1;
158 WHILE (I := I + 1) < 15 DO
159 BEGIN
160 FWRITE(FNUM, RECORD, -10, %0);
161 IF <> THEN
162 BEGIN
163 PRINTFILEINFO(FNUM);
164 QUIT(3000);
165 END;
166 END;
167
168 << Perform a generic FFINDBYKEY with a partial >>
169 << key of length 1 and value "E". The realtional >>
170 << operator will be 2 (greater or equal). >>
171 << FFINDBYKEY will position the KSAM pointer at the >>
172 << first record starting with any kind of "E". >>
173
174 MOVE GEN'KEY := "E";
175
176 FFINDBYKEY(FNUM, GEN'KEY, 0, 1, 2);
177 IF <> THEN
178 BEGIN
179 PRINTFILEINFO(FNUM);
180 QUIT(4000);
181 END;
182
|
183 << Read the subsequent entries and check by >>
184 << using NLKEYCOMPARE whether an exact match was found.>>
185 << When NLKEYCOMPARE returns a 3 as a result, the >>
186 << program is beyond the range of valid keys. >>
187 << If an exact match was found, the record is printed. >>
188
189 RESULT := 0;
190 DISPLAY
191 "THE FOLLOWING RECORDS MATCH GEN-KEY (E) EXACTLY:"
192 ON'STDLIST;
193 WHILE RESULT <> 3 DO
194 BEGIN
195 FREAD(FNUM,L'RECORD,-10);
196 IF <> THEN
197 BEGIN
198 PRINTFILEINFO(FNUM);
199 QUIT(5000);
200 END;
201
202 MOVE KEY := B'RECORD(3),(4);
203 NLKEYCOMPARE(GEN'KEY, 1, KEY, 4, RESULT, LANGNUM,
204 L'ERROR, COLL'TABLE);
205 ERROR'CHECK(9000 + L'ERROR(0));
206 IF RESULT = 0 THEN << exact hit >>
207 BEGIN
208 DISPLAY B'RECORD,(10) ON'STDLIST;
209 END;
210 END;
211
212 << Close the KSAM file and purge it. >>
213
214 FCLOSE(FNUM, 4, 0);
215
216 END.
|
Executing the program results in the following:
:RUN PROGRAM
THE FOLLOWING RECORDS MATCH GEN-KEY (E) EXACTLY:
003EaBCXXX
007EdCDxyx
011EZqrzyx
END OF PROGRAM
:
|
|