Tor  0.4.7.0-alpha-dev
Functions | Variables
unitparse.c File Reference

Functions for parsing values with units from a configuration file. More...

#include "orconfig.h"
#include "lib/confmgt/unitparse.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/string/parse_int.h"
#include "lib/string/printf.h"
#include "lib/string/util_string.h"
#include "lib/intmath/muldiv.h"
#include <string.h>

Go to the source code of this file.

Functions

uint64_t config_parse_units (const char *val, const unit_table_t *u, int *ok, char **errmsg_out)
 
uint64_t config_parse_memunit (const char *s, int *ok)
 
int config_parse_msec_interval (const char *s, int *ok)
 
int config_parse_interval (const char *s, int *ok)
 

Variables

const struct unit_table_t memory_units []
 
const struct unit_table_t time_units []
 
const struct unit_table_t time_msec_units []
 

Detailed Description

Functions for parsing values with units from a configuration file.

Definition in file unitparse.c.

Function Documentation

◆ config_parse_interval()

int config_parse_interval ( const char *  s,
int *  ok 
)

Parse a string in the format "number unit", where unit is a unit of time. On success, set *ok to true and return the number of seconds in the provided interval. Otherwise, set *ok to 0 and return -1.

Definition at line 250 of file unitparse.c.

◆ config_parse_memunit()

uint64_t config_parse_memunit ( const char *  s,
int *  ok 
)

Parse a string in the format "number unit", where unit is a unit of information (byte, KB, M, etc). On success, set *ok to true and return the number of bytes specified. Otherwise, set *ok to false and return 0.

Definition at line 222 of file unitparse.c.

◆ config_parse_msec_interval()

int config_parse_msec_interval ( const char *  s,
int *  ok 
)

Parse a string in the format "number unit", where unit is a unit of time in milliseconds. On success, set *ok to true and return the number of milliseconds in the provided interval. Otherwise, set *ok to 0 and return -1.

Definition at line 233 of file unitparse.c.

◆ config_parse_units()

uint64_t config_parse_units ( const char *  val,
const unit_table_t u,
int *  ok,
char **  errmsg_out 
)

Parse a string val containing a number, zero or more spaces, and an optional unit string. If the unit appears in the table u, then multiply the number by the unit multiplier. On success, set *ok to 1 and return this product. Otherwise, set *ok to 0.

If an error (like overflow or a negative value is detected), put an error message in *errmsg_out if that pointer is non-NULL, and otherwise log a warning.

Definition at line 127 of file unitparse.c.

Referenced by config_parse_interval(), config_parse_memunit(), and config_parse_msec_interval().

Variable Documentation

◆ memory_units

const struct unit_table_t memory_units[]

Table to map the names of memory units to the number of bytes they contain.

Definition at line 1 of file unitparse.c.

Referenced by config_parse_memunit().

◆ time_msec_units

const struct unit_table_t time_msec_units[]
Initial value:
= {
{ "", 1 },
{ "msec", 1 },
{ "millisecond", 1 },
{ "milliseconds", 1 },
{ "second", 1000 },
{ "seconds", 1000 },
{ "minute", 60*1000 },
{ "minutes", 60*1000 },
{ "hour", 60*60*1000 },
{ "hours", 60*60*1000 },
{ "day", 24*60*60*1000 },
{ "days", 24*60*60*1000 },
{ "week", 7*24*60*60*1000 },
{ "weeks", 7*24*60*60*1000 },
{ NULL, 0 },
}

Table to map the names of time units to the number of milliseconds they contain.

Definition at line 1 of file unitparse.c.

Referenced by config_parse_msec_interval().

◆ time_units

const struct unit_table_t time_units[]
Initial value:
= {
{ "", 1 },
{ "second", 1 },
{ "seconds", 1 },
{ "minute", 60 },
{ "minutes", 60 },
{ "hour", 60*60 },
{ "hours", 60*60 },
{ "day", 24*60*60 },
{ "days", 24*60*60 },
{ "week", 7*24*60*60 },
{ "weeks", 7*24*60*60 },
{ "month", 2629728, },
{ "months", 2629728, },
{ NULL, 0 },
}

Table to map the names of time units to the number of seconds they contain.

Definition at line 1 of file unitparse.c.

Referenced by config_parse_interval().