Freeciv-3.3
Loading...
Searching...
No Matches
astring.h
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14/****************************************************************************
15 Allocated/allocatable strings
16 See comments in astring.c
17****************************************************************************/
18
19#ifndef FC__ASTRING_H
20#define FC__ASTRING_H
21
22#ifdef __cplusplus
23extern "C" {
24#endif /* __cplusplus */
25
26#include <string.h> /* strlen() */
27
28/* utility */
29#include "support.h" /* bool, fc__attribute() */
30
31/* Don't let others modules using the fields directly. */
32#define str _private_str_
33#define n _private_n_
34#define n_alloc _private_n_alloc_
35
36struct astring {
37 char *str; /* The string */
38 size_t n; /* Size most recently requested */
39 size_t n_alloc; /* Total allocated */
40};
41
42/* Can assign this in variable declaration to initialize:
43 * Notice a static astring var is exactly this already. */
44#define ASTRING_INIT { nullptr, 0, 0 }
45
46void astr_init(struct astring *astr) fc__attribute((nonnull (1)));
47void astr_free(struct astring *astr) fc__attribute((nonnull (1)));
48
49static inline const char *astr_str(const struct astring *astr)
51static inline size_t astr_len(const struct astring *astr)
53static inline size_t astr_size(const struct astring *astr)
55static inline size_t astr_capacity(const struct astring *astr)
57static inline bool astr_empty(const struct astring *astr)
59
60char *astr_to_str(struct astring *astr)
62void astr_reserve(struct astring *astr, size_t size)
64void astr_clear(struct astring *astr)
66void astr_set(struct astring *astr, const char *format, ...)
70 fc__attribute((nonnull (1, 2)));
71void astr_add(struct astring *astr, const char *format, ...)
73 fc__attribute((nonnull (1, 2)));
74void astr_add_line(struct astring *astr, const char *format, ...)
76 fc__attribute((nonnull (1, 2)));
80 const char *const *items, size_t number);
82 const char *const *items, size_t number);
84 fc__attribute((nonnull (1, 2)));
85
86void fc_astr_init(void);
87void fc_astr_free(void);
88
89
90/************************************************************************/
94{
95 return astr->str;
96}
97
98/************************************************************************/
101static inline size_t astr_len(const struct astring *astr)
102{
103 return (astr->str != nullptr ? strlen(astr->str) : 0);
104}
105
106/************************************************************************/
109static inline size_t astr_size(const struct astring *astr)
110{
111 return astr->n;
112}
113
114/************************************************************************/
117static inline size_t astr_capacity(const struct astring *astr)
118{
119 return astr->n_alloc;
120}
121
122/************************************************************************/
125static inline bool astr_empty(const struct astring *astr)
126{
127 return (0 == astr->n || '\0' == astr->str[0]);
128}
129
130#undef str
131#undef n
132#undef n_alloc
133
134#ifdef __cplusplus
135}
136#endif /* __cplusplus */
137
138#endif /* FC__ASTRING_H */
void astr_reserve(struct astring *astr, size_t size) fc__attribute((nonnull(1)))
Definition astring.c:179
char * astr_to_str(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:163
void astr_vadd(struct astring *astr, const char *format, va_list ap) fc__attribute((nonnull(1
const char * astr_build_or_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:313
static size_t astr_size(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:109
const char * astr_build_and_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:351
static size_t astr_capacity(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:117
static size_t astr_len(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:101
static const char * astr_str(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:93
void astr_clear(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:201
void fc_astr_free(void)
Definition astring.c:406
void void fc_astr_init(void)
Definition astring.c:398
void astr_copy(struct astring *dest, const struct astring *src) fc__attribute((nonnull(1
void astr_init(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:139
void astr_set(struct astring *astr, const char *format,...) fc__attribute((__format__(__printf__
void astr_add_line(struct astring *astr, const char *format,...) fc__attribute((__format__(__printf__
void astr_free(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:148
static bool astr_empty(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:125
void astr_break_lines(struct astring *astr, size_t desired_len) fc__attribute((nonnull(1)))
Definition astring.c:302
void void astr_add(struct astring *astr, const char *format,...) fc__attribute((__format__(__printf__
char * incite_cost
Definition comments.c:76
size_t size
Definition specvec.h:72
char * str
Definition astring.h:37
size_t n_alloc
Definition astring.h:39
size_t n
Definition astring.h:38
#define fc__attribute(x)
Definition support.h:99