cosmetix
[k8jam.git] / src / dstrings.c
blobdb26b1eb149c8b9a452b6e6a59032437378f1b88
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 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "dstrings.h"
17 static inline void dstr_zero (dstring_t *s) {
18 s->str = s->sbuf;
19 s->len = 0;
20 s->size = sizeof(s->sbuf);
21 s->sbuf[0] = 0;
25 void dstr_init (dstring_t *s) {
26 dstr_zero(s);
30 void dstr_init_cstr (dstring_t *s, const void *str) {
31 dstr_init(s);
32 dstr_push_cstr(s, str);
36 void dstr_init_buf (dstring_t *s, const void *start, int len) {
37 dstr_init(s);
38 dstr_push_buf(s, start, len);
42 void dstr_init_memrange (dstring_t *s, const void *start, const void *finish) {
43 dstr_init(s);
44 dstr_push_memrange(s, start, finish);
48 void dstr_done (dstring_t *s) {
49 if (s->str != s->sbuf) free(s->str);
50 dstr_zero(s);
54 void dstr_clear (dstring_t *s) {
55 s->str[s->len=0] = 0;
59 void dstr_empty (dstring_t *s) {
60 if (s->str != s->sbuf) free(s->str);
61 dstr_zero(s);
65 static void dstr_resv (dstring_t *s, int size) {
66 if (size < 0) size = 0;
67 if (size > 0) {
68 if (s->str == s->sbuf) {
69 s->str = malloc(size);
70 if (s->len > 0) memcpy(s->str, s->sbuf, s->len);
71 s->str[s->len] = 0;
72 } else {
73 char *ns = realloc(s->str, size);
74 if (ns == NULL) { fprintf(stderr, "FATAL: out of memory!\n"); abort(); }
75 s->str = ns;
78 s->size = size;
82 void dstr_reserve (dstring_t *s, int size) {
83 if (size > s->size) dstr_resv(s, size);
87 // make room for at least `spc` chars
88 static inline void dstr_expand (dstring_t *s, int spc) {
89 if (spc >= 0) dstr_reserve(s, ((s->len+spc+1)|0x3ff)+1);
93 void dstr_push_buf (dstring_t *s, const void *start, int len) {
94 if (len < 0) len = (start != NULL ? strlen((const char *)start) : 0);
95 if (len > 0) {
96 dstr_expand(s, len);
97 if (start == NULL) memset(s->str+s->len, 32, len); else memmove(s->str+s->len, start, len);
98 s->str[s->len+=len] = 0;
103 void dstr_push_cstr (dstring_t *s, const void *str) {
104 dstr_push_buf(s, str, -1);
108 void dstr_push_memrange (dstring_t *s, const void *start, const void *finish) {
109 if (finish > start) dstr_push_buf(s, start, ((const char *)finish)-((const char *)start));
113 void dstr_push_char (dstring_t *s, int x) {
114 dstr_expand(s, 1);
115 s->str[s->len++] = x&0xff;
116 s->str[s->len] = 0;
120 void dstr_set_cstr (dstring_t *s, const void *cstr) {
121 dstr_clear(s);
122 dstr_push_cstr(s, cstr);
126 void dstr_set_buf (dstring_t *s, const void *start, int len) {
127 dstr_clear(s);
128 dstr_push_buf(s, start, len);
131 void dstr_set_memrange (dstring_t *s, const void *start, const void *finish) {
132 dstr_clear(s);
133 dstr_push_memrange(s, start, finish);
137 void dstr_chop (dstring_t *s, int n) {
138 if (n < 0) n = 0; else if (n > s->len) n = s->len;
139 s->str[s->len=n] = 0;
143 int dstr_pop_char (dstring_t *s) {
144 int res = (s->len > 0 ? (unsigned char)(s->str[--s->len]) : 0);
145 s->str[s->len] = 0;
146 return res;
150 char dstr_last_char (dstring_t *s) {
151 return (s->len > 0 ? s->str[s->len-1] : 0);
155 char *dstr_cstr (dstring_t *s) {
156 return s->str;
160 int dstr_len (dstring_t *s) {
161 return s->len;