aboutsummaryrefslogtreecommitdiffstats
path: root/trio/triostr.c
diff options
context:
space:
mode:
Diffstat (limited to 'trio/triostr.c')
-rw-r--r--trio/triostr.c200
1 files changed, 120 insertions, 80 deletions
diff --git a/trio/triostr.c b/trio/triostr.c
index 069e9225..ce5cc74e 100644
--- a/trio/triostr.c
+++ b/trio/triostr.c
@@ -1,6 +1,6 @@
/*************************************************************************
*
- * $Id: triostr.c,v 1.34 2008/11/09 12:17:39 breese Exp $
+ * $Id: triostr.c,v 1.36 2010/01/26 13:02:02 breese Exp $
*
* Copyright (C) 2001 Bjorn Reese and Daniel Stenberg.
*
@@ -114,7 +114,7 @@
# endif
#endif
-#if defined(USE_MATH)
+#if defined(USE_MATH) && !defined(TRIO_NO_POWL)
# if !defined(HAVE_POWL)
# if defined(PREDEF_STANDARD_C99) \
|| defined(PREDEF_STANDARD_UNIX03)
@@ -160,7 +160,7 @@ struct _trio_string_t
*/
#if !defined(TRIO_EMBED_STRING)
-static TRIO_CONST char rcsid[] = "@(#)$Id: triostr.c,v 1.34 2008/11/09 12:17:39 breese Exp $";
+static TRIO_CONST char rcsid[] = "@(#)$Id: triostr.c,v 1.36 2010/01/26 13:02:02 breese Exp $";
#endif
/*************************************************************************
@@ -215,7 +215,7 @@ TRIO_PRIVATE_STRING trio_string_t *
internal_string_alloc(TRIO_NOARGS)
{
trio_string_t *self;
-
+
self = (trio_string_t *)TRIO_MALLOC(sizeof(trio_string_t));
if (self)
{
@@ -252,7 +252,7 @@ TRIO_ARGS2((self, delta),
new_size = (delta == 0)
? ( (self->allocated == 0) ? 1 : self->allocated * 2 )
: self->allocated + delta;
-
+
new_content = (char *)TRIO_REALLOC(self->content, new_size);
if (new_content)
{
@@ -273,7 +273,8 @@ TRIO_ARGS2((self, delta),
* used (that is, the size of the string is never decreased).
*/
#if defined(TRIO_FUNC_STRING_APPEND) \
- || defined(TRIO_FUNC_XSTRING_APPEND)
+ || defined(TRIO_FUNC_XSTRING_APPEND) \
+ || defined(TRIO_FUNC_XSTRING_APPEND_MAX)
TRIO_PRIVATE_STRING BOOLEAN_T
internal_string_grow_to
@@ -297,16 +298,16 @@ TRIO_ARGS1((source),
int source)
{
# if defined(HAVE_TOUPPER)
-
+
return toupper(source);
-
+
# else
/* Does not handle locales or non-contiguous alphabetic characters */
return ((source >= (int)'a') && (source <= (int)'z'))
? source - 'a' + 'A'
: source;
-
+
# endif
}
@@ -376,7 +377,7 @@ TRIO_ARGS1((string),
@param max Maximum number of characters to count.
@return The maximum value of @p max and number of characters in @p string.
*/
-#if defined(TRIO_FUNC_LENGTH)
+#if defined(TRIO_FUNC_LENGTH_MAX)
TRIO_PUBLIC_STRING size_t
trio_length_max
@@ -398,11 +399,11 @@ TRIO_ARGS2((string, max),
/**
Append @p source at the end of @p target.
-
+
@param target Target string.
@param source Source string.
@return Boolean value indicating success or failure.
-
+
@pre @p target must point to a memory chunk with sufficient room to
contain the @p target string and @p source string.
@pre No boundary checking is performed, so insufficient memory will
@@ -419,7 +420,7 @@ TRIO_ARGS2((target, source),
{
assert(target);
assert(source);
-
+
return (strcat(target, source) != NULL);
}
@@ -427,12 +428,12 @@ TRIO_ARGS2((target, source),
/**
Append at most @p max characters from @p source to @p target.
-
+
@param target Target string.
@param max Maximum number of characters to append.
@param source Source string.
@return Boolean value indicating success or failure.
-
+
@pre @p target must point to a memory chuck with sufficient room to
contain the @p target string and the @p source string (at most @p max
characters).
@@ -450,12 +451,12 @@ TRIO_ARGS3((target, max, source),
TRIO_CONST char *source)
{
size_t length;
-
+
assert(target);
assert(source);
length = trio_length(target);
-
+
if (max > length)
{
strncat(target, source, max - length - 1);
@@ -482,7 +483,7 @@ TRIO_ARGS2((string, substring),
{
assert(string);
assert(substring);
-
+
return (0 != strstr(string, substring));
}
@@ -490,11 +491,11 @@ TRIO_ARGS2((string, substring),
/**
Copy @p source to @p target.
-
+
@param target Target string.
@param source Source string.
@return Boolean value indicating success or failure.
-
+
@pre @p target must point to a memory chunk with sufficient room to
contain the @p source string.
@pre No boundary checking is performed, so insufficient memory will
@@ -511,7 +512,7 @@ TRIO_ARGS2((target, source),
{
assert(target);
assert(source);
-
+
(void)strcpy(target, source);
return TRUE;
}
@@ -519,15 +520,19 @@ TRIO_ARGS2((target, source),
#endif
/**
- Copy at most @p max characters from @p source to @p target.
-
+ Copy at most @p max - 1 characters from @p source to @p target.
+
@param target Target string.
- @param max Maximum number of characters to append.
+ @param max Maximum number of characters to append (one of which is
+ a NUL terminator). In other words @p source must point to at least
+ @p max - 1 bytes, but @p target must point to at least @p max
+ bytes.
@param source Source string.
@return Boolean value indicating success or failure.
-
+
@pre @p target must point to a memory chunk with sufficient room to
- contain the @p source string (at most @p max characters).
+ contain the @p source string and a NUL terminator (at most @p max
+ bytes total).
@pre No boundary checking is performed, so insufficient memory will
result in a buffer overrun.
@post @p target will be zero terminated.
@@ -554,10 +559,10 @@ TRIO_ARGS3((target, max, source),
/**
Duplicate @p source.
-
+
@param source Source string.
@return A copy of the @p source string.
-
+
@post @p target will be zero terminated.
*/
#if defined(TRIO_FUNC_DUPLICATE)
@@ -574,11 +579,11 @@ TRIO_ARGS1((source),
/**
Duplicate at most @p max characters of @p source.
-
+
@param source Source string.
@param max Maximum number of characters to duplicate.
@return A copy of the @p source string.
-
+
@post @p target will be zero terminated.
*/
#if defined(TRIO_FUNC_DUPLICATE_MAX)
@@ -606,11 +611,11 @@ TRIO_ARGS2((source, max),
/**
Compare if two strings are equal.
-
+
@param first First string.
@param second Second string.
@return Boolean indicating whether the two strings are equal or not.
-
+
Case-insensitive comparison.
*/
#if defined(TRIO_FUNC_EQUAL)
@@ -648,11 +653,11 @@ TRIO_ARGS2((first, second),
/**
Compare if two strings are equal.
-
+
@param first First string.
@param second Second string.
@return Boolean indicating whether the two strings are equal or not.
-
+
Case-sensitive comparison.
*/
#if defined(TRIO_FUNC_EQUAL_CASE)
@@ -677,12 +682,12 @@ TRIO_ARGS2((first, second),
/**
Compare if two strings up until the first @p max characters are equal.
-
+
@param first First string.
@param max Maximum number of characters to compare.
@param second Second string.
@return Boolean indicating whether the two strings are equal or not.
-
+
Case-sensitive comparison.
*/
#if defined(TRIO_FUNC_EQUAL_CASE_MAX)
@@ -708,7 +713,7 @@ TRIO_ARGS3((first, max, second),
/**
Compare if two strings are equal.
-
+
@param first First string.
@param second Second string.
@return Boolean indicating whether the two strings are equal or not.
@@ -737,12 +742,12 @@ TRIO_ARGS2((first, second),
/**
Compare if two strings up until the first @p max characters are equal.
-
+
@param first First string.
@param max Maximum number of characters to compare.
@param second Second string.
@return Boolean indicating whether the two strings are equal or not.
-
+
Case-insensitive comparison.
*/
#if defined(TRIO_FUNC_EQUAL_MAX)
@@ -796,7 +801,7 @@ TRIO_ARGS1((error_number),
int error_number)
{
# if defined(USE_STRERROR)
-
+
return strerror(error_number);
# else
@@ -808,9 +813,9 @@ TRIO_ARGS1((error_number),
return ((error_number < 0) || (error_number >= sys_nerr))
? "unknown"
: sys_errlist[error_number];
-
+
# else
-
+
return "unknown";
# endif
@@ -873,7 +878,7 @@ TRIO_ARGS2((string, type),
char ch;
assert(string);
-
+
switch (type)
{
case TRIO_HASH_PLAIN:
@@ -964,7 +969,7 @@ TRIO_ARGS1((target),
@return Boolean value indicating success or failure.
Case-insensitive comparison.
-
+
The following wildcards can be used
@li @c * Match any number of characters.
@li @c ? Match a single character.
@@ -979,7 +984,7 @@ TRIO_ARGS2((string, pattern),
{
assert(string);
assert(pattern);
-
+
for (; ('*' != *pattern); ++pattern, ++string)
{
if (NIL == *string)
@@ -1004,7 +1009,7 @@ TRIO_ARGS2((string, pattern),
}
}
while (*string++);
-
+
return FALSE;
}
@@ -1018,7 +1023,7 @@ TRIO_ARGS2((string, pattern),
@return Boolean value indicating success or failure.
Case-sensitive comparison.
-
+
The following wildcards can be used
@li @c * Match any number of characters.
@li @c ? Match a single character.
@@ -1033,7 +1038,7 @@ TRIO_ARGS2((string, pattern),
{
assert(string);
assert(pattern);
-
+
for (; ('*' != *pattern); ++pattern, ++string)
{
if (NIL == *string)
@@ -1058,7 +1063,7 @@ TRIO_ARGS2((string, pattern),
}
}
while (*string++);
-
+
return FALSE;
}
@@ -1086,7 +1091,7 @@ TRIO_ARGS3((target, source, Function),
assert(target);
assert(source);
assert(Function);
-
+
while (*source != NIL)
{
*target++ = Function(*source++);
@@ -1145,7 +1150,7 @@ TRIO_ARGS3((string, max, substring),
assert(string);
assert(substring);
-
+
size = trio_length(substring);
if (size <= max)
{
@@ -1181,7 +1186,7 @@ TRIO_ARGS2((string, delimiters),
TRIO_CONST char *delimiters)
{
assert(delimiters);
-
+
return strtok(string, delimiters);
}
@@ -1318,7 +1323,7 @@ TRIO_ARGS2((source, endp),
}
}
}
-
+
value = integer + fraction;
if (exponent != 0)
{
@@ -1408,7 +1413,7 @@ TRIO_ARGS3((string, endp, base),
{
assert(string);
assert((base >= 2) && (base <= 36));
-
+
return strtol(string, endp, base);
}
@@ -1428,16 +1433,16 @@ TRIO_ARGS1((source),
int source)
{
# if defined(HAVE_TOLOWER)
-
+
return tolower(source);
-
+
# else
/* Does not handle locales or non-contiguous alphabetic characters */
return ((source >= (int)'A') && (source <= (int)'Z'))
? source - 'A' + 'a'
: source;
-
+
# endif
}
@@ -1461,7 +1466,7 @@ TRIO_ARGS3((string, endp, base),
{
assert(string);
assert((base >= 2) && (base <= 36));
-
+
return strtoul(string, endp, base);
}
@@ -1521,7 +1526,7 @@ TRIO_ARGS1((target),
/**
Create a new dynamic string.
-
+
@param initial_size Initial size of the buffer.
@return Newly allocated dynamic string, or NULL if memory allocation failed.
*/
@@ -1556,7 +1561,7 @@ TRIO_ARGS1((initial_size),
/**
Deallocate the dynamic string and its contents.
-
+
@param self Dynamic string
*/
#if defined(TRIO_FUNC_STRING_DESTROY)
@@ -1567,7 +1572,7 @@ TRIO_ARGS1((self),
trio_string_t *self)
{
assert(self);
-
+
if (self)
{
trio_destroy(self->content);
@@ -1579,11 +1584,11 @@ TRIO_ARGS1((self),
/**
Get a pointer to the content.
-
+
@param self Dynamic string.
@param offset Offset into content.
@return Pointer to the content.
-
+
@p Offset can be zero, positive, or negative. If @p offset is zero,
then the start of the content will be returned. If @p offset is positive,
then a pointer to @p offset number of characters from the beginning of the
@@ -1591,7 +1596,7 @@ TRIO_ARGS1((self),
number of characters from the ending of the string, starting at the
terminating zero, is returned.
*/
-#if defined(TRIO_FUNCT_STRING_GET)
+#if defined(TRIO_FUNC_STRING_GET)
TRIO_PUBLIC_STRING char *
trio_string_get
@@ -1600,7 +1605,7 @@ TRIO_ARGS2((self, offset),
int offset)
{
char *result = NULL;
-
+
assert(self);
if (self->content != NULL)
@@ -1633,10 +1638,10 @@ TRIO_ARGS2((self, offset),
/**
Extract the content.
-
+
@param self Dynamic String
@return Content of dynamic string.
-
+
The content is removed from the dynamic string. This enables destruction
of the dynamic string without deallocation of the content.
*/
@@ -1648,7 +1653,7 @@ TRIO_ARGS1((self),
trio_string_t *self)
{
char *result;
-
+
assert(self);
result = self->content;
@@ -1662,13 +1667,13 @@ TRIO_ARGS1((self),
/**
Set the content of the dynamic string.
-
+
@param self Dynamic String
@param buffer The new content.
-
+
Sets the content of the dynamic string to a copy @p buffer.
An existing content will be deallocated first, if necessary.
-
+
@remark
This function will make a copy of @p buffer.
You are responsible for deallocating @p buffer yourself.
@@ -1723,7 +1728,7 @@ TRIO_ARGS1((self),
/**
Append the second string to the first.
-
+
@param self Dynamic string to be modified.
@param other Dynamic string to copy from.
@return Boolean value indicating success or failure.
@@ -1737,7 +1742,7 @@ TRIO_ARGS2((self, other),
trio_string_t *other)
{
size_t length;
-
+
assert(self);
assert(other);
@@ -1747,7 +1752,7 @@ TRIO_ARGS2((self, other),
trio_copy(&self->content[self->length], other->content);
self->length = length;
return TRUE;
-
+
error:
return FALSE;
}
@@ -1767,7 +1772,7 @@ TRIO_ARGS2((self, other),
TRIO_CONST char *other)
{
size_t length;
-
+
assert(self);
assert(other);
@@ -1777,7 +1782,7 @@ TRIO_ARGS2((self, other),
trio_copy(&self->content[self->length], other);
self->length = length;
return TRUE;
-
+
error:
return FALSE;
}
@@ -1805,7 +1810,42 @@ TRIO_ARGS2((self, character),
self->content[self->length] = character;
self->length++;
return TRUE;
-
+
+ error:
+ return FALSE;
+}
+
+#endif
+
+/*
+ * trio_xstring_append_max
+ */
+#if defined(TRIO_FUNC_XSTRING_APPEND_MAX)
+
+TRIO_PUBLIC_STRING int
+trio_xstring_append_max
+TRIO_ARGS3((self, other, max),
+ trio_string_t *self,
+ TRIO_CONST char *other,
+ size_t max)
+{
+ size_t length;
+
+ assert(self);
+ assert(other);
+
+ length = self->length + trio_length_max(other, max);
+ if (!internal_string_grow_to(self, length))
+ goto error;
+
+ /*
+ * Pass max + 1 since trio_copy_max copies one character less than
+ * this from the source to make room for a terminating zero.
+ */
+ trio_copy_max(&self->content[self->length], max + 1, other);
+ self->length = length;
+ return TRUE;
+
error:
return FALSE;
}
@@ -1814,7 +1854,7 @@ TRIO_ARGS2((self, character),
/**
Search for the first occurrence of second parameter in the first.
-
+
@param self Dynamic string to be modified.
@param other Dynamic string to copy from.
@return Boolean value indicating success or failure.
@@ -1906,7 +1946,7 @@ TRIO_ARGS1((other),
trio_string_t *other)
{
trio_string_t *self;
-
+
assert(other);
self = internal_string_alloc();
@@ -1939,7 +1979,7 @@ TRIO_ARGS1((other),
TRIO_CONST char *other)
{
trio_string_t *self;
-
+
assert(other);
self = internal_string_alloc();