-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdb.cpp
143 lines (109 loc) · 3.97 KB
/
Odb.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//Filename : ODB.DBF
//Description : Object Database handling, it read DBF files
#include <string.h>
#include <ALL.H>
#include <ODB.H>
//-------- Begin of function Database constructor ------//
//!
//! [char*] fileName = the name of the DBF file to be opened
//! [int] bufferAll = read the whole database into memory or not
//!
Database::Database(char* dbFileName, int bufferAll) {
dbf_buf = NULL;
rec_buf = NULL;
last_read_recno = -1;
if( dbFileName )
open( dbFileName, bufferAll );
}
//----------- End of function Database constructor ------//
//-------- Begin of function Database destructor ------//
Database::~Database() {
Database::close();
}
//----------- End of function Database destructor ------//
//-------- Begin of function Database::open --------//
//!
//! Database::open( char* fileName )
//!
//! <char*> fileName = the name of the DBF file to be opened
//! [int] bufferAll = read the whole database into memory or not
//! (default : 0)
//!
//! return 1 : opened successfully
//! 0 : opening failure
//!
void Database::open( char* fileName, int bufferAll ) {
close(); // if there is a opened file attached to current database, close it first
file_open(fileName);
file_read( &dbf_header, sizeof(DbfHeader) );
//..........................................//
if( bufferAll ) { // read the whole database into memory or not
dbf_buf = mem_add( dbf_header.rec_size * dbf_header.last_rec );
file_seek( 1 + dbf_header.data_offset );
file_read( dbf_buf, dbf_header.rec_size*dbf_header.last_rec );
file_close();
dbf_buf_allocated = 1; // we allocated the buffer
}
else
rec_buf = mem_add( dbf_header.rec_size );
cur_recno = 1;
}
//--------- End of function Database::open ---------//
//-------- Begin of function Database::open_from_buf --------//
//!
//! Open the database from an buffer with the database pre-read in
//!
//! <char*> dataPtr = the pointer to the pre-read database in the memory
//!
void Database::open_from_buf(char* dataPtr) {
close(); // if there is a open_from_bufed file attached to current database, close it first
//------- set data pointers ----------//
memcpy( &dbf_header, dataPtr, sizeof(DbfHeader) );
dbf_buf = dataPtr + 1 + dbf_header.data_offset;
dbf_buf_allocated = 0; // we didn't allocate the buffer, so don't bother to free it in deinit()
cur_recno = 1;
}
//--------- End of function Database::open_from_buf ---------//
//--------- Begin of function Database::read --------//
//!
//! Database::read( long recNum )
//!
//! [recNum] = the record number of the record to be read
//! (default : current record no.)
//!
//! return : <char*> success, the pointer to the reading buffer
//! <NULL> fail
//!
char* Database::read( long recNo ) {
if( recNo <= 0 )
recNo = cur_recno;
if( recNo < 1 || recNo > dbf_header.last_rec )
return NULL;
if( dbf_buf ) { // the whole database has been read into memory
return dbf_buf + dbf_header.rec_size * (recNo-1);
}
else { // only a portion of the database is read into the memory at a time
if( recNo == last_read_recno ) // the record to be read is the same as one in buffer, simply return it
return rec_buf;
file_seek( 1+dbf_header.data_offset + dbf_header.rec_size * (recNo-1) );
file_read( rec_buf, dbf_header.rec_size );
last_read_recno = recNo;
return rec_buf;
}
}
//----------- End of function Database::read ---------//
//---------- Begin of function Database::close -------//
//!
//! Database::close()
//!
void Database::close() {
if( rec_buf ) {
mem_del(rec_buf);
rec_buf = NULL;
}
if( dbf_buf && dbf_buf_allocated ) {
mem_del( dbf_buf );
dbf_buf = NULL;
}
}
//----------- End of function Database::close ----------//