2001-07-15 20:15 MEST Martin Vogel <vogel@inttec.de>

This commit is contained in:
Martin Vogel
2001-07-15 18:19:40 +00:00
parent c682c4918e
commit d3fa759ecd
4 changed files with 521 additions and 24 deletions

View File

@@ -1,3 +1,11 @@
2001-07-15 20:15 MEST Martin Vogel <vogel@inttec.de>
* 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 <harbour@PatrickMast.com>
* source/rtl/profiler.prg
+ Added the <lOnlyUsed> parameter. If profiler is used like

View File

@@ -9,6 +9,9 @@
* Copyright 2000 Luiz Rafael Culik <culik@sl.conex.net>
* Documentation for: ABS(),EXP(),LOG(),INT(),MAX()
* MIN(),SQRT(),ROUND()
* Copyright 2001 IntTec GmbH, Neunlindenstr 32, 79106 Freiburg, Germany
* Author: Martin Vogel <vogel@inttec.de>
* 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 <hbmath.h>
* 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 <hbmath.h>
* 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 <hbmath.h>
* 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 <hbmath.h>
* 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 <hbmath.h>
* 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 <hbmath.h>
* 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 <hbmath.h>
* 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$
*/

112
harbour/include/hbmath.h Normal file
View File

@@ -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 <vogel@inttec.de>
* 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 <math.h>
#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_ */

View File

@@ -7,6 +7,11 @@
* Math functions
*
* Copyright 1999 Matthew Hamilton <mhamilton@bunge.com.au>
*
* Functions for user defined math error handlers
* Copyright 2001 IntTec GmbH, Freiburg, Germany,
* Author: Martin Vogel <vogel@inttec.de>
*
* 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 ) );
}