source: abuse/branches/lol/src/specache.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 2.3 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 HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "specache.h"
16
17SpecDirCache g_sd_cache;
18
19void SpecDirCache::Load(bFILE *fp)
20{
21    uint16_t tfn = fp->read_uint16();
22    m_size = 0;
23    for (int i = 0; i < tfn; i++)
24    {
25        char fn[256];
26        uint8_t len;
27        fp->read(&len, 1);
28        fp->read(fn, len);
29        GetSpecDir(fn, fp);
30    }
31}
32
33void SpecDirCache::Save(bFILE *fp)
34{
35    uint16_t total = 0;
36    for (FileNode *f = m_list; f; f = f->m_next)
37        total++;
38    fp->write_uint16(total);
39    for (FileNode *f = m_list; f; f = f->m_next)
40    {
41        uint8_t len = f->m_name.Count() + 1;
42        fp->write(&len, 1);
43        fp->write(f->m_name.C(), len);
44        f->m_sd->write(fp);
45    }
46}
47
48SpecDir *SpecDirCache::GetSpecDir(String const filename, bFILE *fp)
49{
50    FileNode **parent = 0, *p = m_root;
51    while (p)
52    {
53        int cmp = strcmp(p->m_name.C(), filename.C());
54        if (cmp == 0)
55            return p->m_sd;
56        parent = cmp < 0 ? &p->m_left : &p->m_right;
57        p = *parent;
58    }
59
60    bool need_close = false;
61    if (!fp)
62    {
63        fp = open_file(filename.C(), "rb");
64        if (fp->open_failure())
65        {
66            delete fp;
67            return 0;
68        }
69        need_close = true;
70    }
71
72    FileNode *f = new FileNode(filename, new SpecDir(fp));
73    f->m_next = m_list;
74    m_list = f;
75
76    m_size += f->m_sd->m_data.Count();
77    if (parent)
78        *parent = f;
79    else
80        m_root = f;
81
82    if (need_close)
83        delete fp;
84    return f->m_sd;
85}
86
87SpecDirCache::~SpecDirCache()
88{
89    ClearNode(m_root);
90}
91
92void SpecDirCache::Clear()
93{
94    ClearNode(m_root);
95    m_root = nullptr;
96    m_size = 0;
97}
98
99void SpecDirCache::ClearNode(FileNode *f)
100{
101    if (f)
102    {
103        if (f->m_left)
104        {
105            ClearNode(f->m_left);
106            delete f->m_left;
107        }
108        if (f->m_right)
109        {
110            ClearNode(f->m_right);
111            delete f->m_right;
112        }
113    }
114}
115
Note: See TracBrowser for help on using the repository browser.