Skip to content

Commit 2ce0bdc

Browse files
author
jef
committed
fix #1005 by making GRASS work in paths with blanks (windows-only)
git-svn-id: http://svn.osgeo.org/qgis/trunk@8256 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 4783fb0 commit 2ce0bdc

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

src/plugins/grass/qgsgrassmodule.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ extern "C" {
9191

9292
#include <gdal.h> // to collect version information
9393

94+
#if defined(WIN32)
95+
#include <windows.h>
96+
static QString getShortPath(const QString &path)
97+
{
98+
TCHAR buf[MAX_PATH];
99+
GetShortPathName( path.ascii(), buf, MAX_PATH);
100+
return buf;
101+
}
102+
#endif
103+
94104
bool QgsGrassModule::mExecPathInited = 0;
95105
QStringList QgsGrassModule::mExecPath;
96106

@@ -107,10 +117,11 @@ QString QgsGrassModule::findExec ( QString file )
107117

108118
#ifdef WIN32
109119
mExecPath = path.split ( ";" );
120+
mExecPath.prepend ( getShortPath(QgsApplication::applicationDirPath()) );
110121
#else
111122
mExecPath = path.split ( ":" );
112-
#endif
113123
mExecPath.prepend ( QgsApplication::applicationDirPath() );
124+
#endif
114125
mExecPathInited = true;
115126
}
116127

