Freeciv-3.1: /fast/freeciv/nightly/S3_1/utility/astring.h Source File

Freeciv-3.1

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
36 struct 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 { NULL, 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)
50 fc__attribute((nonnull (1)));
51static inline size_t astr_len(const struct astring *astr)
52 fc__attribute((nonnull (1)));
53static inline size_t astr_size(const struct astring *astr)
54 fc__attribute((nonnull (1)));
55static inline size_t astr_capacity(const struct astring *astr)
56 fc__attribute((nonnull (1)));
57static inline bool astr_empty(const struct astring *astr)
58 fc__attribute((nonnull (1)));
59
60char *astr_to_str(struct astring *astr)
61 fc__attribute((nonnull (1)));
62void astr_reserve(struct astring *astr, size_t size)
63 fc__attribute((nonnull (1)));
64void astr_clear(struct astring *astr)
65 fc__attribute((nonnull (1)));
66 void astr_set(struct astring *astr, const char *format, ...)
67 fc__attribute((__format__(__printf__, 2, 3)))
68 fc__attribute((nonnull (1, 2)));
69 void astr_vadd(struct astring *astr, const char *format, va_list ap)
70 fc__attribute((nonnull (1, 2)));
71 void astr_add(struct astring *astr, const char *format, ...)
72 fc__attribute((__format__(__printf__, 2, 3)))
73 fc__attribute((nonnull (1, 2)));
74 void astr_add_line(struct astring *astr, const char *format, ...)
75 fc__attribute((__format__(__printf__, 2, 3)))
76 fc__attribute((nonnull (1, 2)));
77void astr_break_lines(struct astring *astr, size_t desired_len)
78 fc__attribute((nonnull (1)));
79const char *astr_build_or_list(struct astring *astr,
80 const char *const *items, size_t number);
81const char *astr_build_and_list(struct astring *astr,
82 const char *const *items, size_t number);
83 void astr_copy(struct astring *dest, const struct astring *src)
84 fc__attribute((nonnull (1, 2)));
85
86void fc_astr_init(void);
87void fc_astr_free(void);
88
89
90/************************************************************************/
93static inline const char *astr_str(const struct astring *astr)
94{
95 return astr->str;
96}
97
98/************************************************************************/
101 static inline size_t astr_len(const struct astring *astr)
102{
103 return (NULL != astr->str ? strlen(astr->str) : 0);
104}
105
106/************************************************************************/
109 static inline size_t astr_size(const struct astring *astr)
110{
111 return astr->n;
112}
113
114/************************************************************************/
117 static inline size_t astr_capacity(const struct astring *astr)
118{
119 return astr->n_alloc;
120}
121
122/************************************************************************/
125 static 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 */
astr_reserve
void astr_reserve(struct astring *astr, size_t size) fc__attribute((nonnull(1)))
Definition astring.c:183
astr_to_str
char * astr_to_str(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:167
astr_vadd
void astr_vadd(struct astring *astr, const char *format, va_list ap) fc__attribute((nonnull(1
astr_build_or_list
const char * astr_build_or_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:329
astr_size
static size_t astr_size(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:109
astr_build_and_list
const char * astr_build_and_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:367
astr_capacity
static size_t astr_capacity(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:117
astr_len
static size_t astr_len(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:101
astr_str
static const char * astr_str(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:93
astr_clear
void astr_clear(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:205
fc_astr_free
void fc_astr_free(void)
Definition astring.c:422
fc_astr_init
void void fc_astr_init(void)
Definition astring.c:414
astr_copy
void astr_copy(struct astring *dest, const struct astring *src) fc__attribute((nonnull(1
astr_init
void astr_init(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:144
astr_set
void astr_set(struct astring *astr, const char *format,...) fc__attribute((__format__(__printf__
astr_add_line
void astr_add_line(struct astring *astr, const char *format,...) fc__attribute((__format__(__printf__
astr_free
void astr_free(struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.c:153
astr_empty
static bool astr_empty(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:125
astr_break_lines
void astr_break_lines(struct astring *astr, size_t desired_len) fc__attribute((nonnull(1)))
Definition astring.c:318
astr_add
void void astr_add(struct astring *astr, const char *format,...) fc__attribute((__format__(__printf__
size
size_t size
Definition specvec.h:72
astring
Definition astring.h:36
astring::str
char * str
Definition astring.h:37
astring::n_alloc
size_t n_alloc
Definition astring.h:39
astring::n
size_t n
Definition astring.h:38
support.h
fc__attribute
#define fc__attribute(x)
Definition support.h:89