diff --git a/include/helpers/stringhelpers.h b/include/helpers/stringhelpers.h index 7ee2bca..cde6797 100644 --- a/include/helpers/stringhelpers.h +++ b/include/helpers/stringhelpers.h @@ -69,7 +69,13 @@ namespace Nickvision::Helpers::StringHelpers */ std::string lower(std::string s); /** - * @brief Generates a new guid (uuid v4) value. + * @brief Generates a new uuid value. + * @return The uuid value + */ + std::string newUuid(); + /** + * @brief Generates a new guid value. + * @brief This function simple calls newUuid() and returns the result. * @return The guid value */ std::string newGuid(); diff --git a/src/helpers/stringhelpers.cpp b/src/helpers/stringhelpers.cpp index 5873f3c..3f143fb 100644 --- a/src/helpers/stringhelpers.cpp +++ b/src/helpers/stringhelpers.cpp @@ -11,8 +11,8 @@ #include "system/environment.h" #ifdef _WIN32 #include -#elif defined(__linux__) -#include +#else +#include #endif using namespace Nickvision::System; @@ -127,79 +127,31 @@ namespace Nickvision::Helpers return builder.str(); } - std::string StringHelpers::newGuid() + std::string StringHelpers::newUuid() { -#ifdef __APPLE__ - std::string uuid; - FILE* pipe{ popen("uuidgen", "r") }; - if (pipe) - { - char buffer[37]; - if (fgets(buffer, sizeof(buffer), pipe)) - { - uuid = { buffer, 36 }; - } - pclose(pipe); - } - return uuid; -#else - std::array guid; #ifdef _WIN32 - GUID win; + GUID guid; CoInitializeEx(nullptr, COINIT_MULTITHREADED); - if (CoCreateGuid(&win) != S_OK) + if (CoCreateGuid(&guid) != S_OK) { return ""; } - guid = { - (unsigned char)((win.Data1 >> 24) & 0xFF), - (unsigned char)((win.Data1 >> 16) & 0xFF), - (unsigned char)((win.Data1 >> 8) & 0xFF), - (unsigned char)((win.Data1) & 0xff), - - (unsigned char)((win.Data2 >> 8) & 0xFF), - (unsigned char)((win.Data2) & 0xff), - - (unsigned char)((win.Data3 >> 8) & 0xFF), - (unsigned char)((win.Data3) & 0xFF), - - (unsigned char)win.Data4[0], - (unsigned char)win.Data4[1], - (unsigned char)win.Data4[2], - (unsigned char)win.Data4[3], - (unsigned char)win.Data4[4], - (unsigned char)win.Data4[5], - (unsigned char)win.Data4[6], - (unsigned char)win.Data4[7] - }; -#elif defined(__linux__) - uuid_generate(guid.data()); -#endif - std::ostringstream out; - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[0]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[1]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[2]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[3]); - out << "-"; - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[4]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[5]); - out << "-"; - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[6]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[7]); - out << "-"; - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[8]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[9]); - out << "-"; - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[10]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[11]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[12]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[13]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[14]); - out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[15]); - return out.str(); + std::array buffer; + if(StringFromGUID2(guid, buffer.data(), 40) == 0) + { + return ""; + } + return StringHelpers::str({ buffer.data() }).substr(1, 36); +#else + return g_uuid_string_random(); #endif } + std::string StringHelpers::newGuid() + { + return newUuid(); + } + std::string StringHelpers::normalizeForFilename(const std::string& s, bool windowsOnly) { std::string result{ s }; diff --git a/src/keyring/credential.cpp b/src/keyring/credential.cpp index cbcd5c1..733917f 100644 --- a/src/keyring/credential.cpp +++ b/src/keyring/credential.cpp @@ -17,7 +17,7 @@ namespace Nickvision::Keyring } Credential::Credential(const std::string& name, const std::string& uri, const std::string& username, const std::string& password) - : m_id{ static_cast(std::hash{}(StringHelpers::newGuid())) }, + : m_id{ static_cast(std::hash{}(StringHelpers::newUuid())) }, m_name{ name }, m_uri{ uri }, m_username{ username }, diff --git a/src/notifications/notifyicon.cpp b/src/notifications/notifyicon.cpp index 54a627a..32c1aa3 100644 --- a/src/notifications/notifyicon.cpp +++ b/src/notifications/notifyicon.cpp @@ -12,7 +12,7 @@ namespace Nickvision::Notifications std::unordered_map NotifyIcon::m_icons = {}; NotifyIcon::NotifyIcon(HWND hwnd, const std::wstring& tooltip, const NotifyIconMenu& contextMenu, bool hidden) - : m_className{ StringHelpers::wstr(StringHelpers::newGuid()) }, + : m_className{ StringHelpers::wstr(StringHelpers::newUuid()) }, m_isHidden{ hidden }, m_tooltip{ tooltip }, m_contextMenu{ contextMenu }, diff --git a/src/system/process.cpp b/src/system/process.cpp index 0d97e97..26a6f29 100644 --- a/src/system/process.cpp +++ b/src/system/process.cpp @@ -33,7 +33,7 @@ namespace Nickvision::System m_pi{ 0 } #else m_pid{ -1 }, - m_consoleFilePath{ UserDirectories::get(UserDirectory::Cache) / (StringHelpers::newGuid() + ".lnproc") } + m_consoleFilePath{ UserDirectories::get(UserDirectory::Cache) / (StringHelpers::newUuid() + ".lnproc") } #endif { #ifdef _WIN32 diff --git a/tests/stringtests.cpp b/tests/stringtests.cpp index acd31bb..7314286 100644 --- a/tests/stringtests.cpp +++ b/tests/stringtests.cpp @@ -70,12 +70,12 @@ TEST(StringTests, Split3) ASSERT_EQ(cmd[2], "display notification \"Message\" with title \"Title\""); } -TEST(StringTests, Guid1) +TEST(StringTests, Uuid1) { std::string s; - ASSERT_NO_THROW(s = StringHelpers::newGuid()); + ASSERT_NO_THROW(s = StringHelpers::newUuid()); ASSERT_FALSE(s.empty()); - ASSERT_TRUE(s.size() == 36); + ASSERT_EQ(s.size(), 36); } TEST(StringTests, UrlValidity1)