40 #if defined (HAVE_CONFIG_H)
50 #if defined (HAVE_ZLIB_H)
88 normalize_fopen_mode (std::string& mode,
bool& use_zlib)
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
140 fopen_mode_to_ios_mode (
const std::string& mode)
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);
389 do_stream_open (
const std::string& name,
const std::string& mode_arg,
390 const std::string& arch, std::string encoding,
int& fid)
396 if (encoding.compare (
"utf-8"))
400 if (codec ==
reinterpret_cast<void *
> (-1))
403 error (
"fopen: conversion from codepage '%s' not supported",
410 std::string mode = mode_arg;
411 bool use_zlib =
false;
412 normalize_fopen_mode (mode, use_zlib);
414 std::ios::openmode md = fopen_mode_to_ios_mode (mode);
423 if (! (md & std::ios::out))
428 #if defined (HAVE_ZLIB)
435 int fd = fileno (
fptr);
437 gzFile gzf = ::gzdopen (fd, mode.c_str ());
443 retval.
error (std::strerror (errno));
453 retval.
error (std::strerror (errno));
464 const char *fcn,
int& fid)
470 std::string name = tc_name.
xstring_value (
"%s: filename must be a string", fcn);
471 std::string mode = tc_mode.
xstring_value (
"%s: file mode must be a string", fcn);
472 std::string arch = tc_arch.
xstring_value (
"%s: architecture type must be a string", fcn);
473 std::string encoding = tc_encoding.
xstring_value (
"%s: ENCODING must be a string", fcn);
476 std::transform (encoding.begin (), encoding.end (), encoding.begin (),
479 if (encoding ==
"system")
482 #if defined (OCTAVE_HAVE_STRICT_ENCODING_FACET)
483 if (encoding !=
"utf-8")
486 "fopen: encoding must be 'UTF-8' for this version");
491 retval = do_stream_open (name, mode, arch, encoding, fid);
604 int nargin = args.length ();
606 if (nargin < 1 || nargin > 4)
615 if (args(0).is_string ())
621 if (nargout < 2 && args(0).string_value () ==
"all")
628 retval =
ovl (tmp(0), tmp(1), tmp(2), tmp(3));
642 stream os = do_stream_open (args(0), mode, arch, encoding,
"fopen",
649 int error_number = 0;
651 retval =
ovl (-1.0, os.
error (
false, error_number));
710 if (args.length () > 0)
711 warning (
"freport: ignoring extra arguments");
720 DEFMETHOD (frewind, interp, args, nargout,
735 if (args.length () != 1)
770 int nargin = args.length ();
772 if (nargin < 2 || nargin > 3)
781 return ovl (os.
seek (args(1), origin_arg));
792 if (args.length () != 1)
803 printf_internal (
interpreter& interp,
const std::string& who,
806 int nargin = args.
length ();
808 if (! (nargin > 1 || (nargin > 0 && args(0).is_string ())))
818 if (args(0).is_string ())
819 os = streams.
lookup (1, who);
823 os = streams.
lookup (args(0), who);
826 if (! args(fmt_n).is_string ())
827 error (
"%s: format TEMPLATE must be a string", who.c_str ());
831 if (nargin > 1 + fmt_n)
835 for (
int i = fmt_n + 1; i < nargin; i++)
836 tmp_args(i-fmt_n-1) = args(i);
839 result = os.
printf (args(fmt_n), tmp_args, who);
847 DEFMETHOD (fprintf, interp, args, nargout,
867 static const std::string who =
"fprintf";
869 return printf_internal (interp, who, args, nargout);
872 DEFMETHOD (printf, interp, args, nargout,
893 static const std::string who =
"printf";
902 puts_internal (
interpreter& interp,
const std::string& who,
912 return ovl (- (os.
puts (args(1), who) < 0));
929 static const std::string who =
"fputs";
931 return puts_internal (interp, who, args);
976 static const std::string who =
"puts";
983 DEFUN (sprintf, args, ,
999 static const std::string who =
"sprintf";
1001 int nargin = args.
length ();
1015 error (
"%s: unable to create output buffer", who.c_str ());
1020 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1029 for (
int i = 1; i < nargin; i++)
1030 tmp_args(i-1) = args(i);
1034 retval(2) = os.
printf (fmt_arg, tmp_args, who);
1035 retval(1) = os.
error ();
1037 std::string result = ostr->
str ();
1047 scanf_internal (
interpreter& interp,
const std::string& who,
1050 int nargin = args.
length ();
1052 if (nargin < 2 || nargin > 3)
1061 if (! args(1).is_string ())
1062 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1064 if (nargin == 3 && args(2).is_string ())
1066 retval = os.
oscanf (args(1), who);
1074 ? args(2).vector_value ()
1079 retval =
ovl (tmp, count, os.
error ());
1135 static const std::string who =
"fscanf";
1137 return scanf_internal (interp, who, args);
1141 get_scan_string_data (
const octave_value& val,
const std::string& who)
1146 error (
"%s: argument STRING must be a string", who.c_str ());
1155 DEFUN (sscanf, args, ,
1168 static const std::string who =
"sscanf";
1170 int nargin = args.
length ();
1172 if (nargin < 2 || nargin > 3)
1177 std::string data = get_scan_string_data (args(0), who);
1182 error (
"%s: unable to create temporary input buffer", who.c_str ());
1184 if (! args(1).is_string ())
1185 error (
"%s: format TEMPLATE must be a string", who.c_str ());
1187 if (nargin == 3 && args(2).is_string ())
1189 retval = os.
oscanf (args(1), who);
1195 Array<double> size = (nargin == 3) ? args(2).vector_value ()
1204 std::string errmsg = os.
error ();
1206 retval =
ovl (tmp, count, errmsg,
1207 (os.
eof () ? data.length () : os.
tell ()) + 1);
1232 static const std::string who =
"scanf";
1240 textscan_internal (
interpreter& interp,
const std::string& who,
1248 if (args(0).is_string ())
1250 std::string data = get_scan_string_data (args(0), who);
1255 error (
"%s: unable to create temporary input buffer", who.c_str ());
1261 os = streams.
lookup (args(0), who);
1273 else if (args(1).is_string ())
1275 fmt = args(1).string_value ();
1277 if (args(1).is_sq_string ())
1283 error (
"%s: FORMAT must be a string", who.c_str ());
1289 if (args(2).isnumeric ())
1291 ntimes = args(2).idx_type_value ();
1293 if (ntimes < args(2).double_value ())
1294 error (
"%s: REPEAT = %g is too large",
1295 who.c_str (), args(2).double_value ());
1307 std::string errmsg = os.
error ();
1309 return ovl (result, count, errmsg);
1577 static const std::string who =
"textscan";
1579 return textscan_internal (interp, who, args);
1582 DEFMETHOD (__textscan__, interp, args, ,
1592 return textscan_internal (interp, args(0).string_value (),
2395 ## Test unfinished comment
2397 %! c = textscan (
"1 2 /* half comment",
"%n %u8",
"CommentStyle", {
"/*",
"*/"});
2398 %! assert (c, {1, 2});
2400 ## Test reading from a real file
2403 %! fid =
fopen (
f,
"w+");
2405 %! fprintf (fid,
" %f %f /* comment */ %f %f ",
d);
2406 %! fseek (fid, 0,
"bof");
2407 %!
A = textscan (fid,
"%f %f",
"CommentStyle", {
"/*",
"*/"});
2411 %! assert (
A{1}, [
d(1);
d(3)], 1e-6);
2412 %! assert (
A{2}, [
d(2);
d(4)], 1e-6);
2437 std::string prec = prec_arg.
xstring_value (
"fread: PRECISION must be a string");
2446 input_type, output_type);
2450 error (ee,
"fread: invalid PRECISION specified");
2461 error (ee,
"fread: SKIP must be an integer");
2464 std::string arch = arch_arg.
xstring_value (
"fread: ARCH architecture type must be a string");
2469 return os.
read (size, block_size, input_type, output_type, skip,
2630 int nargin = args.
length ();
2632 if (nargin < 1 || nargin > 5)
2646 if (nargin > idx && ! args(idx).is_string ())
2665 octave_value tmp = do_fread (os, size, prec, skip, arch, count);
2667 return ovl (tmp, count);
2675 std::string prec = prec_arg.
xstring_value (
"fwrite: PRECISION must be a string");
2686 error (ee,
"fwrite: invalid PRECISION specified");
2697 error (ee,
"fwrite: SKIP must be an integer");
2700 std::string arch = arch_arg.
xstring_value (
"fwrite: ARCH architecture type must be a string");
2705 return os.
write (data, block_size, output_type, skip, flt_fmt);
2730 int nargin = args.
length ();
2732 if (nargin < 2 || nargin > 5)
2761 return ovl (do_fwrite (os, data, prec, skip, arch));
2783 return ovl (os.
eof () ? 1.0 : 0.0);
2807 int nargin = args.
length ();
2809 if (nargin < 1 || nargin > 2)
2820 std::string opt = args(1).string_value ();
2822 clear = (opt ==
"clear");
2825 int error_number = 0;
2827 std::string error_message = os.
error (clear, error_number);
2829 return ovl (error_message, error_number);
2873 std::string name = args(0).xstring_value (
"popen: COMMAND must be a string");
2874 std::string mode = args(1).xstring_value (
"popen: MODE must be a string");
2884 retval = streams.
insert (ips);
2886 else if (mode ==
"w")
2890 retval = streams.
insert (ops);
2893 error (
"popen: invalid MODE specified");
2915 return ovl (streams.
remove (args(0),
"pclose"));
2918 DEFUN (tempdir, args, ,
2934 std::string tmpdir = sys::env::get_temp_directory ();
2939 return ovl (tmpdir);
2961 DEFUN (tempname, args, ,
2981 int nargin = args.
length ();
2989 dir = args(0).xstring_value (
"tempname: DIR must be a string");
2991 std::string pfx (
"oct-");
2994 pfx = args(1).xstring_value (
"tempname: PREFIX must be a string");
3072 std::string tmpfile (
sys::tempnam (sys::env::get_temp_directory (),
"oct-"));
3078 std::ios::openmode md = fopen_mode_to_ios_mode (
"w+b");
3086 error (
"tmpfile: failed to create stdiostream object");
3094 retval =
ovl (-1, std::strerror (errno));
3123 int nargin = args.
length ();
3125 if (nargin < 1 || nargin > 2)
3128 std::string tmpl8 = args(0).xstring_value (
"mkstemp: TEMPLATE argument must be a string");
3133 strcpy (tmp, tmpl8.c_str ());
3140 retval(2) = std::strerror (errno);
3144 const char *fopen_mode =
"w+b";
3146 FILE *fid = fdopen (fd, fopen_mode);
3151 retval(2) = std::strerror (errno);
3155 std::string nm = tmp;
3157 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode);
3162 error (
"mkstemp: failed to create stdiostream object");
3166 retval(0) = streams.
insert (s);
3169 if (nargin == 2 && args(1).
is_true ())
3180 convert (
int x,
int ibase,
int obase)
3184 int tmp =
x % obase;
3186 if (tmp > ibase - 1)
3187 error (
"umask: invalid digit");
3191 while ((
x = (
x - tmp) / obase))
3195 if (tmp > ibase - 1)
3196 error (
"umask: invalid digit");
3198 retval += mult * tmp;
3226 int mask = args(0).xint_value (
"umask: MASK must be an integer");
3229 error (
"umask: MASK must be a positive integer value");
3231 int oct_mask = convert (mask, 8, 10);
3233 int status = convert (
sys::umask (oct_mask), 10, 8);
3236 return ovl (status);
3280 return const_value (
"SEEK_SET", args, -1);
3291 return const_value (
"SEEK_CUR", args, 0);
3302 return const_value (
"SEEK_END", args, 1);
3327 return const_value (
"stdin", args, streams.
stdin_file ());
3341 return const_value (
"stdout", args, streams.
stdout_file ());
3356 return const_value (
"stderr", args, streams.
stderr_file ());
3359 OCTAVE_END_NAMESPACE(
octave)
octave_value_list Fstdin(octave::interpreter &, const octave_value_list &=octave_value_list(), int=0)
octave_value_list FP_tmpdir(const octave_value_list &=octave_value_list(), int=0)
octave_value_list Fpclose(octave::interpreter &, const octave_value_list &=octave_value_list(), int=0)
octave_value_list Fumask(const octave_value_list &=octave_value_list(), int=0)
octave_value_list FSEEK_END(const octave_value_list &=octave_value_list(), int=0)
octave_value_list Fstderr(octave::interpreter &, const octave_value_list &=octave_value_list(), int=0)
octave_value_list Ffeof(octave::interpreter &, const octave_value_list &=octave_value_list(), int=0)
octave_value_list Fpopen(octave::interpreter &, const octave_value_list &=octave_value_list(), int=0)
octave_value_list Fferror(octave::interpreter &, const octave_value_list &=octave_value_list(), int=0)
octave_value_list FSEEK_SET(const octave_value_list &=octave_value_list(), int=0)
octave_value_list Fstdout(octave::interpreter &, const octave_value_list &=octave_value_list(), int=0)
octave_value_list FSEEK_CUR(const octave_value_list &=octave_value_list(), int=0)
Vector representing the dimensions (size) of an Array.
void mark_for_deletion(const std::string &file)
stream_list & get_stream_list()
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_value_list splice(octave_idx_type offset, octave_idx_type len, const octave_value_list &lst=octave_value_list()) const
octave_idx_type length() const
octave_value_list & prepend(const octave_value &val)
int int_value(bool req_int=false, bool frc_str_conv=false) const
bool is_sq_string() const
octave_value reshape(const dim_vector &dv) const
octave_idx_type numel() const
std::string string_value(bool force=false) const
std::string xstring_value(const char *fmt,...) const
Array< double > xvector_value(const char *fmt,...) 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)
octave_value stdin_file() const
int get_file_number(const octave_value &fid) const
std::string list_open_files() const
octave_value stdout_file() const
stream lookup(int fid, const std::string &who="") const
octave_value open_file_numbers() const
string_vector get_info(int fid) const
int remove(int fid, const std::string &who="")
octave_value stderr_file() const
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
std::string gets(octave_idx_type max_len, bool &err, 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)
int puts(const std::string &s, const std::string &who)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
int seek(off_t offset, int origin)
octave_value_list oscanf(const std::string &fmt, const std::string &who)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
std::string error(bool clear, int &err_num)
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)
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
#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 warning_with_id(const char *id, const char *fmt,...)
void() error(const char *fmt,...)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
std::string dir_sep_str()
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()
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 &)
bool dir_exists(const std::string &dirname)
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)
int octave_mkostemp_wrapper(char *tmpl)
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)
#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()
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file=false)
std::string do_string_escapes(const std::string &s)