Data Accelerator Offload
Loading...
Searching...
No Matches
Data Structures | Functions
dao_dynamic_string.h File Reference
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <rte_config.h>
#include <rte_common.h>
#include <rte_compat.h>

Go to the source code of this file.

Data Structures

struct  dao_ds
 

Functions

void dao_ds_init (struct dao_ds *ds)
 
void dao_ds_clear (struct dao_ds *ds)
 
void dao_ds_truncate (struct dao_ds *ds, size_t new_length)
 
void dao_ds_reserve (struct dao_ds *ds, size_t min_length)
 
char * dao_ds_put_uninit (struct dao_ds *ds, size_t n)
 
void dao_ds_put_utf8 (struct dao_ds *ds, int uc)
 
void dao_ds_put_char_multiple (struct dao_ds *ds, char c, size_t n)
 
void dao_ds_put_buffer (struct dao_ds *ds, const char *buf, size_t n)
 
void dao_ds_put_cstr (struct dao_ds *ds, const char *str)
 
void dao_ds_put_and_free_cstr (struct dao_ds *ds, char *str)
 
void dao_ds_put_format (struct dao_ds *ds, const char *format,...)
 
void dao_ds_put_format_valist (struct dao_ds *ds, const char *format, va_list va)
 
void dao_ds_put_printable (struct dao_ds *ds, const char *buf, size_t size)
 
void dao_ds_put_hex (struct dao_ds *ds, const void *buf, size_t size)
 
void dao_ds_put_hex_dump (struct dao_ds *ds, const void *buf, size_t size, uintptr_t ofs, bool ascii)
 
void dao_ds_put_sparse_hex_dump (struct dao_ds *ds, const void *buf, size_t size, uintptr_t ofs, bool ascii)
 
int dao_ds_get_line (struct dao_ds *ds, FILE *file)
 
int dao_ds_get_preprocessed_line (struct dao_ds *ds, FILE *file, int *line_numberp)
 
int dao_ds_get_test_line (struct dao_ds *ds, FILE *file)
 
char * dao_ds_cstr (struct dao_ds *ds)
 
char * dao_ds_steal_cstr (struct dao_ds *ds)
 
void dao_ds_destroy (struct dao_ds *ds)
 
void dao_ds_swap (struct dao_ds *a, struct dao_ds *b)
 
int dao_ds_last (const struct dao_ds *ds)
 
bool dao_ds_chomp (struct dao_ds *ds, int c)
 
void dao_ds_clone (struct dao_ds *dst, struct dao_ds *source)
 
static void dao_ds_put_char (struct dao_ds *ds, char c)
 

Detailed Description

DAO dynamic string APIs helps to construct/store/extend/print variable length strings

A "dynamic string", that is, a buffer that can be used to construct a string across a series of operations that extend or modify it.

The 'string' member does not always point to a null-terminated string. Initially it is NULL, and even when it is nonnull, some operations do not ensure that it is null-terminated. Use dao_ds_cstr() to ensure that memory is allocated for the string and that it is null-terminated.

Definition in file dao_dynamic_string.h.

Function Documentation

◆ dao_ds_init()

void dao_ds_init ( struct dao_ds ds)

Initialize "dynamic string" object

Parameters
ds"dynamic string" object

◆ dao_ds_clear()

void dao_ds_clear ( struct dao_ds ds)

Set length of string, managed by "dynamic string" object, to 0. Does not free memory

Parameters
ds"dynamic string" object

◆ dao_ds_truncate()

void dao_ds_truncate ( struct dao_ds ds,
size_t  new_length 
)

Reduces length of string, managed by "dynamic string" object, to no more than 'new length'. If its length is already 'new_length' or less, does nothing.

Parameters
ds"dynamic string" object
new_lengthLength to which string to be truncated

◆ dao_ds_reserve()

void dao_ds_reserve ( struct dao_ds ds,
size_t  min_length 
)

Ensures that at least 'min_length + 1' bytes (including space for a null terminator) are allocated for ds->string, allocating or reallocating memory as necessary.

