LibreOffice Module tools (master) 1
Guid.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#pragma once
11
12#include <array>
13#include <rtl/uuid.h>
14#include <rtl/string.hxx>
15#include <rtl/ustring.hxx>
16#include <algorithm>
17#include <stdio.h>
18#include <cctype>
19
20namespace tools
21{
22class Guid
23{
24private:
25 typedef std::array<sal_uInt8, 16> GuidArrayType;
26
28
29 static sal_uInt8 gethex(char nChar)
30 {
31 if (nChar >= '0' && nChar <= '9')
32 return nChar - '0';
33 else if (nChar >= 'a' && nChar <= 'f')
34 return nChar - 'a' + 10;
35 else if (nChar >= 'A' && nChar <= 'F')
36 return nChar - 'A' + 10;
37 else
38 return 0;
39 }
40
41 static sal_uInt8 covertHexChar(char high, char low)
42 {
43 return (gethex(high) << 4) + gethex(low);
44 }
45
46 void parse(std::string_view rString)
47 {
48 if (rString.size() != 38)
49 return;
50
51 if (rString[0] != '{' || rString[37] != '}' || rString[9] != '-' || rString[14] != '-'
52 || rString[19] != '-' || rString[24] != '-')
53 return;
54
55 for (size_t x = 1; x <= 8; x++)
56 if (!std::isxdigit(rString[x]))
57 return;
58 for (size_t x = 10; x <= 13; x++)
59 if (!std::isxdigit(rString[x]))
60 return;
61 for (size_t x = 15; x <= 18; x++)
62 if (!std::isxdigit(rString[x]))
63 return;
64 for (size_t x = 20; x <= 23; x++)
65 if (!std::isxdigit(rString[x]))
66 return;
67 for (size_t x = 25; x <= 36; x++)
68 if (!std::isxdigit(rString[x]))
69 return;
70
71 maGuidArray[0] = covertHexChar(rString[1], rString[2]);
72 maGuidArray[1] = covertHexChar(rString[3], rString[4]);
73 maGuidArray[2] = covertHexChar(rString[5], rString[6]);
74 maGuidArray[3] = covertHexChar(rString[7], rString[8]);
75
76 maGuidArray[4] = covertHexChar(rString[10], rString[11]);
77 maGuidArray[5] = covertHexChar(rString[12], rString[13]);
78
79 maGuidArray[6] = covertHexChar(rString[15], rString[16]);
80 maGuidArray[7] = covertHexChar(rString[17], rString[18]);
81
82 maGuidArray[8] = covertHexChar(rString[20], rString[21]);
83 maGuidArray[9] = covertHexChar(rString[22], rString[23]);
84
85 maGuidArray[10] = covertHexChar(rString[25], rString[26]);
86 maGuidArray[11] = covertHexChar(rString[27], rString[28]);
87 maGuidArray[12] = covertHexChar(rString[29], rString[30]);
88 maGuidArray[13] = covertHexChar(rString[31], rString[32]);
89 maGuidArray[14] = covertHexChar(rString[33], rString[34]);
90 maGuidArray[15] = covertHexChar(rString[35], rString[36]);
91 }
92
93public:
96 {
97 Generate = 0
98 };
99
101 Guid(enum GenerateTag) { rtl_createUuid(maGuidArray.data(), nullptr, false); }
102
104 Guid() { maGuidArray.fill(0); }
105
107 Guid(std::string_view rString)
108 {
109 maGuidArray.fill(0);
110 parse(rString);
111 }
112
114 Guid(const sal_uInt8 aGuidArray[16])
115 {
116 std::copy(aGuidArray, aGuidArray + 16, maGuidArray.begin());
117 }
118
119 Guid(Guid&&) = delete;
120
121 Guid(Guid const& rOther) { *this = rOther; }
122
123 void operator=(Guid const& rOther) { std::copy(rOther.cbegin(), rOther.cend(), begin()); }
124
125 bool isEmpty() { return *std::max_element(maGuidArray.begin(), maGuidArray.end()) == 0; }
126
127 GuidArrayType::iterator begin() { return maGuidArray.begin(); }
128 GuidArrayType::iterator end() { return maGuidArray.end(); }
129 GuidArrayType::const_iterator cbegin() const { return maGuidArray.cbegin(); }
130 GuidArrayType::const_iterator cend() const { return maGuidArray.cend(); }
131
132 OString getString() const
133 {
134 char sBuffer[40];
135 snprintf(sBuffer, sizeof(sBuffer),
136 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
140 maGuidArray[14], maGuidArray[15]);
141
142 return OString(sBuffer);
143 }
144
145 OUString getOUString() { return OStringToOUString(getString(), RTL_TEXTENCODING_ASCII_US); }
146
147 bool operator==(Guid const& rCompare) const
148 {
149 return rtl_compareUuid(maGuidArray.data(), rCompare.maGuidArray.data()) == 0;
150 }
151
152 bool operator!=(Guid const& rCompare) const { return !(*this == rCompare); }
153};
154
155template <typename charT, typename traits>
156inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& rStream,
157 tools::Guid const& rGuid)
158{
159 OString aString = rGuid.getString();
160 rStream << "GUID" << aString.getStr();
161 return rStream;
162}
163}
164
165/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
GuidArrayType maGuidArray
Definition: Guid.hxx:27
void operator=(Guid const &rOther)
Definition: Guid.hxx:123
void parse(std::string_view rString)
Definition: Guid.hxx:46
Guid(Guid const &rOther)
Definition: Guid.hxx:121
OUString getOUString()
Definition: Guid.hxx:145
static sal_uInt8 gethex(char nChar)
Definition: Guid.hxx:29
bool operator==(Guid const &rCompare) const
Definition: Guid.hxx:147
Guid(std::string_view rString)
parse the GUID from the string
Definition: Guid.hxx:107
GuidArrayType::const_iterator cbegin() const
Definition: Guid.hxx:129
Guid()
Default constructor which initializes the values to 0 (empty GUID)
Definition: Guid.hxx:104
std::array< sal_uInt8, 16 > GuidArrayType
Definition: Guid.hxx:25
OString getString() const
Definition: Guid.hxx:132
static sal_uInt8 covertHexChar(char high, char low)
Definition: Guid.hxx:41
GuidArrayType::const_iterator cend() const
Definition: Guid.hxx:130
bool operator!=(Guid const &rCompare) const
Definition: Guid.hxx:152
GuidArrayType::iterator begin()
Definition: Guid.hxx:127
Guid(enum GenerateTag)
Constructor which generates the GUID.
Definition: Guid.hxx:101
bool isEmpty()
Definition: Guid.hxx:125
Guid(const sal_uInt8 aGuidArray[16])
set the GUID from an array
Definition: Guid.hxx:114
Guid(Guid &&)=delete
GenerateTag
GenerateTag is used as a flag for generating the GUID.
Definition: Guid.hxx:96
@ Generate
Definition: Guid.hxx:97
GuidArrayType::iterator end()
Definition: Guid.hxx:128
float x
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, const tools::Rectangle &rectangle)
Definition: gen.hxx:808
unsigned char sal_uInt8