~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/uuid_st.h

  • Committer: Monty Taylor
  • Date: 2010-06-02 22:35:45 UTC
  • mto: This revision was merged to the branch mainline in revision 1586.
  • Revision ID: mordred@inaugust.com-20100602223545-q8ekf9b40a85nwuf
Rearragned unittests into a single exe because of how we need to link it
(thanks lifeless)
Link with server symbols without needing to build a library.
Added an additional atomics test which tests whatever version of the atomics
lib the running platform would actually use.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 * Copyright (c) 2010, Brian Aker
5
 
 * All rights reserved.
6
 
 *
7
 
 * Redistribution and use in source and binary forms, with or without
8
 
 * modification, are permitted provided that the following conditions are met:
9
 
 *     * Redistributions of source code must retain the above copyright
10
 
 *       notice, this list of conditions and the following disclaimer.
11
 
 *     * Redistributions in binary form must reproduce the above copyright
12
 
 *       notice, this list of conditions and the following disclaimer in the
13
 
 *       documentation and/or other materials provided with the distribution.
14
 
 *     * Neither the name of the <organization> nor the
15
 
 *       names of its contributors may be used to endorse or promote products
16
 
 *       derived from this software without specific prior written permission.
17
 
 *
18
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22
 
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 
 *
29
 
 */
30
 
 
31
 
// I looked at code which also had this Copyright header.
32
 
 
33
 
/*
34
 
 * Copyright (C) 1996, 1997 Theodore Ts'o.
35
 
 *
36
 
 * %Begin-Header%
37
 
 * Redistribution and use in source and binary forms, with or without
38
 
 * modification, are permitted provided that the following conditions
39
 
 * are met:
40
 
 * 1. Redistributions of source code must retain the above copyright
41
 
 *    notice, and the entire permission notice in its entirety,
42
 
 *    including the disclaimer of warranties.
43
 
 * 2. Redistributions in binary form must reproduce the above copyright
44
 
 *    notice, this list of conditions and the following disclaimer in the
45
 
 *    documentation and/or other materials provided with the distribution.
46
 
 * 3. The name of the author may not be used to endorse or promote
47
 
 *    products derived from this software without specific prior
48
 
 *    written permission.
49
 
 *
50
 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
51
 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52
 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
53
 
 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
54
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56
 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
57
 
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58
 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
60
 
 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
61
 
 * DAMAGE.
62
 
 * %End-Header%
63
 
 */
64
 
 
65
 
#ifndef DRIZZLED_FIELD_UUID_ST_H
66
 
#define DRIZZLED_FIELD_UUID_ST_H
67
 
 
68
 
#include <cstdio>
69
 
#include <iostream>
70
 
 
71
 
namespace drizzled
72
 
