Skip to content

Commit

Permalink
StringHelpers - Use stduuid for GUID Generation + Better Dependency M…
Browse files Browse the repository at this point in the history
…anagement
  • Loading branch information
nlogozzo committed Jan 27, 2024
1 parent 24013b5 commit 9e90b8d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 65 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[*.{h,cpp,py,json,page,txt}]
indent_style = space
indent_size = 4

[*.{xml,css,md,blp,yml}]
indent_style = space
indent_size = 2
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

# Directories
out/
out-linux/
out-windows/
build/
bin/
.vs/
Expand All @@ -54,4 +56,7 @@ _nickbuild/
CMakeUserPresets.json

# UI Files
*.ui
*.ui

# Setup Files
inno/*.exe
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if(DEFINED ENV{VCPKG_ROOT})
file(TO_CMAKE_PATH $ENV{VCPKG_ROOT} VCPKG_ROOT)
if(EXISTS "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
set(USING_VCPKG ON)
endif()
endif()
if(WIN32)
Expand All @@ -25,6 +26,7 @@ include(CTest)

#libnick Setup
add_compile_definitions(SQLITE_HAS_CODEC)
add_compile_definitions(UUID_SYSTEM_GENERATOR)
if(LINUX)
add_compile_definitions(HAVE_USLEEP)
endif()
Expand Down Expand Up @@ -74,8 +76,14 @@ find_package(CURL REQUIRED)
find_package(jsoncpp CONFIG REQUIRED)
find_package(Intl REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(unofficial-maddy CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost Boost::locale CURL::libcurl JsonCpp::JsonCpp Intl::Intl OpenSSL::SSL OpenSSL::Crypto unofficial::maddy::maddy)
find_package(stduuid CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost Boost::locale CURL::libcurl JsonCpp::JsonCpp Intl::Intl OpenSSL::SSL OpenSSL::Crypto stduuid)
if(USING_VCPKG)
find_package(unofficial-maddy CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::maddy::maddy)
else()
target_link_libraries(${PROJECT_NAME} PUBLIC maddy)
endif()
if(WIN32)
target_link_libraries(${PROJECT_NAME} PUBLIC Advapi32 Dwmapi Gdiplus Shell32)
elseif(LINUX)
Expand All @@ -88,8 +96,7 @@ elseif(LINUX)
pkg_check_modules(gobject REQUIRED IMPORTED_TARGET gobject-2.0)
pkg_check_modules(gthread REQUIRED IMPORTED_TARGET gthread-2.0)
pkg_check_modules(libsecret REQUIRED IMPORTED_TARGET libsecret-1)
find_package(unofficial-libuuid CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC Threads::Threads PkgConfig::glib PkgConfig::gio PkgConfig::gmodule PkgConfig::gobject PkgConfig::gthread PkgConfig::libsecret unofficial::UUID::uuid)
target_link_libraries(${PROJECT_NAME} PUBLIC Threads::Threads PkgConfig::glib PkgConfig::gio PkgConfig::gmodule PkgConfig::gobject PkgConfig::gthread PkgConfig::libsecret)
endif()

#libnick Install
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ The following are a list of dependencies used by libnick.
- maddy
- openssl
- Used for sqlcipher, as libnick manually includes and compiles sqlcipher.
- stduuid

### Linux Only
The above dependencies must be installed, plus the following for linux systems:
- glib
- libsecret
- libuuid

## Consuming libnick via vcpkg
libnick is available through `vcpkg`.
Expand Down
4 changes: 4 additions & 0 deletions include/helpers/stringhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING

#ifndef UUID_SYSTEM_GENERATOR
#define UUID_SYSTEM_GENERATOR
#endif

#include <string>
#include <type_traits>
#include <vector>
Expand Down
61 changes: 2 additions & 59 deletions src/helpers/stringhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
#include <regex>
#include <sstream>
#include <curl/curl.h>
#ifdef _WIN32
#include <windows.h>
#elif defined(__linux__)
#include <uuid/uuid.h>
#endif
#include <stduuid/uuid.h>

namespace Nickvision
{
Expand Down Expand Up @@ -89,60 +85,7 @@ namespace Nickvision

std::string StringHelpers::newGuid() noexcept
{
std::array<unsigned char, 16> guid;
#ifdef _WIN32
GUID win;
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (CoCreateGuid(&win) != 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();
return uuids::to_string(uuids::uuid_system_generator{}());
}

bool StringHelpers::isValidUrl(const std::string& s) noexcept
Expand Down
1 change: 1 addition & 0 deletions tests/stringtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ TEST(StringTests, Guid1)
std::string s;
ASSERT_NO_THROW(s = StringHelpers::newGuid());
ASSERT_FALSE(s.empty());
ASSERT_TRUE(s.size() == 36);
}

TEST(StringTests, UrlValidity1)
Expand Down

0 comments on commit 9e90b8d

Please sign in to comment.