Skip to content

Commit

Permalink
use class to temporary override locale to C
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jun 15, 2015
1 parent b4fc413 commit 0ecef35
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 54 deletions.
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -186,6 +186,7 @@ SET(QGIS_CORE_SRCS
qgsvectorsimplifymethod.cpp qgsvectorsimplifymethod.cpp
qgsxmlutils.cpp qgsxmlutils.cpp
qgsslconnect.cpp qgsslconnect.cpp
qgslocalec.cpp


composer/qgsaddremoveitemcommand.cpp composer/qgsaddremoveitemcommand.cpp
composer/qgsaddremovemultiframecommand.cpp composer/qgsaddremovemultiframecommand.cpp
Expand Down Expand Up @@ -603,6 +604,7 @@ SET(QGIS_CORE_HDRS
qgswebpage.h qgswebpage.h
qgswebframe.h qgswebframe.h
qgswebview.h qgswebview.h
qgslocalec.h


diagram/qgsdiagram.h diagram/qgsdiagram.h
diagram/qgspiediagram.h diagram/qgspiediagram.h
Expand Down
15 changes: 4 additions & 11 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -34,6 +34,7 @@
#include "qgslogger.h" #include "qgslogger.h"
#include "qgsmessagelog.h" #include "qgsmessagelog.h"
#include "qgis.h" //const vals declared here #include "qgis.h" //const vals declared here
#include "qgslocalec.h"


#include <sqlite3.h> #include <sqlite3.h>
#include <proj_api.h> #include <proj_api.h>
Expand Down Expand Up @@ -921,26 +922,18 @@ void QgsCoordinateReferenceSystem::setDescription( QString theDescription )
void QgsCoordinateReferenceSystem::setProj4String( QString theProj4String ) void QgsCoordinateReferenceSystem::setProj4String( QString theProj4String )
{ {
mProj4 = theProj4String; mProj4 = theProj4String;
char *oldlocale = setlocale( LC_NUMERIC, NULL );
/* the next setlocale() invalides the return of previous setlocale() */
if ( oldlocale )
oldlocale = strdup( oldlocale );


setlocale( LC_NUMERIC, "C" ); QgsLocaleNumC l;

OSRDestroySpatialReference( mCRS ); OSRDestroySpatialReference( mCRS );
mCRS = OSRNewSpatialReference( NULL ); mCRS = OSRNewSpatialReference( NULL );
mIsValidFlag = mIsValidFlag = OSRImportFromProj4( mCRS, theProj4String.trimmed().toLatin1().constData() ) == OGRERR_NONE;
OSRImportFromProj4( mCRS, theProj4String.trimmed().toLatin1().constData() )
== OGRERR_NONE;
mWkt.clear(); mWkt.clear();
setMapUnits(); setMapUnits();


#if defined(QGISDEBUG) && QGISDEBUG>=3 #if defined(QGISDEBUG) && QGISDEBUG>=3
debugPrint(); debugPrint();
#endif #endif

setlocale( LC_NUMERIC, oldlocale );
free( oldlocale );
} }
void QgsCoordinateReferenceSystem::setGeographicFlag( bool theGeoFlag ) void QgsCoordinateReferenceSystem::setGeographicFlag( bool theGeoFlag )
{ {
Expand Down
43 changes: 43 additions & 0 deletions src/core/qgslocalec.cpp
@@ -0,0 +1,43 @@
/***************************************************************************
qgslocalec.h - temporary C numeric locale
-------------------
begin : Jun 15th 2015
copyright : (C) 2015 by Juergen E. Fischer
email : jef at norbit dot de
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <qgslocalec.h>

#include <locale.h>
#include <QByteArray>

QMutex QgsLocaleNumC::sLocaleLock;

QgsLocaleNumC::QgsLocaleNumC()
{
sLocaleLock.lock();

mOldlocale = setlocale( LC_NUMERIC, NULL );
if ( mOldlocale )
mOldlocale = qstrdup( mOldlocale );

setlocale( LC_NUMERIC, "C" );
}

QgsLocaleNumC::~QgsLocaleNumC()
{
setlocale( LC_NUMERIC, mOldlocale );
if ( mOldlocale )
delete [] mOldlocale;

sLocaleLock.unlock();
}
34 changes: 34 additions & 0 deletions src/core/qgslocalec.h
@@ -0,0 +1,34 @@
/***************************************************************************
qgslocalec.h - temporary C numeric locale
-------------------
begin : Jun 15th 2015
copyright : (C) 2015 by Juergen E. Fischer
email : jef at norbit dot de
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLOCALENUMC_H
#define QGSLOCALENUMC_H

#include <QMutex>

class CORE_EXPORT QgsLocaleNumC
{
char *mOldlocale;
static QMutex sLocaleLock;

public:
QgsLocaleNumC();
~QgsLocaleNumC();

};

#endif // QGSLOCALENUMC_H
3 changes: 3 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsrendererv2.h" #include "qgsrendererv2.h"
#include "qgssymbollayerv2.h" #include "qgssymbollayerv2.h"
#include "qgsvectordataprovider.h" #include "qgsvectordataprovider.h"
#include "qgslocalec.h"


#include <QFile> #include <QFile>
#include <QSettings> #include <QSettings>
Expand Down Expand Up @@ -1625,6 +1626,8 @@ bool QgsVectorFileWriter::addFeature( QgsFeature& feature, QgsFeatureRendererV2*


OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature ) OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
{ {
QgsLocaleNumC l;

OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn( mLayer ) ); OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn( mLayer ) );


qint64 fid = FID_TO_NUMBER( feature.id() ); qint64 fid = FID_TO_NUMBER( feature.id() );
Expand Down
11 changes: 7 additions & 4 deletions src/plugins/grass/qgsgrassnewmapset.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsmapcanvas.h" #include "qgsmapcanvas.h"
#include "qgsproject.h" #include "qgsproject.h"
#include "qgsprojectionselector.h" #include "qgsprojectionselector.h"
#include "qgslocalec.h"


#include <QCloseEvent> #include <QCloseEvent>
#include <QFileDialog> #include <QFileDialog>
Expand Down Expand Up @@ -427,10 +428,12 @@ void QgsGrassNewMapset::setGrassProjection()
OGRSpatialReferenceH hCRS = NULL; OGRSpatialReferenceH hCRS = NULL;
hCRS = OSRNewSpatialReference( NULL ); hCRS = OSRNewSpatialReference( NULL );
int errcode; int errcode;
const char *oldlocale = setlocale( LC_NUMERIC, NULL );
setlocale( LC_NUMERIC, "C" ); {
errcode = OSRImportFromProj4( hCRS, proj4.toUtf8() ); QgsLocaleNumC l;
setlocale( LC_NUMERIC, oldlocale ); errcode = OSRImportFromProj4( hCRS, proj4.toUtf8() );
}

if ( errcode != OGRERR_NONE ) if ( errcode != OGRERR_NONE )
{ {
QgsDebugMsg( QString( "OGR can't parse PROJ.4-style parameter string:\n%1\nOGR Error code was %2" ).arg( proj4 ).arg( errcode ) ); QgsDebugMsg( QString( "OGR can't parse PROJ.4-style parameter string:\n%1\nOGR Error code was %2" ).arg( proj4 ).arg( errcode ) );
Expand Down
43 changes: 21 additions & 22 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -29,6 +29,7 @@
#include "qgsfield.h" #include "qgsfield.h"
#include "qgsrectangle.h" #include "qgsrectangle.h"
#include "qgsconfig.h" #include "qgsconfig.h"
#include "qgslocalec.h"


#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
Expand Down Expand Up @@ -1723,31 +1724,29 @@ QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crsDirect( const QString
QgsGrass::resetError(); QgsGrass::resetError();
QgsGrass::setLocation( gisdbase, location ); QgsGrass::setLocation( gisdbase, location );


const char *oldlocale = setlocale( LC_NUMERIC, NULL );
setlocale( LC_NUMERIC, "C" );

G_TRY
{
G_get_default_window( &cellhd );
}
G_CATCH( QgsGrass::Exception &e )
{ {
Q_UNUSED( e ); QgsLocaleNumC l;
setlocale( LC_NUMERIC, oldlocale );
QgsDebugMsg( QString( "Cannot get default window: %1" ).arg( e.what() ) );
return QgsCoordinateReferenceSystem();
}


if ( cellhd.proj != PROJECTION_XY ) G_TRY
{ {
struct Key_Value *projinfo = G_get_projinfo(); G_get_default_window( &cellhd );
struct Key_Value *projunits = G_get_projunits(); }
char *wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 ); G_CATCH( QgsGrass::Exception &e )
Wkt = QString( wkt ); {
G_free( wkt ); Q_UNUSED( e );
} QgsDebugMsg( QString( "Cannot get default window: %1" ).arg( e.what() ) );
return QgsCoordinateReferenceSystem();
}


setlocale( LC_NUMERIC, oldlocale ); if ( cellhd.proj != PROJECTION_XY )
{
struct Key_Value *projinfo = G_get_projinfo();
struct Key_Value *projunits = G_get_projunits();
char *wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 );
Wkt = QString( wkt );
G_free( wkt );
}
}


QgsCoordinateReferenceSystem srs; QgsCoordinateReferenceSystem srs;
srs.createFromWkt( Wkt ); srs.createFromWkt( Wkt );
Expand Down
21 changes: 4 additions & 17 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -19,6 +19,7 @@ email : sherman at mrcc.com
#include "qgsogrfeatureiterator.h" #include "qgsogrfeatureiterator.h"
#include "qgslogger.h" #include "qgslogger.h"
#include "qgsmessagelog.h" #include "qgsmessagelog.h"
#include "qgslocalec.h"


#define CPL_SUPRESS_CPLUSPLUS #define CPL_SUPRESS_CPLUSPLUS
#include <gdal.h> // to collect version information #include <gdal.h> // to collect version information
Expand All @@ -45,6 +46,7 @@ email : sherman at mrcc.com
#include "qgsgeometry.h" #include "qgsgeometry.h"
#include "qgscoordinatereferencesystem.h" #include "qgscoordinatereferencesystem.h"
#include "qgsvectorlayerimport.h" #include "qgsvectorlayerimport.h"
#include "qgslocalec.h"


static const QString TEXT_PROVIDER_KEY = "ogr"; static const QString TEXT_PROVIDER_KEY = "ogr";
static const QString TEXT_PROVIDER_DESCRIPTION = static const QString TEXT_PROVIDER_DESCRIPTION =
Expand Down Expand Up @@ -1001,11 +1003,7 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )


QgsAttributes attrs = f.attributes(); QgsAttributes attrs = f.attributes();


char *oldlocale = setlocale( LC_NUMERIC, NULL ); QgsLocaleNumC l;
if ( oldlocale )
oldlocale = strdup( oldlocale );

setlocale( LC_NUMERIC, "C" );


//add possible attribute information //add possible attribute information
for ( int targetAttributeId = 0; targetAttributeId < attrs.count(); ++targetAttributeId ) for ( int targetAttributeId = 0; targetAttributeId < attrs.count(); ++targetAttributeId )
Expand Down Expand Up @@ -1084,10 +1082,6 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
} }
OGR_F_Destroy( feature ); OGR_F_Destroy( feature );


setlocale( LC_NUMERIC, oldlocale );
if ( oldlocale )
free( oldlocale );

return returnValue; return returnValue;
} }


Expand Down Expand Up @@ -1222,10 +1216,7 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
continue; continue;
} }


char *oldlocale = setlocale( LC_NUMERIC, NULL ); QgsLocaleNumC l;
if ( oldlocale )
oldlocale = strdup( oldlocale );
setlocale( LC_NUMERIC, "C" );


for ( QgsAttributeMap::const_iterator it2 = attr.begin(); it2 != attr.end(); ++it2 ) for ( QgsAttributeMap::const_iterator it2 = attr.begin(); it2 != attr.end(); ++it2 )
{ {
Expand Down Expand Up @@ -1287,10 +1278,6 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
{ {
pushError( tr( "OGR error setting feature %1: %2" ).arg( fid ).arg( CPLGetLastErrorMsg() ) ); pushError( tr( "OGR error setting feature %1: %2" ).arg( fid ).arg( CPLGetLastErrorMsg() ) );
} }

setlocale( LC_NUMERIC, oldlocale );
if ( oldlocale )
free( oldlocale );
} }


if ( OGR_L_SyncToDisk( ogrLayer ) != OGRERR_NONE ) if ( OGR_L_SyncToDisk( ogrLayer ) != OGRERR_NONE )
Expand Down

0 comments on commit 0ecef35

Please sign in to comment.