source: abuse/trunk/src/imlib/unpackgl.cpp @ 2

Last change on this file since 2 was 2, checked in by Sam Hocevar, 14 years ago
  • imported original 0.7.0 tarball
File size: 2.1 KB
Line 
1#include "system.h"
2#include "jmalloc.hpp"
3#include "macs.hpp"
4#include <stdio.h>
5#include <ctype.h>
6
7void unpackgl(char *fn, int pict_only)
8{
9  unsigned short dir_length,sl,x,amread,bufsize;
10  unsigned long offset,ret_ofs,length;
11  char name[14],st[50],*buf;
12  FILE *fp,*ot;
13  CHECK(fn);
14  fp=fopen(fn,"rb");
15  if (!fp) return ;
16
17  if (fread(&dir_length,2,1,fp)!=1) return ;
18  dir_length=int_to_local(dir_length)/17-1;
19  ret_ofs=ftell(fp);
20  while (dir_length--)
21  {
22    fseek(fp,ret_ofs,SEEK_SET);
23    if (fread(&offset,4,1,fp)!=1) return ;
24    offset=long_to_local(offset);
25    if (fread(name,13,1,fp)!=1)  return ;
26    name[13]=0;
27    sl=strlen(name);
28    ret_ofs=ftell(fp);  // save the current spot in the file so we can come back to it later
29    if (toupper(name[sl-3])=='P' || toupper(name[sl-3])=='C' || !pict_only)
30    {
31      fseek(fp,offset,SEEK_SET);
32      fread(&length,1,4,fp);
33      length=long_to_local(length);
34      if (length>1000000)
35        exit(1);
36      ot=fopen(name,"rb");
37      if (ot)
38      { printf("File (%s) already exsist, overwrite (y/n)?\n",name);
39        fgets(st,49,stdin);
40        if (!(st[0]=='Y' || st[0]=='y'))
41          length=0;
42        fclose(ot);
43      }
44      if (length)
45        ot=fopen(name,"wb");
46      if (!ot) return ;
47      bufsize=0xf000;
48      do {
49        buf=(char *)jmalloc(bufsize,"unpack_gl::buffer");
50        if (!buf) bufsize-=100;
51      } while (!buf);
52
53      while (length)
54      {
55        if (length>bufsize)
56          amread=bufsize;
57        else amread=length;
58        fread(buf,1,amread,fp);
59        fwrite(buf,1,amread,ot);
60        length-=amread;
61      }
62      jfree(buf);
63      if (ot) fclose(ot);
64    }
65  }
66  fclose(fp);
67}
68
69main(int argc, char **argv)
70{
71  FILE *fp;
72  char fn[100];
73  int t,i,x;
74  if (argc<2)
75  { printf("Usage : unpackgl filename[.gl] [-p]\n");
76    printf("  -p only unpacks picture files\n");
77    printf("  Unpackes the files contain within a GL file (GRASP format)\n");
78    printf("  Does not change the orginal file\n");
79    exit(0);
80  }
81  strcpy(fn,argv[1]);
82  if (fn[strlen(fn)-3]!='.')
83    strcat(fn,".gl");
84  if (*argv[2]=='-' && *(argv[2]+1)=='p')
85    unpackgl(fn,1);
86  else unpackgl(fn,0);
87  return 0;
88}
89
Note: See TracBrowser for help on using the repository browser.