Avoid the "deprecated or antiquated header" warning from g++ 4.3 by using <tr1/unorde...
[synfig.git] / synfig-core / trunk / src / synfig / guid.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file guid.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #define SUBTRACT_RNG_H <ext/functional>
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "guid.h"
36 #include "quick_rng.h"
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <ETL/stringf>
42 #include <functional>
43
44 #ifdef _DEBUG
45 #include "general.h"
46 #endif
47
48 #ifdef HASH_MAP_H
49 #include HASH_MAP_H
50 #endif
51
52 #ifdef SUBTRACT_RNG_H
53 #include SUBTRACT_RNG_H
54 using namespace __gnu_cxx;
55 #endif
56
57 #ifdef _WIN32
58 #include <windows.h>
59 #endif
60
61 #endif
62
63 /* === U S I N G =========================================================== */
64
65 using namespace std;
66 using namespace etl;
67 using namespace synfig;
68
69 /* === M A C R O S ========================================================= */
70
71 #define MANUAL_GUID_CALC
72
73 /* === G L O B A L S ======================================================= */
74
75 /* === P R O C E D U R E S ================================================= */
76
77 /* === M E T H O D S ======================================================= */
78
79 #define GUID_RNG quick_rng
80 //#define GUID_RNG subtractive_rng
81
82
83
84 #ifdef MANUAL_GUID_CALC
85 #include <time.h>
86 static GUID_RNG _a, _b;
87 static void _set_up_rand_long_long(uint64_t &x);
88 static void _get_rand_long_long(uint64_t &x);
89 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
90 static void _set_up_rand_long_long(uint64_t &x)
91 {
92 #ifdef _DEBUG
93         // synfig::info("Starting up GUID system...");
94 #endif
95         _a=GUID_RNG(time(0)+clock());
96         _b=GUID_RNG(clock());
97         get_rand_long_long=_get_rand_long_long;
98         _get_rand_long_long(x);
99 }
100
101 static void _get_rand_long_long(uint64_t &x)
102 {
103         //subtractive_rng _c(clock());
104         unsigned short* data(reinterpret_cast<unsigned short *>(&x));
105         data[0]=_a(65536);
106         data[1]=_a(65536);
107         data[2]=_a(65536);
108         data[3]=_a(65536);
109 }
110
111 #else
112 // Use OS-Dependent method
113
114 #ifdef _WIN32
115 // Win32
116 static void get_rand_long_long(uint64_t &x)
117 {
118         _GUID* guid(reinterpret_cast<_GUID*>(&x));
119         CoCreateGuid(guid);
120 }
121
122 #else
123 // Unix
124 static int rand_fd;
125 static void _set_up_rand_long_long(uint64_t &x);
126 static void _get_rand_long_long(uint64_t &x);
127 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
128 static void _set_up_rand_long_long(uint64_t &x)
129 {
130 #ifdef _DEBUG
131         // synfig::info("Starting up GUID system...");
132 #endif
133         rand_fd=open("/dev/urandom",O_RDONLY);
134         get_rand_long_long=_get_rand_long_long;
135         _get_rand_long_long(x);
136 }
137
138 static void _get_rand_long_long(uint64_t &x){   read(rand_fd,&x,sizeof(x));}
139
140 #endif
141 #endif
142
143
144
145 void
146 synfig::GUID::make_unique()
147 {
148         get_rand_long_long(data.u_64.a);
149         get_rand_long_long(data.u_64.b);
150 }
151
152 synfig::GUID
153 synfig::GUID::hasher(const String& str)
154 {
155 #ifdef HASH_MAP_H
156         HASH_MAP_NAMESPACE::hash<const char*> string_hash_;
157         const unsigned int seed(
158                 string_hash_(
159                         str.c_str()
160                 )
161         );
162 #else
163         const unsigned int seed(0x3B642879);
164         for(int i=0;i<str.size();i++)
165         {
166                 seed^=(seed*str[i])*i
167                 seed=(seed>>(32-(i%24)))^(seed<<(i%24))
168         }
169 #endif
170
171         GUID_RNG random(seed);
172         GUID ret(0);
173         ret.data.u_32.a=random(~(unsigned int)0);
174         ret.data.u_32.b=random(~(unsigned int)0);
175         ret.data.u_32.c=random(~(unsigned int)0);
176         ret.data.u_32.d=random(~(unsigned int)0);
177         return ret;
178 }
179
180 synfig::GUID
181 synfig::GUID::hasher(int i)
182 {
183         GUID ret(0);
184         GUID_RNG random(i);
185         ret.data.u_32.a=random(~(unsigned int)0);
186         ret.data.u_32.b=random(~(unsigned int)0);
187         ret.data.u_32.c=random(~(unsigned int)0);
188         ret.data.u_32.d=random(~(unsigned int)0);
189         return ret;
190 }
191
192 String
193 synfig::GUID::get_string()const
194 {
195         return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
196 }
197
198 synfig::GUID::GUID(const String &str)
199 {
200         strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);
201 }