The COBOL file system allows the end user to specify multiple record layouts as well as structure redefinitions that can be used to read/write data from/to a file. The COBOL program logic decides which record layout or redefines structure to use when reading the record.
CREATE (Common Record Editor) in its operations to edit/transform data files requires information that allows it to make similar decisions and is obtained from the XFD WHEN directives.
There are two formats to the XFD WHEN directive. One for dealing with multiple layouts and the other for dealing with redefines.
Format 1: $XFD WHEN [ [n1:n2] ] <Operator> Operand1 [Operand2]
Format 1 specified above is used for identifying the layout to use when multiple layouts are present in the file section. The code below depicts its use:
$XFD WHEN = "1"
05 F11-FIELD1 PIC X(1).
$XFD WHEN != "A"
05 F11-FIELD2 PIC X(1).
05 F11-FIELD3 PIC X(1).
$XFD WHEN [5:3] != "IGN" OR [15:3] == "DEM"
05 F11-FIELD4 PIC X(27).
$XFD WHEN = "2"
05 F12-FIELD1 PIC X(1).
$XFD WHEN != "B"
05 F12-FIELD2 PIC X(1).
05 F12-FIELD3 PIC X(1).
05 F12-FIELD4 PIC X(27).
FILE1-REC1 is used when the following conditions are met :
F11-FIELD1 = "1"
F11-FIELD2 != "A"
(F11-FIELD4 [5:3] != "IGN" OR F11-FIELD4 [15:3] != "DEM")
FILE1-REC2 is used when the following conditions are met :
F12-FIELD1 = "2"
WHEN F12-FIELD2 != "B"
One can create complex conditions using logical operators with parentheses which are required when mixing logical operators.
$XFD WHEN = "A" OR ( ( ( = "B" AND = "C" ) OR ( = "D" AND = "E" ) ) AND = "F" AND ( = "G" OR = "H" ) ) OR = "I"
Use of COBOL reference modifiers is supported as well.
$XFD WHEN [13:] = "A12" OR ([6:9] = "B" AND = "C")
Format 2: $XFD WHEN <fieldname>[ [n1:n2] ] <Operator> Operand1 [Operand2]
Format 2 is used when redefines are used in the file record layout. The default behavior for the compiler is to generate info for the redefined structure alone ignoring all others. This directive allows the user to include the newly redefined structure and specify the condition for its usage.
05 FIELD-1 PIC X.
05 FIELD-2 PIC X(5).
10 FIELD-3A PIC X.
10 FIELD-3B PIC X(5).
$XFD WHEN FIELD-1 = "A" OR FIELD-2 [3:1] = "X"
05 FIELD-4 REDEFINES FIELD-3.
10 FIELD-4A PIC X.
10 FIELD-4B PIC X(5).
$XFD WHEN FIELD-1 = "B" AND FIELD-2 [3:1] = "Y"
05 FIELD-5 REDEFINES FIELD-3.
10 FIELD-5A PIC X.
10 FIELD-5B PIC X(5).
In the above example if we do not specify any XFD directives the structures FIELD4A and FIELD5A would not be included in the xfd xml generated. All records read in would be formatted using the structure of FIELD-3. By including the XFD directives we instruct the compiler to generate info in the xml that indicates when FIELD4 & FIELD-5 should be used.
FIELD-4 structure will be used when the conditions are met:
FIELD-1 = "A" OR FIELD-2 [3:1] = "X"
FIELD-5 structure will be used when the conditions are met:
FIELD-1 = "B" AND FIELD-2 [3:1] = "Y"
If none of the specified redefinitions are satisfied the default of the base ie FIELD-3 gets used