Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 06ea3c9

Browse files
author
Thomas G. Lockhart
committed
Add upgradepath(), isoldpath(), upgradepoly() and revertpoly() to allow
migration from pre-v6.1 geometric data types. Only allow new input syntax for paths and polygons.
1 parent fe74581 commit 06ea3c9

File tree

1 file changed

+168
-23
lines changed

1 file changed

+168
-23
lines changed

src/backend/utils/adt/geo_ops.c

Lines changed: 168 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.11 1997/06/01 04:16:16 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.12 1997/06/03 14:01:22 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -23,7 +23,7 @@
2323
#include "utils/geo_decls.h"
2424
#include "utils/palloc.h"
2525

26-
#define OLD_FORMAT_IN 1
26+
#define OLD_FORMAT_IN 0
2727
#define OLD_FORMAT_OUT 0
2828

2929
/*
@@ -824,6 +824,8 @@ PATH *path_in(char *str)
824824
#if OLD_FORMAT_IN
825825
int oldstyle = FALSE;
826826
double x, y;
827+
#else
828+
int depth = 0;
827829
#endif
828830

829831
if (!PointerIsValid(str))
@@ -832,9 +834,10 @@ PATH *path_in(char *str)
832834
if ((npts = pair_count(str, ',')) <= 0)
833835
elog(WARN, "Bad path external representation '%s'", str);
834836

835-
#if OLD_FORMAT_IN
836837
s = str;
837838
while (isspace( *s)) s++;
839+
840+
#if OLD_FORMAT_IN
838841
/* identify old style format as having only one left delimiter in string... */
839842
oldstyle = ((*s == LDELIM) && (strrchr( s, LDELIM) == s));
840843

@@ -847,21 +850,30 @@ PATH *path_in(char *str)
847850
isopen = (x == 0);
848851
npts = y;
849852
};
853+
854+
#else
855+
/* skip single leading paren */
856+
if ((*s == LDELIM) && (strrchr( s, LDELIM) == s)) {
857+
s++;
858+
depth++;
859+
};
850860
#endif
851861

852862
size = offsetof(PATH, p[0]) + (sizeof(path->p[0]) * npts);
853863
path = PALLOC(size);
854864

855865
path->size = size;
856-
path->npts = npts;
857-
if (oldstyle) path->closed = (! isopen);
866+
path->npts = npts;
858867

859868
#if OLD_FORMAT_IN
869+
if (oldstyle) path->closed = (! isopen);
870+
860871
if ((! path_decode(TRUE, npts, s, &isopen, &s, &(path->p[0])))
861872
|| ! (oldstyle? (*s++ == RDELIM): (*s == '\0')))
873+
862874
#else
863-
if ((! path_decode(TRUE, npts, s, &isopen, &s, &(path->p[0])))
864-
|| (*s != '\0'))
875+
if ((!path_decode(TRUE, npts, s, &isopen, &s, &(path->p[0])))
876+
&& (!((depth == 0) && (*s == '\0'))) && !((depth >= 1) && (*s == RDELIM)))
865877
#endif
866878
elog (WARN, "Bad path external representation '%s'",str);
867879

@@ -871,10 +883,13 @@ PATH *path_in(char *str)
871883
if (*s != '\0')
872884
elog (WARN, "Bad path external representation '%s'",str);
873885
};
874-
#endif
875886

876887
if (! oldstyle) path->closed = (! isopen);
877888

889+
#else
890+
path->closed = (! isopen);
891+
#endif
892+
878893
return(path);
879894
}
880895

@@ -986,9 +1001,11 @@ path_close(PATH *path)
9861001
{
9871002
PATH *result;
9881003

989-
result = path_copy(path);
990-
if (PointerIsValid((char *)result))
991-
result->closed = TRUE;
1004+
if (!PointerIsValid(path))
1005+
return(NULL);
1006+
1007+
result = path_copy(path);
1008+
result->closed = TRUE;
9921009

9931010
return(result);
9941011
} /* path_close() */
@@ -998,9 +1015,11 @@ path_open(PATH *path)
9981015
{
9991016
PATH *result;
10001017

1018+
if (!PointerIsValid(path))
1019+
return(NULL);
1020+
10011021
result = path_copy(path);
1002-
if (PointerIsValid((char *)result))
1003-
result->closed = FALSE;
1022+
result->closed = FALSE;
10041023

10051024
return(result);
10061025
} /* path_open() */
@@ -1012,9 +1031,6 @@ path_copy(PATH *path)
10121031
PATH *result;
10131032
int size;
10141033

