From 18b92c24f2accb403932d4048c6dc58dfb180f98 Mon Sep 17 00:00:00 2001 From: Syping Date: Fri, 29 Oct 2021 11:52:59 +0200 Subject: [PATCH] added a Simple C API --- CMakeLists.txt | 18 ++++++++++++--- doc/Doxyfile.in | 5 +++- src/RagePhoto.cpp | 31 +++++++++++++++++++++++++ src/RagePhoto.h | 48 +++++++++++++++++++++++++++++++++++++++ src/libragephoto_global.h | 2 +- src/ragephoto.pc.in | 2 +- 6 files changed, 100 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d87962e..e79198a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index ecc5b53..9e1583c 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -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@" diff --git a/src/RagePhoto.cpp b/src/RagePhoto.cpp index 7af78fa..a46df29 100644 --- a/src/RagePhoto.cpp +++ b/src/RagePhoto.cpp @@ -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(instance); + return ragePhoto->load(data, size); +} + +const char* ragephoto_getphotodata(ragephoto_t instance) +{ + RagePhoto *ragePhoto = static_cast(instance); + return ragePhoto->photoData(); +} + +uint32_t ragephoto_getphotosize(ragephoto_t instance) +{ + RagePhoto *ragePhoto = static_cast(instance); + return ragePhoto->photoSize(); +} + +void ragephoto_close(ragephoto_t instance) +{ + RagePhoto *ragePhoto = static_cast(instance); + delete ragePhoto; +} diff --git a/src/RagePhoto.h b/src/RagePhoto.h index 74d0fb0..ab796fb 100644 --- a/src/RagePhoto.h +++ b/src/RagePhoto.h @@ -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 @@ -27,7 +30,9 @@ #include #include +#ifndef LIBRAGEPHOTO_DOXYGEN typedef std::function RagePhotoLoadFunc; +#endif class LIBRAGEPHOTO_EXPORT RagePhoto { @@ -163,5 +168,48 @@ protected: std::unordered_map m_loadFuncs; RagePhotoData m_data; }; +#endif + +#ifdef __cplusplus +extern "C" { +#else +#include "libragephoto_global.h" +#include +#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 diff --git a/src/libragephoto_global.h b/src/libragephoto_global.h index dcbdda6..b689466 100644 --- a/src/libragephoto_global.h +++ b/src/libragephoto_global.h @@ -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"))) diff --git a/src/ragephoto.pc.in b/src/ragephoto.pc.in index f836f20..5a76b7c 100644 --- a/src/ragephoto.pc.in +++ b/src/ragephoto.pc.in @@ -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@