Convert to CSV

CvtToCSV

The CvtToCSV procedure converts data from its typical DB2/400 database format a comma separated values.

The CvtToCSV procedure can be convert the raw input buffer of DB2/400 database record to a comma separated values. Use this procedure when you are not processing a CSV file with the csvOpen and csvWrite procedures, for example, when transferring data via SOCKETS or CGI, or when you simply want to do the conversion and send or store the results yourself.

csv-value  CvtToCSV(
 szInBuffer      30720A    Const VARYING
 szFmtFile          21A    Const 
 szUserSpace        21A    Const OPTIONS(*NOPASS)
 szSepOptions       10A    Const OPTIONS(*NOPASS)
)

See also: CpyToCSVIFS

Parameters

szInBuffer
[input VChar(30720) const]  A fixed-length value containing the database input buffer that is to be converted to comma separated values.
 
szFmtFile
[input Char(21) const]  The name of the database file to use as the format for converting the data passed on the szInBuffer parameter into comma separated values.
 
The format file name must be specified as a qualified name. The library name followed by the file name, or only the file name may be specified. For example:
 *...v....1....v....2
'QGPL/CUSTMAST      '
 

In this example, the qualified database file name is CUSTMAST in the QGPL library.

 
szUserSpace [ optional ]
[input Char(21) const]  User space to contain the database format (field list). This parameter is optional and may be omitted, or passed as '*' or blanks. This parameter is provided as an override purposes, and if unspecified, a default user space name in QTEMP is used.
 
The user space parameter parameter consists of two parts (1) in the first 10 positions, the name of the user space, and (2) in the second 10 positions, the name of the library containing the user space For example:
 *...v....1....v....2
'MYSPACE   QTEMP     '
 

An alternative syntax is also supported for user space parameters. The alternate syntax allows you to specify the user space name similar to that of an OS/400 CL command, that is it may be specified using qualified name syntax, as follows:

 *...v....1....v....2
'QTEMP/MYSPACE       '
 

In this example, qualified user space name is MYSPACE in the QTEMP library. Either syntax is acceptable.

 
szSepParms  [ optional ]  DFT('",PCRLF')
[input VChar(10) const]  The Separator parameters. This parameter is a data structure in the following format:

     D szCsvParms      DS

     D  szCsvQuote                    1A   Inz('"')

     D  szCsvSep                      1A   Inz(',')

     D  szCsvEditCode                 1A   Inz('P')

     D  szCsvCRLF                     4A   Inz('LF')

     D  szReserved                    3A   Inz(*ALLX'00')

 

To override these values, specify new values for each subfield. For any value that you do not want to override, specify a blank or an asterisk.
 
The end-of-line characters may be the literal values LF (for linefeed) and/or CR (for carriage return). Either or both of these symbolic values may be specified or you may specify the actually characters for LF and/or CR. If you want to prevent any linefeed/carriage return symbols from being written to the file, specify all X'00' for this value.
 

Return Value

The comma separated value format of the data is returned.

If the function fails, various error message may be returned and posted to the job long.

Remarks

CvtToCsv() may reuse the list of field definitions that it generates the first time it is called. However, if any other CSVxxxxx procedures are called between calls to CvtToCsv() then it will regenerate the field definition list. This has the potential for poor performance compared with some of the other CSV routines.

Example

In the example that follows, the externally described file CUSTMAST is read and since there is also an externally described data structure declared. Since it has the same field names as the external file, the data is read into the data structure automatically.

The call to CVTTOCSV() converts the input buffer to comma separated values and stores it in the szCSVBuf field. It then writes that data to a file on the IFS (open and close of the IFS file is not shown) and continues onto the next record.

     H BNDDIR('XTOOLS/RPGLIB') OPTIONS(*SRCSTMT:*NODEBUGIO)
 
.....FFileName++IFEASFRlen+LKeylnKFDevice+.Functions+++++++++++
     FCUSTMAST  IF   E             DISK
 
      /INCLUDE xTools/qcpysrc,convert
 
.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++++++
     D CustBuf       E DS                  EXTNAME(CUSTMAST)
     D szCsvBuf        S                   Like(CustBuf)
 
.....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++
     C                   Read      CustRec
     C                   Dow       NOT %EOF()
     C                   eval      szCSVBuf = CvtToCSV(CustBuf:'QGPL/CUSTMAST')
     C                   Callp     ifsWrite(szCsvBuf:%len(szCsvBuf):1:pIfsFile)
     C                   read      CustRec
     C                   enddo