added a Simple C API

This commit is contained in:
Syping 2021-10-29 11:52:59 +02:00
parent 4785fafd67
commit 18b92c24f2
6 changed files with 100 additions and 6 deletions

View file

@ -57,14 +57,15 @@ set(RAGEPHOTO_SOURCES
option(BUILD_SHARED "Build libragephoto as shared library" ON)
if (BUILD_SHARED)
option(WITH_C_API "Build libragephoto with C API support" ON)
add_library(ragephoto SHARED ${RAGEPHOTO_HEADERS} ${RAGEPHOTO_SOURCES})
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(LIBRAGEPHOTO_LIBTYPE -DLIBRAGEPHOTO_SHARED)
set(LIBRAGEPHOTO_LIBTYPE LIBRAGEPHOTO_SHARED)
else()
option(WITH_C_API "Build libragephoto with C API support" OFF)
add_library(ragephoto STATIC ${RAGEPHOTO_HEADERS} ${RAGEPHOTO_SOURCES})
set(LIBRAGEPHOTO_LIBTYPE -DLIBRAGEPHOTO_STATIC)
set(LIBRAGEPHOTO_LIBTYPE LIBRAGEPHOTO_STATIC)
endif()
configure_file(src/ragephoto.pc.in pkgconfig/ragephoto.pc @ONLY)
option(WITH_BENCHMARK "Build with libragephoto benchmark" OFF)
if (WITH_BENCHMARK)
@ -73,11 +74,22 @@ if (WITH_BENCHMARK)
)
endif()
if (WITH_C_API)
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_API)
else()
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_NOAPI)
endif()
list(APPEND LIBRAGEPHOTO_DEFINES
${LIBRAGEPHOTO_API}
)
configure_file(src/ragephoto.pc.in pkgconfig/ragephoto.pc @ONLY)
target_compile_definitions(ragephoto PRIVATE
LIBRAGEPHOTO_LIBRARY
${LIBRAGEPHOTO_DEFINES}
)
target_compile_definitions(ragephoto PUBLIC
${LIBRAGEPHOTO_API}
${LIBRAGEPHOTO_LIBTYPE}
)
install(TARGETS ragephoto DESTINATION ${CMAKE_INSTALL_LIBDIR})

View file

@ -7,4 +7,7 @@ EXTRACT_PRIVATE = NO
ENABLE_PREPROCESSING = YES
MACRO_EXPANSION = YES
EXPAND_ONLY_PREDEF = YES
PREDEFINED = protected=private
PREDEFINED = "protected=private" \
"__cplusplus" \
"LIBRAGEPHOTO_DOXYGEN" \
"@LIBRAGEPHOTO_API@"

View file

@ -864,3 +864,34 @@ inline void RagePhoto::uInt32ToCharLE(uint32_t x, char *y)
y[2] = x >> 16;
y[3] = x >> 24;
}
// C API
ragephoto_t ragephoto_open()
{
return new RagePhoto;
}
bool ragephoto_load(ragephoto_t instance, const char *data, size_t size)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->load(data, size);
}
const char* ragephoto_getphotodata(ragephoto_t instance)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->photoData();
}
uint32_t ragephoto_getphotosize(ragephoto_t instance)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->photoSize();
}
void ragephoto_close(ragephoto_t instance)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
delete ragePhoto;
}

View file

@ -16,9 +16,12 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
/** \file RagePhoto.h */
#ifndef RAGEPHOTO_H
#define RAGEPHOTO_H
#ifdef __cplusplus
#include "libragephoto_data.h"
#include "libragephoto_global.h"
#include <unordered_map>
@ -27,7 +30,9 @@
#include <cstdint>
#include <cstdio>
#ifndef LIBRAGEPHOTO_DOXYGEN
typedef std::function<bool(const char*, size_t, RagePhotoData*)> RagePhotoLoadFunc;
#endif
class LIBRAGEPHOTO_EXPORT RagePhoto
{
@ -163,5 +168,48 @@ protected:
std::unordered_map<uint8_t, RagePhotoLoadFunc> m_loadFuncs;
RagePhotoData m_data;
};
#endif
#ifdef __cplusplus
extern "C" {
#else
#include "libragephoto_global.h"
#include <stdint.h>
#endif
#ifdef LIBRAGEPHOTO_C_API
typedef void* ragephoto_t;
/** Opens a \p ragephoto_t instance.
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_EXPORT ragephoto_t ragephoto_open();
/** Loads a Photo from a const char*.
* \param instance \p ragephoto_t instance
* \param data Photo data
* \param size Photo data size
*/
LIBRAGEPHOTO_EXPORT bool ragephoto_load(ragephoto_t instance, const char *data, size_t size);
/** Returns the Photo JPEG data.
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_EXPORT const char* ragephoto_getphotodata(ragephoto_t instance);
/** Returns the Photo JPEG data size.
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_EXPORT uint32_t ragephoto_getphotosize(ragephoto_t instance);
/** Closes a \p ragephoto_t instance.
* \param instance \p ragephoto_t instance
*/
LIBRAGEPHOTO_EXPORT void ragephoto_close(ragephoto_t instance);
#endif
#ifdef __cplusplus
}
#endif
#endif // RAGEPHOTO_H

View file

@ -27,7 +27,7 @@
#define LIBRAGEPHOTO_EXPORT __declspec(dllimport)
#endif
#else
#define LIBRAGEPHOTO_EXPORT __attribute__((visibility("default")))
#define LIBRAGEPHOTO_EXPORT
#endif
#else
#define LIBRAGEPHOTO_EXPORT __attribute__((visibility("default")))

View file

@ -6,4 +6,4 @@ Name: libragephoto
Description: Open Source RAGE Photo Parser for GTA V
Version: @PROJECT_VERSION@
Libs: -L${libdir} -lragephoto
Cflags: -I${includedir} @LIBRAGEPHOTO_LIBTYPE@
Cflags: -I${includedir} -D@LIBRAGEPHOTO_API@ -D@LIBRAGEPHOTO_LIBTYPE@