For the cobfa_rdkey() function and cobfa_stkey() function, arbitrary record key selection can be specified by using the struct fa_keydesc type. With the cobfa_indexinfo() function, the arbitrary record key in the indexed file that has been opened can be determined.
The values to set for the struct fa_keydesc type members that provide the record key are explained below.
#define FA_NPARTS 254u /* max number of key parts */ struct fa_keydesc { long k_flags; /* flags (duplicatable or not) */ long k_nparts; /* number of parts in key */ struct fa_keypart k_part [FA_NPARTS]; /* each key part */ };
Set either value for <k_flags> indicating the record key attribute:
FA_DUPS: This record key can be duplicated.
FA_NODUPS: This record key does not permit duplication.
Set the number of parts (key parts) in the record key for <k_nparts>. The minimum value is 1 and the maximum value is FA_NPARTS (254).
<k_part> provides information on each key part is declared as the struct fa_keypart type. Explanation follows:
#define FA_NRECSIZE 32760u /* max number of bytes in a record */ #define FA_NKEYSIZE 254u /* max number of bytes in a key */ struct fa_keypart { short kp_start; /* starting byte of key part */ short kp_leng; /* length in bytes */ long kp_flags; /* flags (UCS-2 key part or not) */ };
Set the 0-based byte displacement from the record start <kp_start>. The maximum value of this displacement is FA_NRECSIZE - 1 (typically 32759).
Set the key part length for <kp_leng>. The minimum value is 1 and the upper limit is FA_NKEYSIZE (typically 254). The sum of displacement and length must not exceed FA_NRECSIZE (typically 32760).
Set the information of one of the following categories for <kp_flags>. The value set for this member is valid only when key part flag usage (FA_USEKPFLAGS) has been specified for the open attribute argument of file open (cobfa_open() and cobfa_openW() function).
Key part code system type
FA_UCS2KPCODE: The key part code system type is UCS-2(little-endian).
FA_UTF32KPCODE: The key part code system type is UTF-32(little-endian).
FA_ANYKPCODE: The key part code system type is other than UCS-2(little-endian).
For indexed files handled by COBOL applications whose operating mode is Unicode, specify FA_UCS2KPCODE if the key part is a national item of UCS-2(little-endian) or specify FA_UTF32KPCODE if the key part is a national item of UTF-32(little-endian). If the key part is not a national item of UCS-2(little-endian)/UTF-32(little-endian) or the operating mode is not Unicode, specify FA_ANYKPCODE.
To reference kp_flags when obtaining the record key organization (cobfa_indexinfo() function), use the mask value FA_KPCODEMASK to fetch the code system type of the key part.
Example
FA_KPCODEMASK use example
#include "f4agfcfa.h" #define GET_PRIM_KEY 1 struct fa_keydesc keydesc1; : ret = cobfa_indexinfo ( fd, &keydesc1, GET_PRIM_KEY); for (i = 0; i < keydesc1.k_nparts; i ++) { : switch ( keydesc1.k_part[i].kp_flags & FA_KPCODEMASK ) { case FA_UCS2KPCODE: : break; case FA_ANYKPCODE: : break; } : }
Practical examples
Example
Example 1
The primary record key can be duplicated. The number of key parts is 2.
The first part is at the fifth byte counted from the first byte and its length is 3.
The second part is at the eleventh byte counted from the first byte and its length is 5.
Byte: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Offset:|0--|1--|2--|3--|4--|5--|6--|7--|8--|9--|10-|11-|12-|13-|14-|15-| |============================================================.... |---+---+---| |---+---+---+---+---| first part second part
COBOL program
001000 environment division. 001100 configuration section. 001200 input-output section. 001300 file-control. 001400 select FILENAME-1 assign to SYS006 001500 organization is indexed 001600 record key is part-1 part-2 with duplicates. : 002000 data division. 002100 file section. 002200 fd FILENAME-1. 002300 01 record-1. 002400 02 filler pic x(4). 002500 02 part-1 pic x(3). 002400 02 filler pic x(3). 002500 02 part-2 pic x(5). 002600 02 filler pix x(...
C program
#include "f3bifcfa.h" struct fa_keydesc keydesc1; keydesc1.k_flags = FA_DUPS; keydesc1.k_nparts = 2; /* number of key parts: 2 */ keydesc1.k_part[0].kp_start = 4; /* part_1: 5 - 1 == 4 */ keydesc1.k_part[0].kp_leng = 3; keydesc1.k_part[0].kp_flags = FA_ANYKPCODE; keydesc1.k_part[1].kp_start = 10; /* part_2: 11 - 1 == 10 */ keydesc1.k_part[1].kp_leng = 5; keydesc1.k_part[1].kp_flags = FA_ANYKPCODE;
Example 2
The primary record key does not permit duplication. The number of key parts is~3.
The first part is at the first byte and its length is 3.
The second part is at the ninth byte counted from the first byte and its length is~2.
The third part is at the fifth byte counted from the first byte and its length is 4.
Byte: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Offset:|0--|1--|2--|3--|4--|5--|6--|7--|8--|9--|10-|11-|12-|13-|14-|15-| |============================================================.... |---+---+---| |---+---+---+---|---+---| first part third part second part
COBOL program
001000 environment division. 001100 configuration section. 001200 input-output section. 001300 file-control. 001400 select FILENAME-1 assign to SYS006 001500 organization is indexed 001600 record key is part-1 part-2 part-3. : 002000 data division. 002100 file section. 002200 fd FILENAME-1. 002300 01 record-1. 002400 02 part-1 pic x(3). 002500 02 filler pic x(1). 002400 02 part-3 pic x(4). 002500 02 part-2 pic x(2). 002600 02 filler pix x(...
C program
#include "f3bifcfa.h" struct fa_keydesc keydesc2; keydesc2.k_flags = FA_NODUPS; keydesc2.k_nparts = 3; /* number of key parts: 3 */ keydesc2.k_part[0].kp_start = 0; /* part_1: 1 - 1 == 0 */ keydesc2.k_part[0].kp_leng = 3; keydesc2.k_part[0].kp_flags = FA_ANYKPCODE; keydesc2.k_part[1].kp_start = 8; /* part_2: 9 - 1 == 8 */ keydesc2.k_part[1].kp_leng = 2; keydesc2.k_part[1].kp_flags = FA_ANYKPCODE; keydesc2.k_part[2].kp_start = 4; /* part_3: 5 - 1 == 4 */ keydesc2.k_part[2].kp_leng = 4; keydesc2.k_part[2].kp_flags = FA_ANYKPCODE;