Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / lib / tt / lib / db / tt_client_isam_file.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
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 $                                                        
28 /*
29  * tt_client_isam_file.cc - Defines the TT ISAM file class.  This class
30  *      simplifies opening, closing, reading and writing an ISAM file.
31  *
32  * Copyright (c) 1992 by Sun Microsystems, Inc.
33  */
34
35 #include "db/tt_client_isam.h"
36 #include "db/tt_client_isam_file.h"
37 #include "db/tt_old_db_consts.h"
38
39 _Tt_client_isam_file::_Tt_client_isam_file (const _Tt_string        &file,
40                               int                      mode,
41                               const _Tt_db_client_ptr &db_connection)
42 {
43   dbConnection = db_connection;
44
45   setTtISAMFileDefaults();
46   fileName = file;
47   fileMode = mode;
48
49   if ((fileDescriptor = dbConnection->isopen(file, mode)) != -1) {
50     getISAMFileInfo();
51   }
52   else {
53     getStatusInfo();
54   }
55
56   maxRecordLength = 0;
57   minRecordLength = 0;
58 }
59
60 _Tt_client_isam_file
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,
65                  int                          mode,
66                  const _Tt_db_client_ptr     &db_connection)
67 {
68   dbConnection = db_connection;
69
70   setTtISAMFileDefaults();
71   fileName = file;
72   fileMode = mode;
73
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.
76   if ((fileDescriptor =
77        dbConnection->isopen(file, mode&(~ISEXCLLOCK)|ISMANULOCK)) == -1) {
78     newFlag = TRUE;
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(),
84                                            mode);
85   }
86   
87   if (fileDescriptor != -1) {
88     currentRecordLength = 0;
89     currentRecordNumber = 0;
90     getISAMFileInfo();
91   }
92   else {
93     errorStatus = dbConnection->iserrno;
94     currentRecordLength = -1;
95     currentRecordNumber = -1;
96   }
97   maxRecordLength = 0;
98   minRecordLength = 0;
99 }
100
101 void _Tt_client_isam_file::setTtISAMFileDefaults ()
102 {
103   eraseFlag = FALSE;
104   fileName = (char *)NULL;
105   fileMode = 0;
106   keyDescriptorList = new _Tt_client_isam_key_descriptor_list;
107   newFlag = FALSE;
108
109   dbConnection->iserrno = 0;
110 }
111
112 _Tt_client_isam_file::~_Tt_client_isam_file ()
113 {
114   if (eraseFlag) {
115     (void)dbConnection->iserase(fileName);
116   }
117   else {
118     (void)dbConnection->isclose(fileDescriptor);
119   }
120 }
121
122 void _Tt_client_isam_file::setErase (bool_t flag)
123 {
124   eraseFlag = flag;
125 }
126
127 int _Tt_client_isam_file::addIndex (_Tt_client_isam_key_descriptor_ptr key_descriptor)
128 {
129   dbConnection->iserrno = 0;
130
131   int results = dbConnection->isaddindex(fileDescriptor, key_descriptor->getKeyDescriptor());
132
133   errorStatus = dbConnection->iserrno;
134   if (!results) {
135     getISAMFileInfo();
136   }
137
138   return errorStatus;
139 }
140
141 _Tt_client_isam_record_ptr _Tt_client_isam_file::getEmptyRecord ()
142 {
143   return (new _Tt_client_isam_record(keyDescriptorList,
144                               maxRecordLength,
145                               minRecordLength));
146 }
147
148 int _Tt_client_isam_file
149 ::findStartRecord (const _Tt_client_isam_key_descriptor_ptr &key_descriptor,
150                    int                                length,
151                    const _Tt_client_isam_record_ptr         &record,
152                    int                                mode)
153 {
154   dbConnection->iserrno = 0;
155
156   dbConnection->isreclen = record->getLength();
157   (void)dbConnection->isstart(fileDescriptor,
158                               key_descriptor->getKeyDescriptor(),
159                               length,
160                               (char *)record->getRecord(),
161                               mode);
162
163   getStatusInfo();
164   return errorStatus;
165 }
166
167 _Tt_client_isam_record_ptr _Tt_client_isam_file::readRecord (int mode)
168 {
169   dbConnection->iserrno = 0;
170
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,
176                                                      mode);
177
178   getStatusInfo();
179   if (!results) {
180     record = getFullRecord(record_buffer);
181   }
182
183   return record;
184 }
185
186 int _Tt_client_isam_file::readRecord (int                        mode,
187                                const _Tt_client_isam_record_ptr &record)
188 {
189   dbConnection->iserrno = 0;
190
191   dbConnection->isreclen = record->getLength();
192   int results = dbConnection->isread(fileDescriptor, (char *)record->getRecord(), mode);
193
194   getStatusInfo();
195   if (!results) {
196     record->setLength(currentRecordLength);
197   }
198
199   return errorStatus;
200 }
201
202 int
203 _Tt_client_isam_file::updateRecord (long recnum, const _Tt_client_isam_record_ptr &record)
204 {
205   dbConnection->iserrno = 0;
206
207   dbConnection->isreclen = record->getLength();
208   (void)dbConnection->isrewrec(fileDescriptor, recnum, (char *)record->getRecord());
209
210   getStatusInfo();
211   return errorStatus;
212 }
213
214 int _Tt_client_isam_file::writeRecord (const _Tt_client_isam_record_ptr &record)
215 {
216   dbConnection->iserrno = 0;
217
218   dbConnection->isreclen = record->getLength();
219   (void)dbConnection->iswrite(fileDescriptor, (char *)record->getRecord());
220
221   getStatusInfo();
222   return errorStatus;
223 }
224
225 int
226 _Tt_client_isam_file::deleteRecord (long                              recnum,
227                                     const _Tt_client_isam_record_ptr &record)
228 {
229   dbConnection->iserrno = 0;
230
231   dbConnection->isreclen = record->getLength();
232   (void)dbConnection->isdelrec(fileDescriptor,
233                                recnum,
234                                (char *)record->getRecord());
235
236   getStatusInfo();
237   return errorStatus;
238 }
239
240 int _Tt_client_isam_file::writeMagicString (const _Tt_string &magic_string)
241 {
242   dbConnection->iserrno = 0;
243
244   (void)dbConnection->iscntl(fileDescriptor,
245                              ISCNTL_APPLMAGIC_WRITE,
246                              (char *)magic_string);
247
248   errorStatus = dbConnection->iserrno;
249   return errorStatus;
250 }
251
252 _Tt_string _Tt_client_isam_file::readMagicString ()
253 {
254   dbConnection->iserrno = 0;
255
256   _Tt_string magic_string_bytes(ISAPPLMAGICLEN);
257   memset((char *)magic_string_bytes, '\0', ISAPPLMAGICLEN);
258   
259   (void)dbConnection->iscntl(fileDescriptor,
260                              ISCNTL_APPLMAGIC_READ,
261                              (char *)magic_string_bytes);
262
263   errorStatus = dbConnection->iserrno;
264
265   _Tt_string magic_string = (char *)magic_string_bytes;
266   return magic_string;
267 }
268
269 _Tt_client_isam_record_ptr
270 _Tt_client_isam_file::getFullRecord (const _Tt_string &record_buffer)
271 {
272   _Tt_client_isam_record_ptr record_ptr = new _Tt_client_isam_record(keyDescriptorList,
273                                                        maxRecordLength,
274                                                        minRecordLength);
275   record_ptr->setBytes(0, currentRecordLength, record_buffer);
276   record_ptr->setLength(currentRecordLength);
277
278   return record_ptr;
279 }
280
281 void _Tt_client_isam_file::getISAMFileInfo ()
282 {
283   keyDescriptorList->flush();
284
285   if (!strcmp(TT_OLD_DB_PROPERTY_TABLE_FILE,
286               (char *)fileName+
287                       fileName.len()-
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;
291
292     _Tt_client_isam_key_descriptor_ptr key_descriptor =
293                                 new _Tt_client_isam_key_descriptor;
294
295     key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
296                                TT_OLD_DB_KEY_LENGTH,
297                                BINTYPE);
298     key_descriptor->addKeyPart(TT_OLD_DB_PROPERTY_NAME_OFFSET,
299                                TT_OLD_DB_MAX_PROPERTY_NAME_LENGTH,
300                                CHARTYPE);
301     key_descriptor->setDuplicates(TRUE);
302     
303     keyDescriptorList->append(key_descriptor);
304   }
305   else if (!strcmp(TT_OLD_DB_ACCESS_TABLE_FILE,
306                    (char *)fileName+
307                            fileName.len()-
308                            strlen(TT_OLD_DB_ACCESS_TABLE_FILE))) {
309     maxRecordLength = minRecordLength = TT_OLD_DB_KEY_LENGTH+
310                                         3*TT_OLD_DB_SHORT_SIZE;
311
312     _Tt_client_isam_key_descriptor_ptr key_descriptor =
313                                 new _Tt_client_isam_key_descriptor;
314
315     key_descriptor->addKeyPart(TT_OLD_DB_FIRST_KEY_OFFSET,
316                                TT_OLD_DB_KEY_LENGTH,
317                                BINTYPE);
318
319     keyDescriptorList->append(key_descriptor);
320   }
321   else if (!strcmp(TT_OLD_DB_FILE_TABLE_FILE,
322                    (char *)fileName+
323                            fileName.len()-
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;
327
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,
332                                BINTYPE);
333     keyDescriptorList->append(key_descriptor);
334
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,
338                                CHARTYPE);
339     keyDescriptorList->append(key_descriptor);
340   }
341   // TT_OLD_DB_FILE_OBJECT_MAP_FILE
342   else {
343     maxRecordLength = minRecordLength = 2*TT_OLD_DB_KEY_LENGTH;
344
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,
349                                BINTYPE);
350     keyDescriptorList->append(key_descriptor);
351
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,
355                                CHARTYPE);
356     key_descriptor->setDuplicates(TRUE);
357     keyDescriptorList->append(key_descriptor);
358   }
359
360   errorStatus = dbConnection->iserrno = 0;
361 }