Common Lisp Package: CSV-PARSER

README:

FUNCTION

Public

MAP-CSV-FILE (FILE FN &KEY LIMIT (SKIP-LINES 0) (EXTERNAL-FORMAT DEFAULT) ((FIELD-SEPARATOR *FIELD-SEPARATOR*) *FIELD-SEPARATOR*) ((QUOTE-CHARACTER *QUOTE-CHARACTER*) *QUOTE-CHARACTER*))

Call FN (up to LIMIT times, if specified) with a list containing the fields parsed from the CSV file FILE. SKIP-LINES, if provided, is the number of lines to skip before starting to call FN. *FIELD-SEPARATOR* and *QUOTE-CHARACTER* can be bound to modify what separates fields and delimits fields.

READ-CSV-LINE (STREAM)

Read one line form a stream containing CSV data. Returns two values; a list of strings parsed, and the number of parsed values.

WRITE-CSV-LINE (STREAM FIELDS)

Write one CSV line to STREAM, containing fields. FIELDS is any (1d) sequence containing strings, symbols or numbers. Try to write it as esthetically pleasing as possible, i.e. don't output the *quote-character* unless necessary to protect the integrity of the data.

Private

Undocumented

ADD-CHAR (CHAR)

CHANGE-STATE (STATE)

CSV-TEST-BLANK

CSV-TEST-BLANKS

CSV-TEST-DOUBLE-QUOTES

CSV-TEST-EMBEDDED-COMMAS

CSV-TEST-EMBEDDED-LINES

CSV-TEST-EMPTY

CSV-TEST-EMPTY-1

CSV-TEST-OTHER-DELIMITERS

CSV-TEST-QUOTED

CSV-TEST-SIMPLE

CSV-TEST-SPACE-HANDLING

CSV-TESTS

EMIT-FIELD

END-OF-FIELD-CHAR-P (CHAR)

END-OF-LINE-CHAR-P (CHAR)

GOT-FIRST-QUOTE (CHAR)

GOT-SECOND-QUOTE (CHAR)

MAKE-EMPTY-FIELD

QUOTE-CHAR-P (CHAR)

REGULAR-FIELD (CHAR)

REMOVE-LAST-N-CHARS (N)

SKIP-WHITE-SPACE (CHAR)

SPECIAL-CHAR-P (CHAR)

WHITE-SPACE-CHAR-P (CHAR)

WRITE-CSV-FIELD (STREAM FIELD)

WRITE-CSV-LINE-ON-ARRAY (STREAM FIELDS)

WRITE-CSV-LINE-ON-CONS (STREAM FIELDS)

WRITE-CSV-STRING-SAFELY (STREAM STRING)

WRITE-PROTECTED-COPY (STREAM FIELD)

MACRO

Public

DO-CSV-FILE (((FIELDS NUM-FIELDS) FILE &KEY LIMIT (SKIP-LINES 0) (EXTERNAL-FORMAT DEFAULT) ((FIELD-SEPARATOR *FIELD-SEPARATOR*) *FIELD-SEPARATOR*) ((QUOTE-CHARACTER *QUOTE-CHARACTER*) *QUOTE-CHARACTER*)) &BODY BODY)

Repeatedly call BODY on CSV file FILE, binding FIELDS and NUM-FIELDS to a list containing the parsed fields, and the number of fields. Code runs inside a block with tagname NIL, so you may call (RETURN). *FIELD-SEPARATOR* and *QUOTE-CHARACTER* can be bound to modify what separates fields and delimits fields.

VARIABLE

Public

*FIELD-SEPARATOR*

The character used to indicate the end of a field in a CSV file.

*QUOTE-CHARACTER*

The character used to protect embedded field separators (usually commas) and whitespace within a field. To import the *quote-character* itself, you must have it printed twice in the input stream.

Private

*CURRENT-FIELD*

Holds the field we are currently working on.

*FIELDS*

Holds the fields we have collected/parsed so far.

*SPACES-GOBBLED*

Keeps track of how many blanks have been skipped. When emitting a field, we can trim extra right spaces, if appropriate, using this.

*STATE*

Holds a function which knows how to handle chars based on what we've seen so far.

Undocumented

*NUM-FIELDS*