improvements to the C wrapper and close a memory leak

This commit is contained in:
Chris Cromer 2023-02-21 21:31:04 -03:00
parent 0e9cc0f832
commit e665ff044f
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
4 changed files with 46 additions and 18 deletions

View File

@ -1,25 +1,47 @@
#ifndef OBELISK_INCLUDE_OBELISK_PROGRAM_H
#define OBELISK_INCLUDE_OBELISK_PROGRAM_H
/**
* @brief Struct wrapper around Obelisk class.
*
*/
typedef struct CObelisk CObelisk;
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Create an obelisk object.
*
* @return CObelisk* Returns an obelisk object.
*/
extern CObelisk* obelisk_open();
/**
* @brief Delete an obelisk object.
*
* @param[in] obelisk The obelisk object.
*/
extern void obelisk_close(CObelisk* obelisk);
/**
* @brief Get the obelisk version.
*
* @param[in] obelisk The obelisk object.
* @return const char* Returns a string containing the version. This must be
* freed by the caller.
*/
extern const char* obelisk_get_version();
extern char* obelisk_get_version(CObelisk* obelisk);
/**
* @brief Get the obelisk library so version.
*
* @param[in] obelisk The obelisk object.
* @return int Returns the so version.
*/
extern int obelisk_get_lib_version();
extern int obelisk_get_lib_version(CObelisk* obelisk);
#ifdef __cplusplus
};

View File

@ -4,20 +4,22 @@
#include <stdlib.h>
#include <string.h>
const char* obelisk_get_version()
CObelisk* obelisk_open()
{
CObelisk* obelisk = create_obelisk();
size_t len = strlen(call_obelisk_getVersion(obelisk)) + 1;
char* version = malloc(len);
memcpy(version, call_obelisk_getVersion(obelisk), len);
destroy_obelisk(obelisk);
return version;
return create_obelisk();
}
int obelisk_get_lib_version()
void obelisk_close(CObelisk* obelisk)
{
CObelisk* obelisk = create_obelisk();
int version = call_obelisk_getLibVersion(obelisk);
destroy_obelisk(obelisk);
return version;
}
char* obelisk_get_version(CObelisk* obelisk)
{
return call_obelisk_getVersion(obelisk);
}
int obelisk_get_lib_version(CObelisk* obelisk)
{
return call_obelisk_getLibVersion(obelisk);
}

View File

@ -1,6 +1,8 @@
#include "obelisk.h"
#include "obelisk_wrapper.h"
#include <string.h>
extern "C"
{
CObelisk* create_obelisk()
@ -9,11 +11,12 @@ extern "C"
return reinterpret_cast<CObelisk*>(obelisk);
}
const char* call_obelisk_getVersion(CObelisk* p_obelisk)
char* call_obelisk_getVersion(CObelisk* p_obelisk)
{
obelisk::Obelisk* obelisk
= reinterpret_cast<obelisk::Obelisk*>(p_obelisk);
return obelisk->getVersion().c_str();
auto version = strdup(obelisk->getVersion().c_str());
return version;
}
int call_obelisk_getLibVersion(CObelisk* p_obelisk)

View File

@ -1,7 +1,7 @@
#ifndef OBELISK_INCLUDE_OBELISK_WRAPPER_H
#define OBELISK_INCLUDE_OBELISK_WRAPPER_H
typedef struct CObelisk CObelisk;
#include "include/obelisk_c.h"
#ifdef __cplusplus
extern "C"
@ -19,9 +19,10 @@ extern "C"
* @brief Calls the obelisk method getVersion.
*
* @param[in] p_obelisk The obelisk object pointer.
* @return const char* Returns the version. Must be freed by caller.
* @return const char* Returns the version. This must be freed by the
* caller.
*/
const char *call_obelisk_getVersion(CObelisk *p_obelisk);
char *call_obelisk_getVersion(CObelisk *p_obelisk);
/**
* @brief Calls the obelisk method getLibVersion.