50 #if defined (HAVE_CONFIG_H)
78 #if defined (HAVE_BZLIB_H)
82 #if defined (HAVE_ZLIB_H)
103 CFile (
const std::string& path,
const std::string& mode)
104 : m_fp (sys::
fopen (path, mode))
107 throw std::runtime_error (
"unable to open file");
110 OCTAVE_DISABLE_COPY_MOVE (CFile)
120 if (std::fclose (m_fp))
121 throw std::runtime_error (
"unable to close file");
129 #if defined (HAVE_BZ2)
135 static const constexpr
char *extension =
".bz2";
137 static void zip (
const std::string& source_path,
138 const std::string& dest_path)
140 bz2::zipper z (source_path, dest_path);
153 zipper (
const std::string& source_path,
const std::string& dest_path)
154 : m_status (BZ_OK), m_source (source_path,
"rb"),
155 m_dest (dest_path,
"wb"),
156 m_bz (BZ2_bzWriteOpen (&m_status, m_dest.m_fp, 9, 0, 30))
158 if (m_status != BZ_OK)
159 throw std::runtime_error (
"failed to open bzip2 stream");
162 OCTAVE_DISABLE_COPY_MOVE (zipper)
167 BZ2_bzWriteClose (&m_status, m_bz, 1,
nullptr,
nullptr);
172 const std::size_t buf_len = 8192;
175 while ((n_read = std::fread (buf,
sizeof (buf[0]), buf_len, m_source.m_fp)) != 0)
177 if (std::ferror (m_source.m_fp))
178 throw std::runtime_error (
"failed to read from source file");
179 BZ2_bzWrite (&m_status, m_bz, buf, n_read);
180 if (m_status == BZ_IO_ERROR)
181 throw std::runtime_error (
"failed to write or compress");
183 if (std::ferror (m_source.m_fp))
184 throw std::runtime_error (
"failed to read from source file");
189 int abandon = (m_status == BZ_IO_ERROR) ? 1 : 0;
190 BZ2_bzWriteClose (&m_status, m_bz, abandon,
nullptr,
nullptr);
191 if (m_status != BZ_OK)
192 throw std::runtime_error (
"failed to close bzip2 stream");
244 static const constexpr
char *extension =
".gz";
246 static void zip (
const std::string& source_path,
247 const std::string& dest_path)
249 gz::zipper z (source_path, dest_path);
264 uchar_array () =
delete;
266 uchar_array (
const std::string& str)
268 p =
new Bytef[str.length () + 1];
269 std::strcpy (
reinterpret_cast<char *
> (p), str.c_str ());
272 OCTAVE_DISABLE_COPY_MOVE (uchar_array)
274 ~uchar_array () {
delete[] p; }
277 class gzip_header :
public gz_header
281 gzip_header () =
delete;
283 gzip_header (
const std::string& source_path)
284 : m_basename (sys::
env::base_pathname (source_path))
286 const sys::file_stat source_stat (source_path);
288 throw std::runtime_error (
"unable to stat source file");
293 time = uLong (source_stat.mtime ().unix_time ());
339 #if defined (__WIN32__)
342 #elif defined (__APPLE__)
350 OCTAVE_DISABLE_COPY_MOVE (gzip_header)
352 ~gzip_header () =
default;
357 uchar_array m_basename;
366 zipper (
const std::string& source_path,
const std::string& dest_path)
367 : m_source (source_path,
"rb"), m_dest (dest_path,
"wb"),
368 m_header (source_path), m_strm (new z_stream)
370 m_strm->zalloc = Z_NULL;
371 m_strm->zfree = Z_NULL;
372 m_strm->opaque = Z_NULL;
375 OCTAVE_DISABLE_COPY_MOVE (zipper)
392 int status = deflateInit2 (m_strm, 8, Z_DEFLATED, 31, 8,
395 throw std::runtime_error (
"failed to open zlib stream");
397 deflateSetHeader (m_strm, &m_header);
399 const std::size_t buf_len = 8192;
400 unsigned char buf_in[buf_len];
401 unsigned char buf_out[buf_len];
407 m_strm->avail_in = std::fread (buf_in,
sizeof (buf_in[0]),
408 buf_len, m_source.m_fp);
410 if (std::ferror (m_source.m_fp))
411 throw std::runtime_error (
"failed to read source file");
413 m_strm->next_in = buf_in;
414 flush = (std::feof (m_source.m_fp) ? Z_FINISH : Z_NO_FLUSH);
421 m_strm->avail_out = buf_len;
422 m_strm->next_out = buf_out;
423 status = ::deflate (m_strm, flush);
424 if (status == Z_STREAM_ERROR)
425 throw std::runtime_error (
"failed to deflate");
427 std::fwrite (buf_out,
sizeof (buf_out[0]),
428 buf_len - m_strm->avail_out, m_dest.m_fp);
429 if (std::ferror (m_dest.m_fp))
430 throw std::runtime_error (
"failed to write file");
432 while (m_strm->avail_out == 0);
434 if (m_strm->avail_in != 0)
435 throw std::runtime_error (
"failed to write file");
438 while (flush != Z_FINISH);
440 if (status != Z_STREAM_END)
441 throw std::runtime_error (
"failed to write file");
446 if (deflateEnd (m_strm) != Z_OK)
447 throw std::runtime_error (
"failed to close zlib stream");
460 gzip_header m_header;
471 const std::function<std::string(
const std::string&)>& mk_dest_path)
473 std::list<std::string> dest_paths;
475 std::function<void(
const std::string&)> walk;
476 walk = [&walk, &mk_dest_path, &dest_paths] (
const std::string& path) ->
void
478 const sys::file_stat fs (path);
491 if (dirlist(i) !=
"." && dirlist(i) !=
"..")
496 else if (fs.is_reg ())
498 const std::string dest_path = mk_dest_path (path);
501 X::zip (path, dest_path);
503 catch (
const interrupt_exception&)
517 dest_paths.push_front (dest_path);
538 const std::string ext = X::extension;
539 const std::function<std::string(
const std::string&)> mk_dest_path
540 = [&ext] (
const std::string& source_path) -> std::string
542 return source_path + ext;
544 return xzip<X> (source_patterns, mk_dest_path);
551 const std::string ext = X::extension;
552 const std::function<std::string(
const std::string&)> mk_dest_path
553 = [&out_dir, &ext] (
const std::string& source_path) -> std::string
557 const std::string basename =
558 (pos == std::string::npos ? source_path : source_path.substr (pos+1));
567 return xzip<X> (source_patterns, mk_dest_path);
575 if (nargin < 1 || nargin > 2)
579 = args(0).xcellstr_value (
"%s: FILES must be a character array or cellstr",
585 const std::string out_dir = args(1).string_value ();
624 octave_unused_parameter (args);
625 octave_unused_parameter (nargout);
662 #if defined (HAVE_BZ2)
670 octave_unused_parameter (args);
671 octave_unused_parameter (nargout);
856 OCTAVE_END_NAMESPACE(
octave)
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
octave_idx_type numel() const
Number of elements in the array.
string_vector glob() const
octave_idx_type length() const
octave_idx_type numel() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin function.
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
std::string dir_sep_str()
std::FILE * fopen(const std::string &filename, const std::string &mode)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
std::string tilde_expand(const std::string &name)
int mkdir(const std::string &nm, mode_t md)
int unlink(const std::string &name)
string_vector xzip(const Array< std::string > &source_patterns, const std::function< std::string(const std::string &)> &mk_dest_path)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))