Merge pull request #313 from an-tao/ossp_uuid

This commit is contained in:
An Tao 2019-12-10 07:01:25 +08:00 committed by GitHub
commit 20c43b3c2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 0 deletions

View File

@ -107,6 +107,22 @@ find_package(UUID REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${UUID_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE ${UUID_LIBRARIES})
try_compile(normal_uuid ${CMAKE_BINARY_DIR}/cmaketest
${PROJECT_SOURCE_DIR}/cmake/tests/normal_uuid_lib_test.cc
LINK_LIBRARIES ${UUID_LIBRARIES}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${UUID_INCLUDE_DIR}")
try_compile(ossp_uuid ${CMAKE_BINARY_DIR}/cmaketest
${PROJECT_SOURCE_DIR}/cmake/tests/ossp_uuid_lib_test.cc
LINK_LIBRARIES ${UUID_LIBRARIES}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${UUID_INCLUDE_DIR}")
if(normal_uuid)
add_definitions(-DUSE_OSSP_UUID=0)
elseif(ossp_uuid)
add_definitions(-DUSE_OSSP_UUID=1)
else()
message(FATAL_ERROR "uuid lib error")
endif()
find_package(ZLIB REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${ZLIB_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE ${ZLIB_LIBRARIES})

View File

@ -0,0 +1,7 @@
#include <uuid.h>
int main()
{
uuid_t uu;
uuid_generate(uu);
return 0;
}

View File

@ -0,0 +1,8 @@
#include <uuid.h>
int main()
{
uuid_t *uuid;
uuid_create(&uuid);
uuid_make(uuid, UUID_MAKE_V1);
return 0;
}

View File

@ -260,9 +260,22 @@ std::set<std::string> splitStringToSet(const std::string &str,
std::string getUuid()
{
#if USE_OSSP_UUID
uuid_t *uuid;
uuid_create(&uuid);
uuid_make(uuid, UUID_MAKE_V4);
char *str{nullptr};
size_t len{0};
uuid_export(uuid, UUID_FMT_BIN, &str, &len);
uuid_destroy(uuid);
std::string ret{binaryStringToHex((const unsigned char *)str, len)};
free(str);
return ret;
#else
uuid_t uu;
uuid_generate(uu);
return binaryStringToHex(uu, 16);
#endif
}
std::string base64Encode(const unsigned char *bytes_to_encode,