-
Notifications
You must be signed in to change notification settings - Fork 6
/
dbaseutils.cpp
125 lines (108 loc) · 2.95 KB
/
dbaseutils.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
121
122
123
124
125
#include <stdio.h>
#include "basetypes.h"
#include "error.h"
#include "messages.h"
DWORD Fsize(const char *s)
{
WCFILE *f;
DWORD r;
if((f = wcfopen(s, FILE_ACCESS_MODES_R)) == NULL) {
printhtmlerrorat(LOG_GETFILESIZEFAILED, s);
}
if(wcfseek(f, 0, SEEK_END) != 0) {
printhtmlerrorat(LOG_GETFILESIZEFAILED, s);
}
r = wcftell(f);
wcfclose(f);
return r;
}
int ReadDBMessage(DWORD midx, SMessage *mes)
{
WCFILE *f;
if(midx == NO_MESSAGE_CODE) return 0;
if((f = wcfopen(F_MSGINDEX, FILE_ACCESS_MODES_R)) == NULL) return 0;
if(wcfseek(f, midx, SEEK_SET) != 0)
{
wcfclose(f);
return 0;
}
if(!fCheckedRead(mes, sizeof(SMessage), f))
{
wcfclose(f);
return 0;
}
wcfclose(f);
return 1;
}
int ReadDBMessageBody(char *buf, DWORD index, DWORD size)
{
FILE *f;
if((f = fopen(F_MSGBODY, FILE_ACCESS_MODES_R)) == NULL)return 0;
if(wcfseek(f, index, SEEK_SET) != 0) return 0;
if(fread(buf, 1, size, f) != size) return 0;
fclose(f);
return 1;
}
int WriteDBMessage(DWORD midx, SMessage *mes)
{
WCFILE *f;
if((f = wcfopen(F_MSGINDEX, FILE_ACCESS_MODES_RW)) == NULL) return 0;
lock_file(f);
if(wcfseek(f, midx, SEEK_SET) != 0)
{
unlock_file(f);
wcfclose(f);
return 0;
}
if(!fCheckedWrite(mes, sizeof(SMessage), f))
{
unlock_file(f);
wcfclose(f);
return 0;
}
unlock_file(f);
wcfclose(f);
return 1;
}
int WriteDBMessageBody(char *buf, DWORD index, DWORD size)
{
FILE *f;
if((f = fopen(F_MSGBODY, FILE_ACCESS_MODES_RW)) == NULL)return 0;
if(wcfseek(f, index, SEEK_SET) != 0) return 0;
if(fwrite(buf, 1, size, f) != size) return 0;
fclose(f);
return 1;
}
DWORD VIndexCountInDB()
{
DWORD fsize;
fsize = Fsize(F_VINDEX);
if (fsize > 0)
return (fsize - 3) / sizeof(DWORD);
else
return 0;
}
DWORD TranslateMsgIndexDel(DWORD root)
{
DWORD i;
size_t n;
FILE *f;
if (root == 0)
return NO_MESSAGE_CODE;
if ( (f = wcfopen(F_VINDEX, FILE_ACCESS_MODES_R)) == NULL)
printhtmlerror();
if (wcfseek(f, root*sizeof(DWORD), SEEK_SET))
printhtmlerror();
n = fread(&i, 1, sizeof(i), f);
if (ferror(f))
printhtmlerror();
if (n != sizeof(i)) {
if (n == 0 && feof(f))
i = NO_MESSAGE_CODE;
else
printhtmlerror();
} else if (i == NO_MESSAGE_CODE)
i = 0;
wcfclose(f);
return i;
}