This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCache.cpp
121 lines (90 loc) · 2.4 KB
/
CCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* File: CCache.cpp
* Author: romanius
*
* Created on 1 Апрель 2013 г., 14:45
*/
#include "main.h"
#include "CCache.h"
CCache::CCache()
{
this->Load();
//fclose(this->f);
}
void CCache::Load()
{
if(this->f != NULL)
fclose(this->f);
bzero(&this->file_header, sizeof(this->file_header));
this->f = fopen(CACHE_FILE, "rb");
if(this->f == NULL)
{
this->cleanCache();
this->Save();
}
int readBytes = fread(&this->file_header, sizeof(this->file_header), 1, this->f);
if(readBytes != sizeof(this->file_header))
{
this->cleanCache();
this->Save();
}
if(this->file_header.header != CACHE_HEADER || this->file_header.version != CACHE_VERSION || this->file_header.os != CACHE_OS || this->file_header.number <= 0)
{
this->cleanCache();
this->Save();
}
/*if(this->file_header.number > 0)
{
this->file_data = new data[this->file_header.number];
for(int i = 0; i < this->file_header.number; i++, this->file_data++)
{
data tmp;
fread(&tmp, sizeof(tmp), 1, this->f);
this->file_data[i] = tmp;
}
}*/
fclose(this->f);
}
unsigned long CCache::Find(unsigned short mask)
{
for(int i = 0; i < this->file_header.number; i++)
if(this->file_data[i].mask == mask)
return this->file_data[i].addr;
return NULL;
}
void CCache::Add(unsigned short mask, unsigned long addr)
{
data tmp;
tmp.mask = mask;
tmp.addr = addr;
this->file_data[this->file_header.number] = tmp;
this->file_header.number++;
this->Save();
}
CCache::CCache(const CCache& orig)
{
}
CCache::~CCache()
{
fclose(this->f);
}
void CCache::cleanCache()
{
this->file_header.header = CACHE_HEADER;
this->file_header.version = CACHE_VERSION;
this->file_header.os = CACHE_OS;
this->file_header.number = 0;
}
void CCache::Save()
{
if(this->f != NULL)
fclose(this->f);
this->f= fopen(CACHE_FILE, "wb");
fwrite(&this->file_header, sizeof(this->file_header), 1, this->f);
/*if(this->file_header.number > 0)
for(int i = 0; i < this->file_header.number; i++, this->file_data++)
fwrite(&this->file_data[i], sizeof(data), 1, this->f);
*/
fflush(this->f);
fclose(this->f);
}