From d3fa759ecd5a8b43e46306615b1dd13f9833018e Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 15 Jul 2001 18:19:40 +0000 Subject: [PATCH] 2001-07-15 20:15 MEST Martin Vogel --- harbour/ChangeLog | 8 ++ harbour/doc/en/math.txt | 241 ++++++++++++++++++++++++++++++++++++++ harbour/include/hbmath.h | 112 ++++++++++++++++++ harbour/source/rtl/math.c | 184 +++++++++++++++++++++++++---- 4 files changed, 521 insertions(+), 24 deletions(-) create mode 100644 harbour/include/hbmath.h diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 7bf11bdaff..fb5806c30a 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -1,3 +1,11 @@ +2001-07-15 20:15 MEST Martin Vogel + * source/rtl/math.c + + added functions for custom math handlers + + include/hbmath.h + + created header file for Harbour Math API + * doc/en/math.txt + + added some documentation for Harbour Math API functions + 2001-07-15 16:23 GMT+1 Patrick Mast * source/rtl/profiler.prg + Added the parameter. If profiler is used like diff --git a/harbour/doc/en/math.txt b/harbour/doc/en/math.txt index de8f0b0b43..05ab707b25 100644 --- a/harbour/doc/en/math.txt +++ b/harbour/doc/en/math.txt @@ -9,6 +9,9 @@ * Copyright 2000 Luiz Rafael Culik * Documentation for: ABS(),EXP(),LOG(),INT(),MAX() * MIN(),SQRT(),ROUND() + * Copyright 2001 IntTec GmbH, Neunlindenstr 32, 79106 Freiburg, Germany + * Author: Martin Vogel + * Documentation for Math API functions * See doc/license.txt for licensing terms. * */ @@ -337,3 +340,241 @@ * INT(),STR(),VAL(),SET FIXED * $END$ */ + +/* $DOC$ + * $FUNCNAME$ + * hb_getMathError() + * $CATEGORY$ + * Math API + * $ONELINER$ + * get the last math lib error + * $SYNTAX$ + * C Prototype + * + * #include + * hb_getMathError (void) --> int iMathError + * $ARGUMENTS$ + * + * $RETURNS$ + * + * $DESCRIPTION$ + * + * $EXAMPLES$ + * + * $STATUS$ + * R + * $COMPLIANCE$ + * Compliance is not applicable to API calls. + * $FILES$ + * Library is rtl + * $PLATFORMS$ + * All + * $SEEALSO$ + * + * $END$ + */ + +/* $DOC$ + * $FUNCNAME$ + * hb_resetMathError() + * $CATEGORY$ + * Math API + * $ONELINER$ + * reset the math error, i.e. set it to 0 + * $SYNTAX$ + * C Prototype + * + * #include + * hb_resetMathError (void) --> void + * $ARGUMENTS$ + * + * $RETURNS$ + * + * $DESCRIPTION$ + * + * $EXAMPLES$ + * + * $STATUS$ + * R + * $COMPLIANCE$ + * Compliance is not applicable to API calls. + * $FILES$ + * Library is rtl + * $PLATFORMS$ + * All + * $SEEALSO$ + * + * $END$ + */ + +/* $DOC$ + * $FUNCNAME$ + * hb_isMathHandler() + * $CATEGORY$ + * Math API + * $ONELINER$ + * check if harbour math error handler is available + * $SYNTAX$ + * C Prototype + * + * #include + * hb_isMathHandler (void) --> int iIsMathHandler + * $ARGUMENTS$ + * + * $RETURNS$ + * + * $DESCRIPTION$ + * + * $EXAMPLES$ + * + * $STATUS$ + * R + * $COMPLIANCE$ + * Compliance is not applicable to API calls. + * $FILES$ + * Library is rtl + * $PLATFORMS$ + * All + * $SEEALSO$ + * + * $END$ + */ + +/* $DOC$ + * $FUNCNAME$ + * hb_installMathHandler() + * $CATEGORY$ + * Math API + * $ONELINER$ + * add a custom math handler to the math error handler chain + * $SYNTAX$ + * C Prototype + * + * #include + * hb_installMathHandler (HB_MATH_HANDLERPROC handlerproc) --> HB_MATH_HANDLERHANDLE handle + * $ARGUMENTS$ + * handlerproc custom math handler + * $RETURNS$ + * handle handle to handlerproc in handle chain + * $DESCRIPTION$ + * + * $EXAMPLES$ + * + * $STATUS$ + * R + * $COMPLIANCE$ + * Compliance is not applicable to API calls. + * $FILES$ + * Library is rtl + * $PLATFORMS$ + * All + * $SEEALSO$ + * + * $END$ + */ + +/* $DOC$ + * $FUNCNAME$ + * hb_deinstallMathHandler() + * $CATEGORY$ + * Math API + * $ONELINER$ + * remove custom math handler from the math error handler chain + * $SYNTAX$ + * C Prototype + * + * #include + * hb_deinstallMathHandler (HB_MATH_HANDLERHANDLE handle) --> int iSuccess + * $ARGUMENTS$ + * handle handle returned be hb_installMathHandler() + * $RETURNS$ + * iSuccess success of operation + * $DESCRIPTION$ + * + * $EXAMPLES$ + * + * $STATUS$ + * R + * $COMPLIANCE$ + * Compliance is not applicable to API calls. + * $FILES$ + * Library is rtl + * $PLATFORMS$ + * All + * $SEEALSO$ + * + * $END$ + */ + +/* $DOC$ + * $FUNCNAME$ + * hb_setMathHandlerStatus() + * $CATEGORY$ + * Math API + * $ONELINER$ + * set the status of a custom math handler in the math error handler chain + * $SYNTAX$ + * C Prototype + * + * #include + * hb_setMathHandlerStatus (HB_MATH_HANDLERHANDLE handle, int status) --> int iSuccess + * $ARGUMENTS$ + * handle handle returned be hb_installMathHandler() + * status new status value, can be one of: + * HB_MATH_HANDLER_STATUS_INACTIVE + * --> handler is present but not active + * HB_MATH_HANDLER_STATUS_ACTIVE + * --> handler is present and active + * $RETURNS$ + * iSuccess success of operation + * $DESCRIPTION$ + * + * $EXAMPLES$ + * + * $STATUS$ + * R + * $COMPLIANCE$ + * Compliance is not applicable to API calls. + * $FILES$ + * Library is rtl + * $PLATFORMS$ + * All + * $SEEALSO$ + * + * $END$ + */ + +/* $DOC$ + * $FUNCNAME$ + * hb_getMathHandlerStatus() + * $CATEGORY$ + * Math API + * $ONELINER$ + * get the status of a custom math handler in the math error handler chain + * $SYNTAX$ + * C Prototype + * + * #include + * hb_getMathHandlerStatus (HB_MATH_HANDLERHANDLE handle) --> int iStatus + * $ARGUMENTS$ + * handle handle returned be hb_installMathHandler() + * $RETURNS$ + * iStatus status of math handler + * $DESCRIPTION$ + * + * $EXAMPLES$ + * + * $STATUS$ + * R + * $COMPLIANCE$ + * Compliance is not applicable to API calls. + * $FILES$ + * Library is rtl + * $PLATFORMS$ + * All + * $SEEALSO$ + * + * $END$ + */ + + diff --git a/harbour/include/hbmath.h b/harbour/include/hbmath.h new file mode 100644 index 0000000000..2561269ce0 --- /dev/null +++ b/harbour/include/hbmath.h @@ -0,0 +1,112 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * Harbour math functions and API + * + * Copyright 2001 IntTec GmbH, Neunlindenstr 32, 79106 Freiburg, Germany + * Author: Martin Vogel + * www - http://www.harbour-project.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/). + * + * As a special exception, the Harbour Project gives permission for + * additional uses of the text contained in its release of Harbour. + * + * The exception is that, if you link the Harbour libraries with other + * files to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the Harbour library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the Harbour + * Project under the name Harbour. If you copy code from other + * Harbour Project or Free Software Foundation releases into a copy of + * Harbour, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for Harbour, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. + * + */ + +#ifndef HB_MATH_H_ +#define HB_MATH_H_ + +#include "hbapi.h" +#include + +#if defined(HB_EXTERN_C) +extern "C" { +#endif + +#if defined(__WATCOMC__) + #define HB_MATH_HANDLER + #define exception _exception +#elif defined(__BORLANDC__) + #if (__BORLANDC__ == 1328) && defined(__cplusplus) + /* NOTE: There seem to be a bug in Borland C++ 5.3 C++ mode which prevents + the redefinition of matherr, because nor "_exception" neither + "exception" will work. [vszakats] */ + #else + #define HB_MATH_HANDLER + #define matherr _matherr + /* NOTE: This is needed for Borland C++ 5.5 in C++/STDC mode. [vszakats] */ + #if (__BORLANDC__ >= 1360) + #define exception _exception + #endif + #endif +#elif defined(__MINGW32__) + #define HB_MATH_HANDLER + #define matherr _matherr + #define exception _exception +#endif + +extern int hb_getMathError (void); +extern void hb_resetMathError (void); +extern int hb_isMathHandler (void); + +typedef int (* HB_MATH_HANDLERPROC)(struct exception *err); +typedef struct HB_MATH_HANDLERCHAINELEMENT_ +{ + HB_MATH_HANDLERPROC handlerproc; + int status; + struct HB_MATH_HANDLERCHAINELEMENT_ * pnext; +} HB_MATH_HANDLERCHAINELEMENT, * PHB_MATH_HANDLERCHAINELEMENT; +typedef PHB_MATH_HANDLERCHAINELEMENT HB_MATH_HANDLERHANDLE; + +extern HB_MATH_HANDLERHANDLE hb_installMathHandler (HB_MATH_HANDLERPROC handlerproc); +extern int hb_deinstallMathHandler (HB_MATH_HANDLERHANDLE handle); +extern int hb_setMathHandlerStatus (HB_MATH_HANDLERHANDLE handle, int status); +extern int hb_getMathHandlerStatus (HB_MATH_HANDLERHANDLE handle); + +#define HB_MATH_HANDLER_STATUS_INACTIVE ((int)0) +#define HB_MATH_HANDLER_STATUS_ACTIVE ((int)1) + + +#if defined(HB_EXTERN_C) +} +#endif + +#endif /* HB_MATH_H_ */ diff --git a/harbour/source/rtl/math.c b/harbour/source/rtl/math.c index 2cc4af62d0..d2f553ccdf 100644 --- a/harbour/source/rtl/math.c +++ b/harbour/source/rtl/math.c @@ -7,6 +7,11 @@ * Math functions * * Copyright 1999 Matthew Hamilton + * + * Functions for user defined math error handlers + * Copyright 2001 IntTec GmbH, Freiburg, Germany, + * Author: Martin Vogel + * * www - http://www.harbour-project.org * * This program is free software; you can redistribute it and/or modify @@ -55,28 +60,8 @@ #include "hbapi.h" #include "hbapiitm.h" #include "hbapierr.h" +#include "hbmath.h" -#if defined(__WATCOMC__) - #define HB_MATH_HANDLER - #define exception _exception -#elif defined(__BORLANDC__) - #if (__BORLANDC__ == 1328) && defined(__cplusplus) - /* NOTE: There seem to be a bug in Borland C++ 5.3 C++ mode which prevents - the redefinition of matherr, because nor "_exception" neither - "exception" will work. [vszakats] */ - #else - #define HB_MATH_HANDLER - #define matherr _matherr - /* NOTE: This is needed for Borland C++ 5.5 in C++/STDC mode. [vszakats] */ - #if (__BORLANDC__ >= 1360) - #define exception _exception - #endif - #endif -#elif defined(__MINGW32__) - #define HB_MATH_HANDLER - #define matherr _matherr - #define exception _exception -#endif #if defined(HB_MATH_HANDLER) @@ -92,11 +77,116 @@ void hb_resetMathError( void ) s_internal_math_error = 0; } +/* math handler present ? */ +int hb_isMathHandler( void ) +{ + return (1); +} + + +static PHB_MATH_HANDLERCHAINELEMENT s_pChain = NULL; /* TODO: make this thread safe */ + +/* install custom math handler */ +HB_MATH_HANDLERHANDLE hb_installMathHandler (HB_MATH_HANDLERPROC handlerproc) +{ + + PHB_MATH_HANDLERCHAINELEMENT pChain, pNewChainelement; + + pNewChainelement = hb_xgrab (sizeof (HB_MATH_HANDLERCHAINELEMENT)); + pNewChainelement->handlerproc = handlerproc; + pNewChainelement->status = HB_MATH_HANDLER_STATUS_ACTIVE; + /* initially activated */ + pNewChainelement->pnext = NULL; + + pChain = s_pChain; + if (pChain == NULL) + { + s_pChain = pNewChainelement; + } + else + { + while (pChain->pnext != NULL) + pChain = pChain->pnext; + pChain->pnext = pNewChainelement; + } + + return ((HB_MATH_HANDLERHANDLE)pNewChainelement); + +} + +/* deinstall custom math handler */ +int hb_deinstallMathHandler (HB_MATH_HANDLERHANDLE handle) +{ + + PHB_MATH_HANDLERCHAINELEMENT pChain; + + if (handle != NULL) + { + if (s_pChain == (PHB_MATH_HANDLERCHAINELEMENT)handle) + { + s_pChain = ((PHB_MATH_HANDLERCHAINELEMENT)handle)->pnext; + return (0); + } + else + { + pChain = s_pChain; + + while (pChain != NULL) + { + if (pChain->pnext == (PHB_MATH_HANDLERCHAINELEMENT)handle) + { + pChain->pnext = ((PHB_MATH_HANDLERCHAINELEMENT)handle)->pnext; + hb_xfree ((void *)handle); + return (0); + } + + pChain = pChain->pnext; + } + } + } + + return (-1); /* not found, not deinstalled, so return error code */ + +} + +/* set custom math handler status */ +int hb_setMathHandlerStatus (HB_MATH_HANDLERHANDLE handle, int status) +{ + int oldstatus = ((PHB_MATH_HANDLERCHAINELEMENT)handle)->status; + ((PHB_MATH_HANDLERCHAINELEMENT)handle)->status = status; + + return (oldstatus); +} + +/* get custom math handler status */ +int hb_getMathHandlerStatus (HB_MATH_HANDLERHANDLE handle) +{ + return (((PHB_MATH_HANDLERCHAINELEMENT)handle)->status); +} + + /* define harbour specific error handler for math errors */ int matherr( struct exception * err ) { + + PHB_MATH_HANDLERCHAINELEMENT pChain = s_pChain; + int retval = -1; + HB_TRACE(HB_TR_DEBUG, ("matherr(%p)", err)); + + /* call custom math handlers */ + while (pChain != NULL) + { + int ret; + if (pChain->status == HB_MATH_HANDLER_STATUS_ACTIVE) + { + ret = (*(pChain->handlerproc))(err); + /* store the maximum return value */ + retval = (retval <= ret ? ret : retval); + } + pChain = pChain->pnext; + } switch( err->type ) { @@ -129,12 +219,59 @@ int matherr( struct exception * err ) break; } - err->retval = 0.0; + if (retval == -1) + { + /* default behaviour */ + err->retval = 0.0; + return 1; /* don't print any message and don't set errno */ + } + + return (retval); - return 1; /* don't print any message and don't set errno */ } + +#else /* defined (HB_MATH_HANDLER) */ + +/* the functions don't do anything but they must exist */ + +int hb_getMathError (void) +{ + return (0); +} + +void hb_resetMathError (void) +{ + return; +} + +int hb_isMathHandler (void) +{ + return (0); +} + +HB_MATH_HANDLERHANDLE hb_installMathHandler (HB_MATH_HANDLERPROC handlerproc) +{ + return ((HB_MATH_HANDLERHANDLE)NULL); +} + +int hb_deinstallMathHandler (HB_MATH_HANDLERHANDLE handle) +{ + return (-1); +} + +int hb_setMathHandlerStatus (HB_MATH_HANDLERHANDLE handle, int status) +{ + return (0); +} + +int hb_getMathHandlerStatus (HB_MATH_HANDLERHANDLE handle) +{ + return (0); +} + #endif + HB_FUNC( EXP ) { if( ISNUM( 1 ) ) @@ -208,4 +345,3 @@ HB_FUNC( SQRT ) else hb_errRT_BASE_SubstR( EG_ARG, 1097, NULL, "SQRT", 1, hb_paramError( 1 ) ); } -