2
|
1 /*
|
|
2 * db.c
|
|
3 */
|
|
4
|
|
5
|
|
6 #include <stdio.h>
|
|
7 #include <stdlib.h>
|
|
8 #include <malloc.h>
|
|
9 #include <struct.h>
|
|
10 #include "common.h"
|
|
11 #include "db.h"
|
|
12 #include "main.h"
|
|
13 #include "data.h"
|
|
14
|
|
15
|
|
16 void initDB()
|
|
17 {
|
|
18 struct plnode *p, *pn;
|
|
19 int i;
|
|
20
|
|
21 /* deallocate previous db */
|
|
22 p = firstEnt;
|
|
23 if(numDBentries) {
|
|
24 while(numDBentries) {
|
|
25 pn = p->next;
|
|
26
|
|
27 if(!pn) err_fatal("initDB: numDBentries too large!");
|
|
28
|
|
29 free(p);
|
|
30 p = pn;
|
|
31 numDBentries--;
|
|
32 }
|
|
33 if(!p) err_fatal("initDB: numDBentries too small!");
|
|
34 }
|
|
35
|
|
36 firstEnt = 0;
|
|
37 lastEnt = 0;
|
|
38 numDBentries = 0;
|
|
39 dbDirty = 0;
|
|
40 }
|
|
41
|
|
42
|
|
43 void addDB(struct statentry *player)
|
|
44 {
|
|
45 struct plnode *p;
|
|
46
|
|
47 p = (struct plnode *)malloc(sizeof(struct plnode));
|
|
48
|
|
49 if(p == NULL)
|
|
50 err_sys_fatal("addDB: malloc error");
|
|
51
|
|
52 p->player = *player;
|
|
53 p->next = 0;
|
|
54 p->status = 0;
|
|
55
|
|
56 if(!firstEnt) {
|
|
57 p->prev = 0;
|
|
58 firstEnt = p;
|
|
59 lastEnt = p;
|
|
60 } else {
|
|
61 p->prev = lastEnt;
|
|
62 lastEnt->next = p;
|
|
63 lastEnt = p;
|
|
64 }
|
|
65 numDBentries++;
|
|
66 }
|
|
67
|
|
68 struct plnode *GetNode(int n)
|
|
69 {
|
|
70 struct plnode *p, *pn;
|
|
71 int i = 0;
|
|
72
|
|
73 p = firstEnt;
|
|
74
|
|
75 while(p) {
|
|
76 if(i++ == n) return(p);
|
|
77 pn = p->next;
|
|
78 p = pn;
|
|
79 }
|
|
80 return(NULL);
|
|
81 }
|
|
82
|
|
83 void DeleteNode(int n)
|
|
84 {
|
|
85 struct plnode *p, *pn, *pp;
|
|
86 int i;
|
|
87
|
|
88 if(!numDBentries) return;
|
|
89
|
|
90 p = firstEnt;
|
|
91 if(!p) return;
|
|
92
|
|
93 for(i = 0; i < n; i++) {
|
|
94 p = p->next;
|
|
95 if(!p) return;
|
|
96 }
|
|
97 pn = p->next;
|
|
98 pp = p->prev;
|
|
99 if(pn) pn->prev = pp;
|
|
100 if(pp) pp->next = pn;
|
|
101
|
|
102 if(!n) firstEnt = pn;
|
|
103 numDBentries--;
|
|
104
|
|
105 dbDirty = 1;
|
|
106
|
|
107 free(p);
|
|
108 }
|
|
109
|
|
110 int GetByName(char *name)
|
|
111 {
|
|
112 struct plnode *p;
|
|
113 int i;
|
|
114
|
|
115 if(!numDBentries) return -1;
|
|
116
|
|
117 p = firstEnt;
|
|
118 if(!p) return -1;
|
|
119
|
|
120 for(i = 0; p; i++) {
|
|
121 if(!strncmp(name, p->player.name, 16))
|
|
122 return i;
|
|
123 p = p->next;
|
|
124 }
|
|
125 return -1;
|
|
126 }
|
|
127
|