Debian Codegraph @

src/lib/helper/ctstring.c

raw
1 /* 'checkit_tiff' is a conformance checker for baseline TIFFs
2 *
3 * author: Andreas Romeyke, 2015-2022
4 * licensed under conditions of libtiff
5 * (see http://libtiff.maptools.org/misc.html)
6 *
7 */
8
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h> /* reallocarray requires _GNU_SOURCE */
12 #include <assert.h>
13 #include "ctstring.h"
14
15 string_t str(const char * cstr) {
16 assert(cstr != NULL);
17 string_t s;
18 s.is_const=false;
19 s.len = strlen( cstr);
20 #ifdef DEBUG_OVERFLOWS
21 assert(s.len < 8192); /* to find "overflows" if pointer points somewhere */
22 #endif
23 /* strndup only needed if we need to copy it */
24 #ifdef HAVE_STRNDUP
25 s.string = strndup(cstr, s.len);
26 #else
27 /* hint: change your operating system! */
28 s.string = strdup(cstr);
29 #endif
30 return s;
31 }
32
33
34 string_t const_str(const char * cstr) {
35 assert(cstr != NULL);
36 string_t s;
37 s.is_const=true;
38 s.len = strlen( cstr);
39 assert(s.len < 8192); /* to find "overflows" if pointer points somewhere */
40 s.string = (char *) cstr;
41 return s;
42 }
43
44 static const string_t empty = {
45 .string = (char *) "",
46 .len=0,
47 .is_const=true
48 };
49
50 string_t empty_str(void) {
51 return empty;
52 }
53
54 const char * cstr_of_str (const string_t s) {
55 if (s.len > 0) {
56 return s.string;
57 }
58 return "";
59 }
60
61 size_t strlen_of_str (const string_t s) {
62 return s.len;
63 }
64
65
66 /* dest should not exceed maxsize */
67 stringbuf_t secstrcat_string (stringbuf_t dest, const string_t src) {
68 assert(src.len >= 0);
69 assert(src.string != NULL);
70 #ifdef DEBUG_OVERFLOWS
71 assert(src.len < 8192); /* to find "overflows" if pointer points somewhere */
72 #endif
73 if (dest.pos >= dest.bufsize-1) {
74 fprintf(stderr, "max stringbuf size exceeded %zu (%zu)\n", dest.bufsize, dest.pos);
75 size_t bufsize=dest.bufsize*2;
76 #ifdef COMPAT_NEED_REALLOCARRAY
77 string_t * newbuf = realloc(dest.strings, bufsize * sizeof(string_t));
78 #else
79 string_t * newbuf = reallocarray(dest.strings, bufsize, sizeof(string_t));
80 #endif
81 if (NULL == newbuf) {
82 fprintf( stderr, "could not alloc newbuf to hold %zu string_t elements\n", bufsize);
83 exit(EXIT_FAILURE);
84 }
85 dest.bufsize = bufsize;
86 dest.strings = newbuf;
87 for (size_t i=dest.pos+1; i< dest.bufsize; i++ ) {
88 dest.strings[i]= empty_str();
89 }
90 }
91 dest.strings[dest.pos++] = src;
92 return dest;
93 }
94
95 stringbuf_t secstrcat_cstr (const stringbuf_t dest, const char * src) {
96 stringbuf_t ret = secstrcat_string(dest, const_str(src) );
97 return ret;
98 }
99
100 //#define secstrcat (stringbuf, arg) _Generic( (arg), const char *: secstrcat_cstr, stringbuf_t: secstrcat_string)(stringbuf, arg)
101
102 stringbuf_t empty_stringbuf(void) {
103 stringbuf_t buf;
104 buf.pos=0;
105 buf.bufsize=INITIAL_STRINGBUFF_SIZE;
106 buf.strings = calloc(buf.bufsize, sizeof(string_t));
107 if (NULL == buf.strings) {
108 fprintf(stderr, "could not allocate memory (src=%s)", __FILE__);
109 exit(EXIT_FAILURE);
110 }
111 for (size_t i=0; i<buf.bufsize; i++) {
112 buf.strings[i]=empty_str();
113 }
114 return buf;
115 }
116
117 void _clean_str(string_t * s) {
118 assert(NULL != s);
119 if (! s->is_const) {
120 //fprintf(stderr,"FREEING STRING adress='%p' '%s' caller=\n", s->string, cstr_of_str(*s));
121 //free(s->string);
122 s->string = NULL;
123 }
124 s->len=0;
125 }
126
127 void clean_stringbuf( stringbuf_t * buf){
128 assert(NULL != buf);
129 buf->pos=0;
130 buf->bufsize = 0;
131 free(buf->strings);
132 }
133
134 void clean_stringbuf_with_all_strings(stringbuf_t * buf) {
135 assert(NULL != buf);
136 for (size_t i= 0; i< buf->bufsize; i++) {
137 clean_str( & buf->strings[i]);
138 }
139 clean_stringbuf( buf);
140 }