summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2020-03-20 22:53:22 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2020-03-23 21:37:30 +0100
commit3004f1ed9a3979176ec3b055c627eba88c2004f6 (patch)
tree9be374d67bb7f9bae0e32f4325c96388d101e31f
parent627d135bf5108f514161e1d37acf6b97c4b3c4a3 (diff)
Implement UUID handling to avoid libuuid dependency
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/erebos/uuid.h5
-rw-r--r--src/uuid.cpp19
3 files changed, 18 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ccb2527..c7685ae 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,6 +10,5 @@ find_package(Threads REQUIRED)
find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED)
find_library(B2_LIBRARY b2 REQUIRED)
-find_library(UUID_LIBRARY uuid REQUIRED)
add_subdirectory(src)
diff --git a/include/erebos/uuid.h b/include/erebos/uuid.h
index 31322ea..9b615f8 100644
--- a/include/erebos/uuid.h
+++ b/include/erebos/uuid.h
@@ -1,7 +1,6 @@
#pragma once
-#include <uuid/uuid.h>
-
+#include <array>
#include <string>
namespace erebos {
@@ -14,7 +13,7 @@ struct UUID
bool operator==(const UUID &) const;
bool operator!=(const UUID &) const;
- uuid_t uuid;
+ std::array<uint8_t, 16> uuid;
};
}
diff --git a/src/uuid.cpp b/src/uuid.cpp
index e1f044a..9af32b7 100644
--- a/src/uuid.cpp
+++ b/src/uuid.cpp
@@ -7,16 +7,29 @@ using namespace erebos;
using std::runtime_error;
using std::string;
+static const size_t UUID_STR_LEN = 36;
+
+static const char * FORMAT_STRING = "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-"
+ "%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx";
+
UUID::UUID(string str)
{
- if (uuid_parse(str.c_str(), uuid) != 0)
+ if (str.size() != UUID_STR_LEN)
+ throw runtime_error("invalid UUID");
+
+ if (sscanf(str.c_str(), FORMAT_STRING,
+ &uuid[0], &uuid[1], &uuid[2], &uuid[3], &uuid[4], &uuid[5], &uuid[6], &uuid[7],
+ &uuid[8], &uuid[9], &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15])
+ != 16)
throw runtime_error("invalid UUID");
}
UUID::operator string() const
{
- string str(UUID_STR_LEN - 1, '\0');
- uuid_unparse_lower(uuid, str.data());
+ string str(UUID_STR_LEN, '\0');
+ snprintf(str.data(), UUID_STR_LEN + 1, FORMAT_STRING,
+ uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
+ uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
return str;
}