{
73
 
namespace field
74
 
{
75
 
 
76
 
struct uuid_st {
77
 
        uint32_t        time_low;
78
 
        uint16_t        time_mid;
79
 
        uint16_t        time_hi_and_version;
80
 
        uint16_t        clock_seq;
81
 
        uint8_t node[6];
82
 
 
83
 
  uuid_st() :
84
 
    time_low(0),
85
 
    time_mid(0),
86
 
    time_hi_and_version(0),
87
 
    clock_seq(0)
88
 
  {
89
 
    node[0]= node[1]= node[2]= node[3]= node[4]= node[5]= 0;
90
 
  }
91
 
 
92
 
  void unpack(const unsigned char *in)
93
 
  {
94
 
    const uint8_t       *ptr= reinterpret_cast<const uint8_t *>(in);
95
 
    uint32_t tmp;
96
 
 
97
 
    tmp= *ptr++;
98
 
    tmp= (tmp << 8) | *ptr++;
99
 
    tmp= (tmp << 8) | *ptr++;
100
 
    tmp= (tmp << 8) | *ptr++;
101
 
    time_low= tmp;
102
 
 
103
 
    tmp= *ptr++;
104
 
    tmp= (tmp << 8) | *ptr++;
105
 
    time_mid= tmp;
106
 
 
107
 
    tmp= *ptr++;
108
 
    tmp= (tmp << 8) | *ptr++;
109
 
    time_hi_and_version = tmp;
110
 
 
111
 
    tmp= *ptr++;
112
 
    tmp= (tmp << 8) | *ptr++;
113
 
    clock_seq= tmp;
114
 
 
115
 
    memcpy(node, ptr, 6);
116
 
  }
117
 
 
118
 
  void pack(unsigned char *out)
119
 
  {
120
 
    uint32_t    tmp;
121
 
 
122
 
    tmp = time_low;
123
 
    out[3] = (unsigned char) tmp;
124
 
    tmp >>= 8;
125
 
    out[2] = (unsigned char) tmp;
126
 
    tmp >>= 8;
127
 
    out[1] = (unsigned char) tmp;
128
 
    tmp >>= 8;
129
 
    out[0] = (unsigned char) tmp;
130
 
 
131
 
    tmp = time_mid;
132
 
    out[5] = (unsigned char) tmp;
133
 
    tmp >>= 8;
134
 
    out[4] = (unsigned char) tmp;
135
 
 
136
 
    tmp = time_hi_and_version;
137
 
    out[7] = (unsigned char) tmp;
138
 
    tmp >>= 8;
139
 
    out[6] = (unsigned char) tmp;
140
 
 
141
 
    tmp = clock_seq;
142
 
    out[9] = (unsigned char) tmp;
143
 
    tmp >>= 8;
144
 
    out[8] = (unsigned char) tmp;
145
 
 
146
 
    memcpy(out+10, node, 6);
147
 
  }
148
 
 
149
 
  bool parse(const char *in)
150
 
  {
151
 
    const char  *cp;
152
 
    char buf[3];
153
 
    size_t i;
154
 
 
155
 
    for (i= 0, cp= in; i < DISPLAY_LENGTH; i++, cp++)
156
 
    {
157
 
      if ((i == 8) || (i == 13) || (i == 18) || (i == 23))
158
 
      {
159
 
        if (*cp == '-')
160
 
        {
161
 
          continue;
162
 
        }
163
 
        else
164
 
        {
165
 
          return true;
166
 
        }
167
 
      }
168
 
 
169
 
      if (not isxdigit(*cp))
170
 
        return true;
171
 
    }
172
 
 
173
 
    time_low= strtoul(in, NULL, 16);
174
 
    time_mid= strtoul(in+9, NULL, 16);
175
 
    time_hi_and_version= strtoul(in+14, NULL, 16);
176
 
    clock_seq= strtoul(in+19, NULL, 16);
177
 
    cp= in+24;
178
 
    buf[2]= 0;
179
 
 
180
 
    for (i= 0; i < 6; i++)
181
 
    {
182
 
      buf[0]= *cp++;
183
 
      buf[1]= *cp++;
184
 
      node[i]= strtoul(buf, NULL, 16);
185
 
    }
186
 
 
187
 
    return false;
188
 
  }
189
 
 
190
 
  void unparse(char *out)
191
 
  {
192
 
    snprintf(out, DISPLAY_BUFFER_LENGTH, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
193
 
            time_low,
194
 
            time_mid,
195
 
            time_hi_and_version,
196
 
            clock_seq >> 8,
197
 
            clock_seq & 0xFF,
198
 
            node[0],
199
 
            node[1],
200
 
            node[2],
201
 
            node[3],
202
 
            node[4],
203
 
            node[5]);
204
 
  }
205
 
 
206
 
  void time(struct timeval ret_val)
207
 
  {
208
 
    uint32_t high;
209
 
    uint64_t clock_reg;
210
 
 
211
 
    high= time_mid | ((time_hi_and_version & 0xFFF) << 16);
212
 
    clock_reg= time_low | ((uint64_t) high << 32);
213
 
 
214
 
    clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
215
 
    ret_val.tv_sec = clock_reg / 10000000;
216
 
    ret_val.tv_usec = (clock_reg % 10000000) / 10;
217
 
  }
218
 
 
219
 
  bool isTimeType()
220
 
  {
221
 
    return ((time_hi_and_version >> 12) & 0xF) == 1 ? true : false; 
222
 
  }
223
 
 
224
 
  static const size_t LENGTH= 16;
225
 
  static const size_t DISPLAY_LENGTH= 36;
226
 
  static const size_t DISPLAY_BUFFER_LENGTH= DISPLAY_LENGTH+1;
227
 
};
228
 
 
229
 
} /* namespace field */
230
 
} /* namespace drizzled */
231
 
 
232
 
#endif /* DRIZZLED_FIELD_UUID_ST_H */
233