@@ -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
4854void 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