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

Last change on this file since 129 was 129, checked in by Sam Hocevar, 15 years ago
  • Get rid of jmalloc and replace it with standard malloc. Modern operating systems certainly perform a lot better than this custom implementation, and we have superior tools (eg. valgrind) to debug and profile memory usage without interfering with the code itself.
File size: 2.3 KB
RevLine 
[56]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
[2]15#include "system.h"
16#include "macs.hpp"
17
18void unpackgl(char *fn, int pict_only)
19{
20  unsigned short dir_length,sl,x,amread,bufsize;
21  unsigned long offset,ret_ofs,length;
22  char name[14],st[50],*buf;
23  FILE *fp,*ot;
24  CHECK(fn);
25  fp=fopen(fn,"rb");
26  if (!fp) return ;
27
28  if (fread(&dir_length,2,1,fp)!=1) return ;
29  dir_length=int_to_local(dir_length)/17-1;
30  ret_ofs=ftell(fp);
31  while (dir_length--)
32  {
33    fseek(fp,ret_ofs,SEEK_SET);
34    if (fread(&offset,4,1,fp)!=1) return ;
35    offset=long_to_local(offset);
36    if (fread(name,13,1,fp)!=1)  return ;
37    name[13]=0;
38    sl=strlen(name);
39    ret_ofs=ftell(fp);  // save the current spot in the file so we can come back to it later
40    if (toupper(name[sl-3])=='P' || toupper(name[sl-3])=='C' || !pict_only)
41    {
42      fseek(fp,offset,SEEK_SET);
43      fread(&length,1,4,fp);
44      length=long_to_local(length);
45      if (length>1000000)
46        exit(1);
47      ot=fopen(name,"rb");
48      if (ot)
49      { printf("File (%s) already exsist, overwrite (y/n)?\n",name);
[124]50    fgets(st,49,stdin);
51    if (!(st[0]=='Y' || st[0]=='y'))
52      length=0;
53    fclose(ot);
[2]54      }
55      if (length)
[124]56    ot=fopen(name,"wb");
[2]57      if (!ot) return ;
58      bufsize=0xf000;
59      do {
[129]60    buf=(char *)malloc(bufsize);
[124]61    if (!buf) bufsize-=100;
[2]62      } while (!buf);
63
64      while (length)
65      {
[124]66    if (length>bufsize)
67      amread=bufsize;
68    else amread=length;
69    fread(buf,1,amread,fp);
70    fwrite(buf,1,amread,ot);
71    length-=amread;
[2]72      }
[129]73      free(buf);
[2]74      if (ot) fclose(ot);
75    }
76  }
77  fclose(fp);
78}
79
80main(int argc, char **argv)
81{
82  FILE *fp;
83  char fn[100];
84  int t,i,x;
85  if (argc<2)
86  { printf("Usage : unpackgl filename[.gl] [-p]\n");
87    printf("  -p only unpacks picture files\n");
88    printf("  Unpackes the files contain within a GL file (GRASP format)\n");
89    printf("  Does not change the orginal file\n");
90    exit(0);
91  }
92  strcpy(fn,argv[1]);
93  if (fn[strlen(fn)-3]!='.')
94    strcat(fn,".gl");
95  if (*argv[2]=='-' && *(argv[2]+1)=='p')
96    unpackgl(fn,1);
97  else unpackgl(fn,0);
98  return 0;
99}
100
Note: See TracBrowser for help on using the repository browser.