satyr 0.43
Loading...
Searching...
No Matches
utils.h File Reference

Various utility functions, macros and variables that do not fit elsewhere. More...

#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include <glib.h>

Go to the source code of this file.

Macros

#define SR_lower   "abcdefghijklmnopqrstuvwxyz"
#define SR_upper   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define SR_alpha   SR_lower SR_upper
#define SR_space   " \t\r\n\v\f"
#define SR_digit   "0123456789"
#define SR_alnum   SR_alpha SR_digit
#define __sr_printf(x, y)

Functions

void sr_struniq (char **strings, size_t *size)
int sr_ptrstrcmp (const void *s1, const void *s2)
char * sr_strchr_location (const char *s, int c, int *line, int *column)
char * sr_strstr_location (const char *haystack, const char *needle, int *line, int *column)
size_t sr_strspn_location (const char *s, const char *accept, int *line, int *column)
char * sr_file_to_string (const char *filename, char **error_message)
bool sr_string_to_file (const char *filename, char *contents, char **error_message)
bool sr_skip_char (const char **input, char c)
bool sr_skip_char_limited (const char **input, const char *allowed)
bool sr_parse_char_limited (const char **input, const char *allowed, char *result)
int sr_skip_char_sequence (const char **input, char c)
int sr_skip_char_span (const char **input, const char *chars)
int sr_skip_char_span_location (const char **input, const char *chars, int *line, int *column)
int sr_parse_char_span (const char **input, const char *accept, char **result)
int sr_skip_char_cspan (const char **input, const char *reject)
bool sr_parse_char_cspan (const char **input, const char *reject, char **result)
int sr_skip_string (const char **input, const char *string)
bool sr_parse_string (const char **input, const char *string, char **result)
char sr_parse_digit (const char **input)
int sr_skip_uint (const char **input)
int sr_parse_uint32 (const char **input, uint32_t *result)
int sr_parse_uint64 (const char **input, uint64_t *result)
int sr_skip_hexadecimal_uint (const char **input)
int sr_skip_hexadecimal_0xuint (const char **input)
int sr_parse_hexadecimal_uint64 (const char **input, uint64_t *result)
int sr_parse_hexadecimal_0xuint64 (const char **input, uint64_t *result)
char * sr_skip_whitespace (const char *s)
char * sr_skip_non_whitespace (const char *s)
bool sr_skip_to_next_line_location (const char **s, int *line, int *column)
char * sr_bin2hex (char *dst, const char *str, int count)
char * sr_indent (const char *input, int spaces)
char * sr_indent_except_first_line (const char *input, int spaces)
char * sr_build_path (const char *first_element,...)
void sr_parse_os_release (const char *input, void(*callback)(char *, char *, void *), void *data)
char * anonymize_path (char *file_name)
char * sr_demangle_symbol (const char *sym)

Variables

bool sr_debug_parser

Detailed Description

Various utility functions, macros and variables that do not fit elsewhere.

Definition in file utils.h.

Macro Definition Documentation

◆ __sr_printf

#define __sr_printf ( x,
y )
Value:
__attribute__((format(printf, (x), (y))))

Definition at line 48 of file utils.h.

◆ SR_alnum

#define SR_alnum   SR_alpha SR_digit

Definition at line 46 of file utils.h.

◆ SR_alpha

#define SR_alpha   SR_lower SR_upper

Definition at line 43 of file utils.h.

◆ SR_digit

#define SR_digit   "0123456789"

Definition at line 45 of file utils.h.

◆ SR_lower

#define SR_lower   "abcdefghijklmnopqrstuvwxyz"

Definition at line 41 of file utils.h.

◆ SR_space

#define SR_space   " \t\r\n\v\f"

Definition at line 44 of file utils.h.

◆ SR_upper

#define SR_upper   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Definition at line 42 of file utils.h.

Function Documentation

◆ sr_bin2hex()

char * sr_bin2hex ( char * dst,
const char * str,
int count )

Emit a string of hex representation of bytes.

References sr_bin2hex().

Referenced by sr_bin2hex().

◆ sr_demangle_symbol()

char * sr_demangle_symbol ( const char * sym)

Demangles C++ symbol.

Returns
The demangled symbol (allocated by malloc), or NULL on failure.

References sr_demangle_symbol().

