2
0
mirror of https://github.com/GNOME/libxml2.git synced 2025-03-19 18:50:25 +08:00

Upgraded to trio baseline 1.6

This commit is contained in:
Bjorn Reese 2002-01-19 15:40:18 +00:00
parent 572577e094
commit 026d29f41e
12 changed files with 2834 additions and 1452 deletions

@ -1,3 +1,9 @@
Sat Jan 19 16:36:21 CET 2002 Bjorn Reese <breese@users.sourceforge.net>
* trio.h trio.c triodef.h triop.h trionan.h trionan.c Makefile.am:
Upgraded to trio baseline 1.6
* strio.h strio.c: Replaced by triostr.h and triostr.c
Fri Jan 18 17:22:50 CET 2002 Daniel Veillard <daniel@veillard.com>
* globals.c xmlIO.c xmlcatalog.c: removed the last occurences

@ -21,7 +21,7 @@ libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c \
parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c \
valid.c xlink.c HTMLparser.c HTMLtree.c debugXML.c xpath.c \
xpointer.c xinclude.c nanohttp.c nanoftp.c DOCBparser.c \
catalog.c globals.c threads.c strio.c trio.c
catalog.c globals.c threads.c triostr.c trio.c
else
libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c \
@ -568,7 +568,7 @@ EXTRA_DIST = xml2-config.in xml2Conf.sh.in libxml.spec.in libxml.spec \
libxml.m4 \
example/Makefile.am example/gjobread.c example/gjobs.xml \
$(man_MANS) libxml-2.0.pc.in \
trionan.c trionan.h strio.c strio.h trio.c trio.h \
trionan.c trionan.h triostr.c triostr.h trio.c trio.h \
triop.h triodef.h libxml.h \
testThreadsWin32.c

581
strio.c

