ATLAS Offline Software
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}};
12 static constexpr Guid clid_null("00000000-0000-0000-0000-000000000000");
13 
14 const 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 
30  uuid_t me_;
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 
54 bool 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 
81 bool Guid::operator==(std::string_view str) const {
82  return str.size() == Guid::string::stringSize() && *this == Guid(str);
83 }
84 
85 
86 void 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 
106 std::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 
RunTileTBRec.method
method
Definition: RunTileTBRec.py:73
get_hdefs.buff
buff
Definition: get_hdefs.py:61
Guid::operator==
bool operator==(const Guid &) const =default
Guid::Guid
constexpr Guid()
Standard constructor.
Definition: Guid.h:43
Guid::initGuidGenMethod
static GuidGenMethod initGuidGenMethod()
Checks for POOL_GUID_TIME or POOL_GUID_RANDOM env variables.
Definition: Guid.cxx:20
Guid::setToNull
constexpr void setToNull() noexcept
Definition: Guid.h:135
Guid::fromString
constexpr void fromString(std::string_view s)
Automatic conversion from string representation.
Definition: Guid.h:143
dq_defect_virtual_defect_validation.d1
d1
Definition: dq_defect_virtual_defect_validation.py:79
Guid::string::stringSize
static constexpr int stringSize()
Definition: Guid.h:37
Guid::to_fixed_string
constexpr Guid::string to_fixed_string(bool uppercase=true) const
Definition: Guid.h:70
Guid::fromStringFallBack
void fromStringFallBack(const std::string &)
Definition: Guid.cxx:86
Guid::m_data3
unsigned short m_data3
Definition: Guid.h:116
operator<<
std::ostream & operator<<(std::ostream &os, const Guid &rhs)
Definition: Guid.cxx:106
Guid::GuidGenByTime
@ GuidGenByTime
Definition: Guid.h:84
Guid::create
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
Guid::GuidGenRandom
@ GuidGenRandom
Definition: Guid.h:84
Guid::GuidGenMethod
GuidGenMethod
Definition: Guid.h:84
lumiFormat.i
int i
Definition: lumiFormat.py:85
Guid::GuidGenDefault
@ GuidGenDefault
Definition: Guid.h:84
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
pool_uuid.guid
guid
Definition: pool_uuid.py:112
Guid::m_data1
unsigned int m_data1
Definition: Guid.h:114
Guid::null
static const Guid & null() noexcept
NULL-Guid: static class method.
Definition: Guid.cxx:14
Guid
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition: Guid.h:25
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
Guid::m_guidGenMethod
static const GuidGenMethod m_guidGenMethod
Definition: Guid.h:85
dq_defect_virtual_defect_validation.d2
d2
Definition: dq_defect_virtual_defect_validation.py:81
LArCellConditions.sv
bool sv
Definition: LArCellConditions.py:45
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
str
Definition: BTagTrackIpAccessor.cxx:11
Guid::m_data4
std::array< unsigned char, 8 > m_data4
Definition: Guid.h:117
Guid::m_data2
unsigned short m_data2
Definition: Guid.h:115
Guid.h
This file contains the class definition for the Guid class (migrated from POOL).
Guid::isGuid
static bool isGuid(std::string_view) noexcept
Definition: Guid.cxx:54
python.compressB64.c
def c
Definition: compressB64.py:93