DDString.h File Reference

Cross-platform string manipulation functions. More...

Defines

#define DDvsnprintf
#define newstring(x)
#define strtod(a, b)

Functions

int vsnprintf (char *str, size_t size, const char *fmt, va_list ap)
int snprintf (char *str, size_t size, const char *fmt,...)
size_t strlcat (char *dst, const char *src, size_t siz)
size_t strlcpy (char *dst, const char *src, size_t siz)
char * DDImage_newstring (const char *)
double DDImage_strtod (const char *p, char **endptr)

Detailed Description

Cross-platform string manipulation functions.

You should use this file instead of <string.h>, <stdlib.h>, or <stdio.h>. This defines replacements for several standard library functions to produce the same behavior on all platforms (mostly to copy Linux features to Windows). It also defines functions from BSD that make string manipulation that does not overflow buffers much easier.


Function Documentation

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

Print at most size characters (including a trailing null) into the buffer pointed to by str. Returns the number of bytes written (not including the nul). If the printout is truncated this is supposed to return the number of bytes that would have been printed, however this only works on newer Linux machines, other systems return -1 or zero.

Linux provides this function. On NT it calls _vsnprintf which is almost correct. On Irix and some other systems an emulation function is used. This emulation is rather simple and has the following bugs:

  • Field width & Precision is ignored for %%, %c, and %s.
  • All other formats are printed to an internal buffer that can be overflowed by use of large percision or field width values. 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().

References vsnprintf().

Referenced by DD::Image::Op::critical(), DD::Image::Op::debug(), DD::Image::Op::error(), DD::Image::WriteGeo::internalError(), DD::Image::Write::internalError(), DD::Image::ReadGeo::internalError(), DD::Image::Op::progressMessage(), snprintf(), vsnprintf(), and DD::Image::Op::warning().

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

Print at most size characters (including a trailing null) into the buffer pointed to by str. See vsnprintf for bugs.

References snprintf(), and vsnprintf().

Referenced by DD::Image::plugin_binary(), DD::Image::plugin_load(), DD::Image::plugin_load_one(), and snprintf().

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

BSD no-buffer-overflow string concatanation function. Appends src to buffer 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.

References strlcat().

Referenced by DD::Image::Writer::Description::find(), and strlcat().

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

BSD no-buffer-overflow string copy function. Copy src to buffer 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.

References strlcpy().

Referenced by DD::Image::GeoWriter::close(), DD::Image::Writer::Description::find(), DD::Image::WriteGeo::internalError(), DD::Image::Write::internalError(), DD::Image::ReadGeo::internalError(), DD::Image::GeoWriter::open(), DD::Image::plugin_addpath(), DD::Image::plugin_error(), DD::Image::plugin_find(), DD::Image::plugin_load_all(), and strlcpy().

char* DDImage_newstring ( const char *  )

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, not free()!

If NULL is passed, a NULL is returned.

It is a good idea to use this instead of strdup() so that all your memory allocations pass through the C++ new-handler.

<DDImage/DDString.h> defines newstring() as a macro that calls this. This is to avoid duplicate-symbol conflicts with libraries that define functions by the same name.

References DDImage_newstring().

Referenced by DDImage_newstring().

double DDImage_strtod ( const char *  p,
char **  endptr 
)

Replacement for strtod() from stdlib.h

Skips all whitespace and then attempts to convert the start of the next text into a double. endptr (if not null) is changed to point at the character it stopped at. This is the same as the standard strtod() but it also recognizes:

  • Infinity as printed by Linux, Windows, and Irix
  • NaN as printed by Linux, Windows, and Irix
  • Hex constants like 0xabcde
  • "true" (1)
  • "false" (0)

These changes allow it to read numbers printed to a file from any platform, and to read constants that were intended for other data types.

<DDImage/string.h> defines strtod() as a macro that calls this, thus replacing the C standard library function.

References DDImage_strtod().

Referenced by DDImage_strtod().