40#if defined (HAVE_CONFIG_H)
50#if defined (HAVE_ZLIB_H)
66#include "builtin-defun-decls.h"
103 std::size_t pos = mode.find (
'W');
105 if (pos != std::string::npos)
108 pos = mode.find (
'R');
110 if (pos != std::string::npos)
113 pos = mode.find (
'A');
115 if (pos != std::string::npos)
118 pos = mode.find (
'z');
120 if (pos != std::string::npos)
122#if defined (HAVE_ZLIB)
133 std::size_t bpos = mode.find (
'b');
134 std::size_t tpos = mode.find (
't');
136 if (bpos == std::string::npos && tpos == std::string::npos)
141static std::ios::openmode
144 std::ios::openmode retval = std::ios::in;
147 retval = std::ios::in;
148 else if (mode ==
"wt")
150 else if (mode ==
"at")
151 retval = std::ios::out | std::ios::app;
152 else if (mode ==
"r+t" || mode ==
"rt+")
153 retval = std::ios::in | std::ios::out;
154 else if (mode ==
"w+t" || mode ==
"wt+")
156 else if (mode ==
"a+t" || mode ==
"at+")
157 retval = std::ios::in | std::ios::out | std::ios::app;
158 else if (mode ==
"rb" || mode ==
"r")
159 retval = std::ios::in | std::ios::binary;
160 else if (mode ==
"wb" || mode ==
"w")
162 else if (mode ==
"ab" || mode ==
"a")
163 retval = std::ios::out | std::ios::app | std::ios::binary;
164 else if (mode ==
"r+b" || mode ==
"rb+" || mode ==
"r+")
165 retval = std::ios::in | std::ios::out | std::ios::binary;
166 else if (mode ==
"w+b" || mode ==
"wb+" || mode ==
"w+")
169 else if (mode ==
"a+b" || mode ==
"ab+" || mode ==
"a+")
170 retval = (std::ios::in | std::ios::out | std::ios::app
173 error (
"invalid mode specified");
192 if (args.length () != 1)
195 stream_list& streams = interp.get_stream_list ();
197 return ovl (streams.remove (args(0),
"fclose"));
208 if (args.length () != 1)
211 stream_list& streams = interp.get_stream_list ();
213 int fid = streams.get_file_number (args(0));
215 stream os = streams.lookup (fid,
"fclear");
237 if (args.length () != 1)
242 stream_list& streams = interp.get_stream_list ();
245 int fid = streams.get_file_number (args(0));
255 stream os = streams.lookup (fid,
"fflush");
257 retval = os.flush ();
283 static const std::string who =
"fgetl";
285 int nargin = args.length ();
287 if (nargin < 1 || nargin > 2)
290 stream_list& streams = interp.get_stream_list ();
292 stream os = streams.lookup (args(0), who);
298 std::string tmp = os.getl (len_arg, err, who);
301 return ovl (tmp, tmp.length ());
326 static const std::string who =
"fgets";
328 int nargin = args.length ();
330 if (nargin < 1 || nargin > 2)
333 stream_list& streams = interp.get_stream_list ();
335 stream os = streams.lookup (args(0), who);
341 std::string tmp = os.gets (len_arg, err, who);
344 return ovl (tmp, tmp.length ());
346 return ovl (-1.0, 0.0);
368 static const std::string who =
"fskipl";
370 int nargin = args.length ();
372 if (nargin < 1 || nargin > 2)
375 stream_list& streams = interp.get_stream_list ();
377 stream os = streams.lookup (args(0), who);
383 off_t tmp = os.skipl (count_arg, err, who);
393 const std::string& arch, std::string encoding,
int& fid)
400 std::transform (encoding.begin (), encoding.end (), encoding.begin (),
402 if (encoding.compare (
"utf-8"))
406 if (codec ==
reinterpret_cast<void *
> (-1))
409 error (
"fopen: conversion from codepage '%s' not supported",
416 std::string mode = mode_arg;
417 bool use_zlib =
false;
427 sys::file_stat fs (fname);
429 if (! (md & std::ios::out))
434#if defined (HAVE_ZLIB)
441 int fd = fileno (
fptr);
443 gzFile gzf = ::gzdopen (fd, mode.c_str ());
449 retval.error (std::strerror (errno));
459 retval.error (std::strerror (errno));
470 const char *fcn,
int& fid)
477 std::string mode = tc_mode.
xstring_value (
"%s: file mode must be a string", fcn);
478 std::string arch = tc_arch.
xstring_value (
"%s: architecture type must be a string", fcn);
479 std::string encoding = tc_encoding.
xstring_value (
"%s: ENCODING must be a string", fcn);
595 int nargin = args.length ();
597 if (nargin < 1 || nargin > 4)
602 stream_list& streams = interp.get_stream_list ();
606 if (args(0).is_string ())
612 if (nargout < 2 && args(0).string_value () ==
"all")
613 return streams.open_file_numbers ();
619 retval =
ovl (tmp(0), tmp(1), tmp(2), tmp(3));
637 stream os =
do_stream_open (args(0), mode, arch, encoding,
"fopen",
641 retval =
ovl (streams.insert (os),
"");
644 int error_number = 0;
646 retval =
ovl (-1.0, os.error (
false, error_number));
690 if (args.length () > 0)
691 warning (
"freport: ignoring extra arguments");
693 stream_list& streams = interp.get_stream_list ();
700DEFMETHOD (frewind, interp, args, nargout,
712 if (args.length () != 1)
717 stream_list& streams = interp.get_stream_list ();
719 stream os = streams.lookup (args(0),
"frewind");
721 result = os.rewind ();
748 int nargin = args.length ();
750 if (nargin < 2 || nargin > 3)
753 stream_list& streams = interp.get_stream_list ();
755 stream os = streams.lookup (args(0),
"fseek");
759 return ovl (os.seek (args(1), origin_arg));
770 if (args.length () != 1)
773 stream_list& streams = interp.get_stream_list ();
775 stream os = streams.lookup (args(0),
"ftell");
777 return ovl (os.tell ());
784 int nargin = args.
length ();
786 if (! (nargin > 1 || (nargin > 0 && args(0).is_string ())))
796 if (args(0).is_string ())
797 os = streams.lookup (1, who);
801 os = streams.lookup (args(0), who);
804 if (! args(fmt_n).is_string ())
805 error (
"%s: format TEMPLATE must be a string", who.c_str ());
809 if (nargin > 1 + fmt_n)
813 for (
int i = fmt_n + 1; i < nargin; i++)
814 tmp_args(i-fmt_n-1) = args(i);
817 result = os.printf (args(fmt_n), tmp_args, who);
825DEFMETHOD (fprintf, interp, args, nargout,
844 static const std::string who =
"fprintf";
867 static const std::string who =
"printf";
884 stream os = streams.lookup (args(0), who);
886 return ovl (os.puts (args(1), who));
903 static const std::string who =
"fputs";
921 static const std::string who =
"puts";
928DEFUN (sprintf, args, ,
944 static const std::string who =
"sprintf";
946 int nargin = args.length ();
959 if (! os.is_valid ())
960 error (
"%s: unable to create output buffer", who.c_str ());
965 error (
"%s: format TEMPLATE must be a string", who.c_str ());
974 for (
int i = 1; i < nargin; i++)
975 tmp_args(i-1) = args(i);
979 retval(2) = os.printf (fmt_arg, tmp_args, who);
980 retval(1) = os.error ();
982 std::string result = ostr->
str ();
995 int nargin = args.
length ();
997 if (nargin < 2 || nargin > 3)
1004 stream os = streams.lookup (args(0), who);
1006 if (! args(1).is_string ())
1007 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1009 if (nargin == 3 && args(2).is_string ())
1011 retval = os.oscanf (args(1), who);
1019 ? args(2).vector_value ()
1022 octave_value tmp = os.scanf (args(1), size, count, who);
1024 retval =
ovl (tmp, count, os.error ());
1080 static const std::string who =
"fscanf";
1091 error (
"%s: argument STRING must be a string", who.c_str ());
1100DEFUN (sscanf, args, ,
1113 static const std::string who =
"sscanf";
1115 int nargin = args.length ();
1117 if (nargin < 2 || nargin > 3)
1126 if (! os.is_valid ())
1127 error (
"%s: unable to create temporary input buffer", who.c_str ());
1129 if (! args(1).is_string ())
1130 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1132 if (nargin == 3 && args(2).is_string ())
1134 retval = os.oscanf (args(1), who);
1140 Array<double> size = (nargin == 3) ? args(2).vector_value ()
1144 octave_value tmp = os.scanf (args(1), size, count, who);
1149 std::string errmsg = os.error ();
1151 retval =
ovl (tmp, count, errmsg,
1152 (os.eof () ? data.length () : os.tell ()) + 1);
1177 static const std::string who =
"scanf";
1193 if (args(0).is_string ())
1199 if (! os.is_valid ())
1200 error (
"%s: unable to create temporary input buffer", who.c_str ());
1206 os = streams.lookup (args(0), who);
1218 else if (args(1).is_string ())
1220 fmt = args(1).string_value ();
1222 if (args(1).is_sq_string ())
1228 error (
"%s: FORMAT must be a string", who.c_str ());
1234 if (args(2).isnumeric ())
1236 ntimes = args(2).idx_type_value ();
1238 if (ntimes < args(2).double_value ())
1239 error (
"%s: REPEAT = %g is too large",
1240 who.c_str (), args(2).double_value ());
1250 octave_value result = os.textscan (fmt, ntimes, options, who, count);
1252 std::string errmsg = os.error ();
1254 return ovl (result, count, errmsg);
1522 static const std::string who =
"textscan";
1534 if (args.length () == 0)
1538 args.splice (0, 1));
2336## Test unfinished comment
2338%! c = textscan (
"1 2 /* half comment",
"%n %u8",
"CommentStyle", {
"/*",
"*/"});
2339%! assert (c, {1, 2});
2341## Test reading from a real file
2346%! fprintf (fid,
" %f %f /* comment */ %f %f ",
d);
2347%! fseek (fid, 0,
"bof");
2348%!
A = textscan (fid,
"%f %f",
"CommentStyle", {
"/*",
"*/"});
2352%! assert (
A{1}, [
d(1);
d(3)], 1e-6);
2353%! assert (
A{2}, [
d(2);
d(4)], 1e-6);
2378 std::string prec = prec_arg.
xstring_value (
"fread: PRECISION must be a string");
2387 input_type, output_type);
2391 error (ee,
"fread: invalid PRECISION specified");
2402 error (ee,
"fread: SKIP must be an integer");
2405 std::string arch = arch_arg.
xstring_value (
"fread: ARCH architecture type must be a string");
2410 return os.read (size, block_size, input_type, output_type, skip,
2571 int nargin = args.length ();
2573 if (nargin < 1 || nargin > 5)
2576 stream_list& streams = interp.get_stream_list ();
2578 stream os = streams.lookup (args(0),
"fread");
2587 if (nargin > idx && ! args(idx).is_string ())
2608 return ovl (tmp, count);
2616 std::string prec = prec_arg.
xstring_value (
"fwrite: PRECISION must be a string");
2627 error (ee,
"fwrite: invalid PRECISION specified");
2638 error (ee,
"fwrite: SKIP must be an integer");
2641 std::string arch = arch_arg.
xstring_value (
"fwrite: ARCH architecture type must be a string");
2646 return os.write (data, block_size, output_type, skip, flt_fmt);
2671 int nargin = args.length ();
2673 if (nargin < 2 || nargin > 5)
2676 stream_list& streams = interp.get_stream_list ();
2678 stream os = streams.lookup (args(0),
"fwrite");
2717 if (args.length () != 1)
2720 stream_list& streams = interp.get_stream_list ();
2722 stream os = streams.lookup (args(0),
"feof");
2724 return ovl (os.eof () ? 1.0 : 0.0);
2748 int nargin = args.length ();
2750 if (nargin < 1 || nargin > 2)
2753 stream_list& streams = interp.get_stream_list ();
2755 stream os = streams.lookup (args(0),
"ferror");
2761 std::string opt = args(1).string_value ();
2763 clear = (opt ==
"clear");
2766 int error_number = 0;
2768 std::string error_message = os.error (clear, error_number);
2770 return ovl (error_message, error_number);
2811 if (args.length () != 2)
2814 std::string
name = args(0).xstring_value (
"popen: COMMAND must be a string");
2815 std::string mode = args(1).xstring_value (
"popen: MODE must be a string");
2819 stream_list& streams = interp.get_stream_list ();
2825 retval = streams.insert (ips);
2827 else if (mode ==
"w")
2831 retval = streams.insert (ops);
2834 error (
"popen: invalid MODE specified");
2848 if (args.length () != 1)
2851 stream_list& streams = interp.get_stream_list ();
2853 return ovl (streams.remove (args(0),
"pclose"));
2856DEFUN (tempname, args, ,
2876 int nargin = args.length ();
2884 dir = args(0).xstring_value (
"tempname: DIR must be a string");
2886 dir = sys::env::getenv (
"TMPDIR");
2888 std::string pfx (
"oct-");
2891 pfx = args(1).xstring_value (
"tempname: PREFIX must be a string");
2964 if (args.length () != 0)
2983 error (
"tmpfile: failed to create stdiostream object");
2986 stream_list& streams = interp.get_stream_list ();
2988 retval =
ovl (streams.insert (s),
"");
2991 retval =
ovl (-1, std::strerror (errno));
3020 int nargin = args.length ();
3022 if (nargin < 1 || nargin > 2)
3025 std::string tmpl8 = args(0).xstring_value (
"mkstemp: TEMPLATE argument must be a string");
3030 strcpy (tmp, tmpl8.c_str ());
3037 retval(2) = std::strerror (errno);
3041 const char *fopen_mode =
"w+b";
3043 FILE *fid = fdopen (fd, fopen_mode);
3048 retval(2) = std::strerror (errno);
3052 std::string nm = tmp;
3059 error (
"mkstemp: failed to create stdiostream object");
3061 stream_list& streams = interp.get_stream_list ();
3063 retval(0) = streams.insert (s);
3066 if (nargin == 2 && args(1).
is_true ())
3067 interp.mark_for_deletion (nm);
3081 int tmp =
x % obase;
3083 if (tmp > ibase - 1)
3084 error (
"umask: invalid digit");
3088 while ((
x = (
x - tmp) / obase))
3092 if (tmp > ibase - 1)
3093 error (
"umask: invalid digit");
3095 retval += mult * tmp;
3120 if (args.length () != 1)
3123 int mask = args(0).xint_value (
"umask: MASK must be an integer");
3126 error (
"umask: MASK must be a positive integer value");
3128 int oct_mask =
convert (mask, 8, 10);
3133 return ovl (status);
3160 if (args.length () != 0)
3222 stream_list& streams = interp.get_stream_list ();
3224 return const_value (
"stdin", args, streams.stdin_file ());
3236 stream_list& streams = interp.get_stream_list ();
3238 return const_value (
"stdout", args, streams.stdout_file ());
3251 stream_list& streams = interp.get_stream_list ();
3253 return const_value (
"stderr", args, streams.stderr_file ());
3261 octave::interpreter& interp
3264 interp.mark_for_deletion (file);
3270 octave::interpreter& interp
3273 interp.cleanup_tmp_files ();
Vector representing the dimensions (size) of an Array.
stream_list & get_stream_list(void)
static stream create(const char *data, std::ios::openmode arg_md=std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8")
static data_type string_to_data_type(const std::string &s)
static octave::stream create(const std::string &n, std::ios::openmode arg_md=std::ios::in, octave::mach_info::float_format flt_fmt=octave::mach_info::native_float_format(), const std::string &encoding="utf-8")
static octave::stream create(const std::string &n, std::ios::openmode arg_md=std::ios::out, octave::mach_info::float_format flt_fmt=octave::mach_info::native_float_format(), const std::string &encoding="utf-8")
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length(void) const
octave_value_list splice(octave_idx_type offset, octave_idx_type len, const octave_value_list &lst=octave_value_list()) const
octave_value_list & prepend(const octave_value &val)
OCTINTERP_API std::string xstring_value(const char *fmt,...) const
int int_value(bool req_int=false, bool frc_str_conv=false) const
octave_idx_type numel(void) const
bool is_string(void) const
octave_value reshape(const dim_vector &dv) const
std::string string_value(bool force=false) const
OCTINTERP_API Array< double > xvector_value(const char *fmt,...) const
bool is_sq_string(void) const
static stream create(const std::string &n, FILE *f=nullptr, std::ios::openmode m=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8", c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
static stream create(const std::string &n, gzFile f=nullptr, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, mach_info::float_format ff=mach_info::native_float_format(), const std::string &encoding="utf-8", c_zfile_ptr_buf::close_fcn cf=c_zfile_ptr_buf::file_close)
OCTINTERP_API void print_usage(void)
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
#define DEFUNX(name, fname, args_name, nargout_name, doc)
Macro to define a builtin function with certain internal name.
void warning(const char *fmt,...)
void error(const char *fmt,...)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
static octave_value_list textscan_internal(interpreter &interp, const std::string &who, const octave_value_list &args)
static octave_value_list puts_internal(interpreter &interp, const std::string &who, const octave_value_list &args)
void cleanup_tmp_files_deprecated(void)
OCTAVE_EXPORT octave_value_list Fstdout(octave::interpreter &interp, const octave_value_list &args, int)
OCTAVE_EXPORT octave_value_list FSEEK_CUR(const octave_value_list &args, int)
OCTAVE_EXPORT octave_value_list FSEEK_SET(const octave_value_list &args, int)
static stream do_stream_open(const std::string &name, const std::string &mode_arg, const std::string &arch, std::string encoding, int &fid)
OCTAVE_EXPORT octave_value_list FSEEK_END(const octave_value_list &args, int)
OCTAVE_EXPORT octave_value_list Ffeof(octave::interpreter &interp, const octave_value_list &args, int)
void mark_for_deletion_deprecated(const std::string &file)
OCTAVE_EXPORT octave_value_list Fpclose(octave::interpreter &interp, const octave_value_list &args, int)
static OCTAVE_NAMESPACE_BEGIN void normalize_fopen_mode(std::string &mode, bool &use_zlib)
static std::string get_scan_string_data(const octave_value &val, const std::string &who)
static int do_fwrite(stream &os, const octave_value &data, const octave_value &prec_arg, const octave_value &skip_arg, const octave_value &arch_arg)
static octave_value_list printf_internal(interpreter &interp, const std::string &who, const octave_value_list &args, int nargout)
OCTAVE_EXPORT octave_value_list Fstdin(octave::interpreter &interp, const octave_value_list &args, int)
OCTAVE_EXPORT octave_value_list Fstderr(octave::interpreter &interp, const octave_value_list &args, int)
static octave_value const_value(const char *, const octave_value_list &args, int val)
OCTAVE_EXPORT octave_value_list Fferror(octave::interpreter &interp, const octave_value_list &args, int)
OCTAVE_EXPORT octave_value_list FP_tmpdir(const octave_value_list &args, int)
OCTAVE_EXPORT octave_value_list Fumask(const octave_value_list &args, int)
static octave_value do_fread(stream &os, const octave_value &size_arg, const octave_value &prec_arg, const octave_value &skip_arg, const octave_value &arch_arg, octave_idx_type &count)
OCTAVE_EXPORT octave_value_list Fpopen(octave::interpreter &interp, const octave_value_list &args, int)
static octave_value_list scanf_internal(interpreter &interp, const std::string &who, const octave_value_list &args)
static int convert(int x, int ibase, int obase)
static std::ios::openmode fopen_mode_to_ios_mode(const std::string &mode)
ColumnVector transform(const Matrix &m, double x, double y, double z)
void * octave_iconv_open_wrapper(const char *tocode, const char *fromcode)
int octave_iconv_close_wrapper(void *cd)
double lo_ieee_inf_value(void)
F77_RET_T const F77_INT F77_CMPLX * A
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
const char * octave_locale_charset_wrapper(void)
bool is_true(const std::string &s)
int octave_mkostemp_wrapper(char *tmpl)
float_format string_to_float_format(const std::string &s)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
std::complex< T > trunc(const std::complex< T > &x)
std::string tilde_expand(const std::string &name)
std::string tempnam(const std::string &dir, const std::string &pfx)
int unlink(const std::string &name)
std::FILE * fopen(const std::string &filename, const std::string &mode)
std::string fgets(FILE *f)
std::string fgetl(FILE *f)
interpreter & __get_interpreter__(const std::string &who)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
DEFMETHODX("quad", Fquad, interp, args,, doc:)
std::string get_P_tmpdir(void)
FILE * octave_tmpfile_wrapper(void)
std::string do_string_escapes(const std::string &s)
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file)