1 ;;; csv.el --- Parser and utility for CSV (Comma Separated Value).
3 ;; Copyright (C) 2001 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: CSV, table, database
8 ;; This file is a part of Tomoyo-Tools.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (defun csv-parse-quoted-string (string)
30 (let ((len (length string))
33 (eq (aref string 0) ?\"))
36 (not (eq (aref string i) ?\")))
38 (when (eq (aref string i) ?\")
39 (cons (substring string 1 i)
40 (substring string (1+ i)))))))
42 (defun csv-parse-atom (string)
43 (let ((len (length string))
47 (when (not (eq (aref string 0) ?\"))
49 (not (eq (aref string i) ?,))
50 (not (eq (aref string i) ?\")))
52 (cons (substring string 0 i)
53 (substring string i))))))
55 (defun csv-parse-separator (string)
56 (let ((len (length string)))
58 (eq (aref string 0) ?,))
59 (cons (substring string 0 1)
60 (substring string 1)))))
62 (defun csv-parse-string* (string)
66 (setq ret (or (csv-parse-quoted-string string)
67 (csv-parse-atom string)))
68 (setq string (cdr ret)))
69 (setq dest (cons (car ret) dest))
70 (setq ret (csv-parse-separator string)))
71 (setq string (cdr ret)))
73 (if (> (length string) 0)
77 (defun csv-parse-string (string)
81 (setq ret (or (csv-parse-quoted-string string)
82 (csv-parse-atom string)))
83 (setq string (cdr ret)))
84 (setq dest (cons (car ret) dest))
85 (setq ret (csv-parse-separator string)))
86 (setq string (cdr ret)))
87 (if (> (length string) 0)