[geos-commits] r2174 - in trunk: capi source/headers/geos source/headers/geos/io source/io

svn_geos at osgeo.org svn_geos at osgeo.org
Thu Aug 28 18:20:51 EDT 2008


Author: sgillies
Date: 2008-08-28 18:20:50 -0400 (Thu, 28 Aug 2008)
New Revision: 2174

Added:
   trunk/source/headers/geos/io/CLocalizer.h
   trunk/source/io/CLocalizer.cpp
Modified:
   trunk/capi/geos_c.cpp
   trunk/source/headers/geos/io.h
   trunk/source/io/Makefile.am
Log:
Added CLocalizer class that switches to C locale and restores to the outer context's locale when deleted (#201)

Modified: trunk/capi/geos_c.cpp
===================================================================
--- trunk/capi/geos_c.cpp	2008-08-28 21:35:13 UTC (rev 2173)
+++ trunk/capi/geos_c.cpp	2008-08-28 22:20:50 UTC (rev 2174)
@@ -34,6 +34,7 @@
 #include <geos/io/WKBReader.h>
 #include <geos/io/WKTWriter.h>
 #include <geos/io/WKBWriter.h>
+#include <geos/io/CLocalizer.h>
 #include <geos/simplify/DouglasPeuckerSimplifier.h>
 #include <geos/simplify/TopologyPreservingSimplifier.h>
 #include <geos/operation/valid/IsValidOp.h>
@@ -86,6 +87,7 @@
 using geos::io::WKTWriter;
 using geos::io::WKBReader;
 using geos::io::WKBWriter;
+using geos::io::CLocalizer;
 
 using geos::operation::overlay::OverlayOp;
 using geos::operation::overlay::overlayOp;
