Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / tools / Guid.hxx
blob3922a3da9ff2e3c903841f8c623318b6f5f41eab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #pragma once
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>
20 namespace tools
22 class Guid
24 private:
25 typedef std::array<sal_uInt8, 16> GuidArrayType;
27 GuidArrayType maGuidArray;
29 static sal_uInt8 gethex(char nChar)
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;
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)
49 return;
51 if (rString[0] != '{' || rString[37] != '}' || rString[9] != '-' || rString[14] != '-'
52 || rString[19] != '-' || rString[24] != '-')
53 return;
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;
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]);
93 public:
94 /// GenerateTag is used as a flag for generating the GUID
95 enum GenerateTag
97 Generate = 0
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)
109 maGuidArray.fill(0);
110 parse(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
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}",
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();
161 return rStream;
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */