Skip to content

Commit

Permalink
Use mat_snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeu committed Feb 7, 2024
1 parent 24959aa commit eae9647
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ pkgconfig_DATA = matio.pc
$(pkgconfig_DATA): config.status

if NEED_GETOPT
GETOPT=getopt
GETOPT = getopt
endif

SUBDIRS = src $(GETOPT) tools test documentation
SUBDIRS = snprintf src $(GETOPT) tools test documentation

dosubst = sed -e 's,[@]PACKAGE[@],$(PACKAGE),g' \
-e 's,[@]VERSION[@],$(VERSION),g' \
Expand Down
2 changes: 1 addition & 1 deletion cmake/src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ set(MATIO_SOURCES
${PROJECT_SOURCE_DIR}/src/matvar_struct.c
${PROJECT_SOURCE_DIR}/src/mat4.c
${PROJECT_SOURCE_DIR}/src/mat5.c
${PROJECT_SOURCE_DIR}/src/snprintf.c
${PROJECT_SOURCE_DIR}/src/read_data.c
${PROJECT_SOURCE_DIR}/snprintf/snprintf.c
${PROJECT_SOURCE_DIR}/src/mat5.h
${PROJECT_SOURCE_DIR}/src/mat73.h
${PROJECT_SOURCE_DIR}/src/matio_private.h
Expand Down
2 changes: 1 addition & 1 deletion cmake/test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if(NOT HAVE_GETOPT)
endif()

if(NOT HAVE_SNPRINTF OR UNIX)
add_executable(test_snprintf ${PROJECT_SOURCE_DIR}/test/test_snprintf.c)
add_executable(test_snprintf ${PROJECT_SOURCE_DIR}/test/test_snprintf.c ${PROJECT_SOURCE_DIR}/snprintf/snprintf.c)
if(HAVE_LIBM)
target_link_libraries(test_snprintf m)
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/tools.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(matdump ${PROJECT_SOURCE_DIR}/tools/matdump.c )
add_executable(matdump ${PROJECT_SOURCE_DIR}/tools/matdump.c ${PROJECT_SOURCE_DIR}/snprintf/snprintf.c)
target_link_libraries(matdump matio)
if(NOT HAVE_GETOPT)
target_link_libraries(matdump getopt)
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ AC_SUBST([AUTOTEST], ['$(AUTOM4TE) --language=Autotest'])

AC_CONFIG_FILES([Makefile \
matio.pc \
snprintf/Makefile \
src/Makefile \
getopt/Makefile \
tools/Makefile \
Expand Down
30 changes: 30 additions & 0 deletions snprintf/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Makefile.am -- Process this file with automake to produce Makefile.in
##
## Copyright (c) 2015-2023, The matio contributors
## Copyright (c) 2005-2014, Christopher C. Hulbert
## All rights reserved.
##
## 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##

noinst_LTLIBRARIES = libsnprintf.la
libsnprintf_la_SOURCES = snprintf.c
File renamed without changes.
8 changes: 5 additions & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ if HAVE_ZLIB
ZLIB_SRC = inflate.c
endif

SNPRINTF_LIBS = $(top_builddir)/snprintf/libsnprintf.la

nodist_noinst_HEADERS = matioConfig.h
nodist_include_HEADERS = matio_pubconf.h
include_HEADERS = matio.h
noinst_HEADERS = matio_private.h mat4.h mat5.h mat73.h \
read_data_impl.h safe-math.h
lib_LTLIBRARIES = libmatio.la
libmatio_la_SOURCES = snprintf.c endian.c io.c $(ZLIB_SRC) read_data.c \
libmatio_la_SOURCES = endian.c io.c $(ZLIB_SRC) read_data.c \
mat5.c mat4.c mat.c matvar_cell.c matvar_struct.c
libmatio_la_LIBADD = $(HDF5_LIBS) $(ZLIB_LIBS)
libmatio_la_LIBADD = $(HDF5_LIBS) $(ZLIB_LIBS) $(SNPRINTF_LIBS)
libmatio_la_LDFLAGS = -no-undefined -export-symbols @srcdir@/matio.sym $(AM_LDFLAGS)

if MAT73
libmatio_la_SOURCES+= mat73.c
libmatio_la_SOURCES += mat73.c
endif

EXTRA_DIST = matio.sym
Expand Down
5 changes: 3 additions & 2 deletions src/mat73.c
Original file line number Diff line number Diff line change
Expand Up @@ -1401,11 +1401,12 @@ Mat_VarWriteRef(hid_t id, matvar_t *matvar, enum matio_compression compression,
err = MATIO_E_BAD_ARGUMENT;
} else {
char obj_name[64];
snprintf(obj_name, sizeof(obj_name), "%llu", (unsigned long long)group_info.nlinks);
mat_snprintf(obj_name, sizeof(obj_name), "%llu", (unsigned long long)group_info.nlinks);
if ( NULL != matvar )
matvar->compression = compression;
err = Mat_VarWriteNext73(*refs_id, matvar, obj_name, refs_id);
snprintf(obj_name, sizeof(obj_name), "/#refs#/%llu", (unsigned long long)group_info.nlinks);
mat_snprintf(obj_name, sizeof(obj_name), "/#refs#/%llu",
(unsigned long long)group_info.nlinks);
H5Rcreate(ref, id, obj_name, H5R_OBJECT, -1);
}
return err;
Expand Down
24 changes: 14 additions & 10 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -405,27 +405,27 @@ TEST_DATAFILES = $(datasets_dir)/large_struct_compressed_le.mat \
$(results_dir)/write_struct_complex_2d_numeric-uint32.out \
$(results_dir)/write_struct_complex_2d_numeric-uint64.out
if MAT73
MAT73 = 1
MAT73 = 1
else
MAT73 = 0
MAT73 = 0
endif

if HAVE_ZLIB
COMPRESSION_ZLIB = 1
COMPRESSION_ZLIB = 1
else
COMPRESSION_ZLIB = 0
COMPRESSION_ZLIB = 0
endif

if EXTENDED_SPARSE
EXTENDED_SPARSE = 1
EXTENDED_SPARSE = 1
else
EXTENDED_SPARSE = 0
EXTENDED_SPARSE = 0
endif

if IS_WIN32
IS_WIN32 = 1
IS_WIN32 = 1
else
IS_WIN32 = 0
IS_WIN32 = 0
endif

CHECK_ENVIRONMENT = GREP=$(GREP) MAT73=$(MAT73) MATLABEXE=$(MATLABEXE) \
Expand All @@ -439,10 +439,12 @@ EXTRA_DIST = $(TESTSUITE_AT) $(TESTSUITE) $(srcdir)/package.m4 \
$(TEST_DATAFILES)

if NEED_GETOPT
GETOPT_CFLAGS = -I$(top_builddir)/getopt
GETOPT_LIBS = $(top_builddir)/getopt/libgetopt.la
GETOPT_CFLAGS = -I$(top_builddir)/getopt
GETOPT_LIBS = $(top_builddir)/getopt/libgetopt.la
endif

SNPRINTF_LIBS = $(top_builddir)/snprintf/libsnprintf.la

AM_CFLAGS = -I$(top_srcdir)/src $(GETOPT_CFLAGS) $(HDF5_CFLAGS) $(ZLIB_CFLAGS)
AM_LDFLAGS = $(FCLDFLAGS)
TEST_LIBS = $(top_builddir)/src/libmatio.la $(GETOPT_LIBS) $(HDF5_LIBS) $(ZLIB_LIBS)
Expand All @@ -455,6 +457,8 @@ test_mat_LDADD = $(TEST_LIBS)
test_mat_LDFLAGS = $(TEST_LFLAGS)

test_snprintf_SOURCES = test_snprintf.c
test_snprintf_LDADD = $(SNPRINTF_LIBS)
test_snprintf_LDFLAGS = -L$(top_builddir)/snprintf

atconfig: $(top_builddir)/config.status
cd $(top_builddir) && ./config.status tests/$@
Expand Down
2 changes: 1 addition & 1 deletion test/test_snprintf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "matioConfig.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "snprintf.c"

#if !HAVE_SNPRINTF
int rpl_snprintf(char *, size_t, const char *, ...);
Expand Down
10 changes: 6 additions & 4 deletions tools/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
VPATH = @srcdir@

if NEED_GETOPT
GETOPT_CFLAGS = -I$(top_builddir)/getopt
GETOPT_LIBS = $(top_builddir)/getopt/libgetopt.la
GETOPT_CFLAGS = -I$(top_builddir)/getopt
GETOPT_LIBS = $(top_builddir)/getopt/libgetopt.la
endif

SNPRINTF_LIBS = $(top_builddir)/snprintf/libsnprintf.la

AM_CFLAGS = -I$(top_srcdir)/src $(GETOPT_CFLAGS) $(HDF5_CFLAGS) $(ZLIB_CFLAGS) \
$(LT_CFLAGS)
AM_LDFLAGS = $(LT_LDFLAGS) $(FCLDFLAGS)

bin_PROGRAMS = matdump

matdump_SOURCES = matdump.c
matdump_SOURCES = matdump.c
matdump_LDADD = $(top_builddir)/src/libmatio.la $(HDF5_LIBS) $(ZLIB_LIBS) \
$(GETOPT_LIBS)
$(GETOPT_LIBS) $(SNPRINTF_LIBS)
11 changes: 10 additions & 1 deletion tools/matdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "matioConfig.h"
#include "matio.h"
#include <getopt.h>
#include <stdlib.h>
Expand All @@ -38,6 +39,14 @@
#define SIZE_T_FMTSTR "zu"
#endif

/* snprintf.c */
#if !HAVE_SNPRINTF
int rpl_snprintf(char *, size_t, const char *, ...);
#define mat_snprintf rpl_snprintf
#else
#define mat_snprintf snprintf
#endif /* !HAVE_SNPRINTF */

static const char *optstring = "df:hvHV";
static struct option options[] = {{"data", no_argument, NULL, 'd'},
{"format", required_argument, NULL, 'f'},
Expand Down Expand Up @@ -539,7 +548,7 @@ print_whos(matvar_t *matvar)
printf("%8" SIZE_T_FMTSTR, matvar->dims[0]);
for ( i = 1; i < matvar->rank; i++ ) {
if ( ceil(log10((double)matvar->dims[i])) + 1 < 32 ) {
cnt += snprintf(size + cnt, sizeof(size) - cnt, "x%" SIZE_T_FMTSTR, matvar->dims[i]);
cnt += mat_snprintf(size + cnt, sizeof(size) - cnt, "x%" SIZE_T_FMTSTR, matvar->dims[i]);
if (cnt >= sizeof(size)) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion visual_studio/libmatio/libmatio.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@
>
</File>
<File
RelativePath="..\..\src\snprintf.c"
RelativePath="..\..\snprintf\snprintf.c"
>
</File>
</Filter>
Expand Down
2 changes: 1 addition & 1 deletion visual_studio/libmatio/libmatio.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
<ClCompile Include="..\..\src\matvar_cell.c" />
<ClCompile Include="..\..\src\matvar_struct.c" />
<ClCompile Include="..\..\src\read_data.c" />
<ClCompile Include="..\..\src\snprintf.c" />
<ClCompile Include="..\..\snprintf\snprintf.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\mat4.h" />
Expand Down
2 changes: 1 addition & 1 deletion visual_studio/libmatio/libmatio.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<ClCompile Include="..\..\src\read_data.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\snprintf.c">
<ClCompile Include="..\..\snprintf\snprintf.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions visual_studio/matdump/matdump.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@
RelativePath="..\..\tools\matdump.c"
>
</File>
<File
RelativePath="..\..\snprintf\snprintf.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
Expand Down
1 change: 1 addition & 0 deletions visual_studio/matdump/matdump.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
<ItemGroup>
<ClCompile Include="..\..\getopt\getopt_long.c" />
<ClCompile Include="..\..\tools\matdump.c" />
<ClCompile Include="..\..\snprintf\snprintf.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\getopt\getopt.h" />
Expand Down
3 changes: 3 additions & 0 deletions visual_studio/matdump/matdump.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<ClCompile Include="..\..\tools\matdump.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\snprintf\snprintf.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\getopt\getopt.h">
Expand Down

0 comments on commit eae9647

Please sign in to comment.