Macros | Functions
parse_int.c File Reference
#include "lib/string/parse_int.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.




long tor_parse_long (const char *s, int base, long min, long max, int *ok, char **next)
unsigned long tor_parse_ulong (const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
double tor_parse_double (const char *s, double min, double max, int *ok, char **next)
uint64_t tor_parse_uint64 (const char *s, int base, uint64_t min, uint64_t max, int *ok, char **next)

Detailed Description

Convert strings into the integers they encode, with bounds checking.

Definition in file parse_int.c.

Macro Definition Documentation


/* Did an overflow occur? */ \
if (errno == ERANGE) \
goto err; \
/* Was at least one character converted? */ \
if (endptr == s) \
goto err; \
/* Were there unexpected unconverted characters? */ \
if (!next && *endptr) \
goto err; \
/* Illogical (max, min) inputs? */ \
if (max < min) \
goto err; \
/* Is r within limits? */ \
if (r < min || r > max) \
goto err; \
if (ok) *ok = 1; \
if (next) *next = endptr; \
return r; \
err: \
if (ok) *ok = 0; \
if (next) *next = endptr; \
return 0

Definition at line 19 of file parse_int.c.

Function Documentation

◆ tor_parse_double()

double tor_parse_double ( const char *  s,
double  min,
double  max,
int *  ok,
char **  next 

As tor_parse_long(), but return a double.

Definition at line 94 of file parse_int.c.

◆ tor_parse_long()

long tor_parse_long ( const char *  s,
int  base,
long  min,
long  max,
int *  ok,
char **  next 

Extract a long from the start of s, in the given numeric base. If base is 0, s is parsed as a decimal, octal, or hex number in the syntax of a C integer literal. If there is unconverted data and next is provided, set *next to the first unconverted character. An error has occurred if no characters are converted; or if there are unconverted characters and next is NULL; or if the parsed value is not between min and max. When no error occurs, return the parsed value and set *ok (if provided) to

  1. When an error occurs, return 0 and set *ok (if provided) to 0.

Definition at line 56 of file parse_int.c.

Referenced by cmp_int_strings_(), dirvote_get_intermediate_param_value(), handle_control_protocolinfo(), parse_port_range(), and parse_process_specifier().

◆ tor_parse_uint64()

uint64_t tor_parse_uint64 ( const char *  s,
int  base,
uint64_t  min,
uint64_t  max,
int *  ok,
char **  next 

As tor_parse_long, but return a uint64_t. Only base 10 is guaranteed to work for now.

Definition at line 107 of file parse_int.c.

Referenced by get_stream().

◆ tor_parse_ulong()

unsigned long tor_parse_ulong ( const char *  s,
int  base,
unsigned long  min,
unsigned long  max,
int *  ok,
char **  next 

As tor_parse_long(), but return an unsigned long.

Definition at line 75 of file parse_int.c.

Referenced by get_circ().