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

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