-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstringinfo.c
More file actions
executable file
·192 lines (167 loc) · 4.31 KB
/
Copy pathstringinfo.c
File metadata and controls
executable file
·192 lines (167 loc) · 4.31 KB
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
192
/*
* This file contains code derived from PostgreSQL.
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
* Portions Copyright (c) 2024-2025 ZhangChen
*
* PostgreSQL-derived portions licensed under the PostgreSQL License;
* see LICENSE-PostgreSQL.
*
* Original portions by ZhangChen licensed under the Apache License, Version 2.0;
* see LICENSE.
*/
#include <string.h>
#include <assert.h>
#include "basic.h"
#include "stringinfo.h"
#define MaxAllocSize ((Size) 0x3fffffff)
/**
* initStringInfo - Initialize a StringInfoData struct
*
* @str: Pointer to the StringInfo structure to initialize
*
* Initializes a StringInfoData struct (with previously undefined contents)
* to describe an empty string. Allocates an initial buffer of 1024 bytes.
*/
void
initStringInfo(StringInfo str)
{
int size = 1024;
str->data = (char *) pdu_malloc(size);
if (!str->data) {
str->maxlen = 0;
str->len = 0;
str->cursor = 0;
return;
}
str->maxlen = size;
resetStringInfo(str);
}
/**
* resetStringInfo - Reset the StringInfo to empty state
*
* @str: Pointer to the StringInfo structure to reset
*
* Resets the StringInfo: the data buffer remains valid, but its
* previous content, if any, is cleared. The buffer is zeroed out.
*/
void
resetStringInfo(StringInfo str)
{
str->data[0] = '\0';
memset(str->data,0,str->maxlen);
str->len = 0;
str->cursor = 0;
}
/**
* appendStringInfoString - Append a null-terminated string
*
* @str: Pointer to the StringInfo structure
* @s: Null-terminated string to append
*
* Appends a null-terminated string to the StringInfo buffer.
*/
void
appendStringInfoString(StringInfo str, const char *s)
{
appendBinaryStringInfo(str, s, strlen(s));
}
/**
* appendBinaryStringInfo - Append arbitrary binary data
*
* @str: Pointer to the StringInfo structure
* @data: Binary data to append
* @datalen: Length of data in bytes
*
* Appends arbitrary binary data to a StringInfo, allocating more space
* if necessary. A trailing null byte is always maintained.
*/
void
appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
{
assert(str != NULL);
enlargeStringInfo(str, datalen);
memcpy(str->data + str->len, data, datalen);
str->len += datalen;
str->data[str->len] = '\0';
}
/**
* enlargeStringInfo - Ensure buffer has enough space for additional bytes
*
* @str: Pointer to the StringInfo structure
* @needed: Number of additional bytes needed (excluding null terminator)
*
* Makes sure there is enough space for 'needed' more bytes. External callers
* usually need not concern themselves with this, since all stringinfo.c
* routines do it automatically. However, if a caller knows that a StringInfo
* will eventually become X bytes large, it can save some realloc overhead by
* enlarging the buffer before starting to store data in it.
*/
void
enlargeStringInfo(StringInfo str, int needed)
{
Size newlen;
Size limit;
char *old_data;
limit = MaxAllocSize;
if (needed < 0)
{
printf("INVALID ENLARGEMENT REQUEST SIZE: %d", needed);
exit(1);
}
if (((Size) needed) >= (limit - (Size) str->len))
{
printf("Error: current string %d bytes, requested %d bytes.",
str->len, needed);
exit(1);
}
needed += str->len + 1;
if (needed <= str->maxlen)
return;
newlen = 2 * str->maxlen;
while (needed > newlen)
newlen = 2 * newlen;
if (newlen > limit)
newlen = limit;
old_data = str->data;
str->data = (char *) realloc(str->data, (Size) newlen);
if (str->data == NULL)
{
free(old_data);
printf("Error: realloc() FAIL!\n");
exit(1);
}
str->maxlen = newlen;
}
/**
* appendStringInfoChar - Append a single character
*
* @str: Pointer to the StringInfo structure
* @ch: Character to append
*
* Appends a single character to the StringInfo buffer.
*/
void
appendStringInfoChar(StringInfo str, char ch)
{
if (str->len + 1 >= str->maxlen)
enlargeStringInfo(str, 1);
str->data[str->len] = ch;
str->len++;
str->data[str->len] = '\0';
}
/**
* makeStringInfo - Create a new StringInfo structure
*
* Allocates and initializes a new StringInfo structure.
*
* Returns: Pointer to the newly created StringInfo
*/
StringInfo
makeStringInfo(void)
{
StringInfo res;
res = (StringInfo) malloc(sizeof(StringInfoData));
initStringInfo(res);
return res;
}