source: golgotha/src/i4/loaders/dir_save.cc @ 608

Last change on this file since 608 was 80, checked in by Sam Hocevar, 15 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.2 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 "loaders/dir_save.hh"
10#include "checksum/checksum.hh"
11#include "memory/malloc.hh"
12#include <string.h>
13
14w32 i4_saver_class::tell ()
15{
16  if (state==DIRECTORY_CREATE)
17    return current_offset;
18  else
19    return out->tell();
20}
21
22i4_saver_class::i4_saver_class(i4_file_class *out, i4_bool close_on_delete)
23  : out(out),
24    close_on_delete(close_on_delete),
25    skips(0,256),
26    sections(0,256),
27    sizes(0, 512)
28{
29  state=DIRECTORY_CREATE;
30  current_offset=out->tell();
31}
32
33int i4_saver_class::mark_size()
34{
35  if (state==DIRECTORY_CREATE)
36  {
37    current_offset+=4;
38
39    int num=sizes.size();
40    sizes.add(current_offset);
41   
42    return num;
43  }
44  else
45  {
46    out->write_32(sizes[marker_on++]);
47    return 0;
48  }
49}
50
51void i4_saver_class::end_mark_size(int marker_number)
52{
53  if (state==DIRECTORY_CREATE)
54    sizes[marker_number]=current_offset-sizes[marker_number];
55}
56
57
58void i4_saver_class::start_version(w16 version)
59{
60  if (state==DIRECTORY_CREATE)
61  {
62    last_version_start=current_offset;
63    current_offset+=4;
64  }
65  else
66  {
67    out->write_16(version);
68    out->write_16(skips[current_skip++]);
69  }
70}
71
72void i4_saver_class::end_version()
73{
74  if (state==DIRECTORY_CREATE)
75    skips.add(current_offset-last_version_start);
76}
77
78void i4_saver_class::mark_section(w32 section_id)
79{
80  if (state==DIRECTORY_CREATE)
81  {
82#ifdef DEBUG
83    for (int i=0; i<sections.size(); i++)
84      if (sections[i].section_id==section_id)
85        i4_error("mark_section : section_id arleady used");
86   
87#endif         
88    sections.add(i4_saver_section_type(section_id, current_offset));
89  }
90}
91
92
93void i4_saver_class::mark_section(char *section_name)  // calls above with checksum of name
94{
95  mark_section(i4_check_sum32(section_name, strlen(section_name)));
96}
97
98
99i4_bool i4_saver_class::begin_data_write()
100{
101  marker_on=0;
102  state=DATA_WRITE; 
103  current_skip=0;
104
105  w32 signature=i4_check_sum32("GOLG_SECTION_ID=32",17);
106
107  if (out->write_32(signature)!=sizeof(w32)) return i4_F;
108  if (out->write_32(sections.size())!=sizeof(w32)) return i4_F;
109
110  w32 off=4 + 4 +  sections.size() * (4 + 4);
111
112  for (w32 i=0; i< sections.size(); i++)
113  {
114    if (out->write_32(sections[i].section_id)!=sizeof(w32)) return i4_F;
115    if (out->write_32(sections[i].section_offset + off)!=sizeof(w32)) return i4_F;
116  }
117  return i4_T;
118}
119
120w32 i4_saver_class::read (void *buffer, w32 size)
121{
122  i4_error("Bad bad");
123  return 0;
124}
125
126w32 i4_saver_class::write(const void *buffer, w32 size)
127{
128  if (state==DIRECTORY_CREATE)
129  {
130    current_offset+=size;
131    return size;
132  }
133  else
134    return out->write(buffer,size);
135}
136
137
138i4_saver_class::~i4_saver_class()
139{
140  if (close_on_delete && out)
141    delete out;
142}
143
Note: See TracBrowser for help on using the repository browser.