Class: MPQ::Archive

Inherits:
Object
  • Object
show all
Defined in:
ext/mpq_archive.c,
lib/mpq.rb

Constant Summary collapse

LISTFILE =
"(listfile)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Archive

Returns a new instance of Archive.



11
12
13
# File 'lib/mpq.rb', line 11

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/mpq.rb', line 7

def path
  @path
end

Instance Method Details

#listfileObject



15
16
17
# File 'lib/mpq.rb', line 15

def listfile
  read_file LISTFILE
end

#manifestObject



19
20
21
# File 'lib/mpq.rb', line 19

def manifest
  listfile.split("\n").collect(&:strip)
end

#read_file(name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'ext/mpq_archive.c', line 9

static VALUE rb_mpq_archive_read_file(VALUE self, VALUE name){
  
  // archive struct
  mpq_archive_s *archive;
  // stores the index number for "(listfile)"
  unsigned int file_index;
  // stores the contents of the "(listfile)"
  char *contents;
  // stores the file size for string malloc
  off_t file_size;
  // retrieve the file path from the class
  VALUE file_path = rb_funcall(self, rb_intern("path"), 0);
  // make sure we have a string
  Check_Type(file_path, T_STRING);
  Check_Type(name, T_STRING);
  
  VALUE buf = rb_str_buf_new(1024);
  
  // start your engines
  libmpq__init();
  int return_code;
  if(return_code = libmpq__archive_open(&archive, RSTRING_PTR(file_path), -1) != 0)
  {
    // raise an error? or just return the non-zero int?
    return INT2FIX(return_code) ;
  }
  
  // get filenumber and size for listfile
  if (return_code = libmpq__file_number(archive, RSTRING_PTR(name), &file_index) != 0) {
    printf("No %s in '%s'.\n", RSTRING_PTR(name), RSTRING_PTR(file_path));
    libmpq__archive_close(archive);
    return INT2FIX(return_code);
  }
  
  // we've succesffully opene the archive
  libmpq__file_unpacked_size(archive, file_index, &file_size);
  
  // read listfile content into memory
  contents = malloc(file_size);
  
  libmpq__file_read(archive, file_index, contents, file_size, NULL);
  
  rb_str_cat(buf, contents, file_size);
  
  free(contents);
  
  // free up your shit
  libmpq__shutdown();
  
  return buf;
  
}