arpadboda commented on a change in pull request #674: Minificpp 1007 - ECU C2 
integration.
URL: https://github.com/apache/nifi-minifi-cpp/pull/674#discussion_r353276793
 
 

 ##########
 File path: nanofi/ecu/c2_client.c
 ##########
 @@ -0,0 +1,235 @@
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <coap/c2structs.h>
+#include <coap/c2protocol.h>
+#include <coap/c2agent.h>
+#include <c2_api/c2api.h>
+#include <api/ecu.h>
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "utlist.h"
+#include "uthash.h"
+
+typedef struct config_params {
+    char * key; // name of the param
+    char * value; // value of the param
+    UT_hash_handle hh;
+} config_params;
+
+char ** parse_line(char * line, size_t len) {
+    char * tok = strtok(line, " =");
+    char ** tokens = (char **)malloc(sizeof(char *) * 2);
+    int i = 0;
+    while (tok) {
+        if (i > 2) break;
+        size_t s = strlen(tok);
+        char * token = (char *)malloc(sizeof(char) * (s + 1));
+        memset(token, 0, (s + 1));
+        strcpy(token, tok);
+        tokens[i++] = token;
+        tok = strtok(NULL, " =\n");
+    }
+    return tokens;
+}
+
+struct config_params * read_configuration_file(const char * file_path) {
+    if (!file_path) {
+        printf("no file path provided\n");
+        return NULL;
+    }
+
+    struct config_params * params = NULL;
+    FILE * fp = fopen(file_path, "r");
+    char * line = NULL;
+    size_t size = 0;
+    ssize_t read;
+    if (!fp) {
+        printf("Cannot open file %s\n", file_path);
+        return NULL;
+    }
+#ifndef WIN32
+    while ((read = getline(&line, &size, fp)) > 0) {
+#else
+    size = 1024;
+    line = (char *)malloc(1024);
+    while (fgets(line, size, fp) != NULL) {
+#endif
+        char ** tokens = parse_line(line, size);
+        struct config_params * el = (struct config_params 
*)malloc(sizeof(struct config_params));
+        size_t key_len = strlen(tokens[0]);
+        size_t value_len = strlen(tokens[1]);
+        el->key = (char *)malloc(key_len + 1);
+        memset(el->key, 0, key_len + 1);
+        strcpy(el->key, tokens[0]);
+
+        el->value = (char *)malloc(value_len + 1);
 
 Review comment:
   Why do you reallocate, copy and free? 
   I think the tokens returned should be directly added to the hashmap. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to