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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright 2002 Thamer Alharbash
* Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
*
**********************************************************************/
#include "liblwgeom_internal.h"
#include "stringbuffer.h"
/**
* Allocate a new stringbuffer_t. Use stringbuffer_destroy to free.
*/
stringbuffer_t*
stringbuffer_create(void)
{
return stringbuffer_create_with_size(STRINGBUFFER_STARTSIZE);
}
static void
stringbuffer_init_with_size(stringbuffer_t *s, size_t size)
{
s->str_start = lwalloc(size);
s->str_end = s->str_start;
s->capacity = size;
memset(s->str_start, 0, size);
}
void
stringbuffer_release(stringbuffer_t *s)
{
if ( s->str_start ) lwfree(s->str_start);
}
void
stringbuffer_init(stringbuffer_t *s)
{
stringbuffer_init_with_size(s, STRINGBUFFER_STARTSIZE);
}
void
stringbuffer_init_varlena(stringbuffer_t *s)
{
stringbuffer_init_with_size(s, STRINGBUFFER_STARTSIZE + LWVARHDRSZ);
/* Zero out LWVARHDRSZ bytes at the front of the buffer */
stringbuffer_append_len(s, "\0\0\0\0\0\0\0\0", LWVARHDRSZ);
}
/**
* Allocate a new stringbuffer_t. Use stringbuffer_destroy to free.
*/
stringbuffer_t*
stringbuffer_create_with_size(size_t size)
{
stringbuffer_t *s;
s = lwalloc(sizeof(stringbuffer_t));
stringbuffer_init_with_size(s, size);
return s;
}
/**
* Free the stringbuffer_t and all memory managed within it.
*/
void
stringbuffer_destroy(stringbuffer_t *s)
{
stringbuffer_release(s);
if ( s ) lwfree(s);
}
/**
* Reset the stringbuffer_t. Useful for starting a fresh string
* without the expense of freeing and re-allocating a new
* stringbuffer_t.
*/
void
stringbuffer_clear(stringbuffer_t *s)
{
s->str_start[0] = '\0';
s->str_end = s->str_start;
}
/**
* Return the last character in the buffer.
*/
char
stringbuffer_lastchar(stringbuffer_t *s)
{
if( s->str_end == s->str_start )
return 0;
return *(s->str_end - 1);
}
/**
* Returns a reference to the internal string being managed by
* the stringbuffer. The current string will be null-terminated
* within the internal string.
*/
const char*
stringbuffer_getstring(stringbuffer_t *s)
{
return s->str_start;
}
/**
* Returns a newly allocated string large enough to contain the
* current state of the string. Caller is responsible for
* freeing the return value.
*/
char*
stringbuffer_getstringcopy(stringbuffer_t *s)
{
size_t size = (s->str_end - s->str_start) + 1;
char *str = lwalloc(size);
memcpy(str, s->str_start, size);
str[size - 1] = '\0';
return str;
}
lwvarlena_t *
stringbuffer_getvarlena(stringbuffer_t *s)
{
lwvarlena_t *output = (lwvarlena_t *)(s->str_start);
LWSIZE_SET(output->size, (s->str_end - s->str_start));
return output;
}
lwvarlena_t *
stringbuffer_getvarlenacopy(stringbuffer_t *s)
{
size_t size = (s->str_end - s->str_start);
lwvarlena_t *output = (lwvarlena_t *)lwalloc(size + LWVARHDRSZ);
LWSIZE_SET(output->size, size + LWVARHDRSZ);
memcpy(output->data, s->str_start, size);
return output;
}
/**
* Returns the length of the current string, not including the
* null terminator (same behavior as strlen()).
*/
int
stringbuffer_getlength(stringbuffer_t *s)
{
return (s->str_end - s->str_start);
}
/**
* Clear the stringbuffer_t and re-start it with the specified string.
*/
void
stringbuffer_set(stringbuffer_t *s, const char *str)
{
stringbuffer_clear(s);
stringbuffer_append(s, str);
}
/**
* Copy the contents of src into dst.
*/
void
stringbuffer_copy(stringbuffer_t *dst, stringbuffer_t *src)
{
stringbuffer_set(dst, stringbuffer_getstring(src));
}
/**
* Appends a formatted string to the current string buffer,
* using the format and argument list provided. Returns -1 on error,
* check errno for reasons, documented in the printf man page.
*/
static int
stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap) __attribute__ ((format (printf, 2, 0)));
static int
stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap)
{
int maxlen = (s->capacity - (s->str_end - s->str_start));
int len = 0; /* Length of the output */
va_list ap2;
/* Make a copy of the variadic arguments, in case we need to print twice */
/* Print to our buffer */
va_copy(ap2, ap);
len = vsnprintf(s->str_end, maxlen, fmt, ap2);
va_end(ap2);
/* Propagate errors up */
if ( len < 0 )
#if defined(__MINGW64_VERSION_MAJOR)
va_copy(ap2, ap);
len = _vscprintf(fmt, ap2);/**Assume windows flaky vsnprintf that returns -1 if initial buffer to small and add more space **/
va_end(ap2);
#else
return len;
#endif
/* We didn't have enough space! */
/* Either Unix vsnprint returned write length larger than our buffer */
/* or Windows vsnprintf returned an error code. */
if ( len >= maxlen )
{
stringbuffer_makeroom(s, len + 1);
maxlen = (s->capacity - (s->str_end - s->str_start));
/* Try to print a second time */
len = vsnprintf(s->str_end, maxlen, fmt, ap);
/* Printing error? Error! */
if ( len < 0 ) return len;
/* Too long still? Error! */
if ( len >= maxlen ) return -1;
}
/* Move end pointer forward and return. */
s->str_end += len;
return len;
}
/**
* Appends a formatted string to the current string buffer,
* using the format and argument list provided.
* Returns -1 on error, check errno for reasons,
* as documented in the printf man page.
*/
int
stringbuffer_aprintf(stringbuffer_t *s, const char *fmt, ...)
{
int r;
va_list ap;
va_start(ap, fmt);
r = stringbuffer_avprintf(s, fmt, ap);
va_end(ap);
return r;
}
/**
* Trims whitespace off the end of the stringbuffer. Returns
* the number of characters trimmed.
*/
int
stringbuffer_trim_trailing_white(stringbuffer_t *s)
{
char *ptr = s->str_end;
int dist = 0;
/* Roll backwards until we hit a non-space. */
while( ptr > s->str_start )
{
ptr--;
if( (*ptr == ' ') || (*ptr == '\t') )
{
continue;
}
else
{
ptr++;
dist = s->str_end - ptr;
*ptr = '\0';
s->str_end = ptr;
return dist;
}
}
return dist;
}
/**
* Trims zeroes off the end of the last number in the stringbuffer.
* The number has to be the very last thing in the buffer. Only the
* last number will be trimmed. Returns the number of characters
* trimmed.
*
* eg: 1.22000 -> 1.22
* 1.0 -> 1
* 0.0 -> 0
*/
int
stringbuffer_trim_trailing_zeroes(stringbuffer_t *s)
{
char *ptr = s->str_end;
char *decimal_ptr = NULL;
int dist;
if ( s->str_end - s->str_start < 2)
return 0;
/* Roll backwards to find the decimal for this number */
while( ptr > s->str_start )
{
ptr--;
if ( *ptr == '.' )
{
decimal_ptr = ptr;
break;
}
if ( (*ptr >= '0') && (*ptr <= '9' ) )
continue;
else
break;
}
/* No decimal? Nothing to trim! */
if ( ! decimal_ptr )
return 0;
ptr = s->str_end;
/* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
while( ptr >= decimal_ptr )
{
ptr--;
if ( *ptr == '0' )
continue;
else
break;
}
/* Huh, we get anywhere. Must not have trimmed anything. */
if ( ptr == s->str_end )
return 0;
/* If we stopped at the decimal, we want to null that out.
It we stopped on a numeral, we want to preserve that, so push the
pointer forward one space. */
if ( *ptr != '.' )
ptr++;
/* Add null terminator re-set the end of the stringbuffer. */
*ptr = '\0';
dist = s->str_end - ptr;
s->str_end = ptr;
return dist;
}
|