All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Fields | Private Attributes | Related Functions
as_string Struct Reference

Detailed Description

Container for NULL-terminates string values.

Initialization

An as_string should be initialized via one of the provided function.

To initialize a stack allocated as_string, use as_string_init():

as_string_init(&s, "abc", false);

The 3rd argument indicates whether the string value should be free()d when as_string is destroyed.

To create and initialize a heap allocated as_integer, use as_integer_new():

as_string * s = as_string_new("abc", false);

Destruction

When the as_string instance is no longer required, then you should release the resources associated with it via as_string_destroy():

Usage

There are two functions for getting the boxed value contained by as_string:

as_string_get() returns the contained value. If an error occurred, then NULL is returned. Possible errors is the as_integer instance is NULL.

char * sval = as_string_get(i);

as_string_getorelse() allows you to return a default value if an error occurs:

char * sval = as_string_getorelse(i, "oops!");

Conversions

as_string is derived from as_val, so it is generally safe to down cast:

as_val val = (as_val) s;

However, upcasting is more error prone. When doing so, you should use as_string_fromval(). If conversion fails, then the return value is NULL.

Definition at line 104 of file as_string.h.

#include "as_string.h"

+ Inheritance diagram for as_string:
+ Collaboration diagram for as_string:

Data Fields

bool free
 
size_t len
 
char * value
 
- Data Fields inherited from as_val
cf_atomic32 count
 
bool free
 
enum as_val_t type
 

Private Attributes

as_val _
 

Related Functions

(Note that these are not member functions.)

const char * as_basename (as_string *filename, const char *path)
 
static void as_string_destroy (as_string *string)
 
static as_stringas_string_fromval (const as_val *v)
 
static char * as_string_get (const as_string *string)
 
static char * as_string_getorelse (const as_string *string, char *fallback)
 
as_stringas_string_init (as_string *string, char *value, bool free)
 
size_t as_string_len (as_string *string)
 
as_stringas_string_new (char *value, bool free)
 
static char * as_string_tostring (const as_string *string)
 
static as_valas_string_toval (const as_string *s)
 
bool as_strncpy (char *trg, const char *src, int size)
 

Friends And Related Function Documentation

const char * as_basename ( as_string filename,
const char *  path 
)
related

Return filename component of full path.

If path is empty, the current directory is returned. If path contains trailing directory slashes, create new string to hold filename without slashes. The input path is guaranteed not to be modified. as_string_destroy() must be called when finished with filename.

static void as_string_destroy ( as_string string)
related

Destroy the as_string and associated resources.

Definition at line 179 of file as_string.h.

static as_string * as_string_fromval ( const as_val v)
related

Convert from an as_val.

Definition at line 261 of file as_string.h.

static char * as_string_get ( const as_string string)
related

Get the string value.

Definition at line 214 of file as_string.h.

static char * as_string_getorelse ( const as_string string,
char *  fallback 
)
related

Get the string value. If string is NULL, then return the fallback value.

Definition at line 204 of file as_string.h.

as_string * as_string_init ( as_string string,
char *  value,
bool  free 
)
related

Initialize a stack allocated as_string.

If free is true, then the string value will be freed when the as_string is destroyed.

Parameters
stringThe stack allocated as_string to initialize
valueThe NULL terminated string of character.
freeIf true, then the value will be freed when as_string is destroyed.
Returns
On success, the initialized string. Otherwise NULL.
size_t as_string_len ( as_string string)
related

The length of the string

Parameters
stringThe string to get the length of.
Returns
the length of the string in bytes.
as_string * as_string_new ( char *  value,
bool  free 
)
related

Create and initialize a new heap allocated as_string.

If free is true, then the string value will be freed when the as_string is destroyed.

Parameters
valueThe NULL terminated string of character.
freeIf true, then the value will be freed when as_string is destroyed.
Returns
On success, the new string. Otherwise NULL.
static char * as_string_tostring ( const as_string string)
related

Get the string value.

Definition at line 225 of file as_string.h.

static as_val * as_string_toval ( const as_string s)
related

Convert to an as_val.

Definition at line 251 of file as_string.h.

bool as_strncpy ( char *  trg,
const char *  src,
int  size 
)
related

Copy null terminated src to trg up to a maximum size. If maximum size reached, null terminate last character and and return true that truncation occurred.

as_strncpy does not pad unused bytes with zeroes like the standard strncpy.

char target[64];
as_strncpy(target, "source string", sizeof(target));

Field Documentation

as_val as_string::_
private

as_boolean is a subtype of as_val. You can cast as_boolean to as_val.

Definition at line 111 of file as_string.h.

bool as_string::free

If true, then as_string.value can be freed.

Definition at line 116 of file as_string.h.

size_t as_string::len

The length of the string.

Definition at line 126 of file as_string.h.

char* as_string::value

The string value.

Definition at line 121 of file as_string.h.


The documentation for this struct was generated from the following file: