[Overview][Types][Procedures and functions][Index] Reference for unit 'LCSVUtils' (#lazutils)

LoadFromCSVStream

Loads and processes comma-separated values from the specified stream.

Declaration

Source position: lcsvutils.pas line 23

procedure LoadFromCSVStream(

  AStream: TStream;

  AProc: TCSVRecordProc;

  ADelimiter: Char = ',';

  CSVEncoding: TCSVEncoding = ceAuto

);

Arguments

AStream

  

TStream instance containing the CSV data.

AProc

  

Routine used to load and process lines in the CSV data.

ADelimiter

  

Delimiter used to separate fields in the CSV data.

CSVEncoding

  

Character encoding used for the CSV data.

Description

LoadFromCSVStream is a procedure used to read and process delimited values from the stream specified in AStream.

The procedure name is actually a misnomer; the routine uses the value in ADelimiter as the delimiter between field values. It does not have to be a Comma (,) character. Comma is, however, the default value for the ADelimiter argument.

LoadFromCSVStream reads lines of data from the stream, and calls nested routines to handle character encodings for the byte values. Each line is terminated with one or more line-ending characters (#13, #10).

Remark: The position in AStream is not maintained in the routine. Processing is started using the stream position on entry. The stream position on exit is after the last line of text successfully processed from the stream, and is normally at the end of the stream.

The value in CSVEncoding indicates the encoding for the byte values read from the stream. ceAuto is the default value for the argument, and indicates that the encoding is auto-detected in the routine using the BOM (Byte Order Mark) at the beginning of the processed byte values. The nested routines convert non-UTF-8 encodings to the UTF-8 encoding needed for the native String type.

Leading spaces in a line of text read from the stream are discarded. Field values in the line of text can be enclosed in Quotation (") marks if the delimiter character appears in the field content. Nested Quotation marks are allowed when used in a balanced pair.

LoadFromCSVStream calls the procedure passed in the AProc argument to process and apply each delimited field value from the line of text read from the stream. Field values are stored in a TStringList instance that is passed as argument to the routine in AProc. An application must implement the procedure passed in the argument to perform any actions needed for the field values.

No actions are performed in LoadFromCSVStream when AProc has not been assigned.

Use the LoadFromCSVFile procedure to process delimited values in a specified file name.

LoadFromCSVStream is called from the LoadFromCSVFile routine, and used in the implementation of the LoadFromCSVFile and LoadFromCSVStream methods in TCustomStringGrid. For database-aware applications, consider the TCSVDataset class found in the csvdataset unit from the FCL; also available on the Data Access tab for the Lazarus IDE component palette.

See also

LoadFromCSVFile

  

Loads and processes comma-separated values from the specified file.

TCSVEncoding

  

Represents character encodings used for values in CSV data.

TCSVRecordProc

  

Defines a procedure used to process field values.

TCustomStringGrid.LoadFromCSVFile

TCustomStringGrid.LoadFromCSVStream


Version 3.2 Generated 2024-02-25 Home