Skip to content
Permalink
Browse files

use class to temporary override locale to C

(cherry picked from commit 0ecef35)
  • Loading branch information
jef-n committed Jun 15, 2015
1 parent c980332 commit a7283417f6869c0a0dc932f3689ac02d59873f5e
@@ -174,6 +174,7 @@ SET(QGIS_CORE_SRCS
qgsvectorsimplifymethod.cpp
qgsxmlutils.cpp
qgsslconnect.cpp
qgslocalec.cpp

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

diagram/qgsdiagram.h
diagram/qgspiediagram.h
@@ -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>
@@ -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 )
{
@@ -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();
}
@@ -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
@@ -27,6 +27,7 @@
#include "qgsrendererv2.h"
#include "qgssymbollayerv2.h"
#include "qgsvectordataprovider.h"
#include "qgslocalec.h"

#include <QFile>
#include <QSettings>
@@ -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() );
@@ -25,6 +25,7 @@
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgsprojectionselector.h"
#include "qgslocalec.h"

#include <QCloseEvent>
#include <QFileDialog>
@@ -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 ) );
@@ -23,6 +23,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsrectangle.h"
#include "qgsconfig.h"
#include "qgslocalec.h"

#include <QFileDialog>
#include <QMessageBox>
@@ -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 );
@@ -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
@@ -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 =
@@ -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 )
@@ -1086,10 +1084,6 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
}
OGR_F_Destroy( feature );

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

return returnValue;
}

@@ -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 )
{
@@ -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 )

0 comments on commit a728341

Please sign in to comment.
You can’t perform that action at this time.