forked from dungeons-of-moria/icmoria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.c
167 lines (143 loc) · 4.91 KB
/
master.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* master.c */
/* routines to handle the master file, which keeps track of save files */
#include "imoria.h"
#include "master.h"
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean master_file_open(GDBM_FILE *mf)
{
/* open the master file db, returns true on success */
int trys;
*mf = NULL;
for (trys = 0 ; (trys < 10) && (*mf == NULL) ; trys++) {
priv_switch(1);
*mf = gdbm_open(MORIA_MAS, 1024, GDBM_WRCREAT, 0, 0);
priv_switch(0);
if ((*mf == NULL)) {
sleep(1);
}
}
return (*mf != NULL);
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void master_file_close(GDBM_FILE *mf)
{
/* save changes and close the master file db */
if (*mf != NULL) {
gdbm_close(*mf);
*mf = NULL;
}
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean master_file_read(GDBM_FILE mf, master_key *mkey, master_entry *mentry)
{
/* fetch a db entry, returns true if the entry was found */
boolean return_value = false;
datum key, data;
key.dptr = (char *)mkey;
key.dsize = sizeof(master_key);
if (mf != NULL) {
data = gdbm_fetch(mf, key);
if (data.dptr != NULL) {
/* Will read from data that is bigger than expected, but not smaller. */
/* If more data is added to entries without fixing the database then */
/* handle that case here! */
if (data.dsize >= sizeof(master_entry)) {
memcpy(mentry, data.dptr, sizeof(master_entry));
return_value = true;
}
free(data.dptr);
}
} else {
/* no open db file, that isn't good */
}
return return_value;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean master_file_write(GDBM_FILE mf, master_key *mkey, master_entry *mentry)
{
datum key, data;
int result;
key.dptr = (char *)mkey;
key.dsize = sizeof(master_key);
data.dptr = (char *)mentry;
data.dsize = sizeof(master_entry);
if (mf != NULL) {
mentry->updated = time(0);
result = gdbm_store(mf, key, data, GDBM_REPLACE);
}
return true;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean master_file_delete(GDBM_FILE mf, master_key *mkey)
{
datum key;
int result = -1;
key.dptr = (char *)mkey;
key.dsize = sizeof(master_key);
if (mf != NULL) {
result = gdbm_delete(mf, key); /* 0 if deleted, -1 if not found */
}
return (result == 0) ? true : false;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
int master_file_verify(GDBM_FILE mf, master_key *mkey)
{
boolean foundit;
master_entry mentry;
int return_value = MF_CHAR_NOT_FOUND;
if (mf != NULL) {
foundit = master_file_read(mf, mkey, &mentry);
if (foundit) {
if (py.misc.save_count == mentry.save_count) {
return_value = MF_CHAR_OK;
} else {
return_value = MF_CHAR_MISMATCH;
}
}
}
// if (return_value != MF_CHAR_OK) {
// msg_print("Verify mismatch.");
// msg_print(" ");
// }
return return_value;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
boolean master_file_write_new(GDBM_FILE mf, master_key *mkey,
master_entry *mentry)
{
/* insert a new key, if key already exists then try new keys for a while */
int trys;
datum key;
boolean return_value = false;
key.dptr = (char *)mkey;
key.dsize = sizeof(master_key);
if (mf != NULL) {
for (trys = 0 ; (trys < 500) && !return_value ; trys++) {
if (!gdbm_exists(mf, key)) {
master_file_write(mf, mkey, mentry);
return_value = true;
} else {
mkey->creation_time += randint(40);
}
}
}
return return_value;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/* END FILE master.c */