@ -1,581 +0,0 @@
/*************************************************************************
*
* $Id$
*
* Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
************************************************************************/
/*
* TODO
* - StrToLongDouble
*/
static const char rcsid[] = "@(#)$Id$";
#if defined(unix) || defined(__xlC__) || defined(__QNX__) || defined(__CYGWIN__)
# define PLATFORM_UNIX
#elif defined(WIN32) || defined(_WIN32)
# define PLATFORM_WIN32
#elif defined(AMIGA) && defined(__GNUC__)
# define PLATFORM_UNIX
#endif
#if defined(__STDC__) && (__STDC_VERSION__ >= 199901L)
# define TRIO_C99
#endif
#include "strio.h"
#include <string.h>
#include <locale.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#ifndef DEBUG
# define NDEBUG
#endif
#include <assert.h>
#ifndef NULL
# define NULL 0
#endif
#define NIL ((char)0)
#ifndef FALSE
# define FALSE (1 == 0)
# define TRUE (! FALSE)
#endif
#define VALID(x) (NULL != (x))
#define INVALID(x) (NULL == (x))
#if defined(PLATFORM_UNIX)
# define USE_STRCASECMP
# define USE_STRNCASECMP
# define USE_STRERROR
# if defined(__QNX__)
# define strcasecmp(x,y) stricmp(x,y)
# define strncasecmp(x,y,n) strnicmp(x,y,n)
# endif
#elif defined(PLATFORM_WIN32)
# define USE_STRCASECMP
# define strcasecmp(x,y) strcmpi(x,y)
#endif
/*************************************************************************
* StrAppendMax
*/
char *StrAppendMax(char *target, size_t max, const char *source)
{
assert(VALID(target));
assert(VALID(source));
assert(max > 0);
max -= StrLength(target) + 1;
return (max > 0) ? strncat(target, source, max) : target;
}
/*************************************************************************
* StrCopyMax
*/
char *StrCopyMax(char *target, size_t max, const char *source)
{
assert(VALID(target));
assert(VALID(source));
assert(max > 0); /* Includes != 0 */
target = strncpy(target, source, max - 1);
target[max - 1] = (char)0;
return target;
}
/*************************************************************************
* StrDuplicate
*/
char *StrDuplicate(const char *source)
{
char *target;
assert(VALID(source));
target = StrAlloc(StrLength(source) + 1);
if (target)
{
StrCopy(target, source);
}
return target;
}
/*************************************************************************
* StrDuplicateMax
*/
char *StrDuplicateMax(const char *source, size_t max)
{
char *target;
size_t len;
assert(VALID(source));
assert(max > 0);
/* Make room for string plus a terminating zero */
len = StrLength(source) + 1;
if (len > max)
{
len = max;
}
target = StrAlloc(len);
if (target)
{
StrCopyMax(target, len, source);
}
return target;
}
/*************************************************************************
* StrEqual
*/
int StrEqual(const char *first, const char *second)
{
assert(VALID(first));
assert(VALID(second));
if (VALID(first) && VALID(second))
{
#if defined(USE_STRCASECMP)
return (0 == strcasecmp(first, second));
#else
while ((*first != NIL) && (*second != NIL))
{
if (toupper(*first) != toupper(*second))
{
break;
}
first++;
second++;
}
return ((*first == NIL) && (*second == NIL));
#endif
}
return FALSE;
}
/*************************************************************************
* StrEqualCase
*/
int StrEqualCase(const char *first, const char *second)
{
assert(VALID(first));
assert(VALID(second));
if (VALID(first) && VALID(second))
{
return (0 == strcmp(first, second));
}
return FALSE;
}
/*************************************************************************
* StrEqualCaseMax
*/
int StrEqualCaseMax(const char *first, size_t max, const char *second)
{
assert(VALID(first));
assert(VALID(second));
if (VALID(first) && VALID(second))
{
return (0 == strncmp(first, second, max));
}
return FALSE;
}
/*************************************************************************
* StrEqualLocale
*/
int StrEqualLocale(const char *first, const char *second)
{
assert(VALID(first));
assert(VALID(second));
#if defined(LC_COLLATE)
return (strcoll(first, second) == 0);
#else
return StrEqual(first, second);
#endif
}
/*************************************************************************
* StrEqualMax
*/
int StrEqualMax(const char *first, size_t max, const char *second)
{
assert(VALID(first));
assert(VALID(second));
if (VALID(first) && VALID(second))
{
#if defined(USE_STRNCASECMP)
return (0 == strncasecmp(first, second, max));
#else
/* Not adequately tested yet */
size_t cnt = 0;
while ((*first != NIL) && (*second != NIL) && (cnt <= max))
{
if (toupper(*first) != toupper(*second))
{
break;
}
first++;
second++;
cnt++;
}
return ((cnt == max) || ((*first == NIL) && (*second == NIL)));
#endif
}
return FALSE;
}
/*************************************************************************
* StrError
*/
const char *StrError(int errorNumber)
{
#if defined(USE_STRERROR)
return strerror(errorNumber);
#else
return "unknown";
#endif
}
/*************************************************************************
* StrFormatDate
*/
size_t StrFormatDateMax(char *target,
size_t max,
const char *format,
const struct tm *datetime)
{
assert(VALID(target));
assert(VALID(format));
assert(VALID(datetime));
assert(max > 0);
return strftime(target, max, format, datetime);
}
/*************************************************************************
* StrHash
*/
unsigned long StrHash(const char *string, int type)
{
unsigned long value = 0L;
char ch;
assert(VALID(string));
switch (type)
{
case STRIO_HASH_PLAIN:
while ( (ch = *string++) != NIL )
{
value *= 31;
value += (unsigned long)ch;
}
break;
default:
assert(FALSE);
break;
}
return value;
}
/*************************************************************************
* StrMatch
*/
int StrMatch(char *string, char *pattern)
{
assert(VALID(string));
assert(VALID(pattern));
for (; ('*' != *pattern); ++pattern, ++string)
{
if (NIL == *string)
{
return (NIL == *pattern);
}
if ((toupper((int)*string) != toupper((int)*pattern))
&& ('?' != *pattern))
{
return FALSE;
}
}
/* two-line patch to prevent *too* much recursiveness: */
while ('*' == pattern[1])
pattern++;
do
{
if ( StrMatch(string, &pattern[1]) )
{
return TRUE;
}
}
while (*string++);
return FALSE;
}
/*************************************************************************
* StrMatchCase
*/
int StrMatchCase(char *string, char *pattern)
{
assert(VALID(string));
assert(VALID(pattern));
for (; ('*' != *pattern); ++pattern, ++string)
{
if (NIL == *string)
{
return (NIL == *pattern);
}
if ((*string != *pattern)
&& ('?' != *pattern))
{
return FALSE;
}
}
/* two-line patch to prevent *too* much recursiveness: */
while ('*' == pattern[1])
pattern++;
do
{
if ( StrMatchCase(string, &pattern[1]) )
{
return TRUE;
}
}
while (*string++);
return FALSE;
}
/*************************************************************************
* StrSpanFunction
*
* Untested
*/
size_t StrSpanFunction(char *source, int (*Function)(int))
{
size_t count = 0;
assert(VALID(source));
assert(VALID(Function));
while (*source != NIL)
{
if (Function(*source))
break; /* while */
source++;
count++;
}
return count;
}
/*************************************************************************
* StrSubstringMax
*/
char *StrSubstringMax(const char *string, size_t max, const char *find)
{
size_t count;
size_t size;
char *result = NULL;
assert(VALID(string));
assert(VALID(find));
size = StrLength(find);
if (size <= max)
{
for (count = 0; count <= max - size; count++)
{
if (StrEqualMax(find, size, &string[count]))
{
result = (char *)&string[count];
break;
}
}
}
return result;
}
/*************************************************************************
* StrToDouble
*
* double ::= [ <sign> ]
* ( <number> |
* <number> <decimal_point> <number> |
* <decimal_point> <number> )
* [ <exponential> [ <sign> ] <number> ]
* number ::= 1*( <digit> )
* digit ::= ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )
* exponential ::= ( 'e' | 'E' )
* sign ::= ( '-' | '+' )
* decimal_point ::= '.'
*/
double StrToDouble(const char *source, const char **endp)
{
#if defined(TRIO_C99)
return strtod(source, endp);
#else
/* Preliminary code */
int isNegative = FALSE;
int isExponentNegative = FALSE;
unsigned long integer = 0;
unsigned long fraction = 0;
unsigned long fracdiv = 1;
unsigned long exponent = 0;
double value = 0.0;
/* First try hex-floats */
if ((source[0] == '0') && ((source[1] == 'x') || (source[1] == 'X')))
{
source += 2;
while (isxdigit((int)*source))
{
integer *= 16;
integer += (isdigit((int)*source)
? (*source - '0')
: 10 + (toupper((int)*source) - 'A'));
source++;
}
if (*source == '.')
{
source++;
while (isxdigit((int)*source))
{
fraction *= 16;
fraction += (isdigit((int)*source)
? (*source - '0')
: 10 + (toupper((int)*source) - 'A'));
fracdiv *= 16;
source++;
}
if ((*source == 'p') || (*source == 'P'))
{
source++;
if ((*source == '+') || (*source == '-'))
{
isExponentNegative = (*source == '-');
source++;
}
while (isdigit((int)*source))
{
exponent *= 10;
exponent += (*source - '0');
source++;
}
}
}
}
else /* Then try normal decimal floats */
{
isNegative = (*source == '-');
/* Skip sign */
if ((*source == '+') || (*source == '-'))
source++;
/* Integer part */
while (isdigit((int)*source))
{
integer *= 10;
integer += (*source - '0');
source++;
}
if (*source == '.')
{
source++; /* skip decimal point */
while (isdigit((int)*source))
{
fraction *= 10;
fraction += (*source - '0');
fracdiv *= 10;
source++;
}
}
if ((*source == 'e') || (*source == 'E'))
{
source++; /* Skip exponential indicator */
isExponentNegative = (*source == '-');
if ((*source == '+') || (*source == '-'))
source++;
while (isdigit((int)*source))
{
exponent *= 10;
exponent += (*source - '0');
source++;
}
}
}
value = (double)integer;
if (fraction != 0)
{
value += (double)fraction / (double)fracdiv;
}
if (exponent != 0)
{
if (isExponentNegative)
value /= pow((double)10, (double)exponent);
else
value *= pow((double)10, (double)exponent);
}
if (isNegative)
value = -value;
if (endp)
*endp = source;
return value;
#endif
}
/*************************************************************************
* StrToFloat
*/
float StrToFloat(const char *source, const char **endp)
{
#if defined(TRIO_C99)
return strtof(source, endp);
#else
return (float)StrToDouble(source, endp);
#endif
}
/*************************************************************************
* StrToUpper
*/
int StrToUpper(char *target)
{
int i = 0;
assert(VALID(target));
while (NIL != *target)
{
*target = toupper((int)*target);
target++;
i++;
}
return i;
}

