Universal read_data function



On June 3, 2011, Paul Bowyer wrote:
--------------------------------
[snip]
With the CRLF sequences in the file, the read_data function chokes on
the seemingly blank lines with the CRLF sequence whether I use:
   "if %l # "" then"
or
   "if slength(%l) # 0 then"

as the zero-length test

I had to include:
   "%l : strim(" ", ssubst(" ", ascii(13), %l ) ),"
just prior to the zero-length test for proper operation.

A simple work-around for linux/unix users is to run, from a terminal window:
dos2unix <filename> on the data file if dos2unix is installed.
-------------------------------

Have you tried my last version of read_data(?) which,
(in response to your suggestion) simply ignores any and
all blank lines, as a good read_data function ought to
do anyway, and also doesn't care about choice of
end of line chars.
(see below)

Ted
-------------------------
read_data([%v]) :=
    block ([%s,%r,%l,%filename,%dsep,%mult:true,%mix:false,
              %whole:[],%ln],

     %filename : part (%v,1),

     if not stringp (%filename)
       then ( disp (" file name must be a Maxima string "),
              return (false)),

    if not file_search (%filename) then
      (disp (" file not found "),return (false)),

    if length (%v) = 1 then %mix : true
       else if length(%v) = 2 then %dsep : part (%v,2)
       else if length (%v) = 3 then (%dsep : part (%v,2), %mult : part 
(%v,3))
       else (%dsep : part (%v,2), %mult : part (%v,3),%whole : part(%v,4)),



    %s : openr (%filename),
    %r : [],
    %ln : 0,

    while (%l : readline(%s)) # false do
        if slength(%l) # 0 then
       ( %ln : %ln + 1,
         if %dsep = "text" then
            %r : cons (%l,%r)
         else if not lfreeof (%whole,%ln) then
            %r : cons (%l,%r)
         else if %mix then
            %r : cons (map(parse_string, split(ssubst (" ",",",%l))), %r)
         else %r : cons (map(parse_string, split(%l,%dsep,%mult)), %r)),

    close (%s),
    reverse (%r))$
    -------------------------------