Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / lib / db / tt_db_key.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 librararies 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_db_key.C /main/3 1995/10/23 10:02:59 rswiston $                                                  
28 /*
29  * @(#)tt_db_key.C      1.10 93/09/07
30  + Implements the TT db server key class.
31  *
32  * Copyright (c) 1992 by Sun Microsystems, Inc.
33  */
34
35 #include <util/tt_base64.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40
41 #include "util/tt_base64.h"
42 #include "util/tt_port.h"
43 #include "db/tt_db_key.h"
44
45 _Tt_db_key::_Tt_db_key (short version_number)
46 {
47   static long last_time_sec = 0;
48   static long counter = 0;
49
50   key.version  = version_number;
51   key.padding  = 0;
52   key.hostid   = _tt_gethostid();
53   key.time_sec = time ((time_t *) NULL);
54
55   if (key.time_sec != last_time_sec) {
56     last_time_sec = key.time_sec;
57     counter       = getpid() * 10000;
58   }
59   key.counter = counter++;
60 }
61
62 _Tt_db_key::_Tt_db_key (const _Tt_string &string)
63 {
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);
67   }
68   // Else, assume this is an objid...
69   else {
70     _Tt_string key_string = (char *)string;
71     _Tt_string temp_string;
72
73     // Get the version number
74     key_string = key_string.split('|', temp_string);
75     key.version = (unsigned short)_tt_base64_decode(temp_string);
76     
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
79     // number.
80     
81     // Set the padding to 2 bytes of 0
82     key.padding = (unsigned short)0;
83     
84     // Get the host ID
85     key_string = key_string.split('|', temp_string);
86     key.hostid = _tt_base64_decode(temp_string);
87     
88     // Get the first time component
89     key_string = key_string.split('|', temp_string);
90     key.time_sec = _tt_base64_decode(temp_string);
91     
92     // Get the last time component
93     if (key_string.index(':') == -1) {
94       key.counter = _tt_base64_decode(key_string);
95     }
96     else {
97       key_string = key_string.split(':', temp_string);
98       key.counter = _tt_base64_decode(temp_string);
99     }
100   }
101 }
102
103 _Tt_db_key::~_Tt_db_key ()
104 {
105 }
106
107 _Tt_string _Tt_db_key::string () const
108 {
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));
114   return result;
115 }
116
117 int _Tt_db_key::
118 operator== (const _Tt_db_key &otherkey) const
119 {
120         // Very minor optimization:: compare the most likely differences
121         // first
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);
127 }