227
strio.h

@ -1,227 +0,0 @@
/*************************************************************************
*
* $Id$
*
* Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
************************************************************************/
#ifndef TRIO_STRIO_H
#define TRIO_STRIO_H
#if !(defined(DEBUG) || defined(NDEBUG))
# define NDEBUG
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef STRIO_MALLOC
# define STRIO_MALLOC(n) malloc(n)
#endif
#ifndef STRIO_FREE
# define STRIO_FREE(x) free(x)
#endif
/*
* StrAppend(target, source)
* StrAppendMax(target, maxsize, source)
*
* Append 'source' to 'target'
*
* target = StrAlloc(size)
*
* Allocate a new string
*
* StrContains(target, substring)
*
* Find out if the string 'substring' is
* contained in the string 'target'
*
* StrCopy(target, source)
* StrCopyMax(target, maxsize, source)
*
* Copy 'source' to 'target'
*
* target = StrDuplicate(source)
* target = StrDuplicateMax(source, maxsize)
*
* Allocate and copy 'source' to 'target'
*
* StrEqual(first, second)
* StrEqualMax(first, maxsize, second)
*
* Compare if 'first' is equal to 'second'.
* Case-independent.
*
* StrEqualCase(first, second)
* StrEqualCaseMax(first, maxsize, second)
*
* Compare if 'first' is equal to 'second'
* Case-dependent. Please note that the use of the
* word 'case' has the opposite meaning as that of
* strcasecmp().
*
* StrFormat(target, format, ...)
* StrFormatMax(target, maxsize, format, ...)
*
* Build 'target' according to 'format' and succesive
* arguments. This is equal to the sprintf() and
* snprintf() functions.
*
* StrFormatDate(target, format, ...)
*
* StrFree(target)
*
* De-allocates a string
*
* StrHash(string, type)
*
* Calculates the hash value of 'string' based on the
* 'type'.
*
* StrIndex(target, character)
* StrIndexLast(target, character)
*
* Find the first/last occurrence of 'character' in
* 'target'
*
* StrLength(target)
*
* Return the length of 'target'
*
* StrMatch(string, pattern)
* StrMatchCase(string, pattern)
*
* Find 'pattern' within 'string'. 'pattern' may contain
* wildcards such as * (asterics) and ? (question mark)
* which matches zero or more characters and exactly
* on character respectively
*
* StrScan(source, format, ...)
*
* Equal to sscanf()
*
* StrSubstring(target, substring)
*
* Find the first occurrence of the string 'substring'
* within the string 'target'
*
* StrTokenize(target, list)
*
* Split 'target' into the first token delimited by
* one of the characters in 'list'. If 'target' is
* NULL then next token will be returned.
*
* StrToUpper(target)
*
* Convert all lower case characters in 'target' into
* upper case characters.
*/
enum {
STRIO_HASH_NONE = 0,
STRIO_HASH_PLAIN,
STRIO_HASH_TWOSIGNED
};
#if !defined(DEBUG) || defined(__DECC)
#define StrAlloc(n) (char *)STRIO_MALLOC(n)
#define StrAppend(x,y) strcat((x), (y))
#define StrContains(x,y) (0 != strstr((x), (y)))
#define StrCopy(x,y) strcpy((x), (y))
#define StrIndex(x,y) strchr((x), (y))
#define StrIndexLast(x,y) strrchr((x), (y))
#define StrFree(x) STRIO_FREE(x)
#define StrLength(x) strlen((x))
#define StrSubstring(x,y) strstr((x), (y))
#define StrTokenize(x,y) strtok((x), (y))
#define StrToLong(x,y,n) strtol((x), (y), (n))
#define StrToUnsignedLong(x,y,n) strtoul((x), (y), (n))
#else /* DEBUG */
/*
* To be able to use these macros everywhere, including in
* if() sentences, the assertions are put first in a comma
* seperated list.
*
* Unfortunately the DECC compiler does not seem to like this
* so it will use the un-asserted functions above for the
* debugging case too.
*/
#define StrAlloc(n) \
(assert((n) > 0),\
(char *)STRIO_MALLOC(n))
#define StrAppend(x,y) \
(assert((x) != NULL),\
assert((y) != NULL),\
strcat((x), (y)))
#define StrContains(x,y) \
(assert((x) != NULL),\
assert((y) != NULL),\
(0 != strstr((x), (y))))
#define StrCopy(x,y) \
(assert((x) != NULL),\
assert((y) != NULL),\
strcpy((x), (y)))
#define StrIndex(x,c) \
(assert((x) != NULL),\
strchr((x), (c)))
#define StrIndexLast(x,c) \
(assert((x) != NULL),\
strrchr((x), (c)))
#define StrFree(x) \
(assert((x) != NULL),\
STRIO_FREE(x))
#define StrLength(x) \
(assert((x) != NULL),\
strlen((x)))
#define StrSubstring(x,y) \
(assert((x) != NULL),\
assert((y) != NULL),\
strstr((x), (y)))
#define StrTokenize(x,y) \
(assert((y) != NULL),\
strtok((x), (y)))
#define StrToLong(x,y,n) \
(assert((x) != NULL),\
assert((y) != NULL),\
assert((n) >= 2 && (n) <= 36),\
strtol((x), (y), (n)))
#define StrToUnsignedLong(x,y,n) \
(assert((x) != NULL),\
assert((y) != NULL),\
assert((n) >= 2 && (n) <= 36),\
strtoul((x), (y), (n)))
#endif /* DEBUG */
char *StrAppendMax(char *target, size_t max, const char *source);
char *StrCopyMax(char *target, size_t max, const char *source);
char *StrDuplicate(const char *source);
char *StrDuplicateMax(const char *source, size_t max);
int StrEqual(const char *first, const char *second);
int StrEqualCase(const char *first, const char *second);
int StrEqualCaseMax(const char *first, size_t max, const char *second);
int StrEqualLocale(const char *first, const char *second);
int StrEqualMax(const char *first, size_t max, const char *second);
const char *StrError(int);
size_t StrFormatDateMax(char *target, size_t max, const char *format, const struct tm *datetime);
unsigned long StrHash(const char *string, int type);
int StrMatch(char *string, char *pattern);
int StrMatchCase(char *string, char *pattern);
size_t StrSpanFunction(char *source, int (*Function)(int));
char *StrSubstringMax(const char *string, size_t max, const char *find);
float StrToFloat(const char *source, const char **target);
double StrToDouble(const char *source, const char **target);
int StrToUpper(char *target);
#endif /* TRIO_STRIO_H */

