source: abuse/trunk/src/specache.cpp @ 112

Last change on this file since 112 was 56, checked in by Sam Hocevar, 11 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 1.9 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 "specache.hpp"
13
14spec_directory_cache sd_cache;
15
16void spec_directory_cache::load(bFILE *fp)
17{
18  short tfn=fp->read_uint16();
19  int i;
20  unsigned char len;
21  char fn[256];
22  size=0;
23  for (i=0;i<tfn;i++)
24  {
25    fp->read(&len,1);
26    fp->read(fn,len);
27    get_spec_directory(fn,fp);
28  } 
29}
30
31void spec_directory_cache::save(bFILE *fp)
32{
33  int total = 0;
34  filename_node *f=fn_list;
35  for (;f;f=f->next)
36    total++;
37  fp->write_uint16(total);
38  for (f=fn_list;f;f=f->next)
39  {
40    unsigned char len=strlen(f->filename())+1;
41    fp->write(&len,1);
42    fp->write(f->filename(),len);
43    f->sd->write(fp);
44  }
45}
46
47
48spec_directory *spec_directory_cache::get_spec_directory(char const *filename, bFILE *fp)
49{
50  filename_node **parent=0,*p=fn_root;
51  while (p)
52  {
53    int cmp=strcmp(p->filename(),filename);
54    if (cmp<0)
55      parent=&p->left;
56    else if (cmp>0)
57      parent=&p->right;
58    else
59      return p->sd;
60    p=*parent;       
61  } 
62 
63  int need_close=0;
64  if (!fp)
65  {
66    fp=open_file(filename,"rb");
67    if (fp->open_failure())
68    {
69      delete fp;
70      return 0;
71    }
72    need_close=1;
73  }
74 
75  filename_node *f=new filename_node(filename,new spec_directory(fp));
76  f->next=fn_list;
77  fn_list=f;
78
79  size+=f->sd->size;
80  if (parent)
81    *parent=f;
82  else
83    fn_root=f;
84
85  if (need_close)
86    delete fp;
87  return f->sd;
88}
89
90void spec_directory_cache::clear()
91{
92  size=0;
93  clear(fn_root);
94  fn_root=0;
95}
96
97void spec_directory_cache::clear(filename_node *f)
98{
99  if (f)
100  {
101    if (f->left)
102    {
103      clear(f->left);
104      delete f->left;
105    }
106    if (f->right)
107    {
108      clear(f->right);
109      delete f->right;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.