ATLAS Offline Software
Loading...
Searching...
No Matches
Guid.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <iostream>
8#include <cstdio>
9#include "uuid/uuid.h"
10
11//{ 0x0,0x0,0x0,{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}};
12static constexpr Guid clid_null("00000000-0000-0000-0000-000000000000");
13
14const Guid& Guid::null() noexcept {
15 return clid_null;
16}
17
19
21 char* envv = getenv("POOL_GUID_TIME");
22 if (envv != 0 && *envv) return GuidGenByTime;
23 envv = getenv("POOL_GUID_RANDOM");
24 if (envv != 0 && *envv) return GuidGenRandom;
25 return GuidGenDefault;
26}
27
29void Guid::create(Guid& guid, GuidGenMethod method) {
30 uuid_t me_;
31 if (method == GuidGenDefault) method = m_guidGenMethod;
32 switch(method) {
33 case GuidGenRandom:
34 ::uuid_generate(me_);
35 break;
36 case GuidGenByTime:
37 ::uuid_generate_time(me_);
38 break;
39 default:
40 ::uuid_generate(me_);
41 break;
42 }
43 unsigned int *d1 = (unsigned int*)me_;
44 unsigned short *d2 = (unsigned short*)(me_ + 4);
45 unsigned short *d3 = (unsigned short*)(me_ + 6);
46 guid.m_data1 = *d1;
47 guid.m_data2 = *d2;
48 guid.m_data3 = *d3;
49 for (unsigned int i = 0; i < 8; i++) {
50 guid.m_data4[i] = me_[i + 8];
51 }
52}
53
54bool Guid::isGuid(std::string_view sv) noexcept{
55 // The GUID must be exactly 36 characters long
56 if (sv.size() != 36) {
57 return false;
58 }
59
60 // Check for hyphens at specific positions (0-based indices: 8, 13, 18, 23)
61 if (sv[8] != '-' || sv[13] != '-' || sv[18] != '-' || sv[23] != '-') {
62 return false;
63 }
64
65 // Validate that all other characters are hexadecimal digits (0-9, a-f, A-F)
66 for (size_t i = 0; i < 36; ++i) {
67 // Skip hyphen positions
68 if (i == 8 || i == 13 || i == 18 || i == 23) {
69 continue;
70 }
71
72 char c = sv[i];
73 // Check if it's a valid hex digit
74 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
75 return false;
76 }
77 }
78 return true;
79}
80
81bool Guid::operator==(std::string_view str) const {
82 return str.size() == Guid::string::stringSize() && *this == Guid(str);
83}
84
85
86void Guid::fromStringFallBack(const std::string &s){
87 //If it conforms to correct Guid use fast method
88 if(isGuid(s)){
89 fromString(s);
90 return;
91 }
92 //If not try "old" more error tolerant method
93 //sscanf will correct subtle corner cases:
94 //when the input string was missing a single hexadecimal digit,
95 // e.g., 83B9F174-5E27-11E4-98C2-02163E00A82,
96 // sscanf still reported 11 successful conversions.
97 //So, the outcome is an "auto-corrected" CLID as 83B9F174-5E27-11E4-98C2-02163E00A802
98 static const char* const fmt_Guid = "%08X-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX";
99 if (::sscanf(s.c_str(), fmt_Guid, &m_data1, &m_data2, &m_data3,
100 &m_data4[0], &m_data4[1], &m_data4[2], &m_data4[3], &m_data4[4], &m_data4[5], &m_data4[6], &m_data4[7]) != 11) {
101 setToNull();
102 }
103 return;
104}
105
106std::ostream& operator<<(std::ostream& os, const Guid& rhs) {
107 auto buff = rhs.to_fixed_string();
108 os.write(buff.data(), buff.size());
109 return os;
110}
111
std::ostream & operator<<(std::ostream &os, const Guid &rhs)
Definition Guid.cxx:106
This file contains the class definition for the Guid class (migrated from POOL).
static constexpr int stringSize()
Definition Guid.h:37
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition Guid.h:25
constexpr void fromString(std::string_view s)
Automatic conversion from string representation.
Definition Guid.h:143
static const Guid & null() noexcept
NULL-Guid: static class method.
Definition Guid.cxx:14
bool operator==(const Guid &) const =default
static void create(Guid &guid, GuidGenMethod method=GuidGenDefault)
Create a new Guid default method is currently Random, can be changed by param, API or environment.
Definition Guid.cxx:29
constexpr Guid::string to_fixed_string(bool uppercase=true) const
Definition Guid.h:70
unsigned short m_data2
Definition Guid.h:115
std::array< unsigned char, 8 > m_data4
Definition Guid.h:117
constexpr Guid()
Standard constructor.
Definition Guid.h:43
constexpr void setToNull() noexcept
Definition Guid.h:135
unsigned int m_data1
Definition Guid.h:114
static GuidGenMethod initGuidGenMethod()
Checks for POOL_GUID_TIME or POOL_GUID_RANDOM env variables.
Definition Guid.cxx:20
static const GuidGenMethod m_guidGenMethod
Definition Guid.h:85
GuidGenMethod
Definition Guid.h:84
@ GuidGenDefault
Definition Guid.h:84
@ GuidGenByTime
Definition Guid.h:84
@ GuidGenRandom
Definition Guid.h:84
void fromStringFallBack(const std::string &)
Definition Guid.cxx:86
static bool isGuid(std::string_view) noexcept
Definition Guid.cxx:54
unsigned short m_data3
Definition Guid.h:116
static const char *const fmt_Guid
Definition v5_Guid.cxx:24
static const offline_poolCopy_v5::Guid clid_null(std::string("00000000-0000-0000-0000-000000000000"))