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_db_key.C /main/3 1995/10/23 10:02:59 rswiston $
29 * @(#)tt_db_key.C 1.10 93/09/07
30 + Implements the TT db server key class.
32 * Copyright (c) 1992 by Sun Microsystems, Inc.
35 #include <util/tt_base64.h>
36 #include <sys/types.h>
41 #include "util/tt_base64.h"
42 #include "util/tt_port.h"
43 #include "db/tt_db_key.h"
45 _Tt_db_key::_Tt_db_key (short version_number)
47 static long last_time_sec = 0;
48 static long counter = 0;
50 key.version = version_number;
52 key.hostid = _tt_gethostid();
53 key.time_sec = time ((time_t *) NULL);
55 if (key.time_sec != last_time_sec) {
56 last_time_sec = key.time_sec;
57 counter = getpid() * 10000;
59 key.counter = counter++;
62 _Tt_db_key::_Tt_db_key (const _Tt_string &string)
64 // If this is an actual key...
65 if ((string.len() == TT_DB_KEY_LENGTH) && (string[0] < '0')) {
66 (void)memcpy((char *)&key, (char *)string, TT_DB_KEY_LENGTH);
68 // Else, assume this is an objid...
70 _Tt_string key_string = (char *)string;
71 _Tt_string temp_string;
73 // Get the version number
74 key_string = key_string.split('|', temp_string);
75 key.version = (unsigned short)_tt_base64_decode(temp_string);
77 // In the future, if the key format changes, it may be necessary
78 // to check the version here and decode according to the version
81 // Set the padding to 2 bytes of 0
82 key.padding = (unsigned short)0;
85 key_string = key_string.split('|', temp_string);
86 key.hostid = _tt_base64_decode(temp_string);
88 // Get the first time component
89 key_string = key_string.split('|', temp_string);
90 key.time_sec = _tt_base64_decode(temp_string);
92 // Get the last time component
93 if (key_string.index(':') == -1) {
94 key.counter = _tt_base64_decode(key_string);
97 key_string = key_string.split(':', temp_string);
98 key.counter = _tt_base64_decode(temp_string);
103 _Tt_db_key::~_Tt_db_key ()
107 _Tt_string _Tt_db_key::string () const
109 _Tt_string result(_tt_base64_encode(key.version));
110 result = result.cat("|");
111 result = result.cat(_tt_base64_encode(key.hostid)).cat("|");
112 result = result.cat(_tt_base64_encode(key.time_sec)).cat("|");
113 result = result.cat(_tt_base64_encode(key.counter));
118 operator== (const _Tt_db_key &otherkey) const
120 // Very minor optimization:: compare the most likely differences
122 return (key.time_sec==otherkey.key.time_sec &&
123 key.counter==otherkey.key.counter &&
124 key.version==otherkey.key.version &&
125 key.padding==otherkey.key.padding &&
126 key.hostid==otherkey.key.hostid);