-
Notifications
You must be signed in to change notification settings - Fork 2
/
Database.cc
60 lines (50 loc) · 1.15 KB
/
Database.cc
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
#include "Database.h"
Dbase_Ctrl_Blk::Dbase_Ctrl_Blk(char *infile, int buf_sz)
{
fd = open (infile, O_RDONLY);
if (fd < 0){
printf("ERROR: InvalidFile -- Dbase_Ctrl_Blk()\n");
exit(-1);
}
buf_size = buf_sz;
buf = new int [buf_sz];
cur_buf_pos = 0;
cur_blk_size = 0;
}
Dbase_Ctrl_Blk::~Dbase_Ctrl_Blk()
{
delete [] buf;
close(fd);
}
void Dbase_Ctrl_Blk::get_next_trans_ext(int &numitem, int &tid, int &custid)
{
// Need to get more items from file
int res = cur_blk_size - cur_buf_pos+3;
if (res > 0)
{
// First copy partial transaction to beginning of buffer
memcpy((void *)buf,
(void *)(buf + cur_buf_pos - 3),
res * ITSZ);
cur_blk_size = res;
}
else
{
// No partial transaction in buffer
cur_blk_size = 0;
}
res = read(fd, (void *)(buf + cur_blk_size),
((buf_size - cur_blk_size)*ITSZ));
if (res < 0){
perror("reading in database");
exit(errno);
}
cur_blk_size += res/ITSZ;
if (cur_blk_size > 0)
{
custid = buf[0];
tid = buf[1];
numitem = buf[2];
cur_buf_pos = 3;
}
}