Skip to content

Commit

Permalink
StringHelpers - Better UUID/GUID Generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Jul 24, 2024
1 parent 0f82b04 commit 805f130
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 73 deletions.
8 changes: 7 additions & 1 deletion include/helpers/stringhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
84 changes: 18 additions & 66 deletions src/helpers/stringhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "system/environment.h"
#ifdef _WIN32
#include <windows.h>
#elif defined(__linux__)
#include <uuid/uuid.h>
#else
#include <gio/gio.h>
#endif

using namespace Nickvision::System;
Expand Down Expand Up @@ -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<unsigned char, 16> 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<int>(guid[0]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[1]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[2]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[3]);
out << "-";
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[4]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[5]);
out << "-";
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[6]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[7]);
out << "-";
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[8]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[9]);
out << "-";
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[10]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[11]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[12]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[13]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[14]);
out << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(guid[15]);
return out.str();
std::array<wchar_t, 40> 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 };
Expand Down
2 changes: 1 addition & 1 deletion src/keyring/credential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(std::hash<std::string>{}(StringHelpers::newGuid())) },
: m_id{ static_cast<int>(std::hash<std::string>{}(StringHelpers::newUuid())) },
m_name{ name },
m_uri{ uri },
m_username{ username },
Expand Down
2 changes: 1 addition & 1 deletion src/notifications/notifyicon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Nickvision::Notifications
std::unordered_map<HWND, NotifyIcon*> 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 },
Expand Down
2 changes: 1 addition & 1 deletion src/system/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/stringtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 805f130

Please sign in to comment.