SELKIELogger  1.0.0
strarray.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef SELKIELoggerBase_StrArray
22 #define SELKIELoggerBase_StrArray
23 #include <stdbool.h>
24 
37 typedef struct {
38  size_t length;
39  char *data;
40 } string;
41 
43 typedef struct {
44  int entries;
45  string *strings;
46 } strarray;
47 
49 strarray *sa_new(int entries);
50 
52 bool sa_init(strarray *dst, const int entries);
53 
55 bool sa_copy(strarray *dst, const strarray *src);
56 
58 bool sa_move(strarray *dst, strarray *src);
59 
61 bool sa_set_entry(strarray *array, const int index, string *str);
62 
64 bool sa_create_entry(strarray *array, const int index, const size_t len, const char *src);
65 
67 void sa_clear_entry(strarray *array, const int index);
68 
70 void sa_destroy(strarray *sa);
71 
73 string *str_new(const size_t len, const char *ca);
74 
76 bool str_copy(string *dst, const string *src);
77 
79 string *str_duplicate(const string *src);
80 
82 bool str_update(string *str, const size_t len, const char *src);
83 
85 void str_destroy(string *str);
87 #endif
void sa_destroy(strarray *sa)
Destroy array and contents.
Definition: strarray.c:182
bool sa_copy(strarray *dst, const strarray *src)
Copy an array of strings from src to dst.
Definition: strarray.c:76
bool str_copy(string *dst, const string *src)
Update an existing string by copying the contents from another.
Definition: strarray.c:253
bool sa_init(strarray *dst, const int entries)
Initialise an already allocated array.
Definition: strarray.c:59
bool sa_move(strarray *dst, strarray *src)
Move strings from one array to another.
Definition: strarray.c:112
bool str_update(string *str, const size_t len, const char *src)
Update an existing string from a character array of given length.
Definition: strarray.c:294
bool sa_set_entry(strarray *array, const int index, string *str)
Copy a string to a given position in the array.
Definition: strarray.c:131
string * str_duplicate(const string *src)
Allocate a new string containing the contents of another.
Definition: strarray.c:281
string * str_new(const size_t len, const char *ca)
Create new string from character array.
Definition: strarray.c:217
void str_destroy(string *str)
Destroy a string and free its contents.
Definition: strarray.c:323
strarray * sa_new(int entries)
Allocate storage for a new array.
Definition: strarray.c:37
void sa_clear_entry(strarray *array, const int index)
Clear an array entry.
Definition: strarray.c:163
bool sa_create_entry(strarray *array, const int index, const size_t len, const char *src)
Create an string in a given position from a character array and length.
Definition: strarray.c:149
Array of strings.
Definition: strarray.h:43
int entries
Maximum number of strings in array, set when calling sa_new()
Definition: strarray.h:44
string * strings
Simple array of string structures.
Definition: strarray.h:45
Simple string type.
Definition: strarray.h:37
char * data
Character array, should be null terminated.
Definition: strarray.h:39
size_t length
This should include a terminating null byte where possible.
Definition: strarray.h:38