CMakeLists and RagePhoto C file access improvements

- CMakeLists.txt: CMAKE_CXX_STANDARD -> RAGEPHOTO_CXX_STANDARD
- RagePhoto.c: Fix potential issues with file access
This commit is contained in:
Syping 2024-01-18 19:53:47 +01:00
parent e1cf6df258
commit 1a80125802
6 changed files with 26 additions and 39 deletions

View file

@ -1,6 +1,6 @@
#[[************************************************************************** #[[**************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2023 Syping * Copyright (C) 2021-2024 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -21,7 +21,6 @@ project(ragephoto VERSION 0.6.0 LANGUAGES C CXX)
include(GNUInstallDirs) include(GNUInstallDirs)
# RagePhoto CMake includes # RagePhoto CMake includes
include(cmake/cxxstd.cmake)
include(cmake/unicode.cmake) include(cmake/unicode.cmake)
# RagePhoto Top Level # RagePhoto Top Level
@ -88,6 +87,9 @@ else()
endif() endif()
endif() endif()
# RagePhoto C++ Standard
set(RAGEPHOTO_CXX_STANDARD "11" CACHE STRING "libragephoto C++ standard")
# RagePhoto Win32 Shared Resources # RagePhoto Win32 Shared Resources
if (WIN32) if (WIN32)
string(TIMESTAMP ragephoto_BUILD_YEAR "%Y" UTC) string(TIMESTAMP ragephoto_BUILD_YEAR "%Y" UTC)
@ -113,6 +115,10 @@ else()
SOVERSION "${ragephoto_VERSION}" SOVERSION "${ragephoto_VERSION}"
) )
endif() endif()
set_target_properties(ragephoto PROPERTIES
CXX_STANDARD ${RAGEPHOTO_CXX_STANDARD}
CXX_STANDARD_REQUIRED ON
)
target_compile_definitions(ragephoto PRIVATE target_compile_definitions(ragephoto PRIVATE
LIBRAGEPHOTO_LIBRARY LIBRAGEPHOTO_LIBRARY
${LIBRAGEPHOTO_DEFINES} ${LIBRAGEPHOTO_DEFINES}
@ -178,6 +184,8 @@ if (RAGEPHOTO_EXTRACT)
add_executable(ragephoto-extract ${RAGEPHOTO_HEADERS} ${EXTRACT_SOURCES} ${EXTRACT_RESOURCES}) add_executable(ragephoto-extract ${RAGEPHOTO_HEADERS} ${EXTRACT_SOURCES} ${EXTRACT_RESOURCES})
set_target_properties(ragephoto-extract PROPERTIES set_target_properties(ragephoto-extract PROPERTIES
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
CXX_STANDARD ${RAGEPHOTO_CXX_STANDARD}
CXX_STANDARD_REQUIRED ON
) )
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914) if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
target_compile_options(ragephoto-extract PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>) target_compile_options(ragephoto-extract PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)

View file

@ -16,7 +16,7 @@ sudo cmake --install libragephoto-build
``` ```
##### Optional CMake flags ##### Optional CMake flags
`-DCMAKE_CXX_STANDARD=17` `-DRAGEPHOTO_CXX_STANDARD=17`
`-DRAGEPHOTO_BENCHMARK=ON` `-DRAGEPHOTO_BENCHMARK=ON`
`-DRAGEPHOTO_C_API=OFF` `-DRAGEPHOTO_C_API=OFF`
`-DRAGEPHOTO_C_LIBRARY=ON` `-DRAGEPHOTO_C_LIBRARY=ON`

View file

@ -1,21 +0,0 @@
#[[**************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2023 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
****************************************************************************]]
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 11 CACHE STRING "libragephoto C++ standard")
set(CMAKE_CXX_STANDARD_REQUIRED ON)

View file

@ -17,7 +17,7 @@ To customise your libragephoto build, the following <a href="#flags">Optional CM
<h4 id="flags">Optional CMake flags</h4> <h4 id="flags">Optional CMake flags</h4>
\code{.sh} \code{.sh}
-DCMAKE_CXX_STANDARD=17 -DRAGEPHOTO_CXX_STANDARD=17
-DRAGEPHOTO_BENCHMARK=ON -DRAGEPHOTO_BENCHMARK=ON
-DRAGEPHOTO_C_API=OFF -DRAGEPHOTO_C_API=OFF
-DRAGEPHOTO_C_LIBRARY=ON -DRAGEPHOTO_C_LIBRARY=ON

