fltk/string.h File Reference

#include "FL_API.h"

Functions

char * newstring (const char *)
int snprintf (char *, size_t, const char *,...)
int strcasecmp (const char *, const char *)
size_t strlcat (char *, const char *, size_t)
size_t strlcpy (char *, const char *, size_t)
int strncasecmp (const char *, const char *, size_t)
int vsnprintf (char *, size_t, const char *, va_list ap)

Detailed Description

Provides definitions for C string manipulation functions so that portable programs may be written. None of these functions are in the fltk namespace.

In most cases the functions are provided by your operation system, or are simple renames of operating system functions.

This file is designed to work on Windows, Linux, and BSD systems. It may need to be edited to work on other systems. Please try to do this by adding #if statements so this file remains portable.

Some versions of fltk wrote this file using autoconf. I never liked this because I could not share the header file between systems, so I have reverted to a constant version.


Function Documentation

char* newstring ( const char *  from)

Equivalent to strdup() except the C++ new[] operator is used. A block of memory strlen(from)+1 is allocated and the from string is copied to it. Notice that you must use delete[] to destroy the returned value.

If NULL is passed, a NULL is returned.

It is a good idea to use this instead of strdup() if you want a replacement new-handler to work. FLTK uses this for all strings that it copies internally.

int snprintf ( char *  str,
size_t  size,
const char *  fmt,
  ... 
)

printf a string and set of arguments into an output buffer. At most size-1 bytes will be written, and a NUL terminator is always added. The return value is the number of bytes that would be written if the buffer was big enough, if this is greater than size then you need to reallocate a buffer of retval+1 size to get the full result.

FLTK provides an implementation of this function on the (few) systems that don't provide it. Include the <fltk/string.h> header to call this portably. FLTK's emulation is rather poor and has the following bugs:

  • Field width & Precision is ignored for %%%, %c, and %s.
  • A malicious user who manages to create a %-fmt string that prints more than 99 characters can still overflow the temporary buffer. For instance %110f will overflow.
  • Only handles formats that are both documented in the glibc man page for printf and also handled by your system's sprintf().
  • Return value is not correct. If the buffer overflows a value greater or equal to size is returned, but it is only a guess about the actual length.

Windows, Linux, and BSD all have this function so FLTK's emulation is not used. Warning: some systems do not return the correct value when the buffer overflows. A common alternative is to return -1 or size.

int strcasecmp ( const char *  s,
const char *  t 
)

Do a case-insensitive string comparison. Return less than zero if s is before t in alphabetical order, zero if they are equal, and greater than zero if s is after t. Only ASCII strings are correctly compared. What this does with UTF-8 or ISO-8859-1 is undefined!

FLTK provides this function on systems that don't have it. Use the <fltk/string.h> header to call this portably.

size_t strlcat ( char *  dst,
const char *  src,
size_t  siz 
)

Appends src to string dst of size siz (unlike strncat(), siz is the full size of dst, not space left). At most siz-1 characters will be copied. Always NUL terminates (unless siz == 0). Returns strlen(initial dst) + strlen(src); if retval >= siz, truncation occurred and you may want to allocate a new longer buffer for dst of size retval+1.

FLTK provides this function on systems that don't have it. Use the <fltk/string.h> header to call this portably.

size_t strlcpy ( char *  dst,
const char *  src,
size_t  siz 
)

Copy src to string dst of size siz. At most siz-1 characters will be copied. Always NUL terminates (unless siz == 0). Returns strlen(src); if retval >= siz, truncation occurred and you may want to allocate a new longer buffer for dst of size retval+1.

FLTK provides this function on systems that don't have it. Use the <fltk/string.h> header to call this portably.

int strncasecmp ( const char *  s,
const char *  t,
size_t  n 
)

Do a case-insensitive comparison of up to the first n characters of the strings.

See also:
strcasecmp()

FLTK provides this function on systems that don't have it. Use the <fltk/string.h> header file to call this.

int vsnprintf ( char *  str,
size_t  size,
const char *  fmt,
va_list  ap 
)

See snprintf(). This version takes a va_list so it can be called from another function that has a variable argument list.

FLTK provides an implementation of this function on the (few) systems that don't provide it. Include the <fltk/string.h> header to call this portably. See snprintf() for bugs with this emulation.