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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/**********************************************************************
*
* 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 (C) 2004-2015 Sandro Santilli <strk@kbt.io>
* Copyright (C) 2006 Mark Leslie <mark.leslie@lisasoft.com>
* Copyright (C) 2008-2009 Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
* Copyright (C) 2009-2015 Paul Ramsey <pramsey@cleverelephant.ca>
* Copyright (C) 2010 Olivier Courtin <olivier.courtin@camptocamp.com>
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h> /* for tolower */
/* Global variables */
#include "../postgis_config.h"
#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
/* Default allocators */
static void * default_allocator(size_t size);
static void default_freeor(void *mem);
static void * default_reallocator(void *mem, size_t size);
lwallocator lwalloc_var = default_allocator;
lwreallocator lwrealloc_var = default_reallocator;
lwfreeor lwfree_var = default_freeor;
/* Default reporters */
static void default_noticereporter(const char *fmt, va_list ap);
static void default_errorreporter(const char *fmt, va_list ap);
lwreporter lwnotice_var = default_noticereporter;
lwreporter lwerror_var = default_errorreporter;
/* Default logger */
static void default_debuglogger(int level, const char *fmt, va_list ap);
lwdebuglogger lwdebug_var = default_debuglogger;
#define LW_MSG_MAXLEN 256
static char *lwgeomTypeName[] =
{
"Unknown",
"Point",
"LineString",
"Polygon",
"MultiPoint",
"MultiLineString",
"MultiPolygon",
"GeometryCollection",
"CircularString",
"CompoundCurve",
"CurvePolygon",
"MultiCurve",
"MultiSurface",
"PolyhedralSurface",
"Triangle",
"Tin"
};
/*
* Default lwnotice/lwerror handlers
*
* Since variadic functions cannot pass their parameters directly, we need
* wrappers for these functions to convert the arguments into a va_list
* structure.
*/
void
lwnotice(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* Call the supplied function */
(*lwnotice_var)(fmt, ap);
va_end(ap);
}
void
lwerror(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* Call the supplied function */
(*lwerror_var)(fmt, ap);
va_end(ap);
}
void
lwdebug(int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* Call the supplied function */
(*lwdebug_var)(level, fmt, ap);
va_end(ap);
}
/*
* Default allocators
*
* We include some default allocators that use malloc/free/realloc
* along with stdout/stderr since this is the most common use case
*
*/
static void *
default_allocator(size_t size)
{
void *mem = malloc(size);
return mem;
}
static void
default_freeor(void *mem)
{
free(mem);
}
static void *
default_reallocator(void *mem, size_t size)
{
void *ret = realloc(mem, size);
return ret;
}
static void
default_noticereporter(const char *fmt, va_list ap)
{
char msg[LW_MSG_MAXLEN+1];
vsnprintf (msg, LW_MSG_MAXLEN, fmt, ap);
msg[LW_MSG_MAXLEN]='\0';
printf("%s\n", msg);
}
static void
default_debuglogger(int level, const char *fmt, va_list ap)
{
char msg[LW_MSG_MAXLEN+1];
if ( POSTGIS_DEBUG_LEVEL >= level )
{
/* Space pad the debug output */
int i;
for ( i = 0; i < level; i++ )
msg[i] = ' ';
vsnprintf(msg+i, LW_MSG_MAXLEN-i, fmt, ap);
msg[LW_MSG_MAXLEN]='\0';
printf("%s\n", msg);
}
}
static void
default_errorreporter(const char *fmt, va_list ap)
{
char msg[LW_MSG_MAXLEN+1];
vsnprintf (msg, LW_MSG_MAXLEN, fmt, ap);
msg[LW_MSG_MAXLEN]='\0';
fprintf(stderr, "%s\n", msg);
exit(1);
}
/**
* This function is called by programs which want to set up custom handling
* for memory management and error reporting
*
* Only non-NULL values change their respective handler
*/
void
lwgeom_set_handlers(lwallocator allocator, lwreallocator reallocator,
lwfreeor freeor, lwreporter errorreporter,
lwreporter noticereporter) {
if ( allocator ) lwalloc_var = allocator;
if ( reallocator ) lwrealloc_var = reallocator;
if ( freeor ) lwfree_var = freeor;
if ( errorreporter ) lwerror_var = errorreporter;
if ( noticereporter ) lwnotice_var = noticereporter;
}
void
lwgeom_set_debuglogger(lwdebuglogger debuglogger) {
if ( debuglogger ) lwdebug_var = debuglogger;
}
const char*
lwtype_name(uint8_t type)
{
if ( type > 15 )
{
/* assert(0); */
return "Invalid type";
}
return lwgeomTypeName[(int ) type];
}
void *
lwalloc(size_t size)
{
void *mem = lwalloc_var(size);
LWDEBUGF(5, "lwalloc: %d@%p", size, mem);
return mem;
}
void *
lwrealloc(void *mem, size_t size)
{
LWDEBUGF(5, "lwrealloc: %d@%p", size, mem);
return lwrealloc_var(mem, size);
}
void
lwfree(void *mem)
{
lwfree_var(mem);
}
/*
* Removes trailing zeros and dot for a %f formatted number.
* Modifies input.
*/
void
trim_trailing_zeros(char *str)
{
char *ptr, *totrim=NULL;
int len;
int i;
LWDEBUGF(3, "input: %s", str);
ptr = strchr(str, '.');
if ( ! ptr ) return; /* no dot, no decimal digits */
LWDEBUGF(3, "ptr: %s", ptr);
len = strlen(ptr);
for (i=len-1; i; i--)
{
if ( ptr[i] != '0' ) break;
totrim=&ptr[i];
}
if ( totrim )
{
if ( ptr == totrim-1 ) *ptr = '\0';
else *totrim = '\0';
}
LWDEBUGF(3, "output: %s", str);
}
/*
* Returns a new string which contains a maximum of maxlength characters starting
* from startpos and finishing at endpos (0-based indexing). If the string is
* truncated then the first or last characters are replaced by "..." as
* appropriate.
*
* The caller should specify start or end truncation by setting the truncdirection
* parameter as follows:
* 0 - start truncation (i.e. characters are removed from the beginning)
* 1 - end trunctation (i.e. characters are removed from the end)
*/
char *lwmessage_truncate(char *str, int startpos, int endpos, int maxlength, int truncdirection)
{
char *output;
char *outstart;
/* Allocate space for new string */
output = lwalloc(maxlength + 4);
output[0] = '\0';
/* Start truncation */
if (truncdirection == 0)
{
/* Calculate the start position */
if (endpos - startpos < maxlength)
{
outstart = str + startpos;
strncat(output, outstart, endpos - startpos + 1);
}
else
{
if (maxlength >= 3)
{
/* Add "..." prefix */
outstart = str + endpos + 1 - maxlength + 3;
strncat(output, "...", 3);
strncat(output, outstart, maxlength - 3);
}
else
{
/* maxlength is too small; just output "..." */
strncat(output, "...", 3);
}
}
}
/* End truncation */
if (truncdirection == 1)
{
/* Calculate the end position */
if (endpos - startpos < maxlength)
{
outstart = str + startpos;
strncat(output, outstart, endpos - startpos + 1);
}
else
{
if (maxlength >= 3)
{
/* Add "..." suffix */
outstart = str + startpos;
strncat(output, outstart, maxlength - 3);
strncat(output, "...", 3);
}
else
{
/* maxlength is too small; just output "..." */
strncat(output, "...", 3);
}
}
}
return output;
}
char
getMachineEndian(void)
{
static int endian_check_int = 1; /* dont modify this!!! */
return *((char *) &endian_check_int); /* 0 = big endian | xdr,
* 1 = little endian | ndr
*/
}
void
error_if_srid_mismatch(int srid1, int srid2)
{
if ( srid1 != srid2 )
{
lwerror("Operation on mixed SRID geometries");
}
}
int
clamp_srid(int srid)
{
int newsrid = srid;
if ( newsrid <= 0 ) {
if ( newsrid != SRID_UNKNOWN ) {
newsrid = SRID_UNKNOWN;
lwnotice("SRID value %d converted to the officially unknown SRID value %d", srid, newsrid);
}
} else if ( srid > SRID_MAXIMUM ) {
newsrid = SRID_USER_MAXIMUM + 1 +
/* -1 is to reduce likelyhood of clashes */
/* NOTE: must match implementation in postgis_restore.pl */
( srid % ( SRID_MAXIMUM - SRID_USER_MAXIMUM - 1 ) );
lwnotice("SRID value %d > SRID_MAXIMUM converted to %d", srid, newsrid);
}
return newsrid;
}
|