source: golgotha/src/cd_maker/cd_maker.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "main/main.hh"
10#include "error/alert.hh"
11#include "error/error.hh"
12#include "file/file.hh"
13#include "string/str_checksum.hh"
14#include "memory/malloc.hh"
15#include <stdio.h>
16
17struct cd_file
18{
19   
20  i4_str *name;
21  w32 length;
22  w32 checksum;
23
24  void add(const i4_const_str &str)
25  {
26    char tmp[256];
27    i4_os_string(str, tmp,256);
28    for (char *c=tmp; *c; c++)
29      if (*c=='\\')
30        *c='/';
31   
32    name=new i4_str(tmp);
33    i4_file_class *fp=i4_open(str);
34    I4_ASSERT(fp,"no fp");
35    length=fp->size();
36    delete fp;
37  }
38
39} flist[10000];
40 
41
42int flist_compare(const void *a, const void *b)
43{
44  if (((cd_file *)a)->checksum > ((cd_file *)b)->checksum)
45    return 1;
46  else if (((cd_file *)a)->checksum < ((cd_file *)b)->checksum)
47    return -1;
48  else return 0;
49
50}
51
52void i4_main(w32 argc, i4_const_str *argv)
53{
54  i4_init();
55
56
57
58  if (argc>1)
59    i4_string_man.load(argv[1]);
60  else
61    i4_string_man.load("..\cd_maker\resource.res");
62
63  i4_debug->printf("Building CD image golgotha.cd");
64 
65
66  i4_const_str *files=i4_string_man.get_array("cd_files");
67
68 
69 
70  int i, tfiles=0,j;
71
72  for (i=0; !files[i].null(); i++)
73  {
74    i4_directory_struct d;
75    if (i4_get_directory(files[i], d))
76    {
77      for (j=0; j<d.tfiles; j++)
78      {
79        i4_str *r=i4_const_str("%S/%S").sprintf(150,&files[i],d.files[j]);
80
81        flist[tfiles++].add(*r);
82        delete r;
83      }
84    }
85    else
86    {
87      i4_file_status_struct return_stat;
88      if (i4_get_status(files[i], return_stat))             
89        flist[tfiles++].add(files[i]);
90      else
91        i4_alert("File does not exsist : %S",100,&files[i]);
92    }
93  }
94
95  i4_file_class *out=i4_open("golgotha.cd", I4_WRITE);
96  if (!out)
97    i4_error("could not open output file");
98
99
100  out->write_32(tfiles);
101
102  w32 offset=4 +             // total files
103             (12 * tfiles);  // directory
104
105  for (i=0; i<tfiles; i++)
106    flist[i].checksum = i4_str_checksum(*flist[i].name);
107 
108  qsort(flist, tfiles, sizeof(flist[0]), flist_compare);
109
110
111  for (i=0; i<tfiles; i++)
112  {   
113    out->write_32(offset);
114    offset+=flist[i].length;
115
116    if (i!=0 && flist[i-1].checksum==flist[i].checksum)
117      i4_error("2 files have same checkum");
118
119    out->write_32(flist[i].checksum);
120    out->write_32(flist[i].length);     
121  }
122
123  int tsize=0;
124  for (i=0; i<tfiles; i++)
125  {
126    i4_file_class *fp=i4_open(*flist[i].name);
127   
128    if(fp)
129    {
130      int off=out->tell();
131
132      char fn[100];
133      i4_os_string(*flist[i].name, fn, 100);
134      printf("%d bytes Adding '%s' checksum = %x, off=%d\n",
135             flist[i].length,  fn, flist[i].checksum, off);
136
137      tsize+=flist[i].length;
138
139      if (flist[i].length)
140      {
141        w8 buf[64*1024];
142
143        int size=flist[i].length;
144        while (size)
145        {
146          int to_read=size>sizeof(buf) ? sizeof(buf) : size;
147
148          if (fp->read(buf, to_read)!=to_read)
149            i4_error("file read premature");
150                   
151          if (out->write(buf, to_read)!=to_read)
152            i4_error("write premature, out of space?");
153
154          size-=to_read;
155        }
156      }
157
158      delete fp;
159    }
160    else
161    {
162      char buf[100];
163      i4_os_string(*flist[i].name, buf, 100);
164      i4_error("%s : file not found", buf);
165    }
166  }
167  delete out;
168
169  printf("Total size= %d\n", tsize);
170
171
172  i4_free(files);
173
174  i4_uninit();
175}
176
177
Note: See TracBrowser for help on using the repository browser.