Referenced by sr_demangle_symbol().

◆ sr_file_to_string()

char * sr_file_to_string ( const char * filename,
char ** error_message )

Loads file contents to a string.

Returns
File contents. If file opening/reading fails, NULL is returned.

References sr_file_to_string().

Referenced by sr_file_to_string().

◆ sr_parse_char_cspan()

bool sr_parse_char_cspan ( const char ** input,
const char * reject,
char ** result )

If the input contains characters which are not in string reject, create a string from this sequence and store it to the result, move the input pointer after the sequence, and return true. Otherwise do not modify the input and return false.

If this function returns true, the caller is responsible to free the result.

References sr_parse_char_cspan().

Referenced by sr_parse_char_cspan().

◆ sr_parse_char_limited()

bool sr_parse_char_limited ( const char ** input,
const char * allowed,
char * result )

If the input contains one of allowed characters, store the character to the result, move the input pointer after that character, and return true. Otherwise do not modify the input and return false.

References sr_parse_char_limited().

Referenced by sr_parse_char_limited().

◆ sr_parse_char_span()

int sr_parse_char_span ( const char ** input,
const char * accept,
char ** result )

If the input contains one or more characters from string accept, create a string from this sequence and store it to the result, move the input pointer after the sequence, and return the lenght of the sequence. Otherwise do not modify the input and return 0.

If this function returns nonzero value, the caller is responsible to free the result.

References sr_parse_char_span().

Referenced by sr_parse_char_span().

◆ sr_parse_digit()

char sr_parse_digit ( const char ** input)

If the input contains digit 0-9, return it as a character and move the input pointer after it. Otherwise return '\0' and do not modify the input.

References sr_parse_digit().

Referenced by sr_parse_digit().

◆ sr_parse_hexadecimal_0xuint64()

int sr_parse_hexadecimal_0xuint64 ( const char ** input,
uint64_t * result )

If the input contains 0x[0-9a-f]+, parse the number, and move the input pointer after it. Otherwise do not modify the input.

Returns
The number of characters read from input. 0 if the input does not contain a hexadecimal number.

References sr_parse_hexadecimal_0xuint64().

Referenced by sr_parse_hexadecimal_0xuint64().

◆ sr_parse_hexadecimal_uint64()

int sr_parse_hexadecimal_uint64 ( const char ** input,
uint64_t * result )

If the input contains [0-9a-f]+, parse the number, and move the input pointer after it. Otherwise do not modify the input.

Returns
The number of characters read from input. 0 if the input does not contain a hexadecimal number.

References sr_parse_hexadecimal_uint64().

Referenced by sr_parse_hexadecimal_uint64().

◆ sr_parse_os_release()

void sr_parse_os_release ( const char * input,
void(* callback )(char *, char *, void *),
void * data )

Parses /etc/os-release file, see http://www.freedesktop.org/software/systemd/man/os-release.html for more information. Calls callback for each key-value pair it reads from the file.

References sr_parse_os_release().

Referenced by sr_parse_os_release().

◆ sr_parse_string()

bool sr_parse_string ( const char ** input,
const char * string,
char ** result )

If the input contains the string, copy the string to result, move the input pointer after the string, and return true. Otherwise do not modify the input and return false.

If this function returns true, the caller is responsible to free the result.

References sr_parse_string().

Referenced by sr_parse_string().

◆ sr_parse_uint32()

int sr_parse_uint32 ( const char ** input,
uint32_t * result )

If the input contains [0-9]+, parse it, move the input pointer after the number.

Returns
Number of parsed characters. 0 if input does not contain a number.

References sr_parse_uint32().

Referenced by sr_parse_uint32().

◆ sr_ptrstrcmp()

int sr_ptrstrcmp ( const void * s1,
const void * s2 )

A wrapper around g_strcmp0 that takes pointers to strings. Can be used as a paramter to qsort or bsearch.

References sr_ptrstrcmp().

Referenced by sr_ptrstrcmp().

◆ sr_skip_char()

bool sr_skip_char ( const char ** input,
char c )

If the input contains character c in the current positon, move the input pointer after the character, and return true. Otherwise do not modify the input and return false.

References sr_skip_char().

Referenced by sr_skip_char().

◆ sr_skip_char_cspan()

