libprom  @VERSION@
C based libraries to expose metrics in Promtheus exposition format
Typedefs | Functions
prom_string_builder.h File Reference

A StringBuilder: uses an internal buffer to append strings and characters as needed and keeps track of the constructed string, grows the buffer by factor 2 automagically. More...

#include <stddef.h>
Include dependency graph for prom_string_builder.h:

Go to the source code of this file.

Typedefs

typedef struct psb psb_t
 

Functions

psb_t * psb_new (void)
 Setup a new string builder. More...
 
int psb_destroy (psb_t *self)
 Destroys the given string builder. More...
 
int psb_add_str (psb_t *self, const char *str)
 Append the given string to the buffered string of the given string builder. More...
 
int psb_add_char (psb_t *self, char c)
 Append the given character to the buffered string of the given string builder. More...
 
int psb_clear (psb_t *self)
 Free the allocated string buffer area of the given string builder, allocate a new one with a default initial size and set its length to 0. More...
 
int psb_truncate (psb_t *self, size_t len)
 Set the length of the buffered string of the given string builder to the given length and append '\0'. More...
 
size_t psb_len (psb_t *self)
 Get the length of the buffered string of the given string builder. More...
 
char * psb_dump (psb_t *self)
 Get a copy of the buffered string of the given string builder. More...
 
char * psb_str (psb_t *self)
 Get a reference to the buffered string. More...
 

Detailed Description

A StringBuilder: uses an internal buffer to append strings and characters as needed and keeps track of the constructed string, grows the buffer by factor 2 automagically.

Note
All psb_t parameter in the related functions are expected to be != NULL, otherwise be prepared for core dumps.

Function Documentation

◆ psb_add_char()

int psb_add_char ( psb_t *  self,
char  c 
)

Append the given character to the buffered string of the given string builder.

Parameters
selfWhere to append the character.
cCharacter to append.
Returns
0 on success, a number > 0 otherwise.

◆ psb_add_str()

int psb_add_str ( psb_t *  self,
const char *  str 
)

Append the given string to the buffered string of the given string builder.

Parameters
selfWhere to append the string.
strString to append.
Returns
0 on success, a number > 0 otherwise.

◆ psb_clear()

int psb_clear ( psb_t *  self)

Free the allocated string buffer area of the given string builder, allocate a new one with a default initial size and set its length to 0.

Parameters
selfString builder to free.
Returns
0 on success, a number > 0 otherwise.

◆ psb_destroy()

int psb_destroy ( psb_t *  self)

Destroys the given string builder.

Parameters
selfString builder to destroy.
Returns
0 .

◆ psb_dump()

char* psb_dump ( psb_t *  self)

Get a copy of the buffered string of the given string builder.

Parameters
selfString builder to ask.
Returns
Metric as string in Prometheus exposition format.
Note
The returned string must be freed when no longer needed.

◆ psb_len()

size_t psb_len ( psb_t *  self)

Get the length of the buffered string of the given string builder.

Parameters
selfString builder to query.
Returns
The length of the buffered string.

◆ psb_new()

psb_t* psb_new ( void  )

Setup a new string builder.

Returns
A new string builder or NULL on error.

◆ psb_str()

char* psb_str ( psb_t *  self)

Get a reference to the buffered string.

One should NOT modify the string, treat it as a read-only and never ever free() it, otherwise be prepared for buffer overflows and core dumps. It has been made public solely to avoid the strdup() overhead of psb_dump().

Returns
a string, or NULL if not initialized yet.

◆ psb_truncate()

int psb_truncate ( psb_t *  self,
size_t  len 
)

Set the length of the buffered string of the given string builder to the given length and append '\0'.

Parameters
selfString builder to truncate.
lenThe new length of the buffered string. If it is bigger than its current length, this function is a no-op.
Returns
0 on success, a number > 0 otherwise.