@@ -168,7 +179,7 @@ QStringList QgsGrassModule::execArguments ( QString module )
168179
}
169180
else // script
170181
{
171-
QString cmd = QgsApplication::applicationDirPath() + "/msys/bin/sh";
182+
QString cmd = getShortPath(QgsApplication::applicationDirPath()) + "/msys/bin/sh";
172183
arguments.append ( cmd );
173184

174185
// Important! Otherwise it does not find DLL even if it is in PATH

src/plugins/grass/qgsgrassnewmapset.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ QString temp3(GRASS_VERSION_MINOR);
7272
QString temp4(GRASS_VERSION_RELEASE);
7373
#endif
7474

75+
#if defined(WIN32)
76+
#include <windows.h>
77+
static QString getShortPath(const QString &path)
78+
{
79+
TCHAR buf[MAX_PATH];
80+
GetShortPathName( path.ascii(), buf, MAX_PATH);
81+
return buf;
82+
}
83+
#endif
84+
7585
bool QgsGrassNewMapset::mRunning = false;
7686

7787
QgsGrassNewMapset::QgsGrassNewMapset ( QgisInterface *iface,
@@ -1348,7 +1358,11 @@ void QgsGrassNewMapset::createMapset()
13481358
// TODO: add QgsGrass::setLocation or G_make_location with
13491359
// database path
13501360
QgsGrass::activeMode(); // because it calls private gsGrass::init()
1361+
#if defined(WIN32)
1362+
G__setenv( "GISDBASE", (char *) getShortPath(mDatabaseLineEdit->text()).ascii() );
1363+
#else
13511364
G__setenv( "GISDBASE", (char *) mDatabaseLineEdit->text().ascii() );
1365+
#endif
13521366

13531367
QgsGrass::resetError();
13541368
int ret = G_make_location( (char *) location.ascii(), &mCellHead,

src/plugins/grass/qgsgrasstools.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ extern "C" {
7171
#include "qgsgrassmodel.h"
7272
#include "qgsgrassbrowser.h"
7373

74+
#if defined(WIN32)
75+
#include <windows.h>
76+
static QString getShortPath(const QString &path)
77+
{
78+
TCHAR buf[MAX_PATH];
79+
GetShortPathName( path.ascii(), buf, MAX_PATH);
80+
return buf;
81+
}
82+
#endif
83+
7484
QgsGrassToolsTabWidget::QgsGrassToolsTabWidget( QWidget * parent ):
7585
QTabWidget(parent)
7686
{
@@ -391,7 +401,11 @@ QgsGrassTools::~QgsGrassTools()
391401

392402
QString QgsGrassTools::appDir(void)
393403
{
404+
#if defined(WIN32)
405+
return getShortPath(QgsApplication::applicationDirPath());
406+
#else
394407
return QgsApplication::applicationDirPath();
408+
#endif
395409
}
396410

397411
void QgsGrassTools::close(void)

src/providers/grass/qgsgrass.cpp

+32-13
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ extern "C" {
4141
#include <grass/version.h>
4242
}
4343

44-
#if defined(_MSC_VER)
45-
#include <windows.h> // for GetCurrentProcessId()
44+
#if defined(WIN32)
45+
#include <windows.h>
46+
static QString getShortPath(const QString &path)
47+
{
48+
TCHAR buf[MAX_PATH];
49+
GetShortPathName( path.ascii(), buf, MAX_PATH);
50+
return buf;
51+
}
4652
#endif
4753

4854
void GRASS_EXPORT QgsGrass::init( void )
@@ -104,7 +110,7 @@ void GRASS_EXPORT QgsGrass::init( void )
104110

105111
#ifdef WIN32
106112
// Use the applicationDirPath()/grass
107-
gisBase = QCoreApplication::applicationDirPath() + "/grass";
113+
gisBase = getShortPath( QCoreApplication::applicationDirPath() + "/grass" );
108114
#ifdef QGISDEBUG
109115
std::cerr << "GRASS gisBase = " << gisBase.ascii() << std::endl;
110116
#endif
@@ -148,6 +154,9 @@ void GRASS_EXPORT QgsGrass::init( void )
148154
userGisbase = false;
149155
break;
150156
}
157+
#if defined(WIN32)
158+
gisBase = getShortPath(gisBase);
159+
#endif
151160
}
152161

153162
if (!valid)
@@ -187,14 +196,11 @@ void GRASS_EXPORT QgsGrass::init( void )
187196
#ifdef WIN32
188197
// It seems that QgsApplication::prefixPath()
189198
// is not initialized at this point
190-
path.append ( sep + QCoreApplication::applicationDirPath() );
191-
#endif
199+
path.append ( sep + getShortPath(QCoreApplication::applicationDirPath()) );
192200

193-
#ifdef WIN32
194201
// Add path to MSYS bin
195202
// Warning: MSYS sh.exe will translate this path to '/bin'
196-
path.append ( sep + QCoreApplication::applicationDirPath()
197-
+ "/msys/bin/" );
203+
path.append ( sep + getShortPath(QCoreApplication::applicationDirPath() + "/msys/bin/") );
198204
#endif
199205

200206
QString p = getenv ("PATH");
@@ -262,7 +268,8 @@ bool QgsGrass::isValidGrassBaseDir(QString const gisBase)
262268
#ifdef QGISDEBUG
263269
std::cerr << "isValidGrassBaseDir()" << std::endl;
264270
#endif
265-
if ( gisBase.isEmpty() )
271+
// GRASS currently doesn't handle paths with blanks
272+
if ( gisBase.isEmpty() || gisBase.contains(" ") )
266273
{
267274
return FALSE;
268275
}
@@ -316,7 +323,11 @@ void QgsGrass::setLocation( QString gisdbase, QString location )
316323
init();
317324

318325
// Set principal GRASS variables (in memory)
319-
G__setenv( "GISDBASE", (char *) gisdbase.ascii() );
326+
#if defined(WIN32)
327+
G__setenv( "GISDBASE", (char *) getShortPath(gisdbase).ascii() );
328+
#else
329+
G__setenv( "GISDBASE", (char *) gisdbase.ascii() );
330+
#endif
320331
G__setenv( "LOCATION_NAME", (char *) location.ascii() );
321332
G__setenv( "MAPSET", "PERMANENT"); // PERMANENT must always exist
322333

@@ -334,7 +345,11 @@ void QgsGrass::setMapset( QString gisdbase, QString location, QString mapset )
334345
init();
335346

336347
// Set principal GRASS variables (in memory)
337-
G__setenv( "GISDBASE", (char *) gisdbase.ascii() );
348+
#if defined(WIN32)
349+
G__setenv( "GISDBASE", (char *) getShortPath(gisdbase).ascii() );
350+
#else
351+
G__setenv( "GISDBASE", (char *) gisdbase.ascii() );
352+
#endif
338353
G__setenv( "LOCATION_NAME", (char *) location.ascii() );
339354
G__setenv( "MAPSET", (char *) mapset.ascii() );
340355

@@ -539,8 +554,12 @@ QString GRASS_EXPORT QgsGrass::openMapset ( QString gisdbase, QString location,
539554
putenv( gisrcEnvChar );
540555

541556
// Reinitialize GRASS
542-
G__setenv( "GISRC", const_cast<char *>(gisrcEnv.ascii()) );
543-
G__setenv( "GISDBASE", const_cast<char *>(gisdbase.ascii()) );
557+
G__setenv( "GISRC", const_cast<char *>(gisrcEnv.ascii()) );
558+
#if defined(WIN32)
559+
G__setenv( "GISDBASE", const_cast<char *>(getShortPath(gisdbase).ascii()) );
560+
#else
561+
G__setenv( "GISDBASE", const_cast<char *>(gisdbase.ascii()) );
562+
#endif
544563
G__setenv( "LOCATION_NAME", const_cast<char *>(location.ascii()) );
545564
G__setenv( "MAPSET", const_cast<char *>(mapset.ascii()) );
546565
defaultGisdbase = gisdbase;

0 commit comments

Comments
 (0)