40 #if defined (HAVE_CONFIG_H)
50 #if defined (HAVE_ZLIB_H)
66 #include "builtin-defun-decls.h"
101 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 size_t bpos = mode.find (
'b');
132 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;
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");
190 if (args.length () != 1)
195 return ovl (streams.
remove (args(0),
"fclose"));
206 if (args.length () != 1)
235 if (args.length () != 1)
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 ());
322 static const std::string who =
"fgets";
324 int nargin = args.length ();
326 if (nargin < 1 || nargin > 2)
337 std::string tmp = os.
gets (len_arg, err, who);
340 return ovl (tmp, tmp.length ());
342 return ovl (-1.0, 0.0);
364 static const std::string who =
"fskipl";
366 int nargin = args.length ();
368 if (nargin < 1 || nargin > 2)
379 off_t tmp = os.
skipl (count_arg, err, who);
389 const std::string& arch, std::string encoding,
int& fid)
396 std::transform (encoding.begin (), encoding.end (), encoding.begin (),
398 if (encoding.compare (
"utf-8"))
402 if (codec ==
reinterpret_cast<void *
> (-1))
405 error (
"fopen: conversion from codepage '%s' not supported",
412 std::string mode = mode_arg;
413 bool use_zlib =
false;
425 if (! (md & std::ios::out))
430 #if defined (HAVE_ZLIB)
437 int fd = fileno (
fptr);
439 gzFile gzf = ::gzdopen (fd, mode.c_str ());
445 retval.error (std::strerror (errno));
456 retval.error (std::strerror (errno));
467 const char *fcn,
int& fid)
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);
591 int nargin = args.length ();
593 if (nargin < 1 || nargin > 4)
602 if (args(0).is_string ())
608 if (nargout < 2 && args(0).string_value () ==
"all")
615 retval =
ovl (tmp(0), tmp(1), tmp(2), tmp(3));
640 int error_number = 0;
686 if (args.length () > 0)
687 warning (
"freport: ignoring extra arguments");
696 DEFMETHOD (frewind, interp, args, nargout,
708 if (args.length () != 1)
744 int nargin = args.length ();
746 if (nargin < 2 || nargin > 3)
755 return ovl (os.
seek (args(1), origin_arg));
766 if (args.length () != 1)
780 int nargin = args.
length ();
782 if (! (nargin > 1 || (nargin > 0 && args(0).is_string ())))
792 if (args(0).is_string ())
793 os = streams.
lookup (1, who);
797 os = streams.
lookup (args(0), who);
800 if (! args(fmt_n).is_string ())
801 error (
"%s: format TEMPLATE must be a string", who.c_str ());
805 if (nargin > 1 + fmt_n)
809 for (
int i = fmt_n + 1; i < nargin; i++)
810 tmp_args(i-fmt_n-1) = args(i);
813 result = os.
printf (args(fmt_n), tmp_args, who);
821 DEFMETHOD (fprintf, interp, args, nargout,
840 static const std::string who =
"fprintf";
845 DEFMETHOD (printf, interp, args, nargout,
863 static const std::string who =
"printf";
882 return ovl (os.
puts (args(1), who));
899 static const std::string who =
"fputs";
917 static const std::string who =
"puts";
924 DEFUN (sprintf, args, ,
940 static const std::string who =
"sprintf";
942 int nargin = args.length ();
956 error (
"%s: unable to create output buffer", who.c_str ());
961 error (
"%s: format TEMPLATE must be a string", who.c_str ());
970 for (
int i = 1; i < nargin; i++)
971 tmp_args(i-1) = args(i);
978 std::string result = ostr->
str ();
991 int nargin = args.
length ();
993 if (nargin < 2 || nargin > 3)
1002 if (! args(1).is_string ())
1003 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1005 if (nargin == 3 && args(2).is_string ())
1015 ? args(2).vector_value ()
1076 static const std::string who =
"fscanf";
1087 error (
"%s: argument STRING must be a string", who.c_str ());
1096 DEFUN (sscanf, args, ,
1109 static const std::string who =
"sscanf";
1111 int nargin = args.length ();
1113 if (nargin < 2 || nargin > 3)
1123 error (
"%s: unable to create temporary input buffer", who.c_str ());
1125 if (! args(1).is_string ())
1126 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1128 if (nargin == 3 && args(2).is_string ())
1136 Array<double> size = (nargin == 3) ? args(2).vector_value ()
1145 std::string errmsg = os.
error ();
1148 (os.
eof () ? data.length () : os.
tell ()) + 1);
1173 static const std::string who =
"scanf";
1189 if (args(0).is_string ())
1196 error (
"%s: unable to create temporary input buffer", who.c_str ());
1202 os = streams.
lookup (args(0), who);
1214 else if (args(1).is_string ())
1216 fmt = args(1).string_value ();
1218 if (args(1).is_sq_string ())
1224 error (
"%s: FORMAT must be a string", who.c_str ());
1230 if (args(2).isnumeric ())
1232 ntimes = args(2).idx_type_value ();
1234 if (ntimes < args(2).double_value ())
1235 error (
"%s: REPEAT = %g is too large",
1236 who.c_str (), args(2).double_value ());
1248 std::string errmsg = os.
error ();
1250 return ovl (result, count, errmsg);
1518 static const std::string who =
"textscan";
1523 DEFMETHOD (__textscan__, interp, args, ,
1530 if (args.length () == 0)
1534 args.splice (0, 1));
2291 ## Test unfinished comment
2293 %! c = textscan (
"1 2 /* half comment",
"%n %u8",
"CommentStyle", {
"/*",
"*/"});
2294 %! assert (c, {1, 2});
2296 ## Test reading from a real file
2299 %! fid =
fopen (
f,
"w+");
2301 %! fprintf (fid,
" %f %f /* comment */ %f %f ",
d);
2302 %! fseek (fid, 0,
"bof");
2303 %!
A = textscan (fid,
"%f %f",
"CommentStyle", {
"/*",
"*/"});
2307 %! assert (
A{1}, [
d(1);
d(3)], 1e-6);
2308 %! assert (
A{2}, [
d(2);
d(4)], 1e-6);
2333 std::string prec = prec_arg.
xstring_value (
"fread: PRECISION must be a string");
2342 input_type, output_type);
2344 catch (octave::execution_exception& e)
2346 error (e,
"fread: invalid PRECISION specified");
2355 catch (octave::execution_exception& e)
2357 error (e,
"fread: SKIP must be an integer");
2360 std::string arch = arch_arg.
xstring_value (
"fread: ARCH architecture type must be a string");
2365 return os.
read (size, block_size, input_type, output_type, skip,
2526 int nargin = args.length ();
2528 if (nargin < 1 || nargin > 5)
2542 if (nargin > idx && ! args(idx).is_string ())
2563 return ovl (tmp, count);
2571 std::string prec = prec_arg.
xstring_value (
"fwrite: PRECISION must be a string");
2580 catch (octave::execution_exception& e)
2582 error (e,
"fwrite: invalid PRECISION specified");
2591 catch (octave::execution_exception& e)
2593 error (e,
"fwrite: SKIP must be an integer");
2596 std::string arch = arch_arg.
xstring_value (
"fwrite: ARCH architecture type must be a string");
2601 return os.
write (data, block_size, output_type, skip, flt_fmt);
2626 int nargin = args.length ();
2628 if (nargin < 2 || nargin > 5)
2672 if (args.length () != 1)
2679 return ovl (os.
eof () ? 1.0 : 0.0);
2703 int nargin = args.length ();
2705 if (nargin < 1 || nargin > 2)
2716 std::string opt = args(1).string_value ();
2718 clear = (opt ==
"clear");
2721 int error_number = 0;
2723 std::string error_message = os.
error (clear, error_number);
2725 return ovl (error_message, error_number);
2766 if (args.length () != 2)
2769 std::string
name = args(0).xstring_value (
"popen: COMMAND must be a string");
2770 std::string mode = args(1).xstring_value (
"popen: MODE must be a string");
2782 else if (mode ==
"w")
2789 error (
"popen: invalid MODE specified");
2803 if (args.length () != 1)
2808 return ovl (streams.
remove (args(0),
"pclose"));
2811 DEFUN (tempname, args, ,
2831 int nargin = args.length ();
2839 dir = args(0).xstring_value (
"tempname: DIR must be a string");
2843 std::string pfx (
"oct-");
2846 pfx = args(1).xstring_value (
"tempname: PREFIX must be a string");
2919 if (args.length () != 0)
2938 error (
"tmpfile: failed to create octave_stdiostream object");
2946 retval =
ovl (-1, std::strerror (errno));
2975 int nargin = args.length ();
2977 if (nargin < 1 || nargin > 2)
2980 std::string tmpl8 = args(0).xstring_value (
"mkstemp: TEMPLATE argument must be a string");
2985 strcpy (tmp, tmpl8.c_str ());
2992 retval(2) = std::strerror (errno);
2996 const char *fopen_mode =
"w+b";
2998 FILE *fid = fdopen (fd, fopen_mode);
3003 retval(2) = std::strerror (errno);
3007 std::string nm = tmp;
3014 error (
"mkstemp: failed to create octave_stdiostream object");
3021 if (nargin == 2 && args(1).
is_true ())
3022 interp.mark_for_deletion (nm);
3036 int tmp =
x % obase;
3038 if (tmp > ibase - 1)
3039 error (
"umask: invalid digit");
3043 while ((
x = (
x - tmp) / obase))
3047 if (tmp > ibase - 1)
3048 error (
"umask: invalid digit");
3075 if (args.length () != 1)
3078 int mask = args(0).xint_value (
"umask: MASK must be an integer");
3081 error (
"umask: MASK must be a positive integer value");
3083 int oct_mask =
convert (mask, 8, 10);
3088 return ovl (status);
3115 if (args.length () != 0)
template class OCTAVE_API Array< double >
Vector representing the dimensions (size) of an Array.
static data_type string_to_data_type(const std::string &s)
void cleanup_tmp_files(void)
stream_list & get_stream_list(void)
void mark_for_deletion(const std::string &file)
int remove(int fid, const std::string &who="")
std::string list_open_files(void) const
octave_value stdin_file(void) const
int get_file_number(const octave_value &fid) const
octave_value stderr_file(void) const
octave_value open_file_numbers(void) const
stream lookup(int fid, const std::string &who="") const
octave_value stdout_file(void) const
string_vector get_info(int fid) const
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)
int puts(const std::string &s, const std::string &who)
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)
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
off_t skipl(off_t count, bool &err, const std::string &who)
octave_value textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
int seek(off_t offset, int origin)
octave_value_list oscanf(const std::string &fmt, const std::string &who)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
bool is_valid(void) const
std::string error(bool clear, int &err_num)
static std::string getenv(const std::string &name)
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 char *data, std::ios::openmode arg_md=std::ios::out, octave::mach_info::float_format ff=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")
static octave::stream create(const std::string &n, FILE *f=nullptr, std::ios::openmode m=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::mach_info::native_float_format(), const std::string &encoding="utf-8", c_file_ptr_buf::close_fcn cf=c_file_ptr_buf::file_close)
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)
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
std::string xstring_value(const char *fmt,...) const
bool is_sq_string(void) const
Array< double > xvector_value(const char *fmt,...) const
static octave::stream create(const std::string &n, gzFile f=nullptr, int fid=0, std::ios::openmode m=std::ios::in|std::ios::out, octave::mach_info::float_format ff=octave::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)
void mark_for_deletion(const std::string &file)
static octave_value_list textscan_internal(octave::interpreter &interp, const std::string &who, const octave_value_list &args)
static int do_fwrite(octave::stream &os, const octave_value &data, const octave_value &prec_arg, const octave_value &skip_arg, const octave_value &arch_arg)
static octave_value do_fread(octave::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)
static octave::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 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)
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 octave_value_list puts_internal(octave::interpreter &interp, const std::string &who, const octave_value_list &args)
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)
void cleanup_tmp_files(void)
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)
static octave_value_list scanf_internal(octave::interpreter &interp, const std::string &who, const octave_value_list &args)
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)
OCTAVE_EXPORT octave_value_list Fpopen(octave::interpreter &interp, const octave_value_list &args, int)
static int convert(int x, int ibase, int obase)
static std::ios::openmode fopen_mode_to_ios_mode(const std::string &mode)
static octave_value_list printf_internal(octave::interpreter &interp, const std::string &who, const octave_value_list &args, int nargout)
ColumnVector transform(const Matrix &m, double x, double y, double z)
iconv_t octave_iconv_open_wrapper(const char *tocode, const char *fromcode)
int octave_iconv_close_wrapper(iconv_t 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)
std::complex< T > trunc(const std::complex< T > &x)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
std::string tilde_expand(const std::string &name)
std::string tempnam(const std::string &dir, const std::string &pfx)
std::FILE * fopen(const std::string &filename, const std::string &mode)
int unlink(const std::string &name)
interpreter & __get_interpreter__(const std::string &who)
std::string do_string_escapes(const std::string &s)
std::string get_P_tmpdir(void)
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file)
#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::octave_value(const Array< char > &chm, char type) return retval
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
DEFMETHODX("quad", Fquad, interp, args,, doc:)
FILE * octave_tmpfile_wrapper(void)