1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
14 #include <rtl/string.hxx>
15 #include <rtl/ustring.hxx>
25 typedef std::array
<sal_uInt8
, 16> GuidArrayType
;
27 GuidArrayType maGuidArray
;
29 static sal_uInt8
gethex(char nChar
)
31 if (nChar
>= '0' && nChar
<= '9')
33 else if (nChar
>= 'a' && nChar
<= 'f')
34 return nChar
- 'a' + 10;
35 else if (nChar
>= 'A' && nChar
<= 'F')
36 return nChar
- 'A' + 10;
41 static sal_uInt8
covertHexChar(char high
, char low
)
43 return (gethex(high
) << 4) + gethex(low
);
46 void parse(std::string_view rString
)
48 if (rString
.size() != 38)
51 if (rString
[0] != '{' || rString
[37] != '}' || rString
[9] != '-' || rString
[14] != '-'
52 || rString
[19] != '-' || rString
[24] != '-')
55 for (size_t x
= 1; x
<= 8; x
++)
56 if (!std::isxdigit(rString
[x
]))
58 for (size_t x
= 10; x
<= 13; x
++)
59 if (!std::isxdigit(rString
[x
]))
61 for (size_t x
= 15; x
<= 18; x
++)
62 if (!std::isxdigit(rString
[x
]))
64 for (size_t x
= 20; x
<= 23; x
++)
65 if (!std::isxdigit(rString
[x
]))
67 for (size_t x
= 25; x
<= 36; x
++)
68 if (!std::isxdigit(rString
[x
]))
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]);
76 maGuidArray
[4] = covertHexChar(rString
[10], rString
[11]);
77 maGuidArray
[5] = covertHexChar(rString
[12], rString
[13]);
79 maGuidArray
[6] = covertHexChar(rString
[15], rString
[16]);
80 maGuidArray
[7] = covertHexChar(rString
[17], rString
[18]);
82 maGuidArray
[8] = covertHexChar(rString
[20], rString
[21]);
83 maGuidArray
[9] = covertHexChar(rString
[22], rString
[23]);
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]);
94 /// GenerateTag is used as a flag for generating the GUID
100 /// Constructor which generates the GUID
101 Guid(enum GenerateTag
) { rtl_createUuid(maGuidArray
.data(), nullptr, false); }
103 /// Default constructor which initializes the values to 0 (empty GUID)
104 Guid() { maGuidArray
.fill(0); }
106 /// parse the GUID from the string
107 Guid(std::string_view rString
)
113 /// set the GUID from an array
114 Guid(const sal_uInt8 aGuidArray
[16])
116 std::copy(aGuidArray
, aGuidArray
+ 16, maGuidArray
.begin());
119 Guid(Guid
&&) = delete;
121 Guid(Guid
const& rOther
) { *this = rOther
; }
123 void operator=(Guid
const& rOther
) { std::copy(rOther
.cbegin(), rOther
.cend(), begin()); }
125 bool isEmpty() { return *std::max_element(maGuidArray
.begin(), maGuidArray
.end()) == 0; }
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(); }
132 OString
getString() const
135 snprintf(sBuffer
, sizeof(sBuffer
),
136 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
137 maGuidArray
[0], maGuidArray
[1], maGuidArray
[2], maGuidArray
[3], maGuidArray
[4],
138 maGuidArray
[5], maGuidArray
[6], maGuidArray
[7], maGuidArray
[8], maGuidArray
[9],
139 maGuidArray
[10], maGuidArray
[11], maGuidArray
[12], maGuidArray
[13],
140 maGuidArray
[14], maGuidArray
[15]);
142 return OString(sBuffer
);
145 OUString
getOUString() { return OStringToOUString(getString(), RTL_TEXTENCODING_ASCII_US
); }
147 bool operator==(Guid
const& rCompare
) const
149 return rtl_compareUuid(maGuidArray
.data(), rCompare
.maGuidArray
.data()) == 0;
152 bool operator!=(Guid
const& rCompare
) const { return !(*this == rCompare
); }
155 template <typename charT
, typename traits
>
156 inline std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& rStream
,
157 tools::Guid
const& rGuid
)
159 OString aString
= rGuid
.getString();
160 rStream
<< "GUID" << aString
.getStr();
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */