This installment of Carsten Flensburg’s APIs by Example shows how to use a host of APIs, MI built-n functions, and C library functions to parse an externally defined record buffer. The main RPG ILE service program, CBX109S, includes the following procedures:
GetFldVal (Get field value)
Based on a file name, field name, and key value, the corresponding field value is returned in left adjusted character format.
LstFld (List fields)
Lists to the specified user space a list of the specified file’s fields, including name, data type, and length.
Chain (Read record by key)
Performs the actual keyed access to the file identified by the file pointer passed and returns the record buffer retrieved if a match is found.
RtvFld (Retrieve field)
The buffer offset, data type and field length of the field name specified is retrieved from the user space field list.
EditC (Edit by edit code)
Converts the specified buffer location containing a numeric value in internal format to a readable character format as defined by the specified edit code.
ApyDecFmt (Apply decimal format)
Applies the current job’s decimal format to binary fields having decimal positions.
These procedures give you the ability to parse and read buffer data. Using these routines as a guide, you can also reverse the process and write to an externally described record buffer. The _CVTEFN and _LBCPYNV(R) MI built-in functions enable you to update numeric fields in a record buffer. Those write routines are, as they say, left as an “exercise for the reader.”
Using both the read routines as well as coding your own write routines, you can soft-code file and data exchange programs. Note, though, that updating production at the buffer level requires special precaution and careful design, so test your routines thoroughly before using them in production programs.
To show how to call the routines in CBX109S, test program CBX109T is included in this month’s tip. CBX109T retrieves field values from the TCP/IP host table, which is stored in physical file QATOCHOST in library QUSRSYS. You can easily change the file name, field names, and key values specified in CBX109T to read another physical file and fields of your choice.