source: abuse/trunk/src/specache.cpp

Last change on this file was 555, checked in by Sam Hocevar, 9 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

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