40 #if defined (HAVE_CONFIG_H)
50 #if defined (HAVE_ZLIB_H)
65 #include "builtin-defun-decls.h"
101 std::size_t pos = mode.find (
'W');
103 if (pos != std::string::npos)
106 pos = mode.find (
'R');
108 if (pos != std::string::npos)
111 pos = mode.find (
'A');
113 if (pos != std::string::npos)
116 pos = mode.find (
'z');
118 if (pos != std::string::npos)
120 #if defined (HAVE_ZLIB)
131 std::size_t bpos = mode.find (
'b');
132 std::size_t tpos = mode.find (
't');
134 if (bpos == std::string::npos && tpos == std::string::npos)
139 static std::ios::openmode
142 std::ios::openmode retval = std::ios::in;
145 retval = std::ios::in;
146 else if (mode ==
"wt")
148 else if (mode ==
"at")
149 retval = std::ios::out | std::ios::app;
150 else if (mode ==
"r+t" || mode ==
"rt+")
151 retval = std::ios::in | std::ios::out;
152 else if (mode ==
"w+t" || mode ==
"wt+")
154 else if (mode ==
"a+t" || mode ==
"at+")
155 retval = std::ios::in | std::ios::out | std::ios::app;
156 else if (mode ==
"rb" || mode ==
"r")
157 retval = std::ios::in | std::ios::binary;
158 else if (mode ==
"wb" || mode ==
"w")
160 else if (mode ==
"ab" || mode ==
"a")
161 retval = std::ios::out | std::ios::app | std::ios::binary;
162 else if (mode ==
"r+b" || mode ==
"rb+" || mode ==
"r+")
163 retval = std::ios::in | std::ios::out | std::ios::binary;
164 else if (mode ==
"w+b" || mode ==
"wb+" || mode ==
"w+")
167 else if (mode ==
"a+b" || mode ==
"ab+" || mode ==
"a+")
168 retval = (std::ios::in | std::ios::out | std::ios::app
171 error (
"invalid mode specified");
189 if (args.length () != 1)
194 return ovl (streams.
remove (args(0),
"fclose"));
205 if (args.length () != 1)
234 if (args.length () != 1)
254 retval = os.
flush ();
280 static const std::string who =
"fgetl";
282 int nargin = args.length ();
284 if (nargin < 1 || nargin > 2)
295 std::string tmp = os.
getl (len_arg, err, who);
298 return ovl (tmp, tmp.length ());
323 static const std::string who =
"fgets";
325 int nargin = args.length ();
327 if (nargin < 1 || nargin > 2)
338 std::string tmp = os.
gets (len_arg, err, who);
341 return ovl (tmp, tmp.length ());
343 return ovl (-1.0, 0.0);
365 static const std::string who =
"fskipl";
367 int nargin = args.length ();
369 if (nargin < 1 || nargin > 2)
380 off_t tmp = os.
skipl (count_arg, err, who);
390 const std::string& arch, std::string encoding,
int& fid)
397 std::transform (encoding.begin (), encoding.end (), encoding.begin (),
399 if (encoding.compare (
"utf-8"))
403 if (codec ==
reinterpret_cast<void *
> (-1))
406 error (
"fopen: conversion from codepage '%s' not supported",
413 std::string mode = mode_arg;
414 bool use_zlib =
false;
424 sys::file_stat fs (fname);
426 if (! (md & std::ios::out))
431 #if defined (HAVE_ZLIB)
438 int fd = fileno (
fptr);
440 gzFile gzf = ::gzdopen (fd, mode.c_str ());
446 retval.
error (std::strerror (errno));
456 retval.
error (std::strerror (errno));
467 const char *fcn,
int& fid)
473 std::string name = tc_name.
xstring_value (
"%s: filename must be a string", fcn);
474 std::string mode = tc_mode.
xstring_value (
"%s: file mode must be a string", fcn);
475 std::string arch = tc_arch.
xstring_value (
"%s: architecture type must be a string", fcn);
476 std::string encoding = tc_encoding.
xstring_value (
"%s: ENCODING must be a string", fcn);
592 int nargin = args.length ();
594 if (nargin < 1 || nargin > 4)
603 if (args(0).is_string ())
609 if (nargout < 2 && args(0).string_value () ==
"all")
616 retval =
ovl (tmp(0), tmp(1), tmp(2), tmp(3));
641 int error_number = 0;
643 retval =
ovl (-1.0, os.
error (
false, error_number));
687 if (args.length () > 0)
688 warning (
"freport: ignoring extra arguments");
697 DEFMETHOD (frewind, interp, args, nargout,
712 if (args.length () != 1)
747 int nargin = args.length ();
749 if (nargin < 2 || nargin > 3)
758 return ovl (os.
seek (args(1), origin_arg));
769 if (args.length () != 1)
783 int nargin = args.
length ();
785 if (! (nargin > 1 || (nargin > 0 && args(0).is_string ())))
795 if (args(0).is_string ())
796 os = streams.
lookup (1, who);
800 os = streams.
lookup (args(0), who);
803 if (! args(fmt_n).is_string ())
804 error (
"%s: format TEMPLATE must be a string", who.c_str ());
808 if (nargin > 1 + fmt_n)
812 for (
int i = fmt_n + 1; i < nargin; i++)
813 tmp_args(i-fmt_n-1) = args(i);
816 result = os.
printf (args(fmt_n), tmp_args, who);
824 DEFMETHOD (fprintf, interp, args, nargout,
844 static const std::string who =
"fprintf";
849 DEFMETHOD (printf, interp, args, nargout,
870 static const std::string who =
"printf";
889 return ovl (- (os.
puts (args(1), who) < 0));
906 static const std::string who =
"fputs";
952 static const std::string who =
"puts";
959 DEFUN (sprintf, args, ,
975 static const std::string who =
"sprintf";
977 int nargin = args.length ();
991 error (
"%s: unable to create output buffer", who.c_str ());
996 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1005 for (
int i = 1; i < nargin; i++)
1006 tmp_args(i-1) = args(i);
1010 retval(2) = os.
printf (fmt_arg, tmp_args, who);
1011 retval(1) = os.
error ();
1013 std::string result = ostr->
str ();
1026 int nargin = args.
length ();
1028 if (nargin < 2 || nargin > 3)
1037 if (! args(1).is_string ())
1038 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1040 if (nargin == 3 && args(2).is_string ())
1042 retval = os.
oscanf (args(1), who);
1050 ? args(2).vector_value ()
1055 retval =
ovl (tmp, count, os.
error ());
1111 static const std::string who =
"fscanf";
1122 error (
"%s: argument STRING must be a string", who.c_str ());
1131 DEFUN (sscanf, args, ,
1144 static const std::string who =
"sscanf";
1146 int nargin = args.length ();
1148 if (nargin < 2 || nargin > 3)
1158 error (
"%s: unable to create temporary input buffer", who.c_str ());
1160 if (! args(1).is_string ())
1161 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1163 if (nargin == 3 && args(2).is_string ())
1165 retval = os.
oscanf (args(1), who);
1171 Array<double> size = (nargin == 3) ? args(2).vector_value ()
1180 std::string errmsg = os.
error ();
1182 retval =
ovl (tmp, count, errmsg,
1183 (os.
eof () ? data.length () : os.
tell ()) + 1);
1208 static const std::string who =
"scanf";
1224 if (args(0).is_string ())
1231 error (
"%s: unable to create temporary input buffer", who.c_str ());
1237 os = streams.
lookup (args(0), who);
1249 else if (args(1).is_string ())
1251 fmt = args(1).string_value ();
1253 if (args(1).is_sq_string ())
1259 error (
"%s: FORMAT must be a string", who.c_str ());
1265 if (args(2).isnumeric ())
1267 ntimes = args(2).idx_type_value ();
1269 if (ntimes < args(2).double_value ())
1270 error (
"%s: REPEAT = %g is too large",
1271 who.c_str (), args(2).double_value ());
1283 std::string errmsg = os.
error ();
1285 return ovl (result, count, errmsg);
1553 static const std::string who =
"textscan";
1558 DEFMETHOD (__textscan__, interp, args, ,
1565 if (args.length () == 0)
1569 args.splice (0, 1));
2370 ## Test unfinished comment
2372 %! c =
textscan (
"1 2 /* half comment",
"%n %u8",
"CommentStyle", {
"/*",
"*/"});
2373 %! assert (c, {1, 2});
2375 ## Test reading from a real file
2378 %! fid =
fopen (
f,
"w+");
2380 %! fprintf (fid,
" %f %f /* comment */ %f %f ",
d);
2381 %! fseek (fid, 0,
"bof");
2382 %!
A =
textscan (fid,
"%f %f",
"CommentStyle", {
"/*",
"*/"});
2386 %! assert (
A{1}, [
d(1);
d(3)], 1e-6);
2387 %! assert (
A{2}, [
d(2);
d(4)], 1e-6);
2412 std::string prec = prec_arg.
xstring_value (
"fread: PRECISION must be a string");
2421 input_type, output_type);
2425 error (ee,
"fread: invalid PRECISION specified");
2436 error (ee,
"fread: SKIP must be an integer");
2439 std::string arch = arch_arg.
xstring_value (
"fread: ARCH architecture type must be a string");
2444 return os.
read (size, block_size, input_type, output_type, skip,
2605 int nargin = args.length ();
2607 if (nargin < 1 || nargin > 5)
2621 if (nargin > idx && ! args(idx).is_string ())
2642 return ovl (tmp, count);
2650 std::string prec = prec_arg.
xstring_value (
"fwrite: PRECISION must be a string");
2661 error (ee,
"fwrite: invalid PRECISION specified");
2672 error (ee,
"fwrite: SKIP must be an integer");
2675 std::string arch = arch_arg.
xstring_value (
"fwrite: ARCH architecture type must be a string");
2680 return os.
write (data, block_size, output_type, skip, flt_fmt);
2705 int nargin = args.length ();
2707 if (nargin < 2 || nargin > 5)
2751 if (args.length () != 1)
2758 return ovl (os.
eof () ? 1.0 : 0.0);
2782 int nargin = args.length ();
2784 if (nargin < 1 || nargin > 2)
2795 std::string opt = args(1).string_value ();
2797 clear = (opt ==
"clear");
2800 int error_number = 0;
2802 std::string error_message = os.
error (clear, error_number);
2804 return ovl (error_message, error_number);
2845 if (args.length () != 2)
2848 std::string name = args(0).xstring_value (
"popen: COMMAND must be a string");
2849 std::string mode = args(1).xstring_value (
"popen: MODE must be a string");
2859 retval = streams.
insert (ips);
2861 else if (mode ==
"w")
2865 retval = streams.
insert (ops);
2868 error (
"popen: invalid MODE specified");
2885 if (args.length () != 1)
2890 return ovl (streams.
remove (args(0),
"pclose"));
2893 DEFUN (tempdir, args, ,
2906 if (args.length () > 0)
2914 return ovl (tmpdir);
2936 DEFUN (tempname, args, ,
2956 int nargin = args.length ();
2964 dir = args(0).xstring_value (
"tempname: DIR must be a string");
2966 std::string pfx (
"oct-");
2969 pfx = args(1).xstring_value (
"tempname: PREFIX must be a string");
3042 if (args.length () != 0)
3061 error (
"tmpfile: failed to create stdiostream object");
3069 retval =
ovl (-1, std::strerror (errno));
3098 int nargin = args.length ();
3100 if (nargin < 1 || nargin > 2)
3103 std::string tmpl8 = args(0).xstring_value (
"mkstemp: TEMPLATE argument must be a string");
3108 strcpy (tmp, tmpl8.c_str ());
3115 retval(2) = std::strerror (errno);
3119 const char *fopen_mode =
"w+b";
3121 FILE *fid = fdopen (fd, fopen_mode);
3126 retval(2) = std::strerror (errno);
3130 std::string nm = tmp;
3137 error (
"mkstemp: failed to create stdiostream object");
3141 retval(0) = streams.
insert (s);
3144 if (nargin == 2 && args(1).
is_true ())
3145 interp.mark_for_deletion (nm);
3159 int tmp =
x % obase;
3161 if (tmp > ibase - 1)
3162 error (
"umask: invalid digit");
3166 while ((
x = (
x - tmp) / obase))
3170 if (tmp > ibase - 1)
3171 error (
"umask: invalid digit");
3173 retval += mult * tmp;
3198 if (args.length () != 1)
3201 int mask = args(0).xint_value (
"umask: MASK must be an integer");
3204 error (
"umask: MASK must be a positive integer value");
3206 int oct_mask =
convert (mask, 8, 10);
3211 return ovl (status);
3238 if (args.length () != 0)
template class OCTAVE_CLASS_TEMPLATE_INSTANTIATION_API Array< double >
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
OCTINTERP_API Array< double > xvector_value(const char *fmt,...) 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
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)
OCTINTERP_API octave_value stdout_file(void) const
OCTINTERP_API int insert(stream &os)
OCTINTERP_API octave_value open_file_numbers(void) const
OCTINTERP_API octave_value stderr_file(void) const
OCTINTERP_API std::string list_open_files(void) const
OCTINTERP_API int get_file_number(const octave_value &fid) const
OCTINTERP_API stream lookup(int fid, const std::string &who="") const
OCTINTERP_API string_vector get_info(int fid) const
OCTINTERP_API int remove(int fid, const std::string &who="")
OCTINTERP_API octave_value stdin_file(void) const
OCTINTERP_API octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
OCTINTERP_API std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
bool is_valid(void) const
OCTINTERP_API bool eof(void) const
OCTINTERP_API int rewind(void)
OCTINTERP_API off_t skipl(off_t count, bool &err, const std::string &who)
OCTINTERP_API octave_value textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
OCTINTERP_API int puts(const std::string &s, const std::string &who)
OCTINTERP_API int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
OCTINTERP_API int flush(void)
OCTINTERP_API int seek(off_t offset, int origin)
OCTINTERP_API octave_value_list oscanf(const std::string &fmt, const std::string &who)
OCTINTERP_API off_t tell(void)
OCTINTERP_API std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
OCTINTERP_API std::string error(bool clear, int &err_num)
OCTINTERP_API octave_value read(const Array< double > &size, octave_idx_type block_size, oct_data_conv::data_type input_type, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt, octave_idx_type &count)
OCTINTERP_API octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
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)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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)
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)
OCTAVE_EXPORT octave_value_list Fpclose(octave::interpreter &interp, const octave_value_list &args, int)
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)
static void normalize_fopen_mode(std::string &mode, bool &use_zlib)
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)
int unlink(const std::string &name)
std::string tempnam(const std::string &dir, const std::string &pfx)
std::string dir_sep_str(void)
std::string tilde_expand(const std::string &name)
ColumnVector transform(const Matrix &m, double x, double y, double z)
int octave_iconv_close_wrapper(void *cd)
void * octave_iconv_open_wrapper(const char *tocode, const char *fromcode)
double lo_ieee_inf_value(void)
F77_RET_T const F77_INT F77_CMPLX * A
std::complex< T > trunc(const std::complex< T > &x)
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
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
std::FILE * fopen(const std::string &filename, const std::string &mode)
std::FILE * fopen_tmp(const std::string &name, const std::string &mode)
std::string fgetl(FILE *f)
std::string fgets(FILE *f)
const char * octave_locale_charset_wrapper(void)
float_format string_to_float_format(const std::string &s)
bool is_true(const std::string &s)
static std::string get_temp_directory(void)
int octave_mkostemp_wrapper(char *tmpl)
#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:)
static bool is_dir_sep(char c)
std::string get_P_tmpdir(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)