[geos-commits] r2172 - trunk/capi

svn_geos at osgeo.org svn_geos at osgeo.org
Thu Aug 28 15:16:09 EDT 2008


Author: sgillies
Date: 2008-08-28 15:16:09 -0400 (Thu, 28 Aug 2008)
New Revision: 2172

Modified:
   trunk/capi/geos_c.cpp
Log:
Switch to C locale while reading and writing WKT and restore to the original context's locale afterward (#201)

Modified: trunk/capi/geos_c.cpp
===================================================================
--- trunk/capi/geos_c.cpp	2008-08-22 17:25:40 UTC (rev 2171)
+++ trunk/capi/geos_c.cpp	2008-08-28 19:16:09 UTC (rev 2172)
@@ -507,9 +507,28 @@
 	using geos::io::WKTReader;
 	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)
@@ -530,12 +549,30 @@
 {
 	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() );
-		return result;
+
+        /* Restore saved locale */
+        if (current_locale != NULL)
+        {
+            std::setlocale(LC_NUMERIC, current_locale);
+            free(current_locale);
+        }
+        
+        return result;
 	}
 	catch (const std::exception &e)
 	{
@@ -1967,8 +2004,27 @@
 {
 	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)
@@ -2030,11 +2086,30 @@
 {
 	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)



More information about the geos-commits mailing list