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 |
*...v....1....v....2
'QGPL/CUSTMAST '
In this example, the qualified database file name is CUSTMAST in the QGPL library.
*...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.
- Quotation character - DFT(") Char(1)
- Comma character - DFT(,) Char(1)
- Edit Code for numeric values - DFT(P) Char(1)
- End-of-line character(s) - DFT(LF) Char(4)
- Reserved - Char(3)
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')
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.
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.
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