tor  0.4.2.1-alpha-dev
Functions | Variables
unitparse.c File Reference
#include "orconfig.h"
#include "lib/confmgt/unitparse.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/string/parse_int.h"
#include "lib/string/util_string.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)
 
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 196 of file unitparse.c.

References config_parse_units(), and time_units.

◆ 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 168 of file unitparse.c.

References config_parse_units(), and memory_units.

◆ 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 179 of file unitparse.c.

References config_parse_units(), and time_msec_units.

◆ config_parse_units()

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

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.

Definition at line 114 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 23 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 89 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 70 of file unitparse.c.

Referenced by config_parse_interval().