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)
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 ***********************************************************************/
14 /****************************************************************************
15 Allocated/allocatable strings
16 See comments in astring.c
17 ****************************************************************************/
24 #endif /* __cplusplus */
26 #include <string.h> /* strlen() */
29 #include "support.h" /* bool, fc__attribute() */
31 /* Don't let others modules using the fields directly. */
32 #define str _private_str_
34 #define n_alloc _private_n_alloc_
37 char *str
; /* the string */
38 size_t n
; /* size most recently requested */
39 size_t n_alloc
; /* total allocated */
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 }
46 void astr_init(struct astring
*astr
) fc__attribute((nonnull (1)));
47 void astr_free(struct astring
*astr
) fc__attribute((nonnull (1)));
49 static inline const char *astr_str(const struct astring
*astr
)
50 fc__attribute((nonnull (1)));
51 static inline size_t astr_len(const struct astring
*astr
)
52 fc__attribute((nonnull (1)));
53 static inline size_t astr_size(const struct astring
*astr
)
54 fc__attribute((nonnull (1)));
55 static inline size_t astr_capacity(const struct astring
*astr
)
56 fc__attribute((nonnull (1)));
57 static inline bool astr_empty(const struct astring
*astr
)
58 fc__attribute((nonnull (1)));
60 char *astr_to_str(struct astring
*astr
)
61 fc__attribute((nonnull (1)));
62 void astr_reserve(struct astring
*astr
, size_t size
)
63 fc__attribute((nonnull (1)));
64 void 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_add(struct astring
*astr
, const char *format
, ...)
70 fc__attribute((__format__(__printf__
, 2, 3)))
71 fc__attribute((nonnull (1, 2)));
72 void astr_add_line(struct astring
*astr
, const char *format
, ...)
73 fc__attribute((__format__(__printf__
, 2, 3)))
74 fc__attribute((nonnull (1, 2)));
75 void astr_break_lines(struct astring
*astr
, size_t desired_len
)
76 fc__attribute((nonnull (1)));
77 const char *astr_build_or_list(struct astring
*astr
,
78 const char *const *items
, size_t number
);
79 const char *astr_build_and_list(struct astring
*astr
,
80 const char *const *items
, size_t number
);
81 void astr_copy(struct astring
*dest
, const struct astring
*src
)
82 fc__attribute((nonnull (1, 2)));
86 /****************************************************************************
88 ****************************************************************************/
89 static inline const char *astr_str(const struct astring
*astr
)
94 /****************************************************************************
95 Returns the length of the string.
96 ****************************************************************************/
97 static inline size_t astr_len(const struct astring
*astr
)
99 return (NULL
!= astr
->str
? strlen(astr
->str
) : 0);
102 /****************************************************************************
103 Returns the current size requested for the string.
104 ****************************************************************************/
105 static inline size_t astr_size(const struct astring
*astr
)
110 /****************************************************************************
111 Returns the real size requested for the string.
112 ****************************************************************************/
113 static inline size_t astr_capacity(const struct astring
*astr
)
115 return astr
->n_alloc
;
118 /****************************************************************************
119 Returns whether the string is empty or not.
120 ****************************************************************************/
121 static inline bool astr_empty(const struct astring
*astr
)
123 return (0 == astr
->n
|| '\0' == astr
->str
[0]);
132 #endif /* __cplusplus */
134 #endif /* FC__ASTRING_H */