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

Last change on this file since 528 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

File size: 1.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include "specache.h"
14
15spec_directory_cache sd_cache;
16
17void spec_directory_cache::load(bFILE *fp)
18{
19  short tfn=fp->read_uint16();
20  int i;
21  unsigned char len;
22  char fn[256];
23  size=0;
24  for (i=0; i<tfn; i++)
25  {
26    fp->read(&len,1);
27    fp->read(fn,len);
28    get_spec_directory(fn,fp);
29  }
30}
31
32void spec_directory_cache::save(bFILE *fp)
33{
34  int total = 0;
35  filename_node *f=fn_list;
36  for (; f; f=f->next)
37    total++;
38  fp->write_uint16(total);
39  for (f=fn_list; f; f=f->next)
40  {
41    unsigned char len=strlen(f->filename())+1;
42    fp->write(&len,1);
43    fp->write(f->filename(),len);
44    f->sd->write(fp);
45  }
46}
47
48
49spec_directory *spec_directory_cache::get_spec_directory(char const *filename, bFILE *fp)
50{
51  filename_node **parent=0,*p=fn_root;
52  while (p)
53  {
54    int cmp=strcmp(p->filename(),filename);
55    if (cmp<0)
56      parent=&p->left;
57    else if (cmp>0)
58      parent=&p->right;
59    else
60      return p->sd;
61    p=*parent;
62  }
63
64  int need_close=0;
65  if (!fp)
66  {
67    fp=open_file(filename,"rb");
68    if (fp->open_failure())
69    {
70      delete fp;
71      return 0;
72    }
73    need_close=1;
74  }
75
76  filename_node *f=new filename_node(filename,new spec_directory(fp));
77  f->next=fn_list;
78  fn_list=f;
79
80  size+=f->sd->size;
81  if (parent)
82    *parent=f;
83  else
84    fn_root=f;
85
86  if (need_close)
87    delete fp;
88  return f->sd;
89}
90
91void spec_directory_cache::clear()
92{
93  size=0;
94  clear(fn_root);
95  fn_root=0;
96}
97
98void spec_directory_cache::clear(filename_node *f)
99{
100  if (f)
101  {
102    if (f->left)
103    {
104      clear(f->left);
105      delete f->left;
106    }
107    if (f->right)
108    {
109      clear(f->right);
110      delete f->right;
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.