Parameters
ds"dynamic string" object
min_lengthLength which ds->string should be able to hold at minimum.

◆ dao_ds_put_uninit()

char * dao_ds_put_uninit ( struct dao_ds ds,
size_t  n 
)

Appends space for 'n' bytes to the end of 'ds->string', increasing 'ds->length' by the same amount, and returns the first appended byte. The caller should fill in all 'n' bytes starting at the return value.

Parameters
ds"dynamic string" object
nNumber of bytes to be appended to the string of ds->string
Returns
String pointing to first appended byte

◆ dao_ds_put_utf8()

void dao_ds_put_utf8 ( struct dao_ds ds,
int  uc 
)

Appends unicode code point 'uc' to 'ds' in UTF-8 encoding.

Parameters
ds"dynamic string" object
ucunicode code point

◆ dao_ds_put_char_multiple()

void dao_ds_put_char_multiple ( struct dao_ds ds,
char  c,
size_t  n 
)

Append single character 'c' multiple times to 'ds'

Parameters
ds"dynamic string" object
ccharacter to be added
nNo. of times character to be added in string

◆ dao_ds_put_buffer()

void dao_ds_put_buffer ( struct dao_ds ds,
const char *  buf,
size_t  n 
)

Append buffer 'buf' of length 'n' to 'ds'

Parameters
ds"dynamic string" object
bufbuffer to be appended
nlength of buffer 'buf'

◆ dao_ds_put_cstr()

void dao_ds_put_cstr ( struct dao_ds ds,
const char *  str 
)

Append string 'str' to 'ds'

Parameters
ds"dynamic string" object
strstring to be appended

◆ dao_ds_put_and_free_cstr()

void dao_ds_put_and_free_cstr ( struct dao_ds ds,
char *  str 
)

Append string 'str' to 'ds' and free 'str' afterwards

Parameters
ds"dynamic string" object
strAfter appending 'str' to ds, memory associated with 'str' is freed

◆ dao_ds_put_format()

void dao_ds_put_format ( struct dao_ds ds,
const char *  format,
  ... 
)

Append 'format' string to 'ds' with variable arguments. Functionality similar to sprintf()

Parameters
ds"dynamic string" object
formatstring with variable arguments

◆ dao_ds_put_format_valist()

void dao_ds_put_format_valist ( struct dao_ds ds,
const char *  format,
va_list  va 
)

Append 'format' string to 'ds' with variable arguments and va_list

Parameters
ds"dynamic string" object
formatstring with variable arguments
vava_list

◆ dao_ds_put_printable()

void dao_ds_put_printable ( struct dao_ds ds,
const char *  buf,
size_t  size 
)

