(dump-94x94-ccs-to-ucs-table): Use `encode-char' with `defined-only'
[chise/tomoyo-tools.git] / csv.el
1 ;;; csv.el --- Parser and utility for CSV (Comma Separated Value).
2
3 ;; Copyright (C) 2001 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: CSV, table, database
7
8 ;; This file is a part of Tomoyo-Tools.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (defun csv-parse-quoted-string (string)
30   (let ((len (length string))
31         (i 1))
32     (when (and (>= len 2)
33                (eq (aref string 0) ?\"))
34       (setq len (1- len))
35       (while (and (< i len)
36                   (not (eq (aref string i) ?\")))
37         (setq i (1+ i)))
38       (when (eq (aref string i) ?\")
39         (cons (substring string 1 i)
40               (substring string (1+ i)))))))
41
42 (defun csv-parse-atom (string)
43   (let ((len (length string))
44         (i 0))
45     (if (= len 0)
46         '("")
47       (when (not (eq (aref string 0) ?\"))
48         (while (and (< i len)
49                     (not (eq (aref string i) ?,))
50                     (not (eq (aref string i) ?\")))
51           (setq i (1+ i)))
52         (cons (substring string 0 i)
53               (substring string i))))))
54
55 (defun csv-parse-separator (string)
56   (let ((len (length string)))
57     (when (and (>= len 1)
58                (eq (aref string 0) ?,))
59       (cons (substring string 0 1)
60             (substring string 1)))))
61
62 (defun csv-parse-string* (string)
63   (let (ret dest)
64     (while (and string
65                 (prog1
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)))
72     (cons (nreverse dest)
73           (if (> (length string) 0)
74               string))))
75
76 ;;;###autoload
77 (defun csv-parse-string (string)
78   (let (ret dest)
79     (while (and string
80                 (prog1
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)
88         nil
89       (nreverse dest))))
90
91
92 ;;; @ End.
93 ;;;
94
95 (provide 'csv)
96
97 ;;; csv.el ends here