Repository: nifi-minifi-cpp
Updated Branches:
  refs/heads/master 524118e13 -> 2caa58709


MINIFICPP-693 - getProperty should rely on user provided allocated buffers.

This closes #458.

Signed-off-by: Marc Parisi <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/commit/2caa5870
Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/tree/2caa5870
Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/diff/2caa5870

Branch: refs/heads/master
Commit: 2caa587097ae08b199596afe5e7b76b06552de41
Parents: 524118e
Author: Arpad Boda <[email protected]>
Authored: Fri Dec 7 13:30:10 2018 +0100
Committer: Marc Parisi <[email protected]>
Committed: Tue Dec 11 21:26:47 2018 -0500

----------------------------------------------------------------------
 nanofi/examples/generate_flow.c |  3 ++-
 nanofi/include/api/nanofi.h     | 10 +++++++---
 nanofi/src/api/nanofi.cpp       | 12 ++++++++++++
 nanofi/tests/CAPITests.cpp      |  7 +++----
 4 files changed, 24 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/2caa5870/nanofi/examples/generate_flow.c
----------------------------------------------------------------------
diff --git a/nanofi/examples/generate_flow.c b/nanofi/examples/generate_flow.c
index 707de11..596d65b 100644
--- a/nanofi/examples/generate_flow.c
+++ b/nanofi/examples/generate_flow.c
@@ -42,7 +42,8 @@ int main(int argc, char **argv) {
 
   nifi_instance *instance = create_instance(instance_str, &port);
 
-  flow *new_flow = create_flow(instance, "GenerateFlowFile");
+  flow *new_flow = create_new_flow(instance);
+  processor *generate_proc = add_processor(new_flow, "GenerateFlowFile");
 
   flow_file_record *record = get_next_flow_file(instance, new_flow);
 

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/2caa5870/nanofi/include/api/nanofi.h
----------------------------------------------------------------------
diff --git a/nanofi/include/api/nanofi.h b/nanofi/include/api/nanofi.h
index 99eadf8..0650492 100644
--- a/nanofi/include/api/nanofi.h
+++ b/nanofi/include/api/nanofi.h
@@ -196,12 +196,16 @@ int set_instance_property(nifi_instance *instance, const 
char * name, const char
 
 /**
  * Get a property. Should be used in custom processor logic callbacks.
- * @attention The returned value transfers ownership, it's the callers 
responsibility to free it!
+ * Writes the value of the property to the buffer.
+ * Nothing is written to the buffer in case the property is not found (return 
value != 0)
+ * The result is always null-terminated, at most size-1 characters are written 
to the buffer.
  * @param context the current processor context
  * @param name name of the property
- * @return null-terminated char* in case of success, nullptr otherwise
+ * @param buffer buffer to write the value of the property
+ * @param size size of the buffer
+ * @return 0 in case of success (property found), -1 otherwise
  **/
-char * get_property(const processor_context * context, const char * name);
+uint8_t get_property(const processor_context * context, const char * name, 
char * buffer, size_t size);
 
 /**
  * Free a flow

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/2caa5870/nanofi/src/api/nanofi.cpp
----------------------------------------------------------------------
diff --git a/nanofi/src/api/nanofi.cpp b/nanofi/src/api/nanofi.cpp
index 78d1c09..aa957ac 100644
--- a/nanofi/src/api/nanofi.cpp
+++ b/nanofi/src/api/nanofi.cpp
@@ -461,6 +461,18 @@ int set_standalone_property(standalone_processor *proc, 
const char *name, const
   return -1;
 }
 
+uint8_t get_property(const processor_context * context, const char * name, 
char * buffer, size_t size) {
+  std::string value;
+  if(!context->getDynamicProperty(name, value)) {
+    return -1;
+  }
+  size_t chars_to_copy = std::min(value.length(), size-1);
+
+  strncpy(buffer, value.data(), chars_to_copy);
+  buffer[chars_to_copy] = '\0';
+  return 0;
+}
+
 char * get_property(const processor_context *  context, const char * name) {
   std::string value;
   if(!context->getDynamicProperty(name, value)) {

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/2caa5870/nanofi/tests/CAPITests.cpp
----------------------------------------------------------------------
diff --git a/nanofi/tests/CAPITests.cpp b/nanofi/tests/CAPITests.cpp
index 721a0cd..ca24ba8 100644
--- a/nanofi/tests/CAPITests.cpp
+++ b/nanofi/tests/CAPITests.cpp
@@ -71,12 +71,11 @@ void custom_processor_logic(processor_session * ps, 
processor_context * ctx) {
 
   REQUIRE(add_attribute(ffr, "custom attribute", (void*)custom_value, 
strlen(custom_value)) == 0);
 
-  char * prop_value = get_property(ctx, "Some test propery");
+  char prop_value[20];
 
-  REQUIRE(prop_value != nullptr);
-  REQUIRE(strncmp("test value", prop_value, strlen(prop_value)) == 0);
+  REQUIRE(get_property(ctx, "Some test propery", prop_value, 20) == 0);
 
-  free(prop_value);
+  REQUIRE(strncmp("test value", prop_value, strlen(prop_value)) == 0);
 
   transfer_to_relationship(ffr, ps, SUCCESS_RELATIONSHIP);
 

Reply via email to