option.c: fixed warnings
[k8jam.git] / src / dstrings.h
blobc2c68d86cbf75d08ffef4dc12e1ec38690fda5a7
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
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.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #ifndef __DSTRINGS_H__
17 #define __DSTRINGS_H__
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
23 /* non-GCC compilers sux! */
24 #if defined(__GNUC__) && __GNUC__ > 2
25 # define DSTRINGS_PURE __attribute__((pure))
26 # define DSTRINGS_CONST __attribute__((const))
27 # define DSTRINGS_NORETURN __attribute__((noreturn))
28 # define DSTRINGS_PRINTF(m,n) __attribute__((format(printf,m,n)))
29 # define DSTRINGS_SENTINEL __attribute__((sentinel))
30 # define DSTRINGS_CLEANUP __attribute__((cleanup(dstr_done)))
31 # define DSTRINGS_PCLEANUP __attribute__((cleanup(dstr_donep)))
32 #else
33 # define DSTRINGS_PURE
34 # define DSTRINGS_CONST
35 # define DSTRINGS_NORETURN
36 # define DSTRINGS_PRINTF(m,n)
37 # define DSTRINGS_SENTINEL
38 # define DSTRINGS_CLEANUP
39 # define DSTRINGS_PCLEANUP
40 #endif
43 /* string is always 0-terminated */
44 typedef struct {
45 char *str;
46 int len;
47 int size;
48 char sbuf[256];
49 } dstring_t;
52 extern void dstr_init (dstring_t *s);
53 extern void dstr_init_cstr (dstring_t *s, const void *cstr);
54 extern void dstr_init_buf (dstring_t *s, const void *start, int len); /* len<0: use strlen() */
55 extern void dstr_init_memrange (dstring_t *s, const void *start, const void *finish); /* *finish will not be included */
56 extern void dstr_done (dstring_t *s);
57 extern void dstr_clear (dstring_t *s); /* clear allocated string; will not free allocated memory though */
58 extern void dstr_empty (dstring_t *s); /* clear allocated string; will free allocated memory */
59 extern void dstr_reserve (dstring_t *s, int size); /* 'size' should take trailing '\0' into account */
60 extern void dstr_set_cstr (dstring_t *s, const void *cstr); /* does dstr_clear() first */
61 extern void dstr_set_buf (dstring_t *s, const void *start, int len); /* len<0: use strlen(); does dstr_clear() first */
62 extern void dstr_set_memrange (dstring_t *s, const void *start, const void *finish); /* *finish will not be included; does dstr_clear() first */
63 extern void dstr_push_cstr (dstring_t *s, const void *str);
64 extern void dstr_push_buf (dstring_t *s, const void *start, int len); /* len<0: use strlen() */
65 extern void dstr_push_memrange (dstring_t *s, const void *start, const void *finish); /* *finish will not be included */
66 extern void dstr_chop (dstring_t *s, int n); /* set string length to min(n, s->len), but not less than 0 */
67 extern int dstr_pop_char (dstring_t *s); /* returns 'popped' char (0 if string is empty) */
68 extern void dstr_push_char (dstring_t *s, int x);
69 extern char dstr_last_char (dstring_t *s) DSTRINGS_PURE; /* return last char (0 if string is empty) */
70 extern char *dstr_cstr (dstring_t *s) DSTRINGS_PURE;
71 extern int dstr_len (dstring_t *s) DSTRINGS_PURE;
73 static inline void dstr_done_p (dstring_t **sp) { dstr_done(*sp); }
76 #ifdef __cplusplus
78 #endif
79 #endif