/usr/lib/swi-prolog/library/csv.pl is in swi-prolog-nox 5.10.4-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | /* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@cs.vu.nl
WWW: http://www.swi-prolog.org
Copyright (C): 2009, VU University Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(csv,
[ csv//1, % +Rows
csv//2, % +Rows, +Options
csv_read_file/2, % +File, -Data
csv_read_file/3, % +File, -Data, +Options
csv_write_file/2, % +File, +Data
csv_write_file/3 % +File, +Data, +Options
]).
:- use_module(library(record)).
:- use_module(library(error)).
:- use_module(library(pure_input)).
:- use_module(library(debug)).
/** <module> Process CSV (Comma-Separated Values) data
This library parses and generates CSV data. CSV data is represented in
Prolog as a list of rows. Each row is a compound term, where all rows
have the same name and arity.
@tbd Implement immediate assert of the data to avoid possible stack
overflows.
@tbd Writing creates an intermediate code-list, possibly overflowing
resources. This waits for pure output!
@see RFC 4180
*/
:- record
csv_options(separator:integer=0',,
strip:boolean=false,
convert:boolean=true,
functor:atom=row,
arity:integer).
%% csv_read_file(+File, -Rows) is det.
%% csv_read_file(+File, -Rows, +Options) is det.
%
% Read a CSV file into a list of rows. Each row is a Prolog term
% with the same arity. Options is handed to csv//2. Remaining
% options are processed by phrase_from_file/3.
%
% Suppose we want to create a predicate table/6 from a CSV file
% that we know contains 6 fields per record. This can be done
% using the code below. Without the option arity(6), this would
% generate a predicate table/N, where N is the number of fields
% per record in the data.
%
% ==
% ?- csv_read_file(File, Rows, [functor(table), arity(6)]),
% maplist(assert, Rows).
% ==
csv_read_file(File, Rows) :-
csv_read_file(File, Rows, []).
csv_read_file(File, Rows, Options) :-
make_csv_options(Options, Record, RestOptions),
phrase_from_file(csv_roptions(Rows, Record), File, RestOptions).
%% csv(?Rows)// is det.
%% csv(?Rows, +Options)// is det.
%
% Prolog DCG to `read/write' CSV data. Options:
%
% * separator(+Code)
% The comma-separator. Must be a character code. Default is
% (of course) the comma. Character codes can be specified
% using the 0' notion. E.g., =|separator(0';)|=.
%
% * strip(+Boolean)
% If =true= (default =false=), strip leading and trailing
% blank-space. RFC4180 says that blank space is part of the
% data.
%
% * convert(+Boolean)
% if =true= (Default), use name/2 on the field-data. This
% translates the field into a number if possible.
%
% * functor(+Atom)
% Functor to use for creating row-terms. Default is =row=.
%
% * arity(?Arity)
% Number of fields in each row. This predicate raises
% a domain_error(row_arity(Expected), Found) if a row is
% found with different arity.
csv(Rows) -->
csv(Rows, []).
csv(Rows, Options) -->
{ make_csv_options(Options, Record, _) },
csv_roptions(Rows, Record).
csv_roptions(Rows, Record) -->
{ ground(Rows) }, !,
emit_csv(Rows, Record).
csv_roptions(Rows, Record) -->
csv_data(Rows, Record).
csv_data([], _) -->
eof, !.
csv_data([Row|More], Options) -->
row(Row, Options), !,
{ debug(csv, 'Row: ~p', [Row]) },
csv_data(More, Options).
eof([], []).
row(Row, Options) -->
fields(Fields, Options),
{ csv_options_functor(Options, Functor),
Row =.. [Functor|Fields],
functor(Row, _, Arity),
check_arity(Options, Arity)
}.
check_arity(Options, Arity) :-
csv_options_arity(Options, Arity), !.
check_arity(Options, Arity) :-
csv_options_arity(Options, Expected),
domain_error(row_arity(Expected), Arity).
fields([F|T], Options) -->
field(F, Options),
( separator(Options)
-> fields(T, Options)
; end_of_record
-> { T = [] }
).
field(Value, Options) -->
{ csv_options_strip(Options, true) }, !,
stripped_field(Value, Options).
field(Value, Options) -->
"\"", !,
string_codes(Codes),
{ make_value(Codes, Value, Options) }.
field(Value, Options) -->
{ csv_options_separator(Options, Sep) },
field_codes(Codes, Sep),
{ make_value(Codes, Value, Options) }.
stripped_field(Value, Options) -->
ws,
( "\""
-> string_codes(Codes),
ws
; { csv_options_separator(Options, Sep) },
field_codes(Codes0, Sep),
{ strip_trailing_ws(Codes0, Codes) }
),
{ make_value(Codes, Value, Options) }.
ws --> " ", !, ws.
ws --> "\t", !, ws.
ws --> "".
strip_trailing_ws(List, Stripped) :-
append(Stripped, WS, List),
all_ws(WS).
all_ws([]).
all_ws([32|T]) :- all_ws(T).
all_ws([9|T]) :- all_ws(T).
%% string_codes(-Codes)
%
% Process a double-quotes string where the quote is escaped by
% doubling it. Eats the terminating double-quote.
string_codes(List) -->
[H],
( { H == 0'" }
-> ( "\""
-> { List = [H|T] },
string_codes(T)
; { List = [] }
)
; { List = [H|T] },
string_codes(T)
).
field_codes([], Sep), [Sep] --> [Sep], !.
field_codes([], _), "\n" --> "\r\n", !.
field_codes([], _), "\n" --> "\n", !.
field_codes([H|T], Sep) --> [H], !, field_codes(T, Sep).
field_codes([], _) --> []. % unterminated last record
make_value(Codes, Value, Options) :-
csv_options_convert(Options, true), !,
name(Value, Codes).
make_value(Codes, Value, _) :-
atom_codes(Value, Codes).
separator(Options) -->
{ csv_options_separator(Options, Sep) },
[Sep].
end_of_record --> "\n".
end_of_record --> "\r\n".
end_of_record --> eof. % unterminated last record
/*******************************
* OUTPUT *
*******************************/
%% csv_write_file(+File, +Data) is det.
%% csv_write_file(+File, +Data, +Options) is det.
%
% Write a list of Prolog terms to a CSV file. Options are given
% to csv//2. Remaining options are given to open/4.
%
csv_write_file(File, Data) :-
csv_write_file(File, Data, []).
csv_write_file(File, Data, Options) :-
make_csv_options(Options, Record, RestOptions),
phrase(csv_roptions(Data, Record), String),
setup_call_cleanup(open(File, write, Out, RestOptions),
format(Out, '~s', [String]),
close(Out)).
emit_csv([], _) --> [].
emit_csv([H|T], Options) -->
emit_row(H, Options), "\r\n", % RFC 4180 demands \r\n
emit_csv(T, Options).
emit_row(Row, Options) -->
{ Row =.. [_|Fields] },
emit_fields(Fields, Options).
emit_fields([H|T], Options) -->
emit_field(H, Options),
( { T == [] }
-> []
; { csv_options_separator(Options, Sep) },
[Sep],
emit_fields(T, Options)
).
emit_field(H, Options) -->
{ atom(H), !,
atom_codes(H, Codes)
},
( { needs_quotes(H, Options) }
-> "\"", emit_string(Codes), "\""
; emit_codes(Codes)
).
emit_field(H, _) -->
{ number_codes(H,Codes) },
emit_codes(Codes).
needs_quotes(Atom, _) :-
sub_atom(Atom, _, _, _, '"'), !.
needs_quotes(Atom, Options) :-
csv_options_separator(Options, Sep),
char_code(Char, Sep),
sub_atom(Atom, _, _, _, Char), !.
emit_string([]) --> "".
emit_string([0'"|T]) --> !, "\"\"", emit_string(T).
emit_string([H|T]) --> [H], emit_string(T).
emit_codes([]) --> "".
emit_codes([0'"|T]) --> !, "\"\"", emit_codes(T).
emit_codes([H|T]) --> [H], emit_codes(T).
|