From: Sandro Santilli <strk@keybit.net>
Date: Mon, 9 Feb 2004 18:49:23 +0000 (+0000)
Subject: byte endiannes detected empirically
X-Git-Tag: pgis_0_8_2~113
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=996e2a8b54155afdcc7396cad73fac89f59c3ac2;p=postgis

byte endiannes detected empirically


git-svn-id: http://svn.osgeo.org/postgis/trunk@446 b70326c6-7e19-0410-871a-916f4a2858ee
---

diff --git a/loader/pgsql2shp.c b/loader/pgsql2shp.c
index 83492d181..1802c7cfe 100644
--- a/loader/pgsql2shp.c
+++ b/loader/pgsql2shp.c
@@ -10,6 +10,9 @@
  * 
  **********************************************************************
  * $Log$
+ * Revision 1.42  2004/02/09 18:49:23  strk
+ * byte endiannes detected empirically
+ *
  * Revision 1.41  2004/02/06 08:26:02  strk
  * updated wkb reading funx to reflect changes made by pramsey in postgis_inout.c to be nicer with solaris
  *
@@ -143,6 +146,7 @@ SHPObject * (*shape_creator)(byte *, int);
 #else
 SHPObject * (*shape_creator)(char *, int, SHPHandle, int);
 #endif
+int big_endian = 0;
 
 /* Prototypes */
 int getMaxFieldSize(PGconn *conn, char *table, char *fname);
@@ -167,6 +171,7 @@ char *scan_to_same_level(char *str);
 int points_per_sublist( char *str, int *npoints, long max_lists);
 int reverse_points(int num_points,double *x,double *y,double *z);
 int is_clockwise(int num_points,double *x,double *y,double *z);
+int is_bigendian(void);
 
 /* WKB functions */
 SHPObject * create_polygon3D_WKB(byte *wkb, int shape_id);
@@ -2199,6 +2204,9 @@ initialize()
 	int mainscan_nflds=0;
 	int size;
 
+	/* Detect host endiannes */
+	big_endian = is_bigendian();
+
 	/* Query user attributes name, type and size */
 
 	size = strlen(table);
@@ -2593,24 +2601,30 @@ initialize()
 		{
 #ifdef USE_WKB
 
-#if BYTE_ORDER == LITTLE_ENDIAN
+			if ( big_endian ) 
+			{
 
 #ifdef HEXWKB
-			sprintf(buf, "asbinary(\"%s\", 'NDR')",
+				sprintf(buf, "asbinary(\"%s\", 'XDR')",
+					mainscan_flds[i]);
 #else
-			sprintf(buf, "asbinary(\"%s\", 'NDR')::bytea",
+				sprintf(buf, "asbinary(\"%s\", 'XDR')::bytea",
+					mainscan_flds[i]);
 #endif
 
-#else // BYTE_ORDER != LITTLE_ENDIAN
+			}
+			else // little_endian
+			{
 
 #ifdef HEXWKB
-			sprintf(buf, "asbinary(\"%s\", 'XDR')",
+				sprintf(buf, "asbinary(\"%s\", 'NDR')",
+					mainscan_flds[i]);
 #else // ndef HEXWKB
-			sprintf(buf, "asbinary(\"%s\", 'XDR')::bytea",
+				sprintf(buf, "asbinary(\"%s\", 'NDR')::bytea",
+					mainscan_flds[i]);
 #endif // def HEXWKB
 
-#endif // BYTE_ORDER == LITTLE_ENDIAN
-					mainscan_flds[i]);
+			}
 #else // ndef USE_WKB
 			if ( binary ) sprintf(buf, "\"%s\"::text",
 				mainscan_flds[i]);
@@ -2736,6 +2750,20 @@ HexDecode(byte *hex)
 	return ret;
 }
 
+int is_bigendian()
+{
+	int test = 1;
+
+	if ( (((char *)(&test))[0]) == 1)
+	{
+		return 0; //NDR (little_endian)
+	}
+	else
+	{
+		return 1; //XDR (big_endian)
+	}
+}
+
 /*********************************************************************
  *
  * The following functions might go in a wkb lib