Parse the copybook via the lexer:
lexer := getLexer("resources/float.copybook")
fileStruct := parser.ParseLexData(lexer)
Read the ebcdic bytes in:
readBytes, len := getBytes("resources/float.ebcdic")
Parse the binary data into fields:
parser.ParseBinaryData(&fileStruct, readBytes[0:len])
Print the field:
field, err := fileStruct.Field(fieldLabel)
if err != nil {
panic(err)
}
log.Printf("Type: %T", field.Data)
switch field.Data.(type) {
case int32:
fmt.Printf("Data by field %s pull: %d\n", fieldLabel, field.Data)
case int:
fmt.Printf("Data by field %s pull: %d\n", fieldLabel, field.Data)
case string:
fmt.Printf("Data by field %s pull: %s\n", fieldLabel, field.Data)
case float32:
fmt.Printf("Data by field %s pull: %v\n", fieldLabel, field.Data)
case decimal.Decimal:
fmt.Printf("Data by field %s pull: %v\n", fieldLabel, field.Data)
fmt.Printf("Raw Data: %08b\n", field.GetRawData())
case []uint8:
fmt.Printf("Data by field %s pull: %v\n", fieldLabel, field.Data)
fmt.Printf("Raw Data: %08b\n", field.GetRawData())
case []int8:
fmt.Printf("Data by field %s pull: %v\n", fieldLabel, field.Data)
fmt.Printf("Raw Data: %08b\n", field.GetRawData())
default:
fmt.Printf("Unsupported type in printField: %T, data: %08b", field.Data, field.GetRawData())
}
Update the a field with new data:
err = field.SetData(float32(1.123e+19))
if err != nil {
panic(err)
}
Get the modified data stream:
updatedBytes := parser.GetBinaryData(&fileStruct)
fmt.Printf("Modified Data: %08b", updatedBytes)
Copybook Definitions Supported:
- Signed Binary PIC S9(#) COMP, PIC S9 COMP
- Unsiged Binary PIC 9(#) COMP, PIC 9 COMP
- Float4 PIC S9(#)V9(#) COMP-1, PIC S9(#)V9(#) COMP-2
- Alpha Character PIC A(#)
- Any Character PIC X(#)
- Decimal PIC S9(#)V9(#) COMP-3
Unsupported Definitions:
- Float8
- Didplay Numeric
Made with lots of help from: https://techjogging.com/cobol-data-types.html