Universal read_data function



On May 25, 2011, I wrote:
----------------------------------------
The use of the string processing functions from
van Nek's stringproc.lisp allows construction of
a "Universal" read_data function which not only
doesn't care about line endings, but doesn't care
whether data separators are spaces or commas,
and can handle fractions, decimals, floating point
and strings as data items.
----------------------------------------------------
Well, I was wrong, since the proposed universal
read_data function chokes if a string contains
spaces, such as "my mistake".

To read in string data containing spaces, using
the stringproc.lisp methods, requires (I think)
the use of a comma separated data file.

Hence a re-designed read_data function with
the syntax:
         read_data (filename, data_sep)
in which the second arg is
  1. optional, but if not present, space sep is assumed,
  2. if present, must be either
      "comma"  or "space" .

---------------------------------------
Maxima 5.24.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.8 (a.k.a. GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
                                  2011-05-27

(%i1) load(readwrite);
(%o1) "c:/work2/readwrite.mac"

(%i2) printfile("ndata1.dat")$
2.3e9 "Abc"

(%i3) file_info ("ndata1.dat");
(%o3) ["WINDOWS",13]

(%i4) read_data ("ndata1.dat");
(%o4) [[2.3E+9,"Abc"]]

(%i5) read_data ("ndata1.dat","space");
(%o5) [[2.3E+9,"Abc"]]

/* note that the following data file has a string with
    spaces:  5, "! = ", 120
    there is a space before and after the equal sign */

(%i6) printfile("tmp12.out")$
5, "! = ", 120

(%i7) file_info ("tmp12.out");
(%o7) ["UNIX",16]

(%i8) read_data ("tmp12.out","comma");
(%o8) [[5,"! = ",120]]

(%i9) fundef (read_data);

(%o9) read_data ([%v]) :=
   block ([%s,%r,%l,%filename,%dsep],
             %filename:part(%v,1),
             if not stringp(%filename)
                  then (disp(" filename must be a string "),return(false)),
             if not file_search(%filename)
                  then (disp(" file not found "),return(false)),

             if length(%v) = 1 then %dsep:"space" else %dsep:part(%v,2),

             if lfreeof(["space","comma"],%dsep) then
  (disp(" The allowed data-separators are the strings ``space'' or 
``comma''. "),
                 disp(" The default file type is space separated data. "),
                          return(false)),
            %s:openr(%filename),
           %r:[],

            while (%l:readline(%s)) # false do
                  if %dsep = "space"
                        then %r:cons(map(parse_string,split(%l)),%r)
                        else %r:cons(map(parse_string,split(%l,",")),%r),
            close(%s),reverse(%r))

-------------------------------------------------

Ted Woollett