some file ops, some print ops, some memory ops
This commit is contained in:
63
CMakeLists.txt
Normal file
63
CMakeLists.txt
Normal file
@@ -0,0 +1,63 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(akstdlib LANGUAGES C)
|
||||
|
||||
include(CTest)
|
||||
include(GNUInstallDirs)
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(akerror REQUIRED)
|
||||
|
||||
set(akstdlib_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/akstdlib")
|
||||
set(prefix ${CMAKE_INSTALL_PREFIX})
|
||||
set(exec_prefix "\${prefix}")
|
||||
set(libdir "\${exec_prefix}/lib")
|
||||
set(includedir "\${prefix}/include")
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/akstdlib.pc.in ${CMAKE_CURRENT_BINARY_DIR}/akstdlib.pc @ONLY)
|
||||
|
||||
add_library(akstdlib SHARED
|
||||
src/stdlib.c
|
||||
)
|
||||
|
||||
# Specify include directories for the library's headers (if applicable)
|
||||
target_include_directories(akstdlib PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/>
|
||||
)
|
||||
|
||||
target_link_libraries(akstdlib PUBLIC akerror::akerror)
|
||||
|
||||
set(main_lib_dest "lib/my_library-${MY_LIBRARY_VERSION}")
|
||||
install(TARGETS akstdlib EXPORT akstdlib DESTINATION "lib/")
|
||||
install(TARGETS akstdlib
|
||||
EXPORT akstdlibTargets
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
|
||||
install(EXPORT akstdlib FILE akstdlibTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/akstdlib)
|
||||
|
||||
install(FILES "include/akstdlib.h" DESTINATION "include/")
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/akstdlib.pc DESTINATION "lib/pkgconfig/")
|
||||
|
||||
|
||||
install(EXPORT akstdlib
|
||||
FILE akstdlibTargets.cmake
|
||||
NAMESPACE akstdlib::
|
||||
DESTINATION ${akstdlib_install_cmakedir}
|
||||
)
|
||||
|
||||
configure_package_config_file(
|
||||
cmake/akstdlib.cmake.in
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/akstdlibConfig.cmake"
|
||||
INSTALL_DESTINATION ${akstdlib_install_cmakedir}
|
||||
)
|
||||
|
||||
install(FILES
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/akstdlibConfig.cmake"
|
||||
DESTINATION ${akstdlib_install_cmakedir}
|
||||
)
|
||||
|
||||
# pkgconfig
|
||||
10
akstdlib.pc.in
Normal file
10
akstdlib.pc.in
Normal file
@@ -0,0 +1,10 @@
|
||||
prefix=@CMAKE_INSTALL_PREFIX@
|
||||
exec_prefix=${prefix}
|
||||
libdir=${exec_prefix}/lib
|
||||
includedir=${exec_prefix}/include
|
||||
|
||||
Name: akstdlib
|
||||
Description: C stdlib with akerror sanity wrappers
|
||||
Version: @PROJECT_VERSION@
|
||||
Cflags: -I${includedir}/
|
||||
Libs: -L${libdir} -lakstdlib
|
||||
5
cmake/akstdlib.cmake.in
Normal file
5
cmake/akstdlib.cmake.in
Normal file
@@ -0,0 +1,5 @@
|
||||
# cmake/MyLibraryConfig.cmake.in
|
||||
include(CMakeFindDependencyMacro) # If your library has dependencies
|
||||
# find_dependency(AnotherDependency REQUIRED) # Example dependency
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/akstdlibTargets.cmake")
|
||||
25
include/akstdlib.h
Normal file
25
include/akstdlib.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef _AKSTDLIB_H_
|
||||
#define _AKSTDLIB_H_
|
||||
|
||||
#include <akerror.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fopen(char *pathname, char *mode, FILE **fp);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fwrite(void *ptr, size_t size, size_t nmemb, FILE *fp);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fclose(FILE *stream);
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_malloc(size_t size, void **dst);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_memset(void *s, int c, size_t n);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_memcpy(void *d, void *s, size_t n);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_free(void *ptr);
|
||||
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_printf(int *count, const char *restrict format, ...);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fprintf(int *count, FILE *restrict stream, const char *restrict format, ...);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_sprintf(int *count, char *restrict str, const char *restrict format, ...);
|
||||
|
||||
|
||||
#endif
|
||||
11
rebuild.sh
Normal file
11
rebuild.sh
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
export CMAKE_PREFIX_PATH=/home/andrew/local:/home/andrew/local/lib/cmake
|
||||
export CMAKE_MODULE_PATH=/home/andrew/local/lib/cmake
|
||||
#export SDL3_DIR=/home/andrew/local
|
||||
|
||||
rm -fr ~/local/lib/*akstdlib* ~/local/include/*akstdlib* build
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo
|
||||
cmake --build build --parallel 4
|
||||
cmake --install build --prefix /home/andrew/local
|
||||
128
src/stdlib.c
Normal file
128
src/stdlib.c
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <akstdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_malloc(size_t size, void **dst)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, dst, AKERR_NULLPOINTER, "NULL");
|
||||
*dst = malloc(size);
|
||||
FAIL_ZERO_RETURN(e, *dst, errno, "%ld bytes", size);
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_free(void *ptr)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, ptr, AKERR_NULLPOINTER, "NULL");
|
||||
free(ptr);
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_memset(void *s, int c, size_t n)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, s, AKERR_NULLPOINTER, "s=%p", s);
|
||||
FAIL_ZERO_RETURN(e, memset(s, c, n), errno, "Failed to memset");
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_memcpy(void *d, void *s, size_t n)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, d, AKERR_NULLPOINTER, "d=%p, s=%p", d, s);
|
||||
FAIL_ZERO_RETURN(e, s, AKERR_NULLPOINTER, "d=%p, s=%p", d, s);
|
||||
FAIL_ZERO_RETURN(e, (memcpy(d, s, n) == d), errno, "Failed to memcpy");
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fopen(
|
||||
char *pathname,
|
||||
char *mode,
|
||||
FILE **fp)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, fp, AKERR_NULLPOINTER, "NULL");
|
||||
*fp = fopen(pathname, mode);
|
||||
FAIL_ZERO_RETURN(e, *fp, errno, "%s", pathname);
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fread(
|
||||
void *ptr,
|
||||
size_t size, size_t nmemb,
|
||||
FILE *fp)
|
||||
{
|
||||
size_t nmemr;
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, fp, AKERR_NULLPOINTER, "NULL");
|
||||
nmemr = fread(ptr, size, nmemb, fp);
|
||||
if ( nmemr != nmemb ) {
|
||||
FAIL_NONZERO_RETURN(e, feof(fp), AKERR_EOF, "EOF reached");
|
||||
FAIL_NONZERO_RETURN(e, ferror(fp), AKERR_IO, "Error reading file");
|
||||
}
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fwrite(
|
||||
void *ptr,
|
||||
size_t size, size_t nmemb,
|
||||
FILE *fp)
|
||||
{
|
||||
size_t nmemw;
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, fp, AKERR_NULLPOINTER, "NULL argument");
|
||||
nmemw = fwrite(ptr, size, nmemb, fp);
|
||||
if ( nmemw != nmemb ) {
|
||||
FAIL_NONZERO_RETURN(e, feof(fp), AKERR_EOF, "EOF reached");
|
||||
FAIL_NONZERO_RETURN(e, ferror(fp), AKERR_IO, "Error reading file");
|
||||
}
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fclose(FILE *stream)
|
||||
{
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, stream, AKERR_NULLPOINTER, "NULL");
|
||||
FAIL_NONZERO_RETURN(e, fclose(stream), errno, "Failed to fclose");
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_printf(int *count, const char *restrict format, ...)
|
||||
{
|
||||
va_list args;
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, count, AKERR_NULLPOINTER, "count=%p, format=%p", (void *)count, (void *)format);
|
||||
FAIL_ZERO_RETURN(e, format, AKERR_NULLPOINTER, "count=%p, format=%p", (void *)count, (void *)format);
|
||||
*count = vprintf(format, args);
|
||||
FAIL_NONZERO_RETURN(e, (*count == -1), errno, "Short write");
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fprintf(int *count, FILE *restrict stream, const char *restrict format, ...)
|
||||
{
|
||||
va_list args;
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, count, AKERR_NULLPOINTER, "count=%p, stream=%p, format=%p", (void *)count, (void *)stream, (void *)format);
|
||||
FAIL_ZERO_RETURN(e, stream, AKERR_NULLPOINTER, "count=%p, stream=%p, format=%p", (void *)count, (void *)stream, (void *)format);
|
||||
FAIL_ZERO_RETURN(e, format, AKERR_NULLPOINTER, "count=%p, stream=%p, format=%p", (void *)count, (void *)stream, (void *)format);
|
||||
*count = vfprintf(stream, format, args);
|
||||
FAIL_NONZERO_RETURN(e, (*count == -1), errno, "Short write");
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_sprintf(int *count, char *restrict str, const char *restrict format, ...)
|
||||
{
|
||||
va_list args;
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, count, AKERR_NULLPOINTER, "count=%p, str=%p, format=%p", (void *)count, (void *)str, (void *)format);
|
||||
FAIL_ZERO_RETURN(e, str, AKERR_NULLPOINTER, "count=%p, str=%p, format=%p", (void *)count, (void *)str, (void *)format);
|
||||
FAIL_ZERO_RETURN(e, format, AKERR_NULLPOINTER, "count=%p, str=%p, format=%p", (void *)count, (void *)str, (void *)format);
|
||||
*count = vsprintf(str, format, args);
|
||||
FAIL_NONZERO_RETURN(e, (*count == -1), errno, "Short write");
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
Reference in New Issue
Block a user