1015-
if (!PointerIsValid(path))
1016-
return NULL;
1017-
10181034
size = offsetof(PATH, p[0]) + (sizeof(path->p[0]) * path->npts);
10191035
result = PALLOC(size);
10201036

@@ -1996,9 +2012,8 @@ POLYGON *poly_in(char *str)
19962012
int npts;
19972013
int size;
19982014
int isopen;
1999-
2000-
#if OLD_FORMAT_IN
20012015
char *s;
2016+
#if OLD_FORMAT_IN
20022017
int oldstyle;
20032018
int oddcount;
20042019
int i;
@@ -2431,7 +2446,7 @@ path_add_pt(PATH *path, Point *point)
24312446
PATH *result;
24322447
int i;
24332448

2434-
if (! (PointerIsValid(path) && PointerIsValid(point)))
2449+
if ((!PointerIsValid(path)) || (!PointerIsValid(point)))
24352450
return(NULL);
24362451

24372452
result = path_copy(path);
@@ -2450,7 +2465,7 @@ path_sub_pt(PATH *path, Point *point)
24502465
PATH *result;
24512466
int i;
24522467

2453-
if (! (PointerIsValid(path) && PointerIsValid(point)))
2468+
if ((!PointerIsValid(path)) || (!PointerIsValid(point)))
24542469
return(NULL);
24552470

24562471
result = path_copy(path);
@@ -2474,7 +2489,7 @@ path_mul_pt(PATH *path, Point *point)
24742489
Point *p;
24752490
int i;
24762491

2477-
if (! (PointerIsValid(path) && PointerIsValid(point)))
2492+
if ((!PointerIsValid(path)) || (!PointerIsValid(point)))
24782493
return(NULL);
24792494

24802495
result = path_copy(path);
@@ -2496,7 +2511,7 @@ path_div_pt(PATH *path, Point *point)
24962511
Point *p;
24972512
int i;
24982513

2499-
if (! (PointerIsValid(path) && PointerIsValid(point)))
2514+
if ((!PointerIsValid(path)) || (!PointerIsValid(point)))
25002515
return(NULL);
25012516

25022517
result = path_copy(path);
@@ -2541,6 +2556,53 @@ POLYGON *path_poly(PATH *path)
25412556
} /* path_polygon() */
25422557

25432558

2559+
/* upgradepath()
2560+
* Convert path read from old-style string into correct representation.
2561+
*
2562+
* Old-style: '(closed,#pts,x1,y1,...)' where closed is a boolean flag
2563+
* New-style: '((x1,y1),...)' for closed path
2564+
* '[(x1,y1),...]' for open path
2565+
*/
2566+
PATH
2567+
*upgradepath(PATH *path)
2568+
{
2569+
PATH *result;
2570+
int size, npts;
2571+
int i;
2572+
2573+
if (!PointerIsValid(path) || (path->npts < 2))
2574+
return(NULL);
2575+
2576+
if (! isoldpath(path))
2577+
elog(WARN,"upgradepath: path already upgraded?",NULL);
2578+
2579+
npts = (path->npts-1);
2580+
size = offsetof(PATH, p[0]) + (sizeof(path->p[0]) * npts);
2581+
result = PALLOC(size);
2582+
memset((char *) result, 0, size);
2583+
2584+
result->size = size;
2585+
result->npts = npts;
2586+
result->closed = (path->p[0].x != 0);
2587+
2588+
for (i=0; i<result->npts; i++) {
2589+
result->p[i].x = path->p[i+1].x;
2590+
result->p[i].y = path->p[i+1].y;
2591+
};
2592+
2593+
return(result);
2594+
} /* upgradepath() */
2595+
2596+
bool
2597+
isoldpath(PATH *path)
2598+
{
2599+
if (!PointerIsValid(path) || (path->npts < 2))
2600+
return(0);
2601+
2602+
return(path->npts == (path->p[0].y+1));
2603+
} /* isoldpath() */
2604+
2605+
25442606
/***********************************************************************
25452607
**
25462608
** Routines for 2D polygons.
@@ -2628,6 +2690,89 @@ poly_path(POLYGON *poly)
26282690
} /* poly_path() */
26292691

