## x = dlmread (filename, sep)
##    Read the matrix x from a file, with columns separated by the
##    character sep (default is ",").  NaN values are written as nan.
##
## WARNING: for compatibility, must treat empty fields as zero, but doesn't.
 
## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
## 2001-02-16
##    * first revision

function x = dlmread (filename, sep)

  if (nargin < 1 || nargin > 2)
    usage ("x = dlmread (filename, sep)");
  endif
  if nargin < 3, sep = ","; endif
  
  fid = fopen(filename, "r");
  if (fid >= 0)
    in = setstr(fread (fid)');
    fclose (fid);

    ## zap extra spaces
    if (sep != " ")
      idx = find (in == " ");
      if (length (idx) > 0) in(idx) = []; endif
    endif

    ## zap carriage returns
    if (sep != "\r")
      idx = find (in == "\r");
      if (length (idx) > 0) in(idx) = []; endif
    endif

    ## convert new lines to spaces
    idx = find (in == "\n");
    if (length(idx) > 0) in(idx) = " "; endif

    ## number of rows is number of newlines
    nr = length(idx);

    ## convert separators to spaces
    idx = find (in == sep);
    if (length(idx) > 0) in(idx) = " "; endif

    [x, n, err] = sscanf(in, "%g");
    if (!isempty(err))
      error(["dlmread: ", err]);
    elseif (rem(n, nr) != 0)
      error("dlmread: rows are different lengths")
    else
      x = reshape(x, nr, n/nr);
    endif
  else
    error (["dlmwrite: could not open ", filename]);
  endif

endfunction


