Skip to content
Snippets Groups Projects
Commit 3ccaacdf authored by Jakob Olsson's avatar Jakob Olsson
Browse files

move some functionality to lib_commons from library

parent b9ae2668
No related branches found
No related tags found
No related merge requests found
/** @file libcommons.c */
#include "lib_commons.h"
size_t write_func(void *buffer, size_t size, size_t nmemb, void *data)
{
struct string *str = (struct string *)data;
size_t len = size * nmemb;
size_t new_len = str->len + (len);
char *tmp_ptr = str->ptr;
str->ptr = realloc(str->ptr, new_len + 1);
if (!str->ptr)
{
//debug_print("not enough memory (realloc returned NULL)\n");
free(tmp_ptr);
return 0;
}
memcpy(str->ptr + str->len, buffer, len);
str->ptr[new_len] = '\0';
str->len = new_len;
return len;
}
struct json_object *get_json_string_object_by_key(struct json_object *json_obj, char *string, char *output_key) //"string" should be key?
{
struct json_object_iter iterator;
struct json_object *string_object = NULL;
struct json_object *jstring = NULL;
enum json_type type;
type = json_object_get_type(json_obj);
if (type == json_type_object) // flow could be changed
{
//search the key from the json object
json_object_object_foreachC(json_obj, iterator)
{
printf("iteratorKey is :%s\n", iterator.key);
if (strcmp(string, (char *)iterator.key) == 0) // flow could be changed
{
//debug_print("Input string value: %s\n", (char *)json_object_get_string(iterator.val));
//debug_print("Input string key: %s\n", (char *)iterator.key);
string_object = json_object_new_object();
json_object_object_add(string_object, output_key, iterator.val);
//debug_print("The json object created: %sn", json_object_to_json_string(string_object));
break;
}
}
}
return string_object; // who is responsible for not leaking memory for scope of this function?
}
\ No newline at end of file
#ifndef LIB_COMMON_H
#define LIB_COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json-c/json.h>
/**
* Struct used for parsing the curl response from the dongle.
*/
struct string {
char *ptr; /*!< String for storage */
size_t len; /*!< Length of stored string */
};
/**
* The callback function from performing the curl command, response from server will be reconstructed here.
*
* @param[in] buffer: Contains chunk of response from server.
* @param[in] size: Size (byte) of type in buffer array.
* @param[in] nmemb: Number of members in buffer.
* @param[out] data: Pointer to area allocated for storing results from server.
*
* @return Number of bytes managed (size*nmemb).
*/
size_t write_func(void *buffer, size_t size, size_t nmemb, void *data);
struct json_object *get_json_string_object_by_key(json_object *json_obj, char *string, char *output_key);
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment