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 libraries 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) {
61 ::_Tt_client_isam_file (const _Tt_string &file,
62 int max_record_length,
63 int min_record_length,
64 _Tt_client_isam_key_descriptor_ptr primary_key_descriptor,
66 const _Tt_db_client_ptr &db_connection)
68 dbConnection = db_connection;
70 setTtISAMFileDefaults();
74 // Because a old TT client may be talking with the old DB, make sure
75 // files are opened with a manual lock, not an exclusive lock.
77 dbConnection->isopen(file, mode&(~ISEXCLLOCK)|ISMANULOCK)) == -1) {
79 dbConnection->isreclen = min_record_length;
80 fileDescriptor = dbConnection->isbuild(file,
81 (max_record_length > TT_OLD_MAX_RECORD_LENGTH ?
82 TT_OLD_MAX_RECORD_LENGTH : max_record_length),
83 primary_key_descriptor->getKeyDescriptor(),
87 if (fileDescriptor != -1) {
88 currentRecordLength = 0;
89 currentRecordNumber = 0;
93 errorStatus = dbConnection->iserrno;
94 currentRecordLength = -1;
95 currentRecordNumber = -1;
101 void _Tt_client_isam_file::setTtISAMFileDefaults ()
104 fileName = (char *)NULL;
106 keyDescriptorList = new _Tt_client_isam_key_descriptor_list;
109 dbConnection->iserrno = 0;
112 _Tt_client_isam_file::~_Tt_client_isam_file ()
115 (void)dbConnection->iserase(fileName);
118 (void)dbConnection->isclose(fileDescriptor);
122 void _Tt_client_isam_file::setErase (bool_t flag)
127 int _Tt_client_isam_file::addIndex (_Tt_client_isam_key_descriptor_ptr key_descriptor)
129 dbConnection->iserrno = 0;
131 int results = dbConnection->isaddindex(fileDescriptor, key_descriptor->getKeyDescriptor());
133 errorStatus = dbConnection->iserrno;
141 _Tt_client_isam_record_ptr _Tt_client_isam_file::getEmptyRecord ()
143 return (new _Tt_client_isam_record(keyDescriptorList,
148 int _Tt_client_isam_file
149 ::findStartRecord (const _Tt_client_isam_key_descriptor_ptr &key_descriptor,
151 const _Tt_client_isam_record_ptr &record,
154 dbConnection->iserrno = 0;
156 dbConnection->isreclen = record->getLength();
157 (void)dbConnection->isstart(fileDescriptor,
158 key_descriptor->getKeyDescriptor(),
160 (char *)record->getRecord(),
167 _Tt_client_isam_record_ptr _Tt_client_isam_file::readRecord (int mode)
169 dbConnection->iserrno = 0;
171 _Tt_client_isam_record_ptr record = (_Tt_client_isam_record *)NULL;
172 _Tt_string record_buffer(maxRecordLength);
173 dbConnection->isreclen = maxRecordLength;
174 int results = dbConnection->isread(fileDescriptor,
175 (char *)record_buffer,
180 record = getFullRecord(record_buffer);
186 int _Tt_client_isam_file::readRecord (int mode,
187 const _Tt_client_isam_record_ptr &record)
189 dbConnection->iserrno = 0;
191 dbConnection->isreclen = record->getLength();
192 int results = dbConnection->isread(fileDescriptor, (char *)record->getRecord(), mode);
196 record->setLength(currentRecordLength);
203 _Tt_client_isam_file::updateRecord (long recnum, const _Tt_client_isam_record_ptr &record)
205 dbConnection->iserrno = 0;
207 dbConnection->isreclen = record->getLength();
208 (void)dbConnection->isrewrec(fileDescriptor, recnum, (char *)record->getRecord());
214 int _Tt_client_isam_file::writeRecord (const _Tt_client_isam_record_ptr &record)
216 dbConnection->iserrno = 0;
218 dbConnection->isreclen = record->getLength();
219 (void)dbConnection->iswrite(fileDescriptor, (char *)record->getRecord());
226 _Tt_client_isam_file::deleteRecord (long recnum,
227 const _Tt_client_isam_record_ptr &record)
229 dbConnection->iserrno = 0;
231 dbConnection->isreclen = record->getLength();
232 (void)dbConnection->isdelrec(fileDescriptor,
234 (char *)record->getRecord());
240 int _Tt_client_isam_file::writeMagicString (const _Tt_string &magic_string)
242 dbConnection->iserrno = 0;
244 (void)dbConnection->iscntl(fileDescriptor,
245 ISCNTL_APPLMAGIC_WRITE,
246 (char *)magic_string);
248 errorStatus = dbConnection->iserrno;
252 _Tt_string _Tt_client_isam_file::readMagicString ()
254 dbConnection->iserrno = 0;
256 _Tt_string magic_string_bytes(ISAPPLMAGICLEN);
257 memset((char *)magic_string_bytes, '\0', ISAPPLMAGICLEN);
259 (void)dbConnection->iscntl(fileDescriptor,
260 ISCNTL_APPLMAGIC_READ,
261 (char *)magic_string_bytes);
263 errorStatus = dbConnection->iserrno;
265 _Tt_string magic_string = (char *)magic_string_bytes;
269 _Tt_client_isam_record_ptr
270 _Tt_client_isam_file::getFullRecord (const _Tt_string &record_buffer)
272 _Tt_client_isam_record_ptr record_ptr = new _Tt_client_isam_record(keyDescriptorList,
275 record_ptr->setBytes(0, currentRecordLength, record_buffer);
276 record_ptr->setLength(currentRecordLength);
281 void _Tt_client_isam_file::getISAMFileInfo ()
283 keyDescriptorList->flush();
285 if (!strcmp(TT_OLD_DB_PROPERTY_TABLE_FILE,
288 strlen(TT_OLD_DB_PROPERTY_TABLE_FILE))) {
289 maxRecordLength = TT_OLD_MAX_RECORD_LENGTH;
290 minRecordLength = TT_OLD_DB_KEY_LENGTH+TT_OLD_DB_MAX_PROPERTY_NAME_LENGTH;
292 _Tt_client_isam_key_descriptor_ptr key_descriptor =
293 new _Tt_client_isam_key_descriptor;
295 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
296 TT_OLD_DB_KEY_LENGTH,
298 key_descriptor->addKeyPart(TT_OLD_DB_PROPERTY_NAME_OFFSET,
299 TT_OLD_DB_MAX_PROPERTY_NAME_LENGTH,
301 key_descriptor->setDuplicates(TRUE);
303 keyDescriptorList->append(key_descriptor);
305 else if (!strcmp(TT_OLD_DB_ACCESS_TABLE_FILE,
308 strlen(TT_OLD_DB_ACCESS_TABLE_FILE))) {
309 maxRecordLength = minRecordLength = TT_OLD_DB_KEY_LENGTH+
310 3*TT_OLD_DB_SHORT_SIZE;
312 _Tt_client_isam_key_descriptor_ptr key_descriptor =
313 new _Tt_client_isam_key_descriptor;
315 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
316 TT_OLD_DB_KEY_LENGTH,
319 keyDescriptorList->append(key_descriptor);
321 else if (!strcmp(TT_OLD_DB_FILE_TABLE_FILE,
324 strlen(TT_OLD_DB_FILE_TABLE_FILE))) {
325 maxRecordLength = TT_OLD_DB_KEY_LENGTH+MAXPATHLEN;
326 minRecordLength = TT_OLD_DB_KEY_LENGTH+TT_OLD_DB_MAX_KEY_LENGTH;
328 _Tt_client_isam_key_descriptor_ptr key_descriptor =
329 new _Tt_client_isam_key_descriptor;
330 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
331 TT_OLD_DB_KEY_LENGTH,
333 keyDescriptorList->append(key_descriptor);
335 key_descriptor = new _Tt_client_isam_key_descriptor;
336 key_descriptor->addKeyPart(TT_OLD_DB_FILE_PATH_OFFSET,
337 TT_OLD_DB_MAX_KEY_LENGTH,
339 keyDescriptorList->append(key_descriptor);
341 // TT_OLD_DB_FILE_OBJECT_MAP_FILE
343 maxRecordLength = minRecordLength = 2*TT_OLD_DB_KEY_LENGTH;
345 _Tt_client_isam_key_descriptor_ptr key_descriptor =
346 new _Tt_client_isam_key_descriptor;
347 key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
348 TT_OLD_DB_KEY_LENGTH,
350 keyDescriptorList->append(key_descriptor);
352 key_descriptor = new _Tt_client_isam_key_descriptor;
353 key_descriptor->addKeyPart(TT_OLD_DB_SECOND_KEY_OFFSET,
354 TT_OLD_DB_KEY_LENGTH,
356 key_descriptor->setDuplicates(TRUE);
357 keyDescriptorList->append(key_descriptor);
360 errorStatus = dbConnection->iserrno = 0;