2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $XConsortium: tt_client_isam_file.C /main/3 1995/10/23 09:59:12 rswiston $
29 * tt_client_isam_file.cc - Defines the TT ISAM file class. This class
30 * simplifies opening, closing, reading and writing an ISAM file.
32 * Copyright (c) 1992 by Sun Microsystems, Inc.
35 #include "db/tt_client_isam.h"
36 #include "db/tt_client_isam_file.h"
37 #include "db/tt_old_db_consts.h"
39 _Tt_client_isam_file::_Tt_client_isam_file (const _Tt_string &file,
41 const _Tt_db_client_ptr &db_connection)
43 dbConnection = db_connection;
45 setTtISAMFileDefaults();
49 if ((fileDescriptor = dbConnection->isopen(file, mode)) != -1) {
58 ::_Tt_client_isam_file (const _Tt_string &file,
59 int max_record_length,
60 int min_record_length,
61 _Tt_client_isam_key_descriptor_ptr primary_key_descriptor,
63 const _Tt_db_client_ptr &db_connection)
65 dbConnection = db_connection;
67 setTtISAMFileDefaults();
71 // Because a old TT client may be talking with the old DB, make sure
72 // files are opened with a manual lock, not an exclusive lock.
74 dbConnection->isopen(file, mode&(~ISEXCLLOCK)|ISMANULOCK)) == -1) {
76 dbConnection->isreclen = min_record_length;
77 fileDescriptor = dbConnection->isbuild(file,
78 (max_record_length > TT_OLD_MAX_RECORD_LENGTH ?
79 TT_OLD_MAX_RECORD_LENGTH : max_record_length),
80 primary_key_descriptor->getKeyDescriptor(),
84 if (fileDescriptor != -1) {
88 errorStatus = dbConnection->iserrno;
89 currentRecordLength = -1;
90 currentRecordNumber = -1;
94 void _Tt_client_isam_file::setTtISAMFileDefaults ()
97 fileName = (char *)NULL;
99 keyDescriptorList = new _Tt_client_isam_key_descriptor_list;
102 dbConnection->iserrno = 0;
105 _Tt_client_isam_file::~_Tt_client_isam_file ()
108 (void)dbConnection->iserase(fileName);
111 (void)dbConnection->isclose(fileDescriptor);
115 void _Tt_client_isam_file::setErase (bool_t flag)
120 int _Tt_client_isam_file::addIndex (_Tt_client_isam_key_descriptor_ptr key_descriptor)
122 dbConnection->iserrno = 0;
124 int results = dbConnection->isaddindex(fileDescriptor, key_descriptor->getKeyDescriptor());
126 errorStatus = dbConnection->iserrno;
134 _Tt_client_isam_record_ptr _Tt_client_isam_file::getEmptyRecord ()
136 return (new _Tt_client_isam_record(keyDescriptorList,
141 int _Tt_client_isam_file
142 ::findStartRecord (const _Tt_client_isam_key_descriptor_ptr &key_descriptor,
144 const _Tt_client_isam_record_ptr &record,
147 dbConnection->iserrno = 0;
149 dbConnection->isreclen = record->getLength();
150 (void)dbConnection->isstart(fileDescriptor,
151 key_descriptor->getKeyDescriptor(),
153 (char *)record->getRecord(),
160 _Tt_client_isam_record_ptr _Tt_client_isam_file::readRecord (int mode)
162 dbConnection->iserrno = 0;
164 _Tt_client_isam_record_ptr record = (_Tt_client_isam_record *)NULL;
165 _Tt_string record_buffer(maxRecordLength);
166 dbConnection->isreclen = maxRecordLength;
167 int results = dbConnection->isread(fileDescriptor,
168 (char *)record_buffer,
173 record = getFullRecord(record_buffer);
179 int _Tt_client_isam_file::readRecord (int mode,
180 const _Tt_client_isam_record_ptr &record)
182 dbConnection->iserrno = 0;
184 dbConnection->isreclen = record->getLength();
185 int results = dbConnection->isread(fileDescriptor, (char *)record->getRecord(), mode);
189 record->setLength(currentRecordLength);
196 _Tt_client_isam_file::updateRecord (long recnum, const _Tt_client_isam_record_ptr &record)
198 dbConnection->iserrno = 0;
200 dbConnection->isreclen = record->getLength();
201 (void)dbConnection->isrewrec(fileDescriptor, recnum, (char *)record->getRecord());
207 int _Tt_client_isam_file::writeRecord (const _Tt_client_isam_record_ptr &record)
209 dbConnection->iserrno = 0;
211 dbConnection->isreclen = record->getLength();
212 (void)dbConnection->iswrite(fileDescriptor, (char *)record->getRecord());
219 _Tt_client_isam_file::deleteRecord (long recnum,
220 const _Tt_client_isam_record_ptr &record)
222 dbConnection->iserrno = 0;
224 dbConnection->isreclen = record->getLength();
225 (void)dbConnection->isdelrec(fileDescriptor,
227 (char *)record->getRecord());
233 int _Tt_client_isam_file::writeMagicString (const _Tt_string &magic_string)
235 dbConnection->iserrno = 0;
237 (void)dbConnection->iscntl(fileDescriptor,
238 ISCNTL_APPLMAGIC_WRITE,
239 (char *)magic_string);
241 errorStatus = dbConnection->iserrno;
245 _Tt_string _Tt_client_isam_file::readMagicString ()
247 dbConnection->iserrno = 0;
249 _Tt_string magic_string_bytes(ISAPPLMAGICLEN);
250 memset((char *)magic_string_bytes, '\0', ISAPPLMAGICLEN);
252 (void)dbConnection->iscntl(fileDescriptor,
253 ISCNTL_APPLMAGIC_READ,
254 (char *)magic_string_bytes);
256 errorStatus = dbConnection->iserrno;
258 _Tt_string magic_string = (char *)magic_string_bytes;
262 _Tt_client_isam_record_ptr
263 _Tt_client_isam_file::getFullRecord (const _Tt_string &record_buffer)
265 _Tt_client_isam_record_ptr record_ptr = new _Tt_client_isam_record(keyDescriptorList,
268 record_ptr->setBytes(0, currentRecordLength, record_buffer);
269 record_ptr->setLength(currentRecordLength);
274 void _Tt_client_isam_file::getISAMFileInfo ()
276 keyDescriptorList->flush();
278 if (!strcmp(TT_OLD_DB_PROPERTY_TABLE_FILE,
281 strlen(TT_OLD_DB_PROPERTY_TABLE_FILE))) {
282 maxRecordLength = TT_OLD_MAX_RECORD_LENGTH;
283 minRecordLength = TT_OLD_DB_KEY_LENGTH+TT_OLD_DB_MAX_PROPERTY_NAME_LENGTH;
285 _Tt_client_isam_key_descriptor_ptr key_descriptor =
286 new _Tt_client_isam_key_descriptor;
288 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
289 TT_OLD_DB_KEY_LENGTH,
291 key_descriptor->addKeyPart(TT_OLD_DB_PROPERTY_NAME_OFFSET,
292 TT_OLD_DB_MAX_PROPERTY_NAME_LENGTH,
294 key_descriptor->setDuplicates(TRUE);
296 keyDescriptorList->append(key_descriptor);
298 else if (!strcmp(TT_OLD_DB_ACCESS_TABLE_FILE,
301 strlen(TT_OLD_DB_ACCESS_TABLE_FILE))) {
302 maxRecordLength = minRecordLength = TT_OLD_DB_KEY_LENGTH+
303 3*TT_OLD_DB_SHORT_SIZE;
305 _Tt_client_isam_key_descriptor_ptr key_descriptor =
306 new _Tt_client_isam_key_descriptor;
308 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
309 TT_OLD_DB_KEY_LENGTH,
312 keyDescriptorList->append(key_descriptor);
314 else if (!strcmp(TT_OLD_DB_FILE_TABLE_FILE,
317 strlen(TT_OLD_DB_FILE_TABLE_FILE))) {
318 maxRecordLength = TT_OLD_DB_KEY_LENGTH+MAXPATHLEN;
319 minRecordLength = TT_OLD_DB_KEY_LENGTH+TT_OLD_DB_MAX_KEY_LENGTH;
321 _Tt_client_isam_key_descriptor_ptr key_descriptor =
322 new _Tt_client_isam_key_descriptor;
323 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
324 TT_OLD_DB_KEY_LENGTH,
326 keyDescriptorList->append(key_descriptor);
328 key_descriptor = new _Tt_client_isam_key_descriptor;
329 key_descriptor->addKeyPart(TT_OLD_DB_FILE_PATH_OFFSET,
330 TT_OLD_DB_MAX_KEY_LENGTH,
332 keyDescriptorList->append(key_descriptor);
334 // TT_OLD_DB_FILE_OBJECT_MAP_FILE
336 maxRecordLength = minRecordLength = 2*TT_OLD_DB_KEY_LENGTH;
338 _Tt_client_isam_key_descriptor_ptr key_descriptor =
339 new _Tt_client_isam_key_descriptor;
340 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
341 TT_OLD_DB_KEY_LENGTH,
343 keyDescriptorList->append(key_descriptor);
345 key_descriptor = new _Tt_client_isam_key_descriptor;
346 key_descriptor->addKeyPart(TT_OLD_DB_SECOND_KEY_OFFSET,
347 TT_OLD_DB_KEY_LENGTH,
349 key_descriptor->setDuplicates(TRUE);
350 keyDescriptorList->append(key_descriptor);
353 errorStatus = dbConnection->iserrno = 0;