File size: 4,551 Bytes
d4b77ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

#ifndef __STRING_BUFFER_H
#define __STRING_BUFFER_H

// Enable MinGW secure API for _snprintf_s
#define MINGW_HAS_SECURE_API 1

#ifdef _MSC_VER
#define __INLINE __inline
#else
#define __INLINE inline
#endif

#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

typedef struct string_buffer {
	char* buffer;
	int position;
	int size;
} string_buffer;

typedef struct string_list {
	char** buffer;
	int position;
	int size;
} string_list;

#define BUFFER_INCREMENT_STEP 4096

static __INLINE string_buffer* buffer_create(int L) {
	string_buffer* B = (string_buffer*) malloc(sizeof(string_buffer));
	B->size = L;
	B->buffer = (char*) malloc(sizeof(char) * B->size);
	B->position = 0;
	return B;
}

static __INLINE void buffer_reset(string_buffer* B) {
	B->position = 0;
}

static __INLINE void buffer_destroy(string_buffer** B) {
	if (!(*B)) return;
	if ((*B)->buffer) {
		free((*B)->buffer);
		(*B)->buffer = NULL;
	}
	free((*B));
	(*B) = NULL;
}

static __INLINE char* buffer_extract(const string_buffer* B) {
	char *S = (char*) malloc(sizeof(char) * (B->position + 1));
	memcpy(S, B->buffer, B->position);
	S[B->position] = '\0';
	return S;
}

static __INLINE int buffer_size(const string_buffer* B) {
	return B->position;
}

static __INLINE void buffer_push(string_buffer* B, char C) {
	int required = 1;
	if (required > B->size - B->position) {
		B->size = B->position + BUFFER_INCREMENT_STEP;
		B->buffer = (char*) realloc(B->buffer, sizeof(char) * B->size);
	}
	B->buffer[B->position] = C;
	B->position += required;
}

static __INLINE void buffer_append(string_buffer* B, const char *format, ...) {

	int required;
	va_list args;

#if defined(__OS2__) || defined(__WINDOWS__) || defined(WIN32) || defined(_MSC_VER)

	va_start(args, format);
	required = _vscprintf(format, args) + 1;
	va_end(args);
	if (required >= B->size - B->position) {
		B->size = B->position + required + 1;
		B->buffer = (char*) realloc(B->buffer, sizeof(char) * B->size);
	}
	va_start(args, format);
	required = _vsnprintf_s(&(B->buffer[B->position]), B->size - B->position, _TRUNCATE, format, args);
	va_end(args);
	B->position += required;

#else
	va_start(args, format);
	required = vsnprintf(&(B->buffer[B->position]), B->size - B->position, format, args);
	va_end(args);
	if (required >= B->size - B->position) {
		B->size = B->position + required + 1;
		B->buffer = (char*) realloc(B->buffer, sizeof(char) * B->size);
		va_start(args, format);
		required = vsnprintf(&(B->buffer[B->position]), B->size - B->position, format, args);
		va_end(args);
	}
	B->position += required;
#endif

}

static __INLINE string_list* list_create(int L) {
	string_list* B = (string_list*) malloc(sizeof(string_list));
	B->size = L;
	B->buffer = (char**) malloc(sizeof(char*) * B->size);
	memset(B->buffer, 0, sizeof(char*) * B->size);
	B->position = 0;
	return B;
}

static __INLINE void list_reset(string_list* B) {
	int i;
	for (i = 0; i < B->position; i++) {
		if (B->buffer[i]) free(B->buffer[i]);
		B->buffer[i] = NULL;
	}
	B->position = 0;
}

static __INLINE void list_destroy(string_list **B) {
	int i;

	if (!(*B)) return;

	for (i = 0; i < (*B)->position; i++) {
		if ((*B)->buffer[i]) free((*B)->buffer[i]); (*B)->buffer[i] = NULL;
	}

	if ((*B)->buffer) {
		free((*B)->buffer); (*B)->buffer = NULL;
	}

	free((*B));
	(*B) = NULL;
}

static __INLINE char* list_get(const string_list *B, int I) {
	if (I < 0 || I >= B->position) {
		return NULL;
	} else {
		if (!B->buffer[I]) {
			return NULL;
		} else {
			char *S;
			int length = strlen(B->buffer[I]);
			S = (char*) malloc(sizeof(char) * (length + 1));
			memcpy(S, B->buffer[I], length + 1);
			return S;
		}
	}
}

static __INLINE int list_size(const string_list *B) {
	return B->position;
}

static __INLINE void list_append(string_list *B, char* S) {
	int required = 1;
	int length = strlen(S);
	if (required > B->size - B->position) {
		B->size = B->position + 16;
		B->buffer = (char**) realloc(B->buffer, sizeof(char*) * B->size);
	}
	B->buffer[B->position] = (char*) malloc(sizeof(char) * (length + 1));
	memcpy(B->buffer[B->position], S, length + 1);
	B->position += required;
}

// This version of the append does not copy the string but simply takes the control of its allocation
static __INLINE void list_append_direct(string_list *B, char* S) {
	int required = 1;
	// int length = strlen(S);
	if (required > B->size - B->position) {
		B->size = B->position + 16;
		B->buffer = (char**) realloc(B->buffer, sizeof(char*) * B->size);
	}
	B->buffer[B->position] = S;
	B->position += required;
}


#endif