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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
(C)opyright RISKO Gergely, 2001.
This program can be licensed under the terms of
GNU Lesser General Public License v2.1 or above,
it can be found (on Debian systems) in
/usr/share/common-licenses/LGPL-2.1
This library was written for the game bombardier,
but if you are a programmer who use this library
for your own game it is welcomed. Please mail to
risko@debian.org in this case, just to put here
the name of your game and your name!
The library is dedicated to Kosa Zsuzsanna.
*/
#include "libhs.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
struct hs_table * hs_open(char *progname, char *branch, void **defaultdata, int datasize)
{
struct hs_table *table = malloc(sizeof(struct hs_table));
table->datasize=datasize;
if ((progname==NULL) || (branch==NULL))
{
free(table);
table=NULL;
}
else
{
char * filename=malloc(strlen(HS_DIRPREFIX)+1);
int justcreated=0;
strcpy(filename, HS_DIRPREFIX);
filename[strlen(HS_DIRPREFIX)]=0;
table->progname=malloc(strlen(progname)+1);
strcpy(table->progname, progname);
table->progname[strlen(progname)]=0;
table->branch=malloc(strlen(branch)+1);
strcpy(table->branch, branch);
table->branch[strlen(branch)]=0;
filename=realloc(filename, strlen(filename) + strlen(table->progname) + strlen(table->branch)+2);
strcat(filename, table->progname);
strcat(filename, ".");
strcat(filename, table->branch);
table->fd=open(filename, O_RDWR);
table->head=NULL;
if (table->fd<0)
{
justcreated=1;
umask(002);
table->fd=creat(filename, 0664);
}
if (table->fd>0)
{
if (flock(table->fd, LOCK_EX)==0)
{
/* The file opening and locking is successfull! */
if ((justcreated) && (defaultdata!=NULL))
{
void **cur;
struct hs_item *hscur;
cur=defaultdata;
table->head=malloc(sizeof(struct hs_item));
hscur=table->head;
hscur->next=NULL;
while(*cur!=NULL)
{
hscur->next=malloc(sizeof(struct hs_item));
hscur=hscur->next;
hscur->next=NULL;
hscur->data=malloc(datasize);
memcpy(hscur->data, *cur, datasize);
cur++;
}
hscur=table->head;
table->head=table->head->next;
free(hscur);
}
else
{
struct hs_item *hscur;
void *cur=malloc(datasize);
table->head=malloc(sizeof(struct hs_item));
hscur=table->head;
hscur->next=NULL;
while (read(table->fd, cur, datasize)==datasize)
{
hscur->next=malloc(sizeof(struct hs_item));
hscur=hscur->next;
hscur->next=NULL;
hscur->data=malloc(datasize);
memcpy(hscur->data, cur, datasize);
}
hscur=table->head;
table->head=table->head->next;
free(hscur);
}
}
else
{
close(table->fd);
free(table->progname);
free(table->branch);
free(table);
table=NULL;
}
}
else
{
free(table->progname);
free(table->branch);
free(table);
table=NULL;
}
free(filename);
}
return table;
}
int hs_insert(struct hs_table *table, void *data, hs_compfn compfn)
{
struct hs_item *cur, *newi;
int ret=1;
newi=malloc(sizeof(struct hs_item));
newi->data=malloc(table->datasize);
memcpy(newi->data, data, table->datasize);
if ((table->head==NULL) || (compfn(data, table->head->data)==1))
{
// The new data is greater than the first item!
newi->next=table->head;
table->head=newi;
}
else
{
int l=0;
cur=table->head;
ret++;
while ((!l) && (cur->next!=NULL))
{
if (compfn(data, cur->next->data)==1) l=1;
else
{
cur=cur->next;
ret++;
}
}
newi->next=cur->next;
cur->next=newi;
}
return ret;
}
int hs_write(struct hs_table table, int n)
{
int i=0;
int errcode=1;
struct hs_item *cur=table.head;
lseek(table.fd, 0, SEEK_SET);
while ((i<n) && (cur!=NULL))
{
if (write(table.fd, cur->data, table.datasize)==-1) errcode=0;
cur=cur->next;
i++;
}
ftruncate(table.fd, lseek(table.fd, 0, SEEK_CUR));
return errcode;
}
void hs_free(struct hs_table * table)
{
if (table!=NULL)
{
struct hs_item *cur;
struct hs_item *tofree;
flock(table->fd, LOCK_UN);
close(table->fd);
if (table->progname!=NULL) free(table->progname);
if (table->branch!=NULL) free(table->branch);
cur=table->head;
while (cur!=NULL)
{
tofree=cur;
cur=cur->next;
free(tofree->data);
free(tofree);
}
free(table);
}
}
struct hs_table * hs_readtable(char *progname, char *branch, int datasize)
{
struct hs_table *table = malloc(sizeof(struct hs_table));
table->datasize=datasize;
if ((progname==NULL) || (branch==NULL))
{
free(table);
table=NULL;
}
else
{
char * filename=malloc(strlen(HS_DIRPREFIX)+1);
strcpy(filename, HS_DIRPREFIX);
filename[strlen(HS_DIRPREFIX)]=0;
table->progname=malloc(strlen(progname)+1);
strcpy(table->progname, progname);
table->progname[strlen(progname)]=0;
table->branch=malloc(strlen(branch)+1);
strcpy(table->branch, branch);
table->branch[strlen(branch)]=0;
filename=realloc(filename, strlen(filename) + strlen(table->progname) + strlen(table->branch)+2);
strcat(filename, table->progname);
strcat(filename, ".");
strcat(filename, table->branch);
table->fd=open(filename, O_RDONLY);
if (table->fd>0)
{
if (flock(table->fd, LOCK_SH)==0)
{
/* The file opening and locking was successfull! */
struct hs_item *hscur;
void *cur=malloc(datasize);
table->head=malloc(sizeof(struct hs_item));
hscur=table->head;
hscur->next=NULL;
while (read(table->fd, cur, datasize)==datasize)
{
hscur->next=malloc(sizeof(struct hs_item));
hscur=hscur->next;
hscur->next=NULL;
hscur->data=malloc(datasize);
memcpy(hscur->data, cur, datasize);
}
hscur=table->head;
table->head=table->head->next;
free(hscur);
}
else
{
close(table->fd);
free(table->progname);
free(table->branch);
free(table);
table=NULL;
}
}
else
{
free(table->progname);
free(table->branch);
free(table);
table=NULL;
}
free(filename);
}
return table;
}
|