libarchive-ruby-swig 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,6 +24,8 @@
24
24
  /* --- WRAPPER CODE START --- */
25
25
  %}
26
26
 
27
+ %apply (char *STRING, int LENGTH) { (const char *string, int length) };
28
+
27
29
  %include "reader.h"
28
30
  %include "writer.h"
29
31
  %include "entry.h"
@@ -19,7 +19,8 @@
19
19
  Reader::Reader(struct archive *ar)
20
20
  : _ar(ar),
21
21
  _buf((char*) malloc(1024)),
22
- _buf_size(1024)
22
+ _buf_size(1024),
23
+ _archive_content(0)
23
24
  {}
24
25
 
25
26
 
@@ -38,10 +39,15 @@ void Reader::close()
38
39
  archive_read_finish(_ar);
39
40
  _ar = 0;
40
41
  }
42
+
43
+ if(_archive_content) {
44
+ free(_archive_content);
45
+ _archive_content = 0;
46
+ }
41
47
  }
42
48
 
43
49
 
44
- Reader *Reader::read_open_filename(const char * filename)
50
+ Reader *Reader::read_open_filename(const char *filename)
45
51
  {
46
52
  struct archive *ar = archive_read_new();
47
53
 
@@ -65,6 +71,35 @@ Reader *Reader::read_open_filename(const char * filename)
65
71
  }
66
72
 
67
73
 
74
+ Reader *Reader::read_open_memory(const char *string, int length)
75
+ {
76
+ struct archive *ar = archive_read_new();
77
+ char *archive_content = (char*) malloc(length);
78
+ memcpy((void*) archive_content, (void*) string, length);
79
+
80
+ try {
81
+ if(archive_read_support_compression_all(ar) != ARCHIVE_OK)
82
+ throw 0;
83
+
84
+ if(archive_read_support_format_all(ar) != ARCHIVE_OK)
85
+ throw 0;
86
+
87
+ if(archive_read_open_memory(ar, (void*) archive_content, length) != ARCHIVE_OK)
88
+ throw 0;
89
+
90
+ } catch(...) {
91
+ std::string error_msg = archive_error_string(ar);
92
+ archive_read_finish(ar);
93
+ free(archive_content);
94
+ throw Error(error_msg);
95
+ }
96
+
97
+ Reader *reader_obj = new Reader(ar);
98
+ reader_obj->_archive_content = archive_content;
99
+ return reader_obj;
100
+ }
101
+
102
+
68
103
  Entry *Reader::next_header()
69
104
  {
70
105
  Entry * result = 0;
@@ -40,10 +40,13 @@ class Reader
40
40
 
41
41
  #ifdef SWIG
42
42
  %newobject read_open_filename(const char *filename);
43
+ %newobject read_open_memory(const char *string, int length);
43
44
  %newobject next_header();
44
45
  #endif
45
46
 
46
47
  static Reader *read_open_filename(const char *filename);
48
+ static Reader *read_open_memory(const char *string, int length);
49
+
47
50
  Entry *next_header();
48
51
  VALUE read_data_helper(int len);
49
52
 
@@ -54,6 +57,7 @@ class Reader
54
57
  private:
55
58
  struct archive *_ar;
56
59
  char *_buf;
60
+ char *_archive_content;
57
61
  int _buf_size;
58
62
  };
59
63
 
@@ -82,10 +82,6 @@ class Writer
82
82
 
83
83
  void write_header(Entry *entry);
84
84
 
85
- #ifdef SWIG
86
- %apply (char *STRING, int LENGTH) { (const char *string, int length) };
87
- #endif
88
-
89
85
  void write_data_helper(const char *string, int length);
90
86
 
91
87
  #ifdef SWIG
data/lib/libarchive_rs.rb CHANGED
@@ -102,6 +102,17 @@ module Archive
102
102
  end
103
103
  end
104
104
 
105
+ def self.read_open_memory(string)
106
+ ar = Reader.read_open_memory(string)
107
+
108
+ if block_given?
109
+ yield ar
110
+ ar.close()
111
+ else
112
+ return ar
113
+ end
114
+ end
115
+
105
116
  def self.write_open_filename(filename, compression, format)
106
117
  if compression.is_a? String
107
118
  if compression.index('/').nil?
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libarchive-ruby-swig
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Koch