@@ -504,31 +506,12 @@
 Geometry *
 GEOSGeomFromWKT(const char *wkt)
 {
-	using geos::io::WKTReader;
+    CLocalizer clocale;
 	try
 	{
-        /* Delocalize the writer, saving the current locale */
-        char *current_locale = strdup(std::setlocale(LC_NUMERIC, NULL));
-        if (std::setlocale(LC_NUMERIC, "C") == NULL)
-        {
-            if (current_locale != NULL) 
-            {
-                free(current_locale);
-                current_locale = NULL;
-            }
-        }
-
 		WKTReader r(geomFactory);
 		const std::string wktstring = std::string(wkt);
 		Geometry *g = r.read(wktstring);
-
-        /* Restore saved locale */
-        if (current_locale != NULL)
-        {
-            std::setlocale(LC_NUMERIC, current_locale);
-            free(current_locale);
-        }
-
 		return g;
 	}
 	catch (const std::exception &e)
@@ -547,31 +530,13 @@
 char *
 GEOSGeomToWKT(const Geometry *g1)
 {
+    CLocalizer clocale;
 	try
 	{
-        /* Delocalize the writer, saving the current locale */
-        char *current_locale = strdup(std::setlocale(LC_NUMERIC, NULL));
-        if (std::setlocale(LC_NUMERIC, "C") == NULL)
-        {
-            if (current_locale != NULL) 
-            {
-                free(current_locale);
-                current_locale = NULL;
-            }
-        }
-
 		std::string s = g1->toString();
 		char *result;
 		result = (char*) std::malloc( s.length() + 1);
 		std::strcpy(result, s.c_str() );
-
-        /* Restore saved locale */
-        if (current_locale != NULL)
-        {
-            std::setlocale(LC_NUMERIC, current_locale);
-            free(current_locale);
-        }
-        
         return result;
 	}
 	catch (const std::exception &e)
@@ -2002,29 +1967,11 @@
 Geometry*
 GEOSWKTReader_read(WKTReader *reader, const char *wkt)
 {
+    CLocalizer clocale;
 	try
 	{
-        /* Delocalize the writer, saving the current locale */
-        char *current_locale = strdup(std::setlocale(LC_NUMERIC, NULL));
-        if (std::setlocale(LC_NUMERIC, "C") == NULL)
-        {
-            if (current_locale != NULL) 
-            {
-                free(current_locale);
-                current_locale = NULL;
-            }
-        }
-
 		const std::string wktstring = std::string(wkt);
 		Geometry *g = reader->read(wktstring);
-
-        /* Restore saved locale */
-        if (current_locale != NULL)
-        {
-            std::setlocale(LC_NUMERIC, current_locale);
-            free(current_locale);
-        }
-
 		return g;
 	}
 	catch (const std::exception &e)
@@ -2084,32 +2031,13 @@
 char*
 GEOSWKTWriter_write(WKTWriter *writer, const Geometry *geom)
 {
+    CLocalizer clocale;
 	try
 	{
-        /* Delocalize the writer, saving the current locale */
-        char *current_locale = strdup(std::setlocale(LC_NUMERIC, NULL));
-        if (std::setlocale(LC_NUMERIC, "C") == NULL)
-        {
-            if (current_locale != NULL) 
-            {
-                free(current_locale);
-                current_locale = NULL;
-            }
-        }
-        
 		std::string s = writer->write(geom);
-
 		char *result;
 		result = (char*) std::malloc( s.length() + 1);
 		std::strcpy(result, s.c_str() );
-        
-        /* Restore saved locale */
-        if (current_locale != NULL)
-        {
-            std::setlocale(LC_NUMERIC, current_locale);
-            free(current_locale);
-        }
-
 		return result;
 	}
 	catch (const std::exception &e)

Added: trunk/source/headers/geos/io/CLocalizer.h
===================================================================
--- trunk/source/headers/geos/io/CLocalizer.h	                        (rev 0)
+++ trunk/source/headers/geos/io/CLocalizer.h	2008-08-28 22:20:50 UTC (rev 2174)
@@ -0,0 +1,21 @@
+
+namespace geos {
+namespace io {
+
+/**
+ * \class CLocalizer io.h geos.h
+ */
+
+class CLocalizer
+{
+private:
+    char *outer_locale;
+
+public:
+    CLocalizer();
+    ~CLocalizer();
+};
+
+} // namespace io
+} // namespace geos
+

Modified: trunk/source/headers/geos/io.h
===================================================================
--- trunk/source/headers/geos/io.h	2008-08-28 21:35:13 UTC (rev 2173)
+++ trunk/source/headers/geos/io.h	2008-08-28 22:20:50 UTC (rev 2174)
@@ -55,6 +55,7 @@
 #include <geos/io/WKBWriter.h>
 #include <geos/io/WKTReader.h>
 #include <geos/io/WKTWriter.h>
+#include <geos/io/CLocalizer.h>
 //#include <geos/io/Writer.h>
 
 #ifdef __GNUC__

Added: trunk/source/io/CLocalizer.cpp
===================================================================
--- trunk/source/io/CLocalizer.cpp	                        (rev 0)
+++ trunk/source/io/CLocalizer.cpp	2008-08-28 22:20:50 UTC (rev 2174)
@@ -0,0 +1,34 @@
+#include <geos/io/CLocalizer.h>
+
+#include <string>
+
+using namespace std;
+
+namespace geos {
+namespace io {
+
+CLocalizer::CLocalizer()
+{
+    outer_locale = strdup(std::setlocale(LC_NUMERIC, NULL));
+    if (std::setlocale(LC_NUMERIC, "C") == NULL)
+    {
+        if (outer_locale != NULL)
+        {
+            free(outer_locale);
+            outer_locale = NULL;
+        }
+    }
+}
+
+CLocalizer::~CLocalizer()
+{
+    if (outer_locale != NULL)
+    {
+        std::setlocale(LC_NUMERIC, outer_locale);
+        free(outer_locale);
+        outer_locale = NULL;
+    }
+}
+
+} // namespace geos.io
+} // namespace geos

Modified: trunk/source/io/Makefile.am
===================================================================
--- trunk/source/io/Makefile.am	2008-08-28 21:35:13 UTC (rev 2173)
+++ trunk/source/io/Makefile.am	2008-08-28 22:20:50 UTC (rev 2174)
@@ -14,7 +14,8 @@
 	WKBReader.cpp \
 	WKBWriter.cpp \
 	Writer.cpp \
-	Unload.cpp 
+	Unload.cpp \
+	CLocalizer.cpp
 
 libio_la_LIBADD = 
 



More information about the geos-commits mailing list