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

Last change on this file since 39 was 39, checked in by Sam Hocevar, 12 years ago
  • Fix almost 2,000 warnings by using proper "const" keywords where needed.
File size: 1.6 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_uint16();
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;
23  filename_node *f=fn_list;
24  for (;f;f=f->next)
25    total++;
26  fp->write_uint16(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 const *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  clear(fn_root);
83  fn_root=0;
84}
85
86void spec_directory_cache::clear(filename_node *f)
87{
88  if (f)
89  {
90    if (f->left)
91    {
92      clear(f->left);
93      delete f->left;
94    }
95    if (f->right)
96    {
97      clear(f->right);
98      delete f->right;
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.