Tor  0.4.7.0-alpha-dev
Macros | Functions
scanf.c File Reference

Locale-independent minimal implementation of sscanf(). More...

#include "lib/string/scanf.h"
#include "lib/string/compat_ctype.h"
#include "lib/cc/torint.h"
#include "lib/err/torerr.h"
#include <stdlib.h>

Go to the source code of this file.

Macros

#define MAX_SCANF_WIDTH   9999
 

Functions

static int digit_to_num (char d)
 
static int scan_unsigned (const char **bufp, unsigned long *out, int width, unsigned base)
 
static int scan_signed (const char **bufp, long *out, int width)
 
static int scan_double (const char **bufp, double *out, int width)
 
static int scan_string (const char **bufp, char *out, int width)
 
int tor_vsscanf (const char *buf, const char *pattern, va_list ap)
 
int tor_sscanf (const char *buf, const char *pattern,...)
 

Detailed Description

Locale-independent minimal implementation of sscanf().

Definition in file scanf.c.

Function Documentation

◆ digit_to_num()

static int digit_to_num ( char  d)
static

Helper: given an ASCII-encoded decimal digit, return its numeric value. NOTE: requires that its input be in-bounds.

Definition at line 23 of file scanf.c.

Referenced by tor_vsscanf().

◆ scan_double()

static int scan_double ( const char **  bufp,
double *  out,
int  width 
)
static

Helper: Read a decimal-formatted double from *bufp of up to width characters. (Handle arbitrary width if width is less than 0.) On success, store the result in out, advance bufp to the next character, and return 0. On failure, return -1.

Definition at line 117 of file scanf.c.

◆ scan_signed()

static int scan_signed ( const char **  bufp,
long *  out,
int  width 
)
static

Helper: Read an signed int from *bufp of up to width characters. (Handle arbitrary width if width is less than 0.) On success, store the result in out, advance bufp to the next character, and return 0. On failure, return -1.

Definition at line 72 of file scanf.c.

◆ scan_string()

static int scan_string ( const char **  bufp,
char *  out,
int  width 
)
static

Helper: copy up to width non-space characters from bufp to out. Make sure out is nul-terminated. Advance bufp to the next non-space character or the EOS.

Definition at line 162 of file scanf.c.

◆ scan_unsigned()

static int scan_unsigned ( const char **  bufp,
unsigned long *  out,
int  width,
unsigned  base 
)
static

Helper: Read an unsigned int from *bufp of up to width characters. (Handle arbitrary width if width is less than 0.) On success, store the result in out, advance bufp to the next character, and return 0. On failure, return -1.

Definition at line 35 of file scanf.c.

◆ tor_sscanf()

int tor_sscanf ( const char *  buf,
const char *  pattern,
  ... 
)

Minimal sscanf replacement: parse buf according to pattern and store the results in the corresponding argument fields. Differs from sscanf in that:

  • It only handles u, lu, x, lx, %[NUM]s, d, ld, lf, and c.
  • It only handles decimal inputs for lf. (12.3, not 1.23e1)
  • It does not handle arbitrarily long widths.
  • Numbers do not consume any space characters.
  • It is locale-independent.
  • u and x do not consume any space.
  • It returns -1 on malformed patterns.

(As with other locale-independent functions, we need this to parse data that is in ASCII without worrying that the C library's locale-handling will make miscellaneous characters look like numbers, spaces, and so on.)

Definition at line 309 of file scanf.c.

Referenced by parse_http_time(), parse_iso_time_(), tor_inet_aton(), and tor_inet_pton().

◆ tor_vsscanf()

int tor_vsscanf ( const char *  buf,
const char *  pattern,
va_list  ap 
)

Locale-independent, minimal, no-surprises scanf variant, accepting only a restricted pattern format. For more info on what it supports, see tor_sscanf() documentation.

Definition at line 179 of file scanf.c.

Referenced by tor_sscanf().