
% loadCSV(Filename, ListOfRecords)
% --------------------------------
% load the stats from a csv file into a list of lists:
%    each top level element is one recorded,
%    each record is a list of the comma-separated fields
loadCSV(Fname,Records) :-
   % check we can actually read the file, then open/read/close
   nonvar(Fname), access_file(Fname,read),
   open(Fname, read, Stream), readFile(Stream, Records), close(Stream).

% readFile(Stream, L)
% -------------------
% given an open file stream, read the contents line by line into L,
%    strip out any whitespace and split each line at the commas
readFile(Stream, []) :- at_end_of_stream(Stream).
readFile(Stream, [Current|Rest]) :-
   \+ at_end_of_stream(Stream),               % quit at eof
   read_string(Stream,"\n","\r",End,CurLine), % reads line as string, strip trailing returns
   split_string(CurLine,",","\s",Current),    % splits on commas, strip whitespace padding
   readFile(Stream, Rest),                    % reads rest of file
   char_type(End,_).