int sr_skip_char_cspan ( const char ** input,
const char * reject )

If the input contains one or more characters which are not presentin string reject, move the input pointer after the sequence. Otherwise do not modify the input.

Returns
The number of characters skipped.

References sr_skip_char_cspan().

Referenced by sr_skip_char_cspan().

◆ sr_skip_char_limited()

bool sr_skip_char_limited ( const char ** input,
const char * allowed )

If the input contains one of allowed characters, move the input pointer after that character, and return true. Otherwise do not modify the input and return false.

References sr_skip_char_limited().

Referenced by sr_skip_char_limited().

◆ sr_skip_char_sequence()

int sr_skip_char_sequence ( const char ** input,
char c )

If the input contains the character c one or more times, update it so that the characters are skipped. Returns the number of characters skipped, thus zero if **input does not contain c.

References sr_skip_char_sequence().

Referenced by sr_skip_char_sequence().

◆ sr_skip_char_span()

int sr_skip_char_span ( const char ** input,
const char * chars )

If the input contains one or more characters from string chars, move the input pointer after the sequence. Otherwise do not modify the input.

Returns
The number of characters skipped.

References sr_skip_char_span().

Referenced by sr_skip_char_span().

◆ sr_skip_char_span_location()

int sr_skip_char_span_location ( const char ** input,
const char * chars,
int * line,
int * column )

If the input contains one or more characters from string chars, move the input pointer after the sequence. Otherwise do not modify the input.

Parameters
lineStarts from 1. Corresponds to the returned number.
columnStarts from 0. Corresponds to the returned number.
Returns
The number of characters skipped.

References sr_skip_char_span_location().

Referenced by sr_skip_char_span_location().

◆ sr_skip_hexadecimal_0xuint()

int sr_skip_hexadecimal_0xuint ( const char ** input)

If the input contains 0x[0-9a-f]+, move the input pointer after that.

Returns
The number of characters processed from input. 0 if the input does not contain a hexadecimal number.

References sr_skip_hexadecimal_0xuint().

Referenced by sr_skip_hexadecimal_0xuint().

◆ sr_skip_hexadecimal_uint()

int sr_skip_hexadecimal_uint ( const char ** input)

If the input contains [0-9a-f]+, move the input pointer after that.

Returns
The number of characters processed from input. 0 if the input does not contain a hexadecimal number.

References sr_skip_hexadecimal_uint().

Referenced by sr_skip_hexadecimal_uint().

◆ sr_skip_string()

int sr_skip_string ( const char ** input,
const char * string )

If the input contains the string, move the input pointer after the sequence. Otherwise do not modify the input.

Returns
Number of characters skipped. 0 if the input does not contain the string.

References sr_skip_string().

Referenced by sr_skip_string().

◆ sr_skip_uint()

int sr_skip_uint ( const char ** input)

If the input contains [0-9]+, move the input pointer after the number.

Returns
The number of skipped characters. 0 if input does not start with a digit.

References sr_skip_uint().

Referenced by sr_skip_uint().

◆ sr_strchr_location()

char * sr_strchr_location ( const char * s,
int c,
int * line,
int * column )

A strchr() variant providing line and column in the string s indicating where the char c was found.

Parameters
lineStarts from 1. Its value is valid only when this function does not return NULL.
columnStarts from 0. Its value is valid only when this function does not return NULL.

References sr_strchr_location().

Referenced by sr_strchr_location().

◆ sr_strspn_location()

size_t sr_strspn_location ( const char * s,
const char * accept,
int * line,
int * column )

A strspn() variant providing line and column of the string s which corresponds to the returned length.

Parameters
lineStarts from 1.
columnStarts from 0.

References sr_strspn_location().

Referenced by sr_strspn_location().

◆ sr_strstr_location()

char * sr_strstr_location ( const char * haystack,
const char * needle,
int * line,
int * column )

A strstr() variant providing line and column of the haystick indicating where the needle was found.

Parameters
lineStarts from 1. Its value is valid only when this function does not return NULL.
columnStarts from 0. Its value is valid only when this function does not return NULL.

References sr_strstr_location().

Referenced by sr_strstr_location().

Variable Documentation

◆ sr_debug_parser

bool sr_debug_parser
extern

Debugging output to stdout while parsing. Default value is false.