forked from IfcOpenShell/IfcOpenShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIfcGuidHelper.cpp
More file actions
107 lines (94 loc) · 4.26 KB
/
IfcGuidHelper.cpp
File metadata and controls
107 lines (94 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
/********************************************************************************
* *
* Please consider this a placeholder for an actual GlobalId generation *
* algorithm. A real implementation could for example be based on Boost::uuid *
* *
********************************************************************************/
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "../ifcparse/IfcWrite.h"
#include "../ifcparse/IfcException.h"
static const char* chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$";
// Converts an unsigned integer into a base64 string of length l
std::string base64(unsigned v, int l) {
std::string r;
r.reserve(l);
while ( v ) {
r.push_back(chars[v%64]);
v /= 64;
}
while ( r.size() != l ) r.push_back('0');
std::reverse(r.begin(),r.end());
return r;
}
// Converts a base64 string into an unsigned integer
unsigned from_base64(const std::string& s) {
std::string::size_type zeros = s.find_first_not_of('0');
unsigned r = 0;
if ( zeros != std::string::npos )
for ( std::string::const_iterator i = s.begin()+zeros; i != s.end(); ++ i ) {
r *= 64;
const char* c = strchr(chars,*i);
if ( !c ) throw IfcParse::IfcException("Failed to decode GlobalId");
r += (c-chars);
}
return r;
}
// Compresses the UUID byte array into a base64 representation
std::string compress(unsigned char* v) {
std::string r;
r.reserve(22);
r += base64(v[0],2);
for ( unsigned i = 1; i < 16; i += 3 ) {
r += base64((v[i]<<16) + (v[i+1]<<8) + v[i+2],4);
}
return r;
}
// Expands the base64 representation into a UUID byte array
void expand(const std::string& s, std::vector<unsigned char>& v) {
v.push_back(from_base64(s.substr(0,2)));
for( unsigned i = 0; i < 5; ++i ) {
unsigned d = from_base64(s.substr(2+4*i,4));
for ( unsigned j = 0; j < 3; ++ j ) {
v.push_back((d>>(8*(2-j))) % 256);
}
}
}
// A random number generator for the UUID
static boost::uuids::basic_random_generator<boost::mt19937> gen;
IfcWrite::IfcGuidHelper::IfcGuidHelper() {
boost::uuids::uuid u = gen();
std::vector<unsigned char> v(u.size());
std::copy(u.begin(), u.end(), v.begin());
data = compress(&v[0]);
std::vector<unsigned char> v2;
expand(data,v2);
boost::uuids::uuid u2;
std::copy(v2.begin(), v2.end(), u2.begin());
}
IfcWrite::IfcGuidHelper::operator std::string() const {
return data;
}
bool IfcWrite::IfcGuidHelper::seeded = false;