SUBROUTINE CF.CSV.CONVERT(csv.line,force.quote) * * Application: * Convert dynamic array (delimited by attribute marks) into single line * CSV record, automatically adding quotes as required or to all fields. * * Limitations: * Dependent on the availability of the OCONV(xxx, 'GnAn') construct, and * assumes fewer than 999 double quote characters embedded in entire record. * * csv.line : dynamic record in, CSV record out * * force.quote: 0 = add quotes when need detected * 1 = add quotes to all fields * * ======================================================================== * * Conditions of use: * This source code is the property and copyright of Gulraj Rijhwani and * Courtfields Limited. Specific licence is granted to copy and use this * program source, in accordance with the terms and conditions laid out * following. For the purposes of these terms and conditions the term * "developer" is defined to be the individual or company employing the * author incorporating use of this source. Any use of this source code is * deemed to be full and unreserved acceptance of these stated terms. * * 1) All instances of this source code must carry all attached * annotations and comments including, but not limited to, these * these conditions of use in their entirety. * 2) The developer may not alter the program source in any way, without * first obtaining the express written consent of duly authorised * company officers of Courtfields Limited, or its successors. * 3) This source code may not be included in whole or in part in any * proprietary program suite except as a separately compiled module * in wholly unmodified form. Any installed object code must be * accompanied by the original source code at all times. * 4) In the event of any breach of these terms and conditions the * developer shall be liable for an annual usage charge of 200 pounds * sterling for every end user terminal or session using or having * access to use the resulting executable code. * 5) Gulraj Rijhwani and Courtfields Limited make no warranties for * the fitness of this source code, and may not be held liable * for any failure to fulfil the expectations or requirements of * the developer. * * ======================================================================== * no.attrs = DCOUNT(csv.line,CHAR(254)) IF no.attrs ELSE no.attrs = 1 * FOR attr.ptr=1 TO no.attrs csv.field = csv.line GOSUB 1000 csv.line = csv.field NEXT attr.ptr * FOR attr.ptr=2 TO no.attrs csv.line<1> = csv.line<1>:',':csv.line NEXT attr.ptr csv.line = csv.line<1> * IF force.quote=1 THEN force.quote = 0 * RETURN * 1000 * * csv.field = OCONV(TRIM(csv.field),'MCP') IF INDEX(csv.field,'"',1) THEN no.quotes = COUNT(csv.field,'"') FOR nq=no.quotes TO 1 STEP -1 csv.field = OCONV(csv.field,'G"':nq):'""':OCONV(csv.field,'G':nq:'"999') NEXT nq END IF csv.field[1,1]='0' OR INDEX(csv.field,'"',1) OR INDEX(csv.field,',',1) OR force.quote THEN csv.field = '"':csv.field:'"' * END ELSE * IF csv.field='' THEN csv.field = '""' END * RETURN