FLTK logo

[master] e5c4c14 - Add replacement for potentially missing trunc() (#944)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] e5c4c14 - Add replacement for potentially missing trunc() (#944) "Albrecht Schlosser" 07:17 Apr 02  
 
commit e5c4c1415bff4ae5e08a7d1ec7cdcce3ba589eb2
Author:     Albrecht Schlosser <albrechts.fltk@online.de>
AuthorDate: Tue Apr 2 15:58:55 2024 +0200
Commit:     Albrecht Schlosser <albrechts.fltk@online.de>
CommitDate: Tue Apr 2 16:03:29 2024 +0200

    Add replacement for potentially missing trunc() (#944)
    
    - add configure + CMake checks to define HAVE_TRUNC in config.h
    
    - src/Fl_Timeout.cxx: add local replacement function

 CMake/resources.cmake | 8 ++++++++
 configh.cmake.in      | 8 ++++++++
 configh.in            | 8 ++++++++
 configure.ac          | 3 +++
 src/Fl_Timeout.cxx    | 8 +++++++-
 5 files changed, 34 insertions(+), 1 deletion(-)

diff --git CMake/resources.cmake CMake/resources.cmake
index 435ff32..c483a11 100644
--- CMake/resources.cmake
+++ CMake/resources.cmake
@@ -156,10 +156,12 @@ find_library(LIB_GLEW NAMES GLEW glew32)
 find_library(LIB_jpeg jpeg)
 find_library(LIB_png png)
 find_library(LIB_zlib z)
+find_library(LIB_m m)
 
 mark_as_advanced(LIB_dl LIB_fontconfig LIB_freetype)
 mark_as_advanced(LIB_GL LIB_MesaGL LIB_GLEW)
 mark_as_advanced(LIB_jpeg LIB_png LIB_zlib)
+mark_as_advanced(LIB_m)
 
 #######################################################################
 # functions
@@ -206,6 +208,12 @@ check_function_exists(vsnprintf                 HAVE_VSNPRINTF)
 
 check_function_exists(setenv                    HAVE_SETENV)
 
+if(LIB_m)
+  set(CMAKE_REQUIRED_LIBRARIES ${LIB_m})
+  check_function_exists(trunc                   HAVE_TRUNC)
+  set(CMAKE_REQUIRED_LIBRARIES)
+endif(LIB_m)
+
 if(HAVE_SCANDIR AND NOT HAVE_SCANDIR_POSIX)
    set(MSG "POSIX compatible scandir")
    message(STATUS "Looking for ${MSG}")
diff --git configh.cmake.in configh.cmake.in
index 982e9f8..1bd719b 100644
--- configh.cmake.in
+++ configh.cmake.in
@@ -249,6 +249,14 @@
 #cmakedefine01 HAVE_SETENV
 
 /*
+ * HAVE_TRUNC:
+ *
+ * Whether or not POSIX trunc() is available from math.h.
+ */
+
+#cmakedefine01 HAVE_TRUNC
+
+/*
  * Do we have various image libraries?
  */
 
diff --git configh.in configh.in
index 54ddf96..49ba71e 100644
--- configh.in
+++ configh.in
@@ -248,6 +248,14 @@
 #define HAVE_SETENV 0
 
 /*
+ * HAVE_TRUNC:
+ *
+ * Whether or not POSIX trunc() is available from math.h.
+ */
+
+#define HAVE_TRUNC 0
+
+/*
  * Do we have various image libraries?
  */
 
diff --git configure.ac configure.ac
index 586aec5..9429b04 100644
--- configure.ac
+++ configure.ac
@@ -635,6 +635,9 @@ AC_CHECK_FUNCS([setenv])
 dnl FLTK library uses math library functions...
 AC_SEARCH_LIBS([pow], [m])
 
+AC_CHECK_HEADERS([math.h])
+AC_CHECK_FUNCS([trunc])
+
 
 dnl Check for largefile support...
 AC_SYS_LARGEFILE
diff --git src/Fl_Timeout.cxx src/Fl_Timeout.cxx
index bc3d0cb..921c40c 100644
--- src/Fl_Timeout.cxx
+++ src/Fl_Timeout.cxx
@@ -2,7 +2,7 @@
 // Timeout support functions for the Fast Light Tool Kit (FLTK).
 //
 // Author: Albrecht Schlosser
-// Copyright 2021-2023 by Bill Spitzak and others.
+// Copyright 2021-2024 by Bill Spitzak and others.
 //
 // This library is free software. Distribution and use rights are outlined in
 // the file "COPYING" which should have been included with this file.  If this
@@ -15,12 +15,18 @@
 //     https://www.fltk.org/bugs.php
 //
 
+#include <config.h>
+
 #include "Fl_Timeout.h"
 #include "Fl_System_Driver.H"
 
 #include <stdio.h>
 #include <math.h> // for trunc()
 
+#if !HAVE_TRUNC
+static inline double trunc(double x) { x >= 0 ? floor(x) : ceil(x); }
+#endif // !HAVE_TRUNC
+
 /**
   \file Fl_Timeout.cxx
 */
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.