Archive::MoPaQ - perl interface to libmpq
[perl/schtarr.git] / Archive-MoPaQ / MoPaQ.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "ppport.h"
6
7 #include "libmpq/mpq.h"
8
9 typedef PerlIO *      OutputStream;
10 typedef mpq_archive * Archive__MoPaQ;
11
12
13 MODULE = Archive::MoPaQ         PACKAGE = Archive::MoPaQ                
14
15 Archive::MoPaQ init(package)
16                 char *package;
17         CODE:
18                 RETVAL = malloc(sizeof(mpq_archive));
19                 memset(RETVAL, 0, sizeof(mpq_archive));
20         OUTPUT:
21                 RETVAL
22
23 int open(mpqa,filename)
24                 Archive::MoPaQ mpqa;
25                 char *filename;
26         CODE:
27                 RETVAL = libmpq_archive_open(mpqa, filename);
28         OUTPUT:
29                 RETVAL
30
31 int get_size(mpqa)
32                 Archive::MoPaQ mpqa;
33         CODE:
34                 RETVAL = libmpq_archive_info(mpqa, LIBMPQ_MPQ_ARCHIVE_SIZE);
35         OUTPUT:
36                 RETVAL
37
38 int get_numfiles(mpqa)
39                 Archive::MoPaQ mpqa;
40         CODE:
41                 RETVAL = libmpq_archive_info(mpqa, LIBMPQ_MPQ_NUMFILES);
42         OUTPUT:
43                 RETVAL
44
45 int listopen(mpqa,listfile)
46                 Archive::MoPaQ mpqa;
47                 char* listfile;
48         CODE:
49                 RETVAL = 0;
50                 switch (libmpq_listfile_open(mpqa, listfile)) {
51                         case LIBMPQ_CONF_EFILE_OPEN:
52                                         warn("found filelist, but could not open, so disabling listfile\n");
53                                         break;
54                         case LIBMPQ_CONF_EFILE_CORRUPT:
55                                         warn("found filelist with errors, so disabling listfile\n");
56                                         break;
57                         case LIBMPQ_CONF_EFILE_LIST_CORRUPT:
58                                         warn("found filelist, header matches %s, but filelist is corrupt.\n", mpqa->mpq_l->mpq_name);
59                                         break;
60                         case LIBMPQ_CONF_EFILE_VERSION:
61                                         warn("found filelist, but libmpq %s is required.\n", mpqa->mpq_l->mpq_version);
62                                         break;
63                         case LIBMPQ_CONF_EFILE_NOT_FOUND:
64                                         warn("Filelist not found");
65                                         break;
66                         default:
67 //                                      printf("game: %s, file: %s, version: %s\n", mpqa->mpq_l->mpq_game, mpqa->mpq_l->mpq_name, mpqa->mpq_l->mpq_game_version);
68                                         RETVAL = 1;
69                 }
70         OUTPUT:
71                 RETVAL
72
73 int extract(mpqa,nr)
74                 Archive::MoPaQ mpqa;
75                 int nr;
76         CODE:
77                 if (libmpq_file_check(mpqa, &nr, LIBMPQ_FILE_TYPE_INT)) {
78                         warn("file %i not found\n", nr);
79                 } else {
80                         RETVAL = libmpq_file_extract(mpqa, nr);
81                 }
82         OUTPUT:
83                 RETVAL
84
85 void DESTROY(mpqa)
86                 Archive::MoPaQ mpqa;
87         CODE:
88                 libmpq_listfile_close(mpqa);
89                 libmpq_archive_close(mpqa);
90                 free(mpqa);
91