Class: Mspack::ChmDecompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/mspack/chm_decompressor.rb,
ext/mspack_native/chm_decompressor.c

Defined Under Namespace

Classes: File, Header

Instance Method Summary collapse

Instance Method Details

#close(header) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/mspack_native/chm_decompressor.c', line 115

VALUE chmd_close(VALUE self, VALUE header) {
  if (!CLASS_OF(header) == ChmDHeader) {
    rb_raise(rb_eTypeError, "Parameter must be a CHM decompression header");
  }

  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  struct mschmd_header *headerPtr;
  Data_Get_Struct(header, struct mschmd_header, headerPtr);

  wrapper->decom->close(wrapper->decom, headerPtr);
  return Qnil;
}

#extract(file, dir) ⇒ Object

Expects a ChmDecompressor::File and a string. Calls Mspack.ensure_path and extracts file. Returns the absolute file path.



9
10
11
12
13
# File 'lib/mspack/chm_decompressor.rb', line 9

def extract(file, dir)
  path = Mspack.ensure_path(file.filename, dir)
  extract_to_path(file, path)
  path
end

#extract_to_path(*args) ⇒ Object

Form 1: extract_to_path(file, outputPath)

Form 2: extract_to_path(file) { |data_chunk| do_something(data_chunk) }



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'ext/mspack_native/chm_decompressor.c', line 137

VALUE chmd_extract_to_path(int argc, VALUE* argv, VALUE self) {
  VALUE file;
  VALUE outputPath;
  rb_scan_args(argc, argv, "11", &file, &outputPath);
  const char *pathStr;

  if (!CLASS_OF(file) == ChmDFile) {
    rb_raise(rb_eTypeError, "First parameter must be a CHM decompression file");
  }

  if (argc == 1) {
    rb_need_block();

    VALUE block;
    block = rb_block_proc();

    VALUE blockName = rb_funcall(block, rb_intern("to_s"), 0);
    pathStr = StringValueCStr(blockName);
    add_block(&blockName, &block);
  }
  else {
    Check_Type(outputPath, T_STRING);
    pathStr = StringValueCStr(outputPath);
  }

  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  struct chmd_file_wrapper *headerWrapper;
  Data_Get_Struct(file, struct chmd_file_wrapper, headerWrapper);

  int result = 
    wrapper->decom->extract(wrapper->decom, headerWrapper->file, pathStr);

  return result == MSPACK_ERR_OK ? Qtrue : Qfalse;
}

#fast_find(header, filename) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'ext/mspack_native/chm_decompressor.c', line 186

VALUE chmd_fast_find(VALUE self, VALUE header, VALUE filename) {
  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  struct mschmd_header *headerPtr;
  Data_Get_Struct(header, struct mschmd_header, headerPtr);

  const char *filenameStr = StringValueCStr(filename);

  int structSize = sizeof(struct mschmd_file);
  struct mschmd_file *file = malloc(structSize);

  int result = wrapper->decom->fast_find(
    wrapper->decom, headerPtr, filenameStr, file, structSize);

  if (result != MSPACK_ERR_OK || file->length == 0) {
    free(file);
    return Qnil;
  }  

  file->filename = malloc(sizeof(char) * strlen(filenameStr) + 1);
  strcpy(file->filename, filenameStr);

  struct chmd_file_wrapper *fileWrapper = 
    malloc(sizeof(struct chmd_file_wrapper));
  fileWrapper->is_fast_find = 1;
  fileWrapper->file = file;

  return Data_Wrap_Struct(ChmDFile, NULL, chmd_file_free, fileWrapper);
}

#fast_open(path) ⇒ Object



182
183
184
# File 'ext/mspack_native/chm_decompressor.c', line 182

VALUE chmd_fast_open(VALUE self, VALUE path) {
  return _open(self, path, 1);
}

#last_errorObject



174
175
176
177
178
179
180
# File 'ext/mspack_native/chm_decompressor.c', line 174

VALUE chmd_last_error(VALUE self) {
  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  int error = wrapper->decom->last_error(wrapper->decom);
  return _error_code_sym(error);
}

#open(path) ⇒ Object



111
112
113
# File 'ext/mspack_native/chm_decompressor.c', line 111

VALUE chmd_open(VALUE self, VALUE path) {
  return _open(self, path, 0);
}