1387
trio.c

File diff suppressed because it is too large Load Diff

24
trio.h

@ -13,6 +13,10 @@
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
*************************************************************************
*
* http://ctrio.sourceforge.net/
*
************************************************************************/
#ifndef TRIO_TRIO_H
@ -41,6 +45,11 @@ extern "C" {
# define isascii ((unsigned)(x) < 0x80)
#endif
/* Error macros */
#define TRIO_ERROR_CODE(x) ((-(x)) & 0x00FF)
#define TRIO_ERROR_POSITION(x) ((-(x)) >> 8)
#define TRIO_ERROR_NAME(x) trio_strerror(x)
/*
* Error codes.
*
@ -53,14 +62,10 @@ enum {
TRIO_EDBLREF = 4,
TRIO_EGAP = 5,
TRIO_ENOMEM = 6,
TRIO_ERANGE = 7
TRIO_ERANGE = 7,
TRIO_ERRNO = 8
};
/* Error macros */
#define TRIO_ERROR_CODE(x) ((-(x)) & 0x00FF)
#define TRIO_ERROR_POSITION(x) ((-(x)) >> 8)
#define TRIO_ERROR_NAME(x) trio_strerror(x)
const char *trio_strerror(int);
/*************************************************************************
@ -183,13 +188,6 @@ int trio_sscanfv(const char *buffer, const char *format, void **args);
#define vdscanf trio_vdscanf
#endif
/* strio compatible names */
#define StrScan trio_sscanf
#define StrFormat trio_sprintf
#define StrFormatMax trio_snprintf
#define StrFormatAlloc trio_aprintf
#define StrFormatAppendMax trio_snprintfcat
#ifdef __cplusplus
} /* extern "C" */
#endif