Append buffer 'buf' of 'size' bytes to 'ds' and convert each character in printable format. In other words, print ascii characters in range of [@,A-Z, a-z,\,^.`,~,_,{,},|,]

Parameters
ds"dynamic string" object
bufbuffer to be appended
sizesize of buffer 'buf' in bytes

◆ dao_ds_put_hex()

void dao_ds_put_hex ( struct dao_ds ds,
const void *  buf,
size_t  size 
)

Append buffer 'buf' of 'size' bytes to 'ds' and convert each character in hexadecimal format.

Parameters
ds"dynamic string" object
bufbuffer to be appended
sizesize of buffer 'buf' in bytes

◆ dao_ds_put_hex_dump()

void dao_ds_put_hex_dump ( struct dao_ds ds,
const void *  buf,
size_t  size,
uintptr_t  ofs,
bool  ascii 
)

Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per line. Numeric offsets are also included, starting at 'ofs' for the first byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters are also rendered alongside.

Parameters
ds"dynamic string" object
bufbuffer to be appended
sizesize of buffer 'buf' in bytes
ofsoffset to start with
asciiif 'true' then ASCII characters are also rendered alongside hex

◆ dao_ds_put_sparse_hex_dump()

void dao_ds_put_sparse_hex_dump ( struct dao_ds ds,
const void *  buf,
size_t  size,
uintptr_t  ofs,
bool  ascii 
)

Same as 'dao_ds_put_hex_dump', but doesn't print lines that only contains zero bytes.

Parameters
ds"dynamic string" object
bufbuffer to be appended
sizesize of buffer 'buf' in bytes
ofsoffset to start with
asciiif 'true' then ASCII characters are also rendered alongside hex

◆ dao_ds_get_line()

int dao_ds_get_line ( struct dao_ds ds,
FILE *  file 
)

Get line from 'file' until encounter '
' or 'EOF' and save it to 'ds'

Parameters
ds"dynamic string" object to which a line is written
fileFile from where a line is read
Returns
0: On Success EOF: On Failure

◆ dao_ds_get_preprocessed_line()

int dao_ds_get_preprocessed_line ( struct dao_ds ds,
FILE *  file,
int *  line_numberp 
)

Reads a line from 'file' into 'ds', clearing anything initially in 'ds'. Deletes comments introduced by "#" and skips lines that contains only white space (after deleting comments).

If 'line_numberp' is nonnull, increments '*line_numberp' by the number of lines read from 'file'.

Parameters
ds"dynamic string" object
fileFile pointer
[out]line_numberpIf nonnull, returns number of lines read
Returns
0: On Success EOF: if non-blank line was found

◆ dao_ds_get_test_line()

int dao_ds_get_test_line ( struct dao_ds ds,
FILE *  file 
)

Reads a line from 'file' into 'ds' and does some preprocessing on it:

  • If the line begins with #, prints it on stdout and reads the next line.
  • Otherwise, if the line contains an # somewhere else, strips it and everything following it (as a comment).
  • If (after comment removal) the line contains only white space, prints a blank line on stdout and reads the next line.
  • Otherwise, returns the line to the caller.

This is useful in some of the tests, where we want to check that parsing and then re-formatting some kind of data does not change it, but we also want to be able to put comments in the input.

Parameters
ds"dynamic string" object
fileFile pointer
Returns
0: If Success, EOF: Non-blank line Returns 0 if successful, EOF if no non-blank line was found.

◆ dao_ds_cstr()

char * dao_ds_cstr ( struct dao_ds ds)

Return string corresponding to ds. strxxx()/printf() operations are valid on returned string

Parameters
ds"dynamic object"
Returns
On success: valid string On failure: NULL string

◆ dao_ds_steal_cstr()

char * dao_ds_steal_cstr ( struct dao_ds ds)

Returns a null-terminated string representing the current contents of 'ds', which the caller is expected to free with free(), then clears the contents of 'ds'.

Parameters
ds"dynamic string object
Returns
On success: null-terminated string

◆ dao_ds_destroy()

void dao_ds_destroy ( struct dao_ds ds)

Free string associated with 'ds'

Parameters
ds"dynamic string" object

◆ dao_ds_swap()

void dao_ds_swap ( struct dao_ds a,
struct dao_ds b 
)

Swaps the content of 'a' and 'b'.

Parameters
aFirst string to be swapped with string 'b'
bSecond string to be swapped with string 'a'

◆ dao_ds_last()

int dao_ds_last ( const struct dao_ds ds)

Return last character in string associated by ds

Parameters
ds"dynamic string object
Returns
On Success: last character as integer On Failure: EOF

◆ dao_ds_chomp()

bool dao_ds_chomp ( struct dao_ds ds,
int  c 
)

Chomp character 'c' if it is last character in ds->string and return true otherwise return false

Parameters
ds"dynamic string" object
ccharacter to be chomped from last character of string
Returns
True: if character provided is successfully chompped from last character of string False: If 'c' is not chompped

◆ dao_ds_clone()

void dao_ds_clone ( struct dao_ds dst,
struct dao_ds source 
)

Clone string 'source' into string 'dst'

Parameters
source"dynamic string" to be cloned
dstdestination "dynamic string" cloned from source on return

◆ dao_ds_put_char()

static void dao_ds_put_char ( struct dao_ds ds,
char  c 
)
inlinestatic

Add character to dynamic string

Parameters
ds"dynamic sting" object
ccharacter to be added in ds->string

Definition at line 404 of file dao_dynamic_string.h.