O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
append.c
1/* ************************************************************************** */
2/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
3/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
4/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
5/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
6/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
7/* */
8/* Copyright 2023, SAFRAN T4DS, ALL RIGHTS RESERVED */
9/* */
10/* @file append.c */
11/* @author Antoine GAGNIERE */
12/* */
13/* ************************************************************************** */
14
15#include "o2s/string.h"
16
17#include <stdarg.h> // va_*
18#include <stdio.h> // vsnprintf
19
21bool string_append_char(string_t* self, char character)
22{
23 return array_push_back(self, &character);
24}
25
27bool string_append_cstring(string_t* self, const char* cstring, size_t length)
28{
29 return array_push_back_n(self, cstring, length);
30}
31
33bool string_append(string_t* self, const string_t* other)
34{
35 return array_push_back_n(self, other->start, other->count);
36}
37
39bool string_append_format(string_t* self, const char* format, ...)
40{
41 va_list args1, args2;
42 int length;
43
44 va_start(args1, format);
45 va_copy(args2, args1);
46 length = vsnprintf(NULL, 0, format, args1);
47 va_end(args1);
48 if (length > 0)
49 {
50 string_reserve(self, length + 1);
51 self->count += vsnprintf(array_end(self), length + 1, format, args2);
52 }
53 va_end(args2);
54 return length >= 0;
55}
void * array_end(const array_t *self)
One-past-the-end pointer of the array.
Definition getters.c:25
bool array_push_back_n(array_t *self, const void *elements, size_t count)
Adds count elements at the end of the array.
Definition push_pop.c:28
bool array_push_back(array_t *self, const void *element)
Adds one element at the end of the array.
Definition push_pop.c:43
Dynamic string implementation.
array_t string_t
A string is an array of char.
Definition string.h:30
void * start
Underlying storage.
Definition array.h:25
size_t count
Number of elements currently stored.
Definition array.h:27