@ -15,8 +15,8 @@
*
************************************************************************/
#ifndef __TRIO_TRIODEF_H__
#define __TRIO_TRIODEF_H__
#ifndef TRIO_TRIODEF_H
#define TRIO_TRIODEF_H
/*************************************************************************
* Platform and compiler support detection
@ -48,12 +48,17 @@
# define TRIO_PLATFORM_UNIX
#elif defined(__QNX__)
# define TRIO_PLATFORM_UNIX
# define TRIO_PLATFORM_QNX
#elif defined(__CYGWIN__)
# define TRIO_PLATFORM_UNIX
#elif defined(AMIGA) && defined(TRIO_COMPILER_GCC)
# define TRIO_PLATFORM_UNIX
#elif defined(TRIO_COMPILER_MSVC) || defined(WIN32) || defined(_WIN32)
# define TRIO_PLATFORM_WIN32
#elif defined(VMS) || defined(__VMS)
# define TRIO_PLATFORM_VMS
#elif defined(mpeix) || defined(__mpexl)
# define TRIO_PLATFORM_MPEIX
#endif
#if defined(__STDC__)
@ -81,4 +86,35 @@
# endif
#endif
#endif /* __TRIO_TRIODEF_H__ */
/*************************************************************************
* Generic defines
*/
#if !defined(TRIO_PUBLIC)
# define TRIO_PUBLIC
#endif
#if !defined(TRIO_PRIVATE)
# define TRIO_PRIVATE static
#endif
#if defined(TRIO_COMPILER_SUPPORTS_C90) || defined(__cplusplus)
# define TRIO_CONST const
# define TRIO_VOLATILE volatile
# define TRIO_POINTER void *
# define TRIO_PROTO(x) x
#else
# define TRIO_CONST
# define TRIO_VOLATILE
# define TRIO_POINTER char *
# define TRIO_PROTO(x) ()
#endif
#if defined(TRIO_COMPILER_SUPPORTS_C99) || defined(__cplusplus)
# define TRIO_INLINE inline
#elif defined(TRIO_COMPILER_GCC)
# define TRIO_INLINE __inline__
#else
# define TRIO_INLINE
#endif
#endif /* TRIO_TRIODEF_H */

106
trionan.c

@ -50,6 +50,7 @@
* NetBSD 1.4 x86 gcc
* NetBSD 1.4 StrongARM gcc
* NetBSD 1.5 Alpha gcc
* OpenVMS 7.1 Alpha DEC C 6.0
* RISC OS 4 StrongARM Norcroft C
* Solaris 2.5.1 x86 gcc
* Solaris 2.5.1 Sparc gcc
@ -63,7 +64,6 @@
static const char rcsid[] = "@(#)$Id$";
/*************************************************************************
* Include files
*/
@ -77,15 +77,17 @@ static const char rcsid[] = "@(#)$Id$";
#if defined(TRIO_PLATFORM_UNIX)
# include <signal.h>
#endif
#if defined(TRIO_COMPILER_DECC)
# include <fp_class.h>
#endif
#include <assert.h>
#ifdef __STDC__
# define CONST const
# define VOLATILE volatile
#else
# define CONST
# define VOLATILE
#if defined(TRIO_DOCUMENTATION)
# include "doc/doc_nan.h"
#endif
/** @addtogroup SpecialQuantities
@{
*/
/*************************************************************************
* Definitions
@ -94,7 +96,11 @@ static const char rcsid[] = "@(#)$Id$";
/* We must enable IEEE floating-point on Alpha */
#if defined(__alpha) && !defined(_IEEE_FP)
# if defined(TRIO_COMPILER_DECC)
# error "Must be compiled with option -ieee"
# if defined(TRIO_PLATFORM_VMS)
# error "Must be compiled with option /IEEE_MODE=UNDERFLOW_TO_ZERO/FLOAT=IEEE"
# else
# error "Must be compiled with option -ieee"
# endif
# elif defined(TRIO_COMPILER_GCC) && (defined(__osf__) || defined(__linux__))
# error "Must be compiled with option -mieee"
# endif
@ -128,57 +134,61 @@ static const char rcsid[] = "@(#)$Id$";
* Endian-agnostic indexing macro.
*
* The value of internalEndianMagic, when converted into a 64-bit
* integer, becomes 0x0001020304050607 (we could have used a 64-bit
* integer, becomes 0x0706050403020100 (we could have used a 64-bit
* integer value instead of a double, but not all platforms supports
* that type). The value is automatically encoded with the correct
* endianess by the compiler, which means that we can support any
* kind of endianess. The individual bytes are then used as an index
* for the IEEE 754 bit-patterns and masks.
*/
#define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[(x)])
#define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[7-(x)])
static CONST double internalEndianMagic = 1.4015997730788920e-309;
static TRIO_CONST double internalEndianMagic = 7.949928895127363e-275;
/* Mask for the exponent */
static CONST unsigned char ieee_754_exponent_mask[] = {
static TRIO_CONST unsigned char ieee_754_exponent_mask[] = {
0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* Mask for the mantissa */
static CONST unsigned char ieee_754_mantissa_mask[] = {
static TRIO_CONST unsigned char ieee_754_mantissa_mask[] = {
0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
/* Bit-pattern for infinity */
static CONST unsigned char ieee_754_infinity_array[] = {
static TRIO_CONST unsigned char ieee_754_infinity_array[] = {
0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* Bit-pattern for quiet NaN */
static CONST unsigned char ieee_754_qnan_array[] = {
static TRIO_CONST unsigned char ieee_754_qnan_array[] = {
0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/*************************************************************************
* Functions
*/
/*
* trio_make_double
*/
static double
trio_make_double(CONST unsigned char *values)
TRIO_PRIVATE double
trio_make_double(TRIO_CONST unsigned char *values)
{
VOLATILE double result;
TRIO_VOLATILE double result;
int i;
for (i = 0; i < (int)sizeof(double); i++) {
((VOLATILE unsigned char *)&result)[TRIO_DOUBLE_INDEX(i)] = values[i];
((TRIO_VOLATILE unsigned char *)&result)[TRIO_DOUBLE_INDEX(i)] = values[i];
}
return result;
}
/*************************************************************************
/*
* trio_examine_double
*/
static int
TRIO_PRIVATE int
trio_is_special_quantity(double number,
int *has_mantissa)
{
@ -200,9 +210,11 @@ trio_is_special_quantity(double number,
#endif /* USE_IEEE_754 */
/*************************************************************************
* trio_pinf
*/
/**
Generate positive infinity.
@return Floating-point representation of positive infinity.
*/
TRIO_PUBLIC double
trio_pinf(void)
{
@ -242,9 +254,11 @@ trio_pinf(void)
return result;
}
/*************************************************************************
* trio_ninf
*/
/**
Generate negative infinity.
@return Floating-point value of negative infinity.
*/
TRIO_PUBLIC double
trio_ninf(void)
{
@ -261,9 +275,11 @@ trio_ninf(void)
return result;
}
/*************************************************************************
* trio_nan
*/
/**
Generate NaN.
@return Floating-point representation of NaN.
*/
TRIO_PUBLIC double
trio_nan(void)
{
@ -306,11 +322,14 @@ trio_nan(void)
return result;
}
/*************************************************************************
* trio_isnan
*/
/**
Check for NaN.
@param number An arbitrary floating-point number.
@return Boolean value indicating whether or not the number is a NaN.
*/
TRIO_PUBLIC int
trio_isnan(VOLATILE double number)
trio_isnan(TRIO_VOLATILE double number)
{
#if defined(isnan) || defined(TRIO_COMPILER_SUPPORTS_UNIX95)
/*
@ -370,11 +389,14 @@ trio_isnan(VOLATILE double number)
#endif
}
/*************************************************************************
* trio_isinf
*/
/**
Check for infinity.
@param number An arbitrary floating-point number.
@return 1 if positive infinity, -1 if negative infinity, 0 otherwise.
*/
TRIO_PUBLIC int
trio_isinf(VOLATILE double number)
trio_isinf(TRIO_VOLATILE double number)
{
#if defined(TRIO_COMPILER_DECC)
/*
@ -438,7 +460,15 @@ trio_isinf(VOLATILE double number)
#endif
}
/** @} SpecialQuantities */
/*************************************************************************
* For test purposes.
*
* Add the following compiler option to include this test code.
*
* Unix : -DSTANDALONE
* VMS : /DEFINE=(STANDALONE)
*/
#if defined(STANDALONE)
# include <stdio.h>

@ -15,50 +15,42 @@
*
************************************************************************/
#ifndef __TRIO_NAN_H__
#define __TRIO_NAN_H__
#ifndef TRIO_NAN_H
#define TRIO_NAN_H
#include "triodef.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef TRIO_PUBLIC
#define TRIO_PUBLIC
#endif
/*
* Return NaN (Not-a-Number).
*/
TRIO_PUBLIC
double trio_nan(void);
TRIO_PUBLIC double trio_nan(void);
/*
* Return positive infinity.
*/
TRIO_PUBLIC
double trio_pinf(void);
TRIO_PUBLIC double trio_pinf(void);
/*
* Return negative infinity.
*/
TRIO_PUBLIC
double trio_ninf(void);
TRIO_PUBLIC double trio_ninf(void);
/*
* If number is a NaN return non-zero, otherwise return zero.
*/
TRIO_PUBLIC
int trio_isnan(double number);
TRIO_PUBLIC int trio_isnan(double number);
/*
* If number is positive infinity return 1, if number is negative
* infinity return -1, otherwise return 0.
*/
TRIO_PUBLIC
int trio_isinf(double number);
TRIO_PUBLIC int trio_isinf(double number);
#ifdef __cplusplus
}
#endif
#endif /* __TRIO_NAN_H__ */
#endif /* TRIO_NAN_H */

@ -27,6 +27,7 @@
#define TRIO_TRIOP_H
#include <stdlib.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
@ -53,7 +54,7 @@ extern "C" {
#ifndef TRIO_EXTENSION
# define TRIO_EXTENSION 1
#endif
#ifndef TRIO_WIDECHAR
#ifndef TRIO_WIDECHAR /* Does not work yet. Do not enable */
# define TRIO_WIDECHAR 0
#endif
#ifndef TRIO_ERRORS
@ -70,7 +71,11 @@ extern "C" {
# define TRIO_FREE(x) free(x)
#endif
typedef int (*trio_callback_t)(void *ref);
/*************************************************************************
* User-defined specifiers
*/
typedef int (*trio_callback_t)(void *);
void *trio_register(trio_callback_t callback, const char *name);
void trio_unregister(void *handle);

1753
triostr.c Normal file

File diff suppressed because it is too large Load Diff

119
triostr.h Normal file

@ -0,0 +1,119 @@
/*************************************************************************
*
* $Id$
*
* Copyright (C) 2001 Bjorn Reese and Daniel Stenberg.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
************************************************************************/
#ifndef TRIO_TRIOSTR_H
#define TRIO_TRIOSTR_H
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "triodef.h"
#include "triop.h"
enum {
TRIO_HASH_NONE = 0,
TRIO_HASH_PLAIN,
TRIO_HASH_TWOSIGNED
};
/*************************************************************************
* String functions
*/
int trio_append(char *target, const char *source);
int trio_append_max(char *target, size_t max, const char *source);
int trio_contains(const char *string, const char *substring);
int trio_copy(char *target, const char *source);
int trio_copy_max(char *target, size_t max, const char *source);
char *trio_create(size_t size);
void trio_destroy(char *string);
char *trio_duplicate(const char *source);
char *trio_duplicate_max(const char *source, size_t max);
int trio_equal(const char *first, const char *second);
int trio_equal_case(const char *first, const char *second);
int trio_equal_case_max(const char *first, size_t max, const char *second);
int trio_equal_locale(const char *first, const char *second);
int trio_equal_max(const char *first, size_t max, const char *second);
const char *trio_error(int);
size_t trio_format_date_max(char *target, size_t max, const char *format, const struct tm *datetime);
unsigned long trio_hash(const char *string, int type);
char *trio_index(const char *string, char character);
char *trio_index_last(const char *string, char character);
size_t trio_length(const char *string);
int trio_lower(char *target);
int trio_match(const char *string, const char *pattern);
int trio_match_case(const char *string, const char *pattern);
size_t trio_span_function(char *target, const char *source, int (*Function)(int));
char *trio_substring(const char *string, const char *substring);
char *trio_substring_max(const char *string, size_t max, const char *substring);
char *trio_tokenize(char *string, const char *delimiters);
float trio_to_float(const char *source, const char **endp);
double trio_to_double(const char *source, const char **endp);
long trio_to_long(const char *source, char **endp, int base);
unsigned long trio_to_unsigned_long(const char *source, char **endp, int base);
int trio_upper(char *target);
/*************************************************************************
* Dynamic string functions
*/
/*
* Opaque type for dynamic strings
*/
typedef struct _trio_string_t trio_string_t;
trio_string_t *trio_string_create(int initial_size);
void trio_string_destroy(trio_string_t *self);
char *trio_string_get(trio_string_t *self, int offset);
void trio_xstring_set(trio_string_t *self, char *buffer);
char *trio_string_extract(trio_string_t *self);
int trio_string_size(trio_string_t *self);
void trio_string_terminate(trio_string_t *self);
int trio_string_append(trio_string_t *self, trio_string_t *other);
int trio_string_contains(trio_string_t *self, trio_string_t *other);
int trio_string_copy(trio_string_t *self, trio_string_t *other);
trio_string_t *trio_string_duplicate(trio_string_t *other);
int trio_string_equal(trio_string_t *self, trio_string_t *other);
int trio_string_equal_max(trio_string_t *self, size_t max, trio_string_t *second);
int trio_string_equal_case(trio_string_t *self, trio_string_t *other);
int trio_string_equal_case_max(trio_string_t *self, size_t max, trio_string_t *other);
size_t trio_string_format_date_max(trio_string_t *self, size_t max, const char *format, const struct tm *datetime);
char *trio_string_index(trio_string_t *self, int character);
char *trio_string_index_last(trio_string_t *self, int character);
int trio_string_length(trio_string_t *self);
int trio_string_lower(trio_string_t *self);
int trio_string_match(trio_string_t *self, trio_string_t *other);
int trio_string_match_case(trio_string_t *self, trio_string_t *other);
char *trio_string_substring(trio_string_t *self, trio_string_t *other);
int trio_string_upper(trio_string_t *self);
int trio_xstring_append_char(trio_string_t *self, char character);
int trio_xstring_append(trio_string_t *self, const char *other);
int trio_xstring_contains(trio_string_t *self, const char *other);
int trio_xstring_copy(trio_string_t *self, const char *other);
trio_string_t *trio_xstring_duplicate(const char *other);
int trio_xstring_equal(trio_string_t *self, const char *other);
int trio_xstring_equal_max(trio_string_t *self, size_t max, const char *other);
int trio_xstring_equal_case(trio_string_t *self, const char *other);
int trio_xstring_equal_case_max(trio_string_t *self, size_t max, const char *other);
int trio_xstring_match(trio_string_t *self, const char *other);
int trio_xstring_match_case(trio_string_t *self, const char *other);
char *trio_xstring_substring(trio_string_t *self, const char *other);
#endif /* TRIO_TRIOSTR_H */