Skip to content

Commit

Permalink
use class to temporary override locale to C
Browse files Browse the repository at this point in the history
(cherry picked from commit 0ecef35)
  • Loading branch information
jef-n committed Jun 29, 2015
1 parent c980332 commit a728341
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 @@ -174,6 +174,7 @@ SET(QGIS_CORE_SRCS
qgsvectorsimplifymethod.cpp
qgsxmlutils.cpp
qgsslconnect.cpp
qgslocalec.cpp

composer/qgsaddremoveitemcommand.cpp
composer/qgsaddremovemultiframecommand.cpp
Expand Down Expand Up @@ -552,6 +553,7 @@ SET(QGIS_CORE_HDRS
qgsvectorlayerundocommand.h
qgsvectorsimplifymethod.h
qgsxmlutils.h
qgslocalec.h

diagram/qgsdiagram.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 "qgsmessagelog.h"
#include "qgis.h" //const vals declared here
#include "qgslocalec.h"

#include <sqlite3.h>
#include <proj_api.h>
Expand Down Expand Up @@ -921,26 +922,18 @@ void QgsCoordinateReferenceSystem::setDescription( QString theDescription )
void QgsCoordinateReferenceSystem::setProj4String( QString 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 );
mCRS = OSRNewSpatialReference( NULL );
mIsValidFlag =
OSRImportFromProj4( mCRS, theProj4String.trimmed().toLatin1().constData() )
== OGRERR_NONE;
mIsValidFlag = OSRImportFromProj4( mCRS, theProj4String.trimmed().toLatin1().constData() ) == OGRERR_NONE;
mWkt.clear();
setMapUnits();

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

setlocale( LC_NUMERIC, oldlocale );
free( oldlocale );
}
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 "qgssymbollayerv2.h"
#include "qgsvectordataprovider.h"
#include "qgslocalec.h"

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

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

OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn( mLayer ) );

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 "qgsproject.h"
#include "qgsprojectionselector.h"
#include "qgslocalec.h"

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

{
QgsLocaleNumC l;
errcode = OSRImportFromProj4( hCRS, proj4.toUtf8() );
}

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 ) );
Expand Down
43 changes: 21 additions & 22 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsrectangle.h"
#include "qgsconfig.h"
#include "qgslocalec.h"

#include <QFileDialog>
#include <QMessageBox>
Expand Down Expand Up @@ -1409,31 +1410,29 @@ QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crsDirect( QString gisdb
QgsGrass::resetError();
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 );
setlocale( LC_NUMERIC, oldlocale );
QgsDebugMsg( QString( "Cannot get default window: %1" ).arg( e.what() ) );
return QgsCoordinateReferenceSystem();
}
QgsLocaleNumC l;

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 );
}
G_TRY
{
G_get_default_window( &cellhd );
}
G_CATCH( QgsGrass::Exception &e )
{
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;
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 "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgslocalec.h"

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

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

const QgsAttributes& attrs = f.attributes();

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

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

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

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

return returnValue;
}

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

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

for ( QgsAttributeMap::const_iterator it2 = attr.begin(); it2 != attr.end(); ++it2 )
{
Expand Down Expand Up @@ -1289,10 +1280,6 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
{
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 )
Expand Down

0 comments on commit a728341

Please sign in to comment.