View file

@ -618,29 +618,29 @@ bool ragephoto_loadfile(ragephoto_t instance_t, const char *filename)
#endif #endif
if (!file) if (!file)
return false; return false;
const int64_t fseek_end = fseek(file, 0, SEEK_END); int fseek_ret = fseek(file, 0, SEEK_END);
if (fseek_end == -1) { if (!fseek_ret) {
fclose(file); fclose(file);
return false; return false;
} }
const int64_t fileSize = ftell(file); const long fileSize = ftell(file);
if (fileSize == -1) { if (fileSize == -1L) {
fclose(file); fclose(file);
return false; return false;
} }
const int64_t fseek_set = fseek(file, 0, SEEK_SET); fseek_ret = fseek(file, 0, SEEK_SET);
if (fseek_set == -1) { if (!fseek_ret) {
fclose(file); fclose(file);
return false; return false;
} }
char *data = (char*)(malloc(fileSize)); char *data = (char*)malloc(fileSize);
if (!data) { if (!data) {
fclose(file); fclose(file);
return false; return false;
} }
const size_t fileRsize = fread(data, 1, fileSize, file); const size_t readSize = fread(data, 1, fileSize, file);
fclose(file); fclose(file);
if (fileSize != fileRsize) { if (fileSize != readSize) {
free(data); free(data);
return false; return false;
} }
@ -986,7 +986,7 @@ bool ragephoto_savefilef(ragephoto_t instance_t, const char *filename, uint32_t
{ {
RagePhotoInstance *instance = (RagePhotoInstance*)instance_t; RagePhotoInstance *instance = (RagePhotoInstance*)instance_t;
const size_t fileSize = ragephotodata_getsavesizef(instance->data, instance->parser, photoFormat); const size_t fileSize = ragephotodata_getsavesizef(instance->data, instance->parser, photoFormat);
char *data = (char*)(malloc(fileSize)); char *data = (char*)malloc(fileSize);
if (!data) if (!data)
return false; return false;
if (!ragephotodata_savef(instance->data, instance->parser, data, photoFormat)) { if (!ragephotodata_savef(instance->data, instance->parser, data, photoFormat)) {
@ -1003,10 +1003,10 @@ bool ragephoto_savefilef(ragephoto_t instance_t, const char *filename, uint32_t
free(data); free(data);
return false; return false;
} }
const size_t fileWsize = fwrite(data, sizeof(char), fileSize, file); const size_t writeSize = fwrite(data, sizeof(char), fileSize, file);
fclose(file); fclose(file);
free(data); free(data);
return (fileSize == fileWsize); return (fileSize == writeSize);
} }
bool ragephoto_savefile(ragephoto_t instance_t, const char *filename) bool ragephoto_savefile(ragephoto_t instance_t, const char *filename)

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2023 Syping * Copyright (C) 2023-2024 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -22,7 +22,7 @@
/* CMAKE CONFIG BEGIN */ /* CMAKE CONFIG BEGIN */
#define @LIBRAGEPHOTO_API@ #define @LIBRAGEPHOTO_API@
#define @LIBRAGEPHOTO_LIBTYPE@ #define @LIBRAGEPHOTO_LIBTYPE@
#define RAGEPHOTO_CXX_STD @CMAKE_CXX_STANDARD@ #define RAGEPHOTO_CXX_STD @RAGEPHOTO_CXX_STANDARD@
#define RAGEPHOTO_VERSION "@ragephoto_VERSION@" #define RAGEPHOTO_VERSION "@ragephoto_VERSION@"
#define RAGEPHOTO_VERSION_MAJOR @ragephoto_VERSION_MAJOR@ #define RAGEPHOTO_VERSION_MAJOR @ragephoto_VERSION_MAJOR@
#define RAGEPHOTO_VERSION_MINOR @ragephoto_VERSION_MINOR@ #define RAGEPHOTO_VERSION_MINOR @ragephoto_VERSION_MINOR@