mspack_rb 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06da1ce0f18e5111b2bcf727ea5d7170a100b75b
4
- data.tar.gz: 8c273f7a6d168312e49148e954941fa371064e8a
3
+ metadata.gz: 5d95c98f63d77452303839ff28381a303748b7ef
4
+ data.tar.gz: be1332875b3b16279ef38f24d729afd113221362
5
5
  SHA512:
6
- metadata.gz: 028b0e36cdbb51dd0b3d3a46171c20b719d57cc5c5f4765db701af46f0b77d1f74408df8311c5a41ec934a74a593d02b40230c3e612dc988cbbca8bff1cdfc37
7
- data.tar.gz: 8a4c05e292ab931537c3868c338cb8235c5d849596095ffed2b47556e007438077d6a749e3e7251bd17e22535ce5f6d972658f6738af84769f658027c39374a4
6
+ metadata.gz: de94dacd83888f31544b2343b091996730ce4326020a200630c37eb651727f12c9c8101cb4a1b3c6c394b3fd2deac4342eefbbcf6996a2990e4a6648d8d02fe8
7
+ data.tar.gz: e768f570b844cd92a7793ba5d51f8a3e08a1181fa0e0d58a476cfcd57c1b337d3eeb643dbc2da873349c4a333a02b4abad0e5f8d8b82b66018e4fbe59a940c6d
@@ -146,13 +146,10 @@ VALUE chmd_extract_to_path(int argc, VALUE* argv, VALUE self) {
146
146
 
147
147
  if (argc == 1) {
148
148
  rb_need_block();
149
-
150
- VALUE block;
151
- block = rb_block_proc();
152
-
153
- VALUE blockName = rb_funcall(block, rb_intern("to_s"), 0);
154
- pathStr = StringValueCStr(blockName);
155
- add_block(&blockName, &block);
149
+ VALUE block = rb_block_proc();
150
+ VALUE block_name = rb_funcall(block, rb_intern("object_id"), 0);
151
+ VALUE block_name_str = rb_funcall(block_name, rb_intern("to_s"), 0);
152
+ pathStr = StringValueCStr(block_name_str);
156
153
  }
157
154
  else {
158
155
  Check_Type(outputPath, T_STRING);
@@ -1,6 +1,8 @@
1
1
  #include "io_system.h"
2
2
  #include "chm_decompressor.h"
3
3
 
4
+ #include <ruby.h>
5
+
4
6
  #include <stdarg.h>
5
7
  #include <stdio.h>
6
8
  #include <string.h>
@@ -9,43 +11,21 @@
9
11
 
10
12
  union io_file_value {
11
13
  FILE *file;
12
- VALUE *block;
14
+ VALUE block;
13
15
  };
14
16
 
15
17
  struct io_file {
16
- char *name; // only used for block handle
18
+ int is_block;
17
19
  union io_file_value value;
18
20
  };
19
21
 
20
- struct io_file *blocks[IO_SYSTEM_MAX_BLOCKS];
21
-
22
- inline struct io_file *pop_block(const char *name) {
23
- for (int i = 0; i < IO_SYSTEM_MAX_BLOCKS - 1; ++i) {
24
- struct io_file *block = blocks[i];
25
-
26
- if (block) {
27
- if (strcmp(name, block->name) == 0) {
28
- blocks[i] = NULL;
29
- return block;
30
- }
31
- }
32
- }
33
-
34
- return NULL;
35
- }
36
-
37
22
  struct mspack_file *
38
23
  io_open(struct mspack_system *self, const char *filename, int mode) {
39
- struct io_file *block = NULL;
40
-
41
- if (mode != MSPACK_SYS_OPEN_READ) {
42
- block = pop_block(filename);
24
+ if (strlen(filename) < 1) {
25
+ return NULL;
43
26
  }
44
27
 
45
- if (block) {
46
- return (struct mspack_file *)block;
47
- }
48
- else {
28
+ if (filename[0] == '/') {
49
29
  const char *modeStr;
50
30
 
51
31
  switch (mode) {
@@ -67,7 +47,7 @@ io_open(struct mspack_system *self, const char *filename, int mode) {
67
47
 
68
48
  struct io_file *file = malloc(sizeof(struct io_file));
69
49
  file->value.file = fopen(filename, modeStr);
70
- file->name = NULL;
50
+ file->is_block = 0;
71
51
 
72
52
  if (file->value.file) {
73
53
  return (struct mspack_file *)file;
@@ -77,15 +57,29 @@ io_open(struct mspack_system *self, const char *filename, int mode) {
77
57
  return NULL;
78
58
  }
79
59
  }
60
+
61
+ else {
62
+ VALUE block_id_str = rb_str_new_cstr(filename);
63
+ VALUE block_id = rb_funcall(block_id_str, rb_intern("to_i"), 0);
64
+ VALUE object_space = rb_const_get(rb_cModule, rb_intern("ObjectSpace"));
65
+ VALUE block = rb_funcall(object_space, rb_intern("_id2ref"), 1, block_id);
66
+
67
+ if (block == Qnil) {
68
+ return NULL;
69
+ }
70
+ else {
71
+ struct io_file *file = malloc(sizeof(struct io_file));
72
+ file->value.block = block;
73
+ file->is_block = 1;
74
+ return (struct mspack_file *)file;
75
+ }
76
+ }
80
77
  }
81
78
 
82
79
  void io_close(struct mspack_file *file) {
83
- if (!((struct io_file *)file)->name) {
80
+ if (!((struct io_file *)file)->is_block) {
84
81
  fclose(((struct io_file *)file)->value.file);
85
82
  }
86
- else {
87
- free(((struct io_file *)file)->name);
88
- }
89
83
 
90
84
  free(file);
91
85
  }
@@ -95,15 +89,13 @@ int io_read(struct mspack_file *file, void *buffer, int bytes) {
95
89
  }
96
90
 
97
91
  int io_write(struct mspack_file *file, void *buffer, int bytes) {
98
- if (!((struct io_file *)file)->name) {
92
+ if (!((struct io_file *)file)->is_block) {
99
93
  return (int)fwrite(buffer, 1, bytes, ((struct io_file *)file)->value.file);
100
94
  }
101
95
  else {
102
96
  VALUE data = rb_str_new((char *)buffer, bytes);
103
-
104
- VALUE *block;
105
- block = ((struct io_file *)file)->value.block;
106
- rb_funcall(*block, rb_intern("yield"), 1, data);
97
+ VALUE block = ((struct io_file *)file)->value.block;
98
+ rb_funcall(block, rb_intern("yield"), 1, data);
107
99
  return bytes;
108
100
  }
109
101
  }
@@ -150,20 +142,3 @@ struct mspack_system *io_system() {
150
142
  system->null_ptr = NULL;
151
143
  return system;
152
144
  }
153
-
154
- // TODO: chuck an error if we're full
155
- void add_block(VALUE *name, VALUE *block) {
156
- for (int i = 0; i < IO_SYSTEM_MAX_BLOCKS - 1; ++i) {
157
-
158
- if (!blocks[i]) {
159
- struct io_file *file = malloc(sizeof(struct io_file));
160
- file->value.block = block;
161
-
162
- const char *nameStr = StringValueCStr(*name);
163
- file->name = malloc(sizeof(char) * strlen(nameStr) + 1);
164
- strcpy(file->name, nameStr);
165
-
166
- blocks[i] = file;
167
- }
168
- }
169
- }
@@ -2,7 +2,6 @@
2
2
  #define IO_SYSTEM_H
3
3
 
4
4
  #include <mspack.h>
5
- #include <ruby.h>
6
5
 
7
6
  struct mspack_file *
8
7
  io_open(struct mspack_system *self, const char *filename, int mode);
@@ -25,9 +24,6 @@ void io_free(void *ptr);
25
24
 
26
25
  void io_copy(void *src, void *dest, size_t bytes);
27
26
 
28
-
29
27
  struct mspack_system *io_system();
30
28
 
31
- void add_block(VALUE *name, VALUE *block);
32
-
33
29
  #endif
@@ -1,3 +1,3 @@
1
1
  module Mspack
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mspack_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-24 00:00:00.000000000 Z
11
+ date: 2017-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler