2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
24 void * attr_fastcall
array_realloc_mayfail(void *p
, size_t element_size
, size_t old_length
, size_t new_length
, void **err_ptr
, ajla_error_t
*mayfail
);
25 void * attr_fastcall
array_finish_realloc(void *p
, size_t length
);
27 #define array_init_mayfail(type, ptr, len, mayfail) \
30 *(ptr) = mem_alloc_mayfail(type *, 0, mayfail), \
31 (!((is_constant(mayfail) && !return_ptr(mayfail))) && !*(ptr) ? false : true)\
34 #define array_init(type, ptr, len) \
35 (void)array_init_mayfail(type, ptr, len, NULL)
37 #define array_add_mayfail(type, ptr, len, var, err_ptr, mayfail) \
40 *(ptr) = cast_ptr(type *, array_realloc_mayfail(*(ptr), sizeof(type), (*(len) - 1), *(len), err_ptr, mayfail)),\
41 (!((is_constant(mayfail) && !return_ptr(mayfail))) && !*(ptr) ? (*(len))--, false : ((*(ptr))[*(len) - 1] = (var), true))\
44 #define array_add(type, ptr, len, var) \
45 (void)array_add_mayfail(type, ptr, len, var, NULL, NULL)
47 #define array_add_multiple_mayfail(type, ptr, len, var, varlen, err_ptr, mayfail)\
49 (*(len)) += (varlen), \
50 *(ptr) = cast_ptr(type *, array_realloc_mayfail(*(ptr), sizeof(type), (*(len) - (varlen)), *(len), err_ptr, mayfail)),\
51 (!((is_constant(mayfail) && !return_ptr(mayfail))) && !*(ptr) ? (*(len)) -= (varlen), false : (memcpy(&(*(ptr))[*(len) - (varlen)], (var), (varlen) * sizeof(type)), true))\
54 #define array_add_multiple(type, ptr, len, var, varlen) \
55 (void)array_add_multiple_mayfail(type, ptr, len, var, varlen, NULL, NULL)
57 #define array_finish(type, ptr, len) \
58 (*(ptr) = (type *)array_finish_realloc(*(ptr), *(len) * sizeof(type)))
61 #define str_init(ptr, len) \
63 array_init(char, (ptr), (len)); \
66 #define str_finish(ptr, len) \
68 str_add_char((ptr), (len), 0); \
69 array_finish(char, (ptr), (len)); \
72 void attr_fastcall
str_add_bytes(char **, size_t *, const char *, size_t);
73 void attr_fastcall
str_add_string(char **, size_t *, const char *);
74 void attr_fastcall
str_add_char(char **, size_t *, char);
75 void attr_fastcall
str_add_unsigned(char **, size_t *, uintbig_t
, int);
76 void attr_fastcall
str_add_signed(char **, size_t *, intbig_t
, int);
77 void attr_fastcall
str_add_hex(char **s
, size_t *l
, const char *hex
);
79 char *str_dup(const char *str
, size_t max_len
, ajla_error_t
*err
);
81 static inline char *str_from_unsigned(uintbig_t i
, int base
)
85 str_init(&str
, &str_l
);
86 str_add_unsigned(&str
, &str_l
, i
, base
);
87 str_finish(&str
, &str_l
);
91 static inline char *str_from_signed(uintbig_t i
, int base
)
95 str_init(&str
, &str_l
);
96 str_add_signed(&str
, &str_l
, i
, base
);
97 str_finish(&str
, &str_l
);