changeset 32076:7ce8302036c1

fileread.m: Add encoding parameter (bug #64139). * scripts/io/fileread.m: Add "Encoding" input parameter. Add BISTs for input validation.
author Guillaume Flandin <guillaume.offline@gmail.com>
date Fri, 05 May 2023 10:24:17 +0100
parents 9583d971e603
children ed3a18fe328a
files scripts/io/fileread.m
diffstat 1 files changed, 45 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/io/fileread.m	Thu May 04 18:06:08 2023 +0200
+++ b/scripts/io/fileread.m	Fri May 05 10:24:17 2023 +0100
@@ -24,12 +24,23 @@
 ########################################################################
 
 ## -*- texinfo -*-
-## @deftypefn {} {@var{str} =} fileread (@var{filename})
+## @deftypefn  {} {@var{str} =} fileread (@var{filename})
+## @deftypefnx {} {@var{str} =} fileread (@var{filename}, @var{param}, @var{value}, @dots{})
 ## Read the contents of @var{filename} and return it as a string.
-## @seealso{fread, fscanf, importdata, textscan, type}
+##
+## @var{param}, @var{value} are optional pairs of parameters and values.  Valid
+## options are:
+##
+## @table @asis
+## @item @qcode{"Encoding"}
+## Specify encoding used when reading from the file.  This is a character
+## string of a valid encoding identifier.  The default is @qcode{"utf-8"}.
+## @end table
+##
+## @seealso{fopen, fread, fscanf, importdata, textscan, type}
 ## @end deftypefn
 
-function str = fileread (filename)
+function str = fileread (filename, varargin)
 
   if (nargin < 1)
     print_usage ();
@@ -39,7 +50,32 @@
     error ("fileread: FILENAME argument must be a string");
   endif
 
-  fid = fopen (filename, "r");
+  encoding = "utf-8";
+
+  if (nargin > 1)
+
+    ## Check for parameter/value arguments
+    for i_arg = 1:2:numel (varargin)
+
+      if (! ischar (varargin{i_arg}))
+        error ("fileread: parameter %d must be a string", i_arg);
+      endif
+      parameter = varargin{i_arg};
+      if (i_arg+1 > numel (varargin))
+        error ('fileread: parameter "%s" missing value', parameter);
+      endif
+
+      switch (lower (parameter))
+        case "encoding"
+          encoding = varargin{i_arg+1};
+        otherwise
+          error ('fileread: Unknown option "%s"', parameter);
+      endswitch
+
+    endfor
+  endif
+
+  fid = fopen (filename, "r", "n", encoding);
   if (fid < 0)
     error ("fileread: cannot open file");
   endif
@@ -66,3 +102,8 @@
 ## Test input validation
 %!error <Invalid call> fileread ()
 %!error <FILENAME argument must be a string> fileread (1)
+%!error <parameter "Encoding" missing value> fileread ("filename", "Encoding")
+%!error <Unknown option "UnknownParam">
+%! fileread ("filename", "UnknownParam", "UnknownValue")
+%!error <conversion from codepage 'unknownvalue' not supported>
+%! fileread ("filename", "Encoding", "UnknownValue")