asfgit closed pull request #467: MINIFICPP-703 - UUID generation in C URL: https://github.com/apache/nifi-minifi-cpp/pull/467
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/nanofi/CMakeLists.txt b/nanofi/CMakeLists.txt index bd2d952c..34e0942e 100644 --- a/nanofi/CMakeLists.txt +++ b/nanofi/CMakeLists.txt @@ -32,7 +32,7 @@ else() include_directories(../libminifi/opsys/posix) endif() -file(GLOB NANOFI_SOURCES "src/api/*.cpp" "src/cxx/*.cpp" ) +file(GLOB NANOFI_SOURCES "src/api/*.cpp" "src/core/*.cpp" "src/cxx/*.cpp") file(GLOB NANOFI_EXAMPLES_SOURCES "examples/*.c" ) diff --git a/nanofi/include/core/cuuid.h b/nanofi/include/core/cuuid.h new file mode 100644 index 00000000..d4f6dea8 --- /dev/null +++ b/nanofi/include/core/cuuid.h @@ -0,0 +1,34 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NIFI_MINIFI_CPP_CUUID_H +#define NIFI_MINIFI_CPP_CUUID_H + +#include "uuid/uuid.h" + +#define CUUID_TIME_IMPL 0 +#define CUUID_RANDOM_IMPL 1 +#define CUUID_DEFAULT_IMPL 2 + +typedef struct CIDGenerator { + int implementation_; +} CIDGenerator; + +void generate_uuid(const CIDGenerator * generator, char * out); + +#endif //NIFI_MINIFI_CPP_CUUID_H diff --git a/nanofi/src/core/cuuid.cpp b/nanofi/src/core/cuuid.cpp new file mode 100644 index 00000000..7f3f3723 --- /dev/null +++ b/nanofi/src/core/cuuid.cpp @@ -0,0 +1,35 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/cuuid.h" + +void generate_uuid(const CIDGenerator * generator, char * out) { + UUID_FIELD output; + switch (generator->implementation_) { + case CUUID_RANDOM_IMPL: + uuid_generate_random(output); + break; + case CUUID_DEFAULT_IMPL: + uuid_generate(output); + break; + default: + uuid_generate_time(output); + break; + } + uuid_unparse_lower(output, out); +} diff --git a/nanofi/tests/CUUIDTests.cpp b/nanofi/tests/CUUIDTests.cpp new file mode 100644 index 00000000..3a3ed4bb --- /dev/null +++ b/nanofi/tests/CUUIDTests.cpp @@ -0,0 +1,50 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string> +#include "TestBase.h" +#include "core/cuuid.h" + +bool verify_uuid(const char * uuid) { + std::string uuid_str(uuid, 36); + if(uuid_str.length() != 36) { + return false; + } + for(int i = 0; i < uuid_str.length(); ++i) { + if(i % 5 == 3 && i > 5 && i < 25) { + if (uuid_str[i] != '-') { + return false; + } + } else { + if(!isxdigit(uuid_str[i])) { + return false; + } + } + } + return true; +} + +TEST_CASE("Test C UUID generation", "[testCUUID]") { + char uuid[36]; + CIDGenerator gen; + for(int i = 0; i < 4; ++i) { + generate_uuid(&gen, uuid); + REQUIRE(verify_uuid(uuid)); + gen.implementation_ = i; + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services