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

Last change on this file since 659 was 555, checked in by Sam Hocevar, 11 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
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[555]11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
[56]14
[481]15#include "specache.h"
[2]16
17spec_directory_cache sd_cache;
18
19void spec_directory_cache::load(bFILE *fp)
20{
[17]21  short tfn=fp->read_uint16();
[2]22  int i;
23  unsigned char len;
24  char fn[256];
25  size=0;
[494]26  for (i=0; i<tfn; i++)
[2]27  {
28    fp->read(&len,1);
29    fp->read(fn,len);
30    get_spec_directory(fn,fp);
[124]31  }
[2]32}
33
34void spec_directory_cache::save(bFILE *fp)
35{
36  int total = 0;
37  filename_node *f=fn_list;
[494]38  for (; f; f=f->next)
[2]39    total++;
[17]40  fp->write_uint16(total);
[494]41  for (f=fn_list; f; f=f->next)
[2]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
[39]51spec_directory *spec_directory_cache::get_spec_directory(char const *filename, bFILE *fp)
[2]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;
[124]61    else
[2]62      return p->sd;
[124]63    p=*parent;
64  }
65
[2]66  int need_close=0;
67  if (!fp)
68  {
69    fp=open_file(filename,"rb");
[124]70    if (fp->open_failure())
[2]71    {
72      delete fp;
73      return 0;
74    }
75    need_close=1;
76  }
[124]77
[2]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;
[124]108    }
[2]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.