Mercurial > octave
annotate libinterp/dldfcn/xzip.cc @ 22269:a76b931d2c4b
* xzip.cc (Fbzip2): Fix typo of "octave_unused_parameter".
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 11 Aug 2016 10:38:19 -0700 |
parents | 15b9d7cb3098 |
children | 16efd0403698 |
rev | line source |
---|---|
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
1 // Copyright (C) 2016 Carnë Draug |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
2 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
3 // This program is free software: you can redistribute it and/or modify |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
4 // it under the terms of the GNU General Public License as published by |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
5 // the Free Software Foundation, either version 3 of the License, or |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
6 // (at your option) any later version. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
7 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
8 // This program is distributed in the hope that it will be useful, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
11 // GNU General Public License for more details. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
12 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
13 // You should have received a copy of the GNU General Public License |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
14 // along with this program. If not, see <http://www.gnu.org/licenses/>. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
15 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
16 //! Octave interface to the compression and uncompression libraries. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
17 /*! |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
18 This was originally implemented as an m file which directly called |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
19 bzip2 and gzip applications. This may look simpler but causes some |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
20 issues (see bug #43431) because we have no control over the output |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
21 file: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
22 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
23 - created file is always in the same directory as the original file; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
24 - automatically skip files that already have gz/bz2/etc extension; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
25 - some olders versions miss the --keep option. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
26 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
27 In addition, because system() does not have a method that allows |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
28 passing a list of arguments, there is the issue of having to escape |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
29 filenames. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
30 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
31 A solution is to pipe file contents into the applications instead of |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
32 filenames. However, that solution causes: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
33 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
34 # missing file header with original file information; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
35 # implementing ourselves the recursive transversion of directories; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
36 # do the above in a m file which will be slow; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
37 # popen2 is frail on windows. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
38 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
39 */ |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
40 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
41 #if defined (HAVE_CONFIG_H) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
42 # include "config.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
43 #endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
44 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
45 #include <cstdio> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
46 #include <cstring> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
47 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
48 #include <string> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
49 #include <list> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
50 #include <functional> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
51 #include <stdexcept> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
52 #include <iostream> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
53 #include <fstream> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
54 |
22188 | 55 #if defined (HAVE_BZLIB_H) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
56 # include <bzlib.h> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
57 #endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
58 |
22188 | 59 #if defined (HAVE_ZLIB_H) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
60 # include <zlib.h> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
61 #endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
62 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
63 #include "Array.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
64 #include "str-vec.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
65 #include "glob-match.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
66 #include "file-ops.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
67 #include "dir-ops.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
68 #include "file-stat.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
69 #include "oct-env.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
70 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
71 #include "defun-dld.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
72 #include "defun-int.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
73 #include "errwarn.h" |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
74 |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
75 //! RIIA wrapper for std::FILE* |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
76 /*! If error handling is available for failing to close the file, use |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
77 the close method which throws. |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
78 |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
79 If the file has been closed, fp is set to nullptr. Remember that |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
80 behavior is undefined if the value of the pointer stream is used |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
81 after fclose. |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
82 */ |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
83 class CFile |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
84 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
85 public: |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
86 std::FILE* fp; |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
87 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
88 CFile (const std::string& path, const std::string& mode) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
89 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
90 fp = std::fopen (path.c_str (), mode.c_str ()); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
91 if (! fp) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
92 throw std::runtime_error ("unable to open file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
93 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
94 |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
95 void |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
96 close (void) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
97 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
98 if (std::fclose (fp)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
99 throw std::runtime_error ("unable to close file"); |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
100 fp = nullptr; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
101 } |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
102 |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
103 ~CFile () |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
104 { |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
105 if (fp) |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
106 std::fclose (fp); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
107 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
108 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
109 |
22188 | 110 #if defined (HAVE_BZ2) |
111 | |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
112 class bz2 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
113 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
114 private: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
115 class zipper |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
116 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
117 private: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
118 int status = BZ_OK; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
119 CFile source; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
120 CFile dest; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
121 BZFILE* bz; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
122 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
123 public: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
124 zipper (const std::string& source_path, const std::string& dest_path) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
125 : source (source_path, "rb"), dest (dest_path, "wb") |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
126 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
127 bz = BZ2_bzWriteOpen (&status, dest.fp, 9, 0, 30); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
128 if (status != BZ_OK) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
129 throw std::runtime_error ("failed to open bzip2 stream"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
130 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
131 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
132 void |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
133 deflate (void) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
134 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
135 const std::size_t buf_len = 8192; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
136 char buf[buf_len]; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
137 std::size_t n_read; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
138 while ((n_read = std::fread (buf, sizeof (buf[0]), buf_len, source.fp)) != 0) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
139 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
140 if (std::ferror (source.fp)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
141 throw std::runtime_error ("failed to read from source file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
142 BZ2_bzWrite (&status, bz, buf, n_read); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
143 if (status == BZ_IO_ERROR) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
144 throw std::runtime_error ("failed to write or compress"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
145 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
146 if (std::ferror (source.fp)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
147 throw std::runtime_error ("failed to read from source file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
148 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
149 |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
150 void |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
151 close (void) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
152 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
153 int abandon = (status == BZ_IO_ERROR) ? 1 : 0; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
154 BZ2_bzWriteClose (&status, bz, abandon, 0, 0); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
155 if (status != BZ_OK) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
156 throw std::runtime_error ("failed to close bzip2 stream"); |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
157 bz = nullptr; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
158 |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
159 // We have no error handling for failing to close source, let |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
160 // the destructor close it. |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
161 dest.close (); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
162 } |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
163 |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
164 ~zipper () |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
165 { |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
166 if (bz != nullptr) |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
167 BZ2_bzWriteClose (&status, bz, 1, 0, 0); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
168 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
169 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
170 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
171 public: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
172 static const constexpr char* extension = ".bz2"; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
173 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
174 static void |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
175 zip (const std::string& source_path, const std::string& dest_path) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
176 { |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
177 bz2::zipper z (source_path, dest_path); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
178 z.deflate (); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
179 z.close (); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
180 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
181 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
182 }; |
22188 | 183 |
184 #endif | |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
185 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
186 // Note about zlib and gzip |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
187 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
188 // gzip is a format for compressed single files. zlib is a format |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
189 // designed for in-memory and communication channel applications. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
190 // gzip uses the same format internally for the compressed data but |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
191 // has different headers and trailers. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
192 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
193 // zlib is also a library but gzip is not. Very old versions of zlib do |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
194 // not include functions to create useful gzip headers and trailers: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
195 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
196 // Note that you cannot specify special gzip header contents (e.g. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
197 // a file name or modification date), nor will inflate tell you what |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
198 // was in the gzip header. If you need to customize the header or |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
199 // see what's in it, you can use the raw deflate and inflate |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
200 // operations and the crc32() function and roll your own gzip |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
201 // encoding and decoding. Read the gzip RFC 1952 for details of the |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
202 // header and trailer format. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
203 // zlib FAQ |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
204 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
205 // Recent versions (on which we are already dependent) have deflateInit2() |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
206 // to do it. We still need to get the right metadata for the header |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
207 // ourselves though. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
208 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
209 // The header is defined in RFC #1952 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
210 // GZIP file format specification version 4.3 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
211 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
212 |
22188 | 213 #if defined (HAVE_Z) |
214 | |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
215 class gz |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
216 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
217 private: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
218 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
219 // Util class to get a non-const char* |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
220 class uchar_array |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
221 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
222 public: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
223 // Bytef is a typedef for unsigned char |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
224 unsigned char* p; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
225 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
226 uchar_array (const std::string& str) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
227 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
228 p = new Bytef[str.length () +1]; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
229 std::strcpy (reinterpret_cast<char*> (p), str.c_str ()); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
230 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
231 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
232 ~uchar_array (void) { delete[] p; } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
233 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
234 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
235 class gzip_header : public gz_header |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
236 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
237 private: |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
238 // This must be kept for gz_header.name |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
239 uchar_array basename; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
240 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
241 public: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
242 gzip_header (const std::string& source_path) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
243 : basename (octave::sys::env::base_pathname (source_path)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
244 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
245 const octave::sys::file_stat source_stat (source_path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
246 if (! source_stat) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
247 throw std::runtime_error ("unable to stat source file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
248 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
249 // time_t may be a signed int in which case it will be a |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
250 // positive number so it is safe to uLong. Or is it? Can |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
251 // unix_time really never be negative? |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
252 time = uLong (source_stat.mtime ().unix_time ()); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
253 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
254 // If FNAME is set, an original file name is present, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
255 // terminated by a zero byte. The name must consist of ISO |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
256 // 8859-1 (LATIN-1) characters; on operating systems using |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
257 // EBCDIC or any other character set for file names, the name |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
258 // must be translated to the ISO LATIN-1 character set. This |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
259 // is the original name of the file being compressed, with any |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
260 // directory components removed, and, if the file being |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
261 // compressed is on a file system with case insensitive names, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
262 // forced to lower case. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
263 name = basename.p; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
264 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
265 // If we don't set it to Z_NULL, then it will set FCOMMENT (4th bit) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
266 // on the FLG byte, and then write {0, 3} comment. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
267 comment = Z_NULL; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
268 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
269 // Seems to already be the default but we are not taking chances. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
270 extra = Z_NULL; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
271 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
272 // We do not want a CRC for the header. That would be only 2 more |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
273 // bytes, and maybe it would be a good thing but we want to generate |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
274 // gz files similar to the default gzip application. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
275 hcrc = 0; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
276 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
277 // OS (Operating System): |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
278 // 0 - FAT filesystem (MS-DOS, OS/2, NT/Win32) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
279 // 1 - Amiga |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
280 // 2 - VMS (or OpenVMS) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
281 // 3 - Unix |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
282 // 4 - VM/CMS |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
283 // 5 - Atari TOS |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
284 // 6 - HPFS filesystem (OS/2, NT) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
285 // 7 - Macintosh |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
286 // 8 - Z-System |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
287 // 9 - CP/M |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
288 // 10 - TOPS-20 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
289 // 11 - NTFS filesystem (NT) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
290 // 12 - QDOS |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
291 // 13 - Acorn RISCOS |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
292 // 255 - unknown |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
293 // |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
294 // The list is problematic because it mixes OS and filesystem. It |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
295 // also does not specify whether filesystem relates to source or |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
296 // destination file. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
297 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
298 #if defined (__WIN32__) |
22188 | 299 // Or should it be 11? |
300 os = 0; | |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
301 #elif defined (__APPLE__) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
302 os = 7; |
22188 | 303 #else |
304 // Unix by default? | |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
305 os = 3; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
306 #endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
307 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
308 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
309 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
310 class zipper |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
311 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
312 private: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
313 CFile source; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
314 CFile dest; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
315 gzip_header header; |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
316 z_stream* strm; |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
317 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
318 public: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
319 zipper (const std::string& source_path, const std::string& dest_path) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
320 : source (source_path, "rb"), dest (dest_path, "wb"), |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
321 header (source_path), strm (new z_stream) |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
322 { |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
323 strm->zalloc = Z_NULL; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
324 strm->zfree = Z_NULL; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
325 strm->opaque = Z_NULL; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
326 } |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
327 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
328 void |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
329 deflate () |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
330 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
331 // int deflateInit2 (z_streamp strm, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
332 // int level, // compression level (default is 8) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
333 // int method, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
334 // int windowBits, // 15 (default) + 16 (gzip format) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
335 // int memLevel, // memory usage (default is 8) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
336 // int strategy); |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
337 int status = deflateInit2 (strm, 8, Z_DEFLATED, 31, 8, |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
338 Z_DEFAULT_STRATEGY); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
339 if (status != Z_OK) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
340 throw std::runtime_error ("failed to open zlib stream"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
341 |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
342 deflateSetHeader (strm, &header); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
343 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
344 const std::size_t buf_len = 8192; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
345 unsigned char buf_in[buf_len]; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
346 unsigned char buf_out[buf_len]; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
347 |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
348 while ((strm->avail_in = std::fread (buf_in, sizeof (buf_in[0]), |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
349 buf_len, source.fp)) != 0) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
350 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
351 if (std::ferror (source.fp)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
352 throw std::runtime_error ("failed to read source file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
353 |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
354 strm->next_in = buf_in; |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
355 const int flush = std::feof (source.fp) ? Z_FINISH : Z_NO_FLUSH; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
356 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
357 // If deflate returns Z_OK and with zero avail_out, it must be |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
358 // called again after making room in the output buffer because |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
359 // there might be more output pending. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
360 do |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
361 { |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
362 strm->avail_out = buf_len; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
363 strm->next_out = buf_out; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
364 status = ::deflate (strm, flush); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
365 if (status == Z_STREAM_ERROR) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
366 throw std::runtime_error ("failed to deflate"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
367 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
368 std::fwrite (buf_out, sizeof (buf_out[0]), |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
369 buf_len - strm->avail_out, dest.fp); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
370 if (std::ferror (dest.fp)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
371 throw std::runtime_error ("failed to write file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
372 } |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
373 while (strm->avail_out == 0); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
374 |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
375 if (strm->avail_in != 0) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
376 throw std::runtime_error ("failed to wrote file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
377 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
378 } |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
379 |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
380 void |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
381 close (void) |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
382 { |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
383 if (deflateEnd (strm) != Z_OK) |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
384 throw std::runtime_error ("failed to close zlib stream"); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
385 strm = nullptr; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
386 |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
387 // We have no error handling for failing to close source, let |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
388 // the destructor close it. |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
389 dest.close (); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
390 } |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
391 |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
392 ~zipper (void) |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
393 { |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
394 if (strm) |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
395 deflateEnd (strm); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
396 delete strm; |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
397 } |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
398 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
399 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
400 public: |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
401 static const constexpr char* extension = ".gz"; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
402 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
403 static void |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
404 zip (const std::string& source_path, const std::string& dest_path) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
405 { |
22187
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
406 gz::zipper z (source_path, dest_path); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
407 z.deflate (); |
2aae8894885b
xzip.cc: replace throwing exceptions in destructors (bug #48640)
Carnë Draug <carandraug@octave.org>
parents:
22173
diff
changeset
|
408 z.close (); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
409 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
410 }; |
22188 | 411 |
412 #endif | |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
413 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
414 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
415 template<typename X> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
416 string_vector |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
417 xzip (const Array<std::string>& source_patterns, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
418 const std::function<std::string(const std::string&)>& mk_dest_path) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
419 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
420 std::list<std::string> dest_paths; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
421 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
422 std::function<void(const std::string&)> walk; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
423 walk = [&walk, &mk_dest_path, &dest_paths] (const std::string& path) -> void |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
424 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
425 const octave::sys::file_stat fs (path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
426 // is_dir and is_reg will return false if failed to stat. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
427 if (fs.is_dir ()) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
428 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
429 octave::sys::dir_entry dir (path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
430 if (dir) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
431 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
432 // Collect the whole list of filenames first, before recursion |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
433 // to avoid issues with infinite loop if the action generates |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
434 // files in the same directory (highly likely). |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
435 string_vector dirlist = dir.read (); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
436 for (octave_idx_type i = 0; i < dirlist.numel (); i++) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
437 if (dirlist(i) != "." && dirlist(i) != "..") |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
438 walk (octave::sys::file_ops::concat (path, dirlist(i))); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
439 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
440 // Note that we skip any problem with directories. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
441 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
442 else if (fs.is_reg ()) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
443 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
444 const std::string dest_path = mk_dest_path (path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
445 try |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
446 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
447 X::zip (path, dest_path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
448 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
449 catch (...) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
450 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
451 // Error "handling" is not including filename on the output list. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
452 // Also remove created file which maybe was not even created |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
453 // in the first place. Note that it is possible for the file |
22173
8de49f15e182
doc: Fix "doubled words" typos (\b(\w+)\s+\1\b)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
22160
diff
changeset
|
454 // to exist in the first place and for X::zip to not have |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
455 // clobber it yet but we remove it anyway by design. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
456 octave::sys::unlink (dest_path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
457 return; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
458 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
459 dest_paths.push_front (dest_path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
460 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
461 // Skip all other file types and errors. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
462 return; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
463 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
464 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
465 for (octave_idx_type i = 0; i < source_patterns.numel (); i++) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
466 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
467 const glob_match pattern (octave::sys::file_ops::tilde_expand (source_patterns(i))); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
468 const string_vector filepaths = pattern.glob (); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
469 for (octave_idx_type j = 0; j < filepaths.numel (); j++) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
470 walk (filepaths(j)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
471 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
472 return string_vector (dest_paths); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
473 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
474 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
475 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
476 template<typename X> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
477 string_vector |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
478 xzip (const Array<std::string>& source_patterns) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
479 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
480 const std::string ext = X::extension; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
481 const std::function<std::string(const std::string&)> mk_dest_path |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
482 = [&ext] (const std::string& source_path) -> std::string |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
483 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
484 return source_path + ext; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
485 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
486 return xzip<X> (source_patterns, mk_dest_path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
487 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
488 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
489 template<typename X> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
490 string_vector |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
491 xzip (const Array<std::string>& source_patterns, const std::string& out_dir) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
492 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
493 const std::string ext = X::extension; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
494 const std::function<std::string(const std::string&)> mk_dest_path |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
495 = [&out_dir, &ext] (const std::string& source_path) -> std::string |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
496 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
497 const std::string basename = octave::sys::env::base_pathname (source_path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
498 return octave::sys::file_ops::concat (out_dir, basename + ext); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
499 }; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
500 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
501 // We don't care if mkdir fails. Maybe it failed because it already |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
502 // exists, or maybe it can't bre created. If the first, then there's |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
503 // nothing to do, if the later, then it will be handled later. Any |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
504 // is to be handled by not listing files in the output. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
505 octave::sys::mkdir (out_dir, 0777); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
506 return xzip<X> (source_patterns, mk_dest_path); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
507 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
508 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
509 template<typename X> |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
510 static octave_value_list |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
511 xzip (const std::string& func_name, const octave_value_list& args) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
512 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
513 const octave_idx_type nargin = args.length (); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
514 if (nargin < 1 || nargin > 2) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
515 print_usage (); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
516 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
517 const Array<std::string> source_patterns |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
518 = args(0).xcellstr_value ("%s: FILES must be a character array or cellstr", |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
519 func_name.c_str ()); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
520 if (nargin == 1) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
521 return octave_value (Cell (xzip<X> (source_patterns))); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
522 else // nargin == 2 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
523 { |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
524 const std::string out_dir = args(1).string_value (); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
525 return octave_value (Cell (xzip<X> (source_patterns, out_dir))); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
526 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
527 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
528 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
529 DEFUN_DLD (gzip, args, , |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
530 doc: /* -*- texinfo -*- |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
531 @deftypefn {} {@var{filelist} =} gzip (@var{files}) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
532 @deftypefnx {} {@var{filelist} =} gzip (@var{files}, @var{dir}) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
533 Compress the list of files and directories specified in @var{files}. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
534 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
535 @var{files} is a character array or cell array of strings. Shell wildcards |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
536 in the filename such as @samp{*} or @samp{?} are accepted and expanded. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
537 Each file is compressed separately and a new file with a @file{".gz"} |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
538 extension is created. The original files are not modified, but existing |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
539 compressed files will be silently overwritten. If a directory is |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
540 specified then @code{gzip} recursively compresses all files in the |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
541 directory. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
542 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
543 If @var{dir} is defined the compressed files are placed in this directory, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
544 rather than the original directory where the uncompressed file resides. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
545 Note that this does not replicate a directory tree in @var{dir} which may |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
546 lead to files overwritting each other if there are multiple files with the |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
547 same name. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
548 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
549 If @var{dir} does not exist it is created. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
550 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
551 The optional output @var{filelist} is a list of the compressed files. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
552 @seealso{gunzip, unpack, bzip2, zip, tar} |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
553 @end deftypefn */) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
554 { |
22265
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
555 #if defined (HAVE_Z) |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
556 |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
557 return xzip<gz> ("gzip", args); |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
558 |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
559 #else |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
560 |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
561 octave_unused_parameter (args); |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
562 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
563 err_disabled_feature ("gzip", "gzip"); |
22265
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
564 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
565 #endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
566 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
567 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
568 /* |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
569 %!error gzip () |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
570 %!error gzip ("1", "2", "3") |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
571 %!error <FILES must be a character array or cellstr> gzip (1) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
572 */ |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
573 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
574 DEFUN_DLD (bzip2, args, , |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
575 doc: /* -*- texinfo -*- |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
576 @deftypefn {} {@var{filelist} =} bzip2 (@var{files}) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
577 @deftypefnx {} {@var{filelist} =} bzip2 (@var{files}, @var{dir}) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
578 Compress the list of files specified in @var{files}. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
579 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
580 @var{files} is a character array or cell array of strings. Shell wildcards |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
581 in the filename such as @samp{*} or @samp{?} are accepted and expanded. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
582 Each file is compressed separately and a new file with a @file{".bz2"} |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
583 extension is created. The original files are not modified, but existing |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
584 compressed files will be silently overwritten. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
585 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
586 If @var{dir} is defined the compressed files are placed in this directory, |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
587 rather than the original directory where the uncompressed file resides. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
588 Note that this does not replicate a directory tree in @var{dir} which may |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
589 lead to files overwritting each other if there are multiple files with the |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
590 same name. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
591 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
592 If @var{dir} does not exist it is created. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
593 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
594 The optional output @var{filelist} is a list of the compressed files. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
595 @seealso{bunzip2, unpack, gzip, zip, tar} |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
596 @end deftypefn */) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
597 { |
22265
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
598 #if defined (HAVE_BZ2) |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
599 |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
600 return xzip<bz2> ("bzip2", args); |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
601 |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
602 #else |
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
603 |
22269
a76b931d2c4b
* xzip.cc (Fbzip2): Fix typo of "octave_unused_parameter".
Mike Miller <mtmiller@octave.org>
parents:
22265
diff
changeset
|
604 octave_unused_parameter (args); |
22265
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
605 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
606 err_disabled_feature ("bzip2", "bzip2"); |
22265
15b9d7cb3098
eliminate unused parameter warnings
John W. Eaton <jwe@octave.org>
parents:
22194
diff
changeset
|
607 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
608 #endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
609 } |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
610 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
611 // Tests for both gzip/bzip2 and gunzip/bunzip2 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
612 /* |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
613 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
614 ## Takes a single argument, a function handle for the test. This other |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
615 ## function must accept two arguments, a directory for the tests, and |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
616 ## a cell array with zip function, unzip function, and file extension. |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
617 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
618 %!function run_test_function (test_function) |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
619 %! enabled_zippers = struct ("zip", {}, "unzip", {}, "ext", {}); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
620 %! if (__octave_config_info__ ().build_features.BZ2) |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
621 %! enabled_zippers(end+1).zip = @bzip2; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
622 %! enabled_zippers(end).unzip = @bunzip2; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
623 %! enabled_zippers(end).ext = ".bz2"; |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
624 %! endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
625 %! if (__octave_config_info__ ().build_features.Z) |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
626 %! enabled_zippers(end+1).zip = @gzip; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
627 %! enabled_zippers(end).unzip = @gunzip; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
628 %! enabled_zippers(end).ext = ".gz"; |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
629 %! endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
630 %! |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
631 %! for z = enabled_zippers |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
632 %! test_dir = tempname (); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
633 %! if (! mkdir (test_dir)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
634 %! error ("unable to create directory for tests"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
635 %! endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
636 %! unwind_protect |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
637 %! test_function (test_dir, z) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
638 %! unwind_protect_cleanup |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
639 %! confirm_recursive_rmdir (false, "local"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
640 %! rmdir (test_dir, "s"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
641 %! end_unwind_protect |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
642 %! endfor |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
643 %!endfunction |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
644 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
645 %!function create_file (fpath, data) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
646 %! fid = fopen (fpath, "wb"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
647 %! if (fid < 0) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
648 %! error ("unable to open file for writing"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
649 %! endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
650 %! if (fwrite (fid, data, class (data)) != numel (data)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
651 %! error ("unable to write to file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
652 %! endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
653 %! if (fflush (fid) || fclose (fid)) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
654 %! error ("unable to flush or close file"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
655 %! endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
656 %!endfunction |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
657 |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
658 %!function unlink_or_error (filepath) |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
659 %! [err, msg] = unlink (filepath); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
660 %! if (err) |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
661 %! error ("unable to remove file required for the test"); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
662 %! endif |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
663 %!endfunction |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
664 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
665 ## Test with large files because of varied buffer size |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
666 %!function test_large_file (test_dir, z) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
667 %! test_file = tempname (test_dir); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
668 %! create_file (test_file, rand (500000, 1)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
669 %! md5 = hash ("md5", fileread (test_file)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
670 %! |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
671 %! z_file = [test_file z.ext]; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
672 %! z_filelist = z.zip (test_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
673 %! assert (z_filelist, {z_file}) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
674 %! |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
675 %! unlink_or_error (test_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
676 %! uz_filelist = z.unzip (z_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
677 %! assert (uz_filelist, {test_file}) |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
678 %! |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
679 %! assert (hash ("md5", fileread (test_file)), md5) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
680 %!endfunction |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
681 %!test run_test_function (@test_large_file) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
682 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
683 ## Test that xzipped files are rexzipped |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
684 %!function test_z_z (test_dir, z) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
685 %! ori_file = tempname (test_dir); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
686 %! create_file (ori_file, rand (100, 1)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
687 %! md5_ori = hash ("md5", fileread (ori_file)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
688 %! |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
689 %! z_file = [ori_file z.ext]; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
690 %! z_filelist = z.zip (ori_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
691 %! assert (z_filelist, {z_file}) # check output |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
692 %! assert (exist (z_file), 2) # confirm file exists |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
693 %! assert (exist (ori_file), 2) # and did not remove original file |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
694 %! |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
695 %! unlink_or_error (ori_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
696 %! uz_filelist = z.unzip (z_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
697 %! assert (uz_filelist, {ori_file}) # bug #48598 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
698 %! assert (hash ("md5", fileread (ori_file)), md5_ori) |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
699 %! assert (exist (z_file), 2) # bug #48597 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
700 %! |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
701 %! ## xzip should dutifully re-xzip files even if they already are zipped |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
702 %! z_z_file = [z_file z.ext]; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
703 %! z_z_filelist = z.zip (z_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
704 %! assert (z_z_filelist, {z_z_file}) # check output |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
705 %! assert (exist (z_z_file), 2) # confirm file exists |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
706 %! assert (exist (z_file), 2) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
707 %! |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
708 %! md5_z = hash ("md5", fileread (z_file)); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
709 %! unlink_or_error (z_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
710 %! uz_z_filelist = z.unzip (z_z_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
711 %! assert (uz_z_filelist, {z_file}) # bug #48598 |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
712 %! assert (exist (z_z_file), 2) # bug #48597 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
713 %! assert (hash ("md5", fileread (z_file)), md5_z) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
714 %!endfunction |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
715 %!test run_test_function (@test_z_z) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
716 |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
717 %!function test_xzip_dir (test_dir, z) # bug #43431 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
718 %! fpaths = fullfile (test_dir, {"test1", "test2", "test3"}); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
719 %! md5s = cell (1, 3); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
720 %! for idx = 1:numel(fpaths) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
721 %! create_file (fpaths{idx}, rand (100, 1)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
722 %! md5s(idx) = hash ("md5", fileread (fpaths{idx})); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
723 %! endfor |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
724 %! |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
725 %! test_dir = [test_dir filesep()]; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
726 %! |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
727 %! z_files = strcat (fpaths, z.ext); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
728 %! z_filelist = z.zip (test_dir); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
729 %! assert (sort (z_filelist), z_files(:)) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
730 %! for idx = 1:numel(fpaths) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
731 %! assert (exist (z_files{idx}), 2) |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
732 %! unlink_or_error (fpaths{idx}); |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
733 %! endfor |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
734 %! |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
735 %! ## only gunzip handles directory (bunzip2 should too though) |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
736 %! if (z.unzip == @gunzip) |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
737 %! uz_filelist = z.unzip (test_dir); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
738 %! else |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
739 %! uz_filelist = cell (1, numel (z_filelist)); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
740 %! for idx = 1:numel(z_filelist) |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
741 %! uz_filelist(idx) = z.unzip (z_filelist{idx}); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
742 %! endfor |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
743 %! endif |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
744 %! assert (sort (uz_filelist), fpaths(:)) # bug #48598 |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
745 %! for idx = 1:numel(fpaths) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
746 %! assert (hash ("md5", fileread (fpaths{idx})), md5s{idx}) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
747 %! endfor |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
748 %!endfunction |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
749 %!test run_test_function (@test_xzip_dir) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
750 |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
751 %!function test_save_to_dir (test_dir, z) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
752 %! filename = "test-file"; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
753 %! filepath = fullfile (test_dir, filename); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
754 %! create_file (filepath, rand (100, 1)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
755 %! md5 = hash ("md5", fileread (filepath)); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
756 %! |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
757 %! ## test with existing and non-existing directory |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
758 %! out_dirs = {tempname (test_dir), tempname (test_dir)}; |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
759 %! if (! mkdir (out_dirs{1})) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
760 %! error ("unable to create directory for test"); |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
761 %! endif |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
762 %! for idx = 1:numel(out_dirs) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
763 %! out_dir = out_dirs{idx}; |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
764 %! uz_file = fullfile (out_dir, filename); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
765 %! z_file = [uz_file z.ext]; |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
766 %! |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
767 %! z_filelist = z.zip (filepath, out_dir); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
768 %! assert (z_filelist, {z_file}) |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
769 %! assert (exist (z_file, "file"), 2) |
22194
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
770 %! |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
771 %! uz_filelist = z.unzip (z_file); |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
772 %! assert (uz_filelist, {uz_file}) # bug #48598 |
b1ebad209360
xzip.cc: modify tests for readability.
Carnë Draug <carandraug@octave.org>
parents:
22188
diff
changeset
|
773 %! |
22160
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
774 %! assert (hash ("md5", fileread (uz_file)), md5) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
775 %! endfor |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
776 %!endfunction |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
777 %!test run_test_function (@test_save_to_dir) |
766f934db568
Rewrite gzip and bzip2 in C++ instead of using its applications (bug #43431)
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
778 */ |