source: abuse/tags/pd/macabuse/src/specache.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 1.7 KB
Line 
1#include "specache.hpp"
2
3spec_directory_cache sd_cache;
4
5void spec_directory_cache::load(bFILE *fp)
6{
7  short tfn=fp->read_short();
8  int i;
9  unsigned char len;
10  char fn[256];
11  size=0;
12  for (i=0;i<tfn;i++)
13  {
14    fp->read(&len,1);
15    fp->read(fn,len);
16    get_spec_directory(fn,fp);
17  } 
18}
19
20void spec_directory_cache::save(bFILE *fp)
21{
22  int total=0,i;
23  filename_node *f=fn_list;
24  for (;f;f=f->next)
25    total++;
26  fp->write_short(total);
27  for (f=fn_list;f;f=f->next)
28  {
29    unsigned char len=strlen(f->filename())+1;
30    fp->write(&len,1);
31    fp->write(f->filename(),len);
32    f->sd->write(fp);
33  }
34}
35
36
37spec_directory *spec_directory_cache::get_spec_directory(char *filename, bFILE *fp)
38{
39  filename_node **parent=0,*p=fn_root;
40  while (p)
41  {
42    int cmp=strcmp(p->filename(),filename);
43    if (cmp<0)
44      parent=&p->left;
45    else if (cmp>0)
46      parent=&p->right;
47    else
48      return p->sd;
49    p=*parent;       
50  } 
51 
52  int need_close=0;
53  if (!fp)
54  {
55    fp=open_file(filename,"rb");
56    if (fp->open_failure())
57    {
58      delete fp;
59      return 0;
60    }
61    need_close=1;
62  }
63 
64  filename_node *f=new filename_node(filename,new spec_directory(fp));
65  f->next=fn_list;
66  fn_list=f;
67
68  size+=f->sd->size;
69  if (parent)
70    *parent=f;
71  else
72    fn_root=f;
73
74  if (need_close)
75    delete fp;
76  return f->sd;
77}
78
79void spec_directory_cache::clear()
80{
81  size=0;
82  if (fn_root)
83  {
84    clear(fn_root);
85    delete fn_root;
86    fn_root=0;
87  }
88}
89
90void spec_directory_cache::clear(filename_node *f)
91{
92  if (f)
93  {
94    if (f->left)
95    {
96      clear(f->left);
97      delete f->left;
98    }
99    if (f->right)
100    {
101      clear(f->right);
102      delete f->right;
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.