Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

ODB.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ODB.CPP

Go to the documentation of this file.
00001 //Filename    : ODB.DBF
00002 //Description : Object Database handling, it read DBF files
00003 
00004 #include <string.h>
00005 
00006 #include <ALL.H>
00007 #include <ODB.H>
00008 
00009 //-------- Begin of function Database constructor ------//
00014 Database::Database(char* dbFileName, int bufferAll) {
00015     dbf_buf  = NULL;
00016     rec_buf  = NULL;
00017 
00018     last_read_recno = -1;
00019 
00020     if( dbFileName )
00021         open( dbFileName, bufferAll );
00022 }
00023 
00024 //----------- End of function Database constructor ------//
00025 
00026 //-------- Begin of function Database destructor ------//
00027 
00028 Database::~Database() {
00029     Database::close();
00030 }
00031 
00032 //----------- End of function Database destructor ------//
00033 
00034 //-------- Begin of function Database::open --------//
00045 void Database::open( char* fileName, int bufferAll ) {
00046     close();                                        // if there is a opened file attached to current database, close it first
00047 
00048     file_open(fileName);
00049     file_read( &dbf_header, sizeof(DbfHeader) );
00050 
00051     //..........................................//
00052 
00053     if( bufferAll ) {                               // read the whole database into memory or not
00054         dbf_buf = mem_add( dbf_header.rec_size * dbf_header.last_rec );
00055 
00056         file_seek( 1 + dbf_header.data_offset );
00057         file_read( dbf_buf, dbf_header.rec_size*dbf_header.last_rec );
00058         file_close();
00059 
00060         dbf_buf_allocated = 1;                        // we allocated the buffer
00061     }
00062     else
00063         rec_buf = mem_add( dbf_header.rec_size );
00064 
00065     cur_recno = 1;
00066 }
00067 
00068 //--------- End of function Database::open ---------//
00069 
00070 //-------- Begin of function Database::open_from_buf --------//
00076 void Database::open_from_buf(char* dataPtr) {
00077     close();                                        // if there is a open_from_bufed file attached to current database, close it first
00078 
00079     //------- set data pointers ----------//
00080 
00081     memcpy( &dbf_header, dataPtr, sizeof(DbfHeader) );
00082 
00083     dbf_buf = dataPtr + 1 + dbf_header.data_offset;
00084 
00085     dbf_buf_allocated = 0;                          // we didn't allocate the buffer, so don't bother to free it in deinit()
00086 
00087     cur_recno = 1;
00088 }
00089 
00090 //--------- End of function Database::open_from_buf ---------//
00091 
00092 //--------- Begin of function Database::read --------//
00102 char* Database::read( long recNo ) {
00103     if( recNo <= 0 )
00104         recNo = cur_recno;
00105 
00106     if( recNo < 1 || recNo > dbf_header.last_rec )
00107         return NULL;
00108 
00109     if( dbf_buf ) {                                 // the whole database has been read into memory
00110         return dbf_buf + dbf_header.rec_size * (recNo-1);
00111     }
00112     else {                                          // only a portion of the database is read into the memory at a time
00113         if( recNo == last_read_recno )                // the record to be read is the same as one in buffer, simply return it
00114             return rec_buf;
00115 
00116         file_seek( 1+dbf_header.data_offset + dbf_header.rec_size * (recNo-1) );
00117         file_read( rec_buf, dbf_header.rec_size );
00118 
00119         last_read_recno = recNo;
00120 
00121         return rec_buf;
00122     }
00123 }
00124 
00125 //----------- End of function Database::read ---------//
00126 
00127 //---------- Begin of function Database::close -------//
00131 void Database::close() {
00132     if( rec_buf ) {
00133         mem_del(rec_buf);
00134         rec_buf = NULL;
00135     }
00136 
00137     if( dbf_buf && dbf_buf_allocated ) {
00138         mem_del( dbf_buf );
00139         dbf_buf = NULL;
00140     }
00141 }
00142 
00143 //----------- End of function Database::close ----------//

Generated on Fri Aug 23 01:37:20 2002 for VirtualU by doxygen1.2.17