7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
23
23
#include "utils/geo_decls.h"
24
24
#include "utils/palloc.h"
25
25
26
- #define OLD_FORMAT_IN 1
26
+ #define OLD_FORMAT_IN 0
27
27
#define OLD_FORMAT_OUT 0
28
28
29
29
/*
@@ -824,6 +824,8 @@ PATH *path_in(char *str)
824
824
#if OLD_FORMAT_IN
825
825
int oldstyle = FALSE;
826
826
double x , y ;
827
+ #else
828
+ int depth = 0 ;
827
829
#endif
828
830
829
831
if (!PointerIsValid (str ))
@@ -832,9 +834,10 @@ PATH *path_in(char *str)
832
834
if ((npts = pair_count (str , ',' )) <= 0 )
833
835
elog (WARN , "Bad path external representation '%s'" , str );
834
836
835
- #if OLD_FORMAT_IN
836
837
s = str ;
837
838
while (isspace ( * s )) s ++ ;
839
+
840
+ #if OLD_FORMAT_IN
838
841
/* identify old style format as having only one left delimiter in string... */
839
842
oldstyle = ((* s == LDELIM ) && (strrchr ( s , LDELIM ) == s ));
840
843
@@ -847,21 +850,30 @@ PATH *path_in(char *str)
847
850
isopen = (x == 0 );
848
851
npts = y ;
849
852
};
853
+
854
+ #else
855
+ /* skip single leading paren */
856
+ if ((* s == LDELIM ) && (strrchr ( s , LDELIM ) == s )) {
857
+ s ++ ;
858
+ depth ++ ;
859
+ };
850
860
#endif
851
861
852
862
size = offsetof(PATH , p [0 ]) + (sizeof (path -> p [0 ]) * npts );
853
863
path = PALLOC (size );
854
864
855
865
path -> size = size ;
856
- path -> npts = npts ;
857
- if (oldstyle ) path -> closed = (! isopen );
866
+ path -> npts = npts ;
858
867
859
868
#if OLD_FORMAT_IN
869
+ if (oldstyle ) path -> closed = (! isopen );
870
+
860
871
if ((! path_decode (TRUE, npts , s , & isopen , & s , & (path -> p [0 ])))
861
872
|| ! (oldstyle ? (* s ++ == RDELIM ): (* s == '\0' )))
873
+
862
874
#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 ) ))
865
877
#endif
866
878
elog (WARN , "Bad path external representation '%s'" ,str );
867
879
@@ -871,10 +883,13 @@ PATH *path_in(char *str)
871
883
if (* s != '\0' )
872
884
elog (WARN , "Bad path external representation '%s'" ,str );
873
885
};
874
- #endif
875
886
876
887
if (! oldstyle ) path -> closed = (! isopen );
877
888
889
+ #else
890
+ path -> closed = (! isopen );
891
+ #endif
892
+
878
893
return (path );
879
894
}
880
895
@@ -986,9 +1001,11 @@ path_close(PATH *path)
986
1001
{
987
1002
PATH * result ;
988
1003
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;
992
1009
993
1010
return (result );
994
1011
} /* path_close() */
@@ -998,9 +1015,11 @@ path_open(PATH *path)
998
1015
{
999
1016
PATH * result ;
1000
1017
1018
+ if (!PointerIsValid (path ))
1019
+ return (NULL );
1020
+
1001
1021
result = path_copy (path );
1002
- if (PointerIsValid ((char * )result ))
1003
- result -> closed = FALSE;
1022
+ result -> closed = FALSE;
1004
1023
1005
1024
return (result );
1006
1025
} /* path_open() */
@@ -1012,9 +1031,6 @@ path_copy(PATH *path)
1012
1031
PATH * result ;
1013
1032
int size ;
1014
1033
1015
- if (!PointerIsValid (path ))
1016
- return NULL ;
1017
-
1018
1034
size = offsetof(PATH , p [0 ]) + (sizeof (path -> p [0 ]) * path -> npts );
1019
1035
result = PALLOC (size );
1020
1036
@@ -1996,9 +2012,8 @@ POLYGON *poly_in(char *str)
1996
2012
int npts ;
1997
2013
int size ;
1998
2014
int isopen ;
1999
-
2000
- #if OLD_FORMAT_IN
2001
2015
char * s ;
2016
+ #if OLD_FORMAT_IN
2002
2017
int oldstyle ;
2003
2018
int oddcount ;
2004
2019
int i ;
@@ -2431,7 +2446,7 @@ path_add_pt(PATH *path, Point *point)
2431
2446
PATH * result ;
2432
2447
int i ;
2433
2448
2434
- if (! ( PointerIsValid (path ) && PointerIsValid (point )))
2449
+ if ((! PointerIsValid (path )) || (! PointerIsValid (point )))
2435
2450
return (NULL );
2436
2451
2437
2452
result = path_copy (path );
@@ -2450,7 +2465,7 @@ path_sub_pt(PATH *path, Point *point)
2450
2465
PATH * result ;
2451
2466
int i ;
2452
2467
2453
- if (! ( PointerIsValid (path ) && PointerIsValid (point )))
2468
+ if ((! PointerIsValid (path )) || (! PointerIsValid (point )))
2454
2469
return (NULL );
2455
2470
2456
2471
result = path_copy (path );
@@ -2474,7 +2489,7 @@ path_mul_pt(PATH *path, Point *point)
2474
2489
Point * p ;
2475
2490
int i ;
2476
2491
2477
- if (! ( PointerIsValid (path ) && PointerIsValid (point )))
2492
+ if ((! PointerIsValid (path )) || (! PointerIsValid (point )))
2478
2493
return (NULL );
2479
2494
2480
2495
result = path_copy (path );
@@ -2496,7 +2511,7 @@ path_div_pt(PATH *path, Point *point)
2496
2511
Point * p ;
2497
2512
int i ;
2498
2513
2499
- if (! ( PointerIsValid (path ) && PointerIsValid (point )))
2514
+ if ((! PointerIsValid (path )) || (! PointerIsValid (point )))
2500
2515
return (NULL );
2501
2516
2502
2517
result = path_copy (path );
@@ -2541,6 +2556,53 @@ POLYGON *path_poly(PATH *path)
2541
2556
} /* path_polygon() */
2542
2557
2543
2558
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
+
2544
2606
/***********************************************************************
2545
2607
**
2546
2608
** Routines for 2D polygons.
@@ -2628,6 +2690,89 @@ poly_path(POLYGON *poly)
2628
2690
} /* poly_path() */
2629
2691
2630
2692
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
+
2631
2776
/*-------------------------------------------------------------------------
2632
2777
*
2633
2778
* circle.c--
@@ -2637,7 +2782,7 @@ poly_path(POLYGON *poly)
2637
2782
*
2638
2783
*
2639
2784
* 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 $
2641
2786
*
2642
2787
*-------------------------------------------------------------------------
2643
2788
*/
0 commit comments