CrystalSpace

Public API Reference

csutil/scanstr.h File Reference

String scanning (sscanf() flavor). More...

#include "csextern.h"

Go to the source code of this file.

Functions

int csScanStr (const char *in, const char *format,...)
 Own version of sscanf that is a bit more relaxed towards spaces and also accepts quoted strings (the quotes will not be included into the result string).

Detailed Description

String scanning (sscanf() flavor).

Definition in file scanstr.h.


Function Documentation

int csScanStr ( const char *  in,
const char *  format,
  ... 
)

Own version of sscanf that is a bit more relaxed towards spaces and also accepts quoted strings (the quotes will not be included into the result string).

Safe string arguments
The string formats %s and %S expect a pointer to a character array as the destination value; the scanned string is then copied to that array. As there is no way for csScanStr to know the size of the destination buffer it might be overflowed, causing memory corruption.
However, csScanStr supports the safe formats %as and %aS; they behave like %s and %S, but expect a pointer to a pointer as the destination. csScanStr will allocate a buffer (using cs_malloc) of a sufficient size to hold the scanned string and store that pointer in the destination.

Example:

 char* scanned_str (nullptr);
 csScanStr (input, "%as", &scanned_str);
 if (scanned_str)
 {
   // Do stuff
 }
 cs_free (scanned_str);
Supported format commands
  • %d -- integer number
  • %f -- floating point
  • %b -- boolean (0, 1, true, false, yes, no, on, off)
  • %s -- string (with or without single quotes)
  • %S -- string (delimited with double quotes)
    \n will be converted to a newline
    \t will be converted to a tab
    \\ produces a \
    \" produces a \c "
    all other combinations of \ are copied.
  • %D -- list of integers, first argument should be a pointer to an array of integers, second argument a pointer to an integer which will contain the number of elements inserted in the list.
  • %F -- similarly, a list of floats.
  • %n -- this returns the amount of the input string thats been consumed, in characters. Does NOT increment the return count and does not read from the input string.

Returns the number of successfully scanned arguments. Hence if there is a mismatch effectively the number of the last successfully scanned argument is returned.


Generated for Crystal Space 2.1 by doxygen 1.6.1