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");
122 if (std::fclose (m_fp))
123 throw std::runtime_error (
"unable to close file");
131 #if defined (HAVE_BZ2)
137 static const constexpr
char *extension =
".bz2";
139 static void zip (
const std::string& source_path,
140 const std::string& dest_path)
142 bz2::zipper z (source_path, dest_path);
153 zipper (
void) =
delete;
155 zipper (
const std::string& source_path,
const std::string& dest_path)
156 : m_status (BZ_OK), m_source (source_path,
"rb"),
157 m_dest (dest_path,
"wb"),
158 m_bz (BZ2_bzWriteOpen (&m_status, m_dest.m_fp, 9, 0, 30))
160 if (m_status != BZ_OK)
161 throw std::runtime_error (
"failed to open bzip2 stream");
164 zipper (
const zipper&) =
delete;
166 zipper& operator = (
const zipper&) =
delete;
171 BZ2_bzWriteClose (&m_status, m_bz, 1,
nullptr,
nullptr);
176 const std::size_t buf_len = 8192;
179 while ((n_read = std::fread (buf,
sizeof (buf[0]), buf_len, m_source.m_fp)) != 0)
181 if (std::ferror (m_source.m_fp))
182 throw std::runtime_error (
"failed to read from source file");
183 BZ2_bzWrite (&m_status, m_bz, buf, n_read);
184 if (m_status == BZ_IO_ERROR)
185 throw std::runtime_error (
"failed to write or compress");
187 if (std::ferror (m_source.m_fp))
188 throw std::runtime_error (
"failed to read from source file");
193 int abandon = (m_status == BZ_IO_ERROR) ? 1 : 0;
194 BZ2_bzWriteClose (&m_status, m_bz, abandon,
nullptr,
nullptr);
195 if (m_status != BZ_OK)
196 throw std::runtime_error (
"failed to close bzip2 stream");
248 static const constexpr
char *extension =
".gz";
250 static void zip (
const std::string& source_path,
251 const std::string& dest_path)
253 gz::zipper z (source_path, dest_path);
268 uchar_array (
void) =
delete;
270 uchar_array (
const std::string& str)
272 p =
new Bytef[str.length () + 1];
273 std::strcpy (
reinterpret_cast<char *
> (p), str.c_str ());
276 uchar_array (
const uchar_array&) =
delete;
278 uchar_array& operator = (
const uchar_array&) =
delete;
280 ~uchar_array (
void) {
delete[] p; }
283 class gzip_header :
public gz_header
287 gzip_header (
void) =
delete;
289 gzip_header (
const std::string& source_path)
290 : m_basename (sys::
env::base_pathname (source_path))
292 const sys::file_stat source_stat (source_path);
294 throw std::runtime_error (
"unable to stat source file");
299 time = uLong (source_stat.mtime ().unix_time ());
345 #if defined (__WIN32__)
348 #elif defined (__APPLE__)
356 gzip_header (
const gzip_header&) =
delete;
358 gzip_header& operator = (
const gzip_header&) =
delete;
360 ~gzip_header (
void) =
default;
365 uchar_array m_basename;
372 zipper (
void) =
delete;
374 zipper (
const std::string& source_path,
const std::string& dest_path)
375 : m_source (source_path,
"rb"), m_dest (dest_path,
"wb"),
376 m_header (source_path), m_strm (new z_stream)
378 m_strm->zalloc = Z_NULL;
379 m_strm->zfree = Z_NULL;
380 m_strm->opaque = Z_NULL;
383 zipper (
const zipper&) =
delete;
385 zipper& operator = (
const zipper&) =
delete;
402 int status = deflateInit2 (m_strm, 8, Z_DEFLATED, 31, 8,
405 throw std::runtime_error (
"failed to open zlib stream");
407 deflateSetHeader (m_strm, &m_header);
409 const std::size_t buf_len = 8192;
410 unsigned char buf_in[buf_len];
411 unsigned char buf_out[buf_len];
417 m_strm->avail_in = std::fread (buf_in,
sizeof (buf_in[0]),
418 buf_len, m_source.m_fp);
420 if (std::ferror (m_source.m_fp))
421 throw std::runtime_error (
"failed to read source file");
423 m_strm->next_in = buf_in;
424 flush = (std::feof (m_source.m_fp) ? Z_FINISH : Z_NO_FLUSH);
431 m_strm->avail_out = buf_len;
432 m_strm->next_out = buf_out;
433 status = ::deflate (m_strm, flush);
434 if (status == Z_STREAM_ERROR)
435 throw std::runtime_error (
"failed to deflate");
437 std::fwrite (buf_out,
sizeof (buf_out[0]),
438 buf_len - m_strm->avail_out, m_dest.m_fp);
439 if (std::ferror (m_dest.m_fp))
440 throw std::runtime_error (
"failed to write file");
442 while (m_strm->avail_out == 0);
444 if (m_strm->avail_in != 0)
445 throw std::runtime_error (
"failed to write file");
448 while (flush != Z_FINISH);
450 if (status != Z_STREAM_END)
451 throw std::runtime_error (
"failed to write file");
456 if (deflateEnd (m_strm) != Z_OK)
457 throw std::runtime_error (
"failed to close zlib stream");
469 gzip_header m_header;
480 const std::function<std::string(
const std::string&)>& mk_dest_path)
482 std::list<std::string> dest_paths;
484 std::function<void(
const std::string&)> walk;
485 walk = [&walk, &mk_dest_path, &dest_paths] (
const std::string& path) ->
void
487 const sys::file_stat fs (path);
500 if (dirlist(i) !=
"." && dirlist(i) !=
"..")
505 else if (fs.is_reg ())
507 const std::string dest_path = mk_dest_path (path);
510 X::zip (path, dest_path);
512 catch (
const interrupt_exception&)
526 dest_paths.push_front (dest_path);
547 const std::string ext = X::extension;
548 const std::function<std::string(
const std::string&)> mk_dest_path
549 = [&ext] (
const std::string& source_path) -> std::string
551 return source_path + ext;
553 return xzip<X> (source_patterns, mk_dest_path);
560 const std::string ext = X::extension;
561 const std::function<std::string(
const std::string&)> mk_dest_path
562 = [&out_dir, &ext] (
const std::string& source_path) -> std::string
567 (pos == std::string::npos ? source_path : source_path.substr (pos+1));
576 return xzip<X> (source_patterns, mk_dest_path);
584 if (nargin < 1 || nargin > 2)
588 = args(0).xcellstr_value (
"%s: FILES must be a character array or cellstr",
594 const std::string out_dir = args(1).string_value ();
633 octave_unused_parameter (args);
634 octave_unused_parameter (nargout);
671 #if defined (HAVE_BZ2)
679 octave_unused_parameter (args);
680 octave_unused_parameter (nargout);
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
RIIA wrapper for std::FILE*.
CFile(const std::string &path, const std::string &mode)
CFile(const CFile &)=delete
string_vector glob(void) const
octave_idx_type length(void) const
octave_idx_type numel(void) 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.
OCTINTERP_API void print_usage(void)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
int unlink(const std::string &name)
std::string dir_sep_str(void)
int mkdir(const std::string &nm, mode_t md)
std::string tilde_expand(const std::string &name)
string_vector xzip(const Array< std::string > &source_patterns, const std::function< std::string(const std::string &)> &mk_dest_path)
std::FILE * fopen(const std::string &filename, const std::string &mode)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
static std::string basename(const std::string &s, bool strip_path=false)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))