cosmetix
[k8jam.git] / src / dstrings.h
blob5190005c1e7e2d4e86419267083c7f5fb81c88ff
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. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 #ifndef __DSTRINGS_H__
11 #define __DSTRINGS_H__
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
17 /* non-GCC compilers sux! */
18 #if defined(__GNUC__) && __GNUC__ > 2
19 # define DSTRINGS_PURE __attribute__((pure))
20 # define DSTRINGS_CONST __attribute__((const))
21 # define DSTRINGS_NORETURN __attribute__((noreturn))
22 # define DSTRINGS_PRINTF(m,n) __attribute__((format(printf,m,n)))
23 # define DSTRINGS_SENTINEL __attribute__((sentinel))
24 # define DSTRINGS_CLEANUP __attribute__((cleanup(dstr_done)))
25 # define DSTRINGS_PCLEANUP __attribute__((cleanup(dstr_donep)))
26 #else
27 # define DSTRINGS_PURE
28 # define DSTRINGS_CONST
29 # define DSTRINGS_NORETURN
30 # define DSTRINGS_PRINTF(m,n)
31 # define DSTRINGS_SENTINEL
32 # define DSTRINGS_CLEANUP
33 # define DSTRINGS_PCLEANUP
34 #endif
37 /* string is always 0-terminated */
38 typedef struct {
39 char *str;
40 int len;
41 int size;
42 char sbuf[256];
43 } dstring_t;
46 extern void dstr_init (dstring_t *s);
47 extern void dstr_init_cstr (dstring_t *s, const void *cstr);
48 extern void dstr_init_buf (dstring_t *s, const void *start, int len); /* len<0: use strlen() */
49 extern void dstr_init_memrange (dstring_t *s, const void *start, const void *finish); /* *finish will not be included */
50 extern void dstr_done (dstring_t *s);
51 extern void dstr_clear (dstring_t *s); /* clear allocated string; will not free allocated memory though */
52 extern void dstr_empty (dstring_t *s); /* clear allocated string; will free allocated memory */
53 extern void dstr_reserve (dstring_t *s, int size); /* 'size' should take trailing '\0' into account */
54 extern void dstr_set_cstr (dstring_t *s, const void *cstr); /* does dstr_clear() first */
55 extern void dstr_set_buf (dstring_t *s, const void *start, int len); /* len<0: use strlen(); does dstr_clear() first */
56 extern void dstr_set_memrange (dstring_t *s, const void *start, const void *finish); /* *finish will not be included; does dstr_clear() first */
57 extern void dstr_push_cstr (dstring_t *s, const void *str);
58 extern void dstr_push_buf (dstring_t *s, const void *start, int len); /* len<0: use strlen() */
59 extern void dstr_push_memrange (dstring_t *s, const void *start, const void *finish); /* *finish will not be included */
60 extern void dstr_chop (dstring_t *s, int n); /* set string length to min(n, s->len), but not less than 0 */
61 extern int dstr_pop_char (dstring_t *s); /* returns 'popped' char (0 if string is empty) */
62 extern void dstr_push_char (dstring_t *s, int x);
63 extern char dstr_last_char (dstring_t *s) DSTRINGS_PURE; /* return last char (0 if string is empty) */
64 extern char *dstr_cstr (dstring_t *s) DSTRINGS_PURE;
65 extern int dstr_len (dstring_t *s) DSTRINGS_PURE;
67 static inline void dstr_done_p (dstring_t **sp) { dstr_done(*sp); }
70 #ifdef __cplusplus
72 #endif
73 #endif