26302692

2693+
/* upgradepoly()
2694+
* Convert polygon read as pre-v6.1 string to new interpretation.
2695+
* Old-style: '(x1,x2,...,y1,y2,...)'
2696+
* New-style: '(x1,y1,x2,y2,...)'
2697+
*/
2698+
POLYGON
2699+
*upgradepoly(POLYGON *poly)
2700+
{
2701+
POLYGON *result;
2702+
int size;
2703+
int n2, i, ii;
2704+
2705+
if (!PointerIsValid(poly) || (poly->npts < 1))
2706+
return(NULL);
2707+
2708+
size = offsetof(POLYGON, p[0]) + (sizeof(poly->p[0]) * poly->npts);
2709+
result = PALLOC(size);
2710+
memset((char *) result, 0, size);
2711+
2712+
result->size = size;
2713+
result->npts = poly->npts;
2714+
2715+
n2 = poly->npts/2;
2716+
2717+
for (i=0; i<n2; i++) {
2718+
result->p[2*i].x = poly->p[i].x; /* even indices */
2719+
result->p[2*i+1].x = poly->p[i].y; /* odd indices */
2720+
};
2721+
2722+
if ((ii = ((poly->npts % 2)? 1: 0))) {
2723+
result->p[poly->npts-1].x = poly->p[n2].x;
2724+
result->p[0].y = poly->p[n2].y;
2725+
};
2726+
2727+
for (i=0; i<n2; i++) {
2728+
result->p[2*i+ii].y = poly->p[i+n2+ii].x; /* even (+offset) indices */
2729+
result->p[2*i+ii+1].y = poly->p[i+n2+ii].y; /* odd (+offset) indices */
2730+
};
2731+
2732+
return(result);
2733+
} /* upgradepoly() */
2734+
2735+
/* revertpoly()
2736+
* Reverse effect of upgradepoly().
2737+
*/
2738+
POLYGON
2739+
*revertpoly(POLYGON *poly)
2740+
{
2741+
POLYGON *result;
2742+
int size;
2743+
int n2, i, ii;
2744+
2745+
if (!PointerIsValid(poly) || (poly->npts < 1))
2746+
return(NULL);
2747+
2748+
size = offsetof(POLYGON, p[0]) + (sizeof(poly->p[0]) * poly->npts);
2749+
result = PALLOC(size);
2750+
memset((char *) result, 0, size);
2751+
2752+
result->size = size;
2753+
result->npts = poly->npts;
2754+
2755+
n2 = poly->npts/2;
2756+
2757+
for (i=0; i<n2; i++) {
2758+
result->p[i].x = poly->p[2*i].x; /* even indices */
2759+
result->p[i].y = poly->p[2*i+1].x; /* odd indices */
2760+
};
2761+
2762+
if ((ii = ((poly->npts % 2)? 1: 0))) {
2763+
result->p[n2].x = poly->p[poly->npts-1].x;
2764+
result->p[n2].y = poly->p[0].y;
2765+
};
2766+
2767+
for (i=0; i<n2; i++) {
2768+
result->p[i+n2+ii].x = poly->p[2*i+ii].y; /* even (+offset) indices */
2769+
result->p[i+n2+ii].y = poly->p[2*i+ii+1].y; /* odd (+offset) indices */
2770+
};
2771+
2772+
return(result);
2773+
} /* revertpoly() */
2774+
2775+
26312776
/*-------------------------------------------------------------------------
26322777
*
26332778
* circle.c--
@@ -2637,7 +2782,7 @@ poly_path(POLYGON *poly)
26372782
*
26382783
*
26392784
* IDENTIFICATION
2640-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.11 1997/06/01 04:16:16 momjian Exp $
2785+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.12 1997/06/03 14:01:22 thomas Exp $
26412786
*
26422787
*-------------------------------------------------------------------------
26432788
*/

0 commit comments

Comments
 (0)