This is an automated email from the ASF dual-hosted git repository.
phrocker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
The following commit(s) were added to refs/heads/master by this push:
new e47f8f1 MINIFICPP-703 - UUID generation in C
e47f8f1 is described below
commit e47f8f129259124599367c77b465480f0df44696
Author: Arpad Boda <[email protected]>
AuthorDate: Fri Jan 4 14:10:44 2019 +0100
MINIFICPP-703 - UUID generation in C
This closes #467.
Signed-off-by: Marc Parisi <[email protected]>
---
nanofi/CMakeLists.txt | 2 +-
nanofi/include/core/cuuid.h | 34 ++++++++++++++++++++++++++++++
nanofi/src/core/cuuid.cpp | 35 +++++++++++++++++++++++++++++++
nanofi/tests/CUUIDTests.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 120 insertions(+), 1 deletion(-)
diff --git a/nanofi/CMakeLists.txt b/nanofi/CMakeLists.txt
index bd2d952..34e0942 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 0000000..d4f6dea
--- /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 0000000..7f3f372
--- /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 0000000..3a3ed4b
--- /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;
+ }
+}