return cmp;
}
+/*!
+ \brief Parse given string to retrieve environment variable.
+
+ Looks through the string for the patterns: ${name} or $(name) or %name%.
+ If string contains variable satisfying any pattern, the variable name
+ is returned, start index of the variable is returned in the \a start parameter,
+ and length of the variable is returned in the \a len parameter.
+
+ \param str string being processed
+ \param start if variable is found, this parameter contains its starting
+ position in the \a str
+ \param len if variable is found, this parameter contains its length
+ \return first found variable or null QString if there is no ones
+*/
+QString Qtx::findEnvVar( const QString& str, int& start, int& len )
+{
+ QString varName;
+ len = 0;
+
+ QRegExp rx( "(^\\$\\{|[^\\$]\\$\\{)([a-zA-Z]+[a-zA-Z0-9_]*)(\\})|(^\\$\\(|[^\\$]\\$\\()([a-zA-Z]+[a-zA-Z0-9_]*)(\\))|(^\\$|[^\\$]\\$)([a-zA-Z]+[a-zA-Z0-9_]*)|(^%|[^%]%)([a-zA-Z]+[a-zA-Z0-9_]*)(%[^%]|%$)" );
+
+ int pos = rx.indexIn( str, start );
+ if ( pos != -1 )
+ {
+ int i = 1;
+ while ( i <= rx.numCaptures() && varName.isEmpty() )
+ {
+ QString capStr = rx.cap( i );
+ if ( !capStr.contains( "%" ) && !capStr.contains( "$" ) )
+ varName = capStr;
+ i++;
+ }
+
+ if ( !varName.isEmpty() )
+ {
+ int capIdx = i - 1;
+ start = rx.pos( capIdx );
+ int end = start + varName.length();
+ if ( capIdx > 1 && rx.cap( capIdx - 1 ).contains( QRegExp( "\\$|%" ) ) )
+ start = rx.pos( capIdx - 1 ) + rx.cap( capIdx - 1 ).indexOf( QRegExp( "\\$|%" ) );
+ if ( capIdx < rx.numCaptures() && !rx.cap( capIdx - 1 ).isEmpty() )
+ end++;
+ len = end - start;
+ }
+ }
+ return varName;
+}
+
+/*!
+ \brief Substitute environment variables by their values.
+
+ Environment variable is substituted by its value.
+
+ \param str string to be processed
+ \return processed string (with all substitutions made)
+*/
+QString Qtx::makeEnvVarSubst( const QString& str, const SubstMode mode )
+{
+ QString res = str;
+ if ( mode != Never )
+ {
+ QMap<QString, int> ignoreMap;
+
+ int start( 0 ), len( 0 );
+ while ( true )
+ {
+ QString envName = findEnvVar( res, start, len );
+ if ( envName.isNull() )
+ break;
+
+ QString newStr;
+ if ( ::getenv( envName.toLatin1() ) || mode == Always )
+ newStr = QString( ::getenv( envName.toLatin1() ) );
+
+ if ( newStr.isNull() )
+ {
+ if ( ignoreMap.contains( envName ) )
+ {
+ start += len;
+ continue;
+ }
+ ignoreMap.insert( envName, 0 );
+ }
+ res.replace( start, len, newStr );
+ }
+
+ res.replace( "$$", "$" );
+ res.replace( "%%", "%" );
+ }
+
+ return res;
+}
+
/*!
\brief Pack the specified color into integer RGB set.
\param c unpacked color
Toggled //!< it should be possible to show/hide the column with help of popup menu
} Appropriate; //!< appropriate status
+ //! Environment variables substitution mode
+ typedef enum {
+ Always, //!< substitute environment variable by it's value if variable exists, and "" otherwise
+ Never, //!< keep environment variable as is without any substitution
+ Auto //!< substitute environment variable by it's value if variable exists, and keep it as is otherwise
+ } SubstMode;
+
static QString toQString( const char*, const int = -1 );
static QString toQString( const short*, const int = -1 );
static QString toQString( const unsigned char*, const int = -1 );
static QString addSlash( const QString& );
static QCompleter* pathCompleter( const PathType, const QString& = QString() );
+ static QString findEnvVar( const QString&, int&, int& );
+ static QString makeEnvVarSubst( const QString&, const SubstMode = Auto );
static int rgbSet( const QColor& );
static int rgbSet( const int, const int, const int );
void QtxPathEdit::onBrowse( bool /*on*/ )
{
QString path;
- QString initial = QFileInfo( myPath->text() ).path();
+ QString initial = QFileInfo( Qtx::makeEnvVarSubst( myPath->text() ) ).filePath();
switch ( pathType() )
{
case Qtx::PT_OpenFile:
base->setSpacing( 5 );
base->addWidget( myPath = new QLineEdit( this ) );
- myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
+ myPath->setValidator( new QRegExpValidator( QRegExp( "^([\\$]|[\\%]|[\\w/]{2}|[A-Z]:)[^:;\\*\\?]*[\\w\\\\/\\.]$" ), myPath ) );
QToolButton* browse = new QToolButton( this );
browse->setIcon( QPixmap( browse_icon ) );
if ( pathType() == Qtx::PT_SaveFile )
return true;
- bool ok = QFileInfo( str ).exists();
+ QFileInfo aFI = QFileInfo( Qtx::makeEnvVarSubst( str ) );
+ bool ok = aFI.exists();
if ( !ok && msg )
ok = QMessageBox::question( this, tr( "Warning" ), tr( "Path \"%1\" doesn't exist. Add it to list anyway?" ).arg( str ),
QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes;
- if ( ok && QFileInfo( str ).exists() )
+ if ( ok && aFI.exists() )
{
switch ( pathType() )
{
case Qtx::PT_OpenFile:
- ok = QFileInfo( str ).isFile();
+ ok = aFI.isFile();
if ( !ok && msg )
QMessageBox::warning( this, tr( "Error" ), tr( "Location \"%1\" doesn't point to file" ).arg( str ) );
break;
case Qtx::PT_Directory:
- ok = QFileInfo( str ).isDir();
+ ok = aFI.isDir();
if ( !ok && msg )
QMessageBox::warning( this, tr( "Error" ), tr( "Location \"%1\" doesn't point to directory" ).arg( str ) );
break;
QPixmap loadPixmap( const QString&, const QString&, const QString& ) const;
QTranslator* loadTranslator( const QString&, const QString&, const QString& ) const;
- QString environmentVariable( const QString&, int&, int& ) const;
QString makeSubstitution( const QString&, const QString&, const QString& ) const;
void clear();
return trans;
}
-/*!
- \brief Parse given string to retrieve environment variable.
-
- Looks through the string for the patterns: ${name} or $(name) or %name%.
- If string contains variable satisfying any pattern, the variable name
- is returned, start index of the variable is returned in the \a start parameter,
- and length of the variable is returned in the \a len parameter.
-
- \param str string being processed
- \param start if variable is found, this parameter contains its starting
- position in the \a str
- \param len if variable is found, this parameter contains its length
- \return first found variable or null QString if there is no ones
-*/
-QString QtxResourceMgr::Resources::environmentVariable( const QString& str, int& start, int& len ) const
-{
- QString varName;
- len = 0;
-
- QRegExp rx( "(^\\$\\{|[^\\$]\\$\\{)([a-zA-Z]+[a-zA-Z0-9_]*)(\\})|(^\\$\\(|[^\\$]\\$\\()([a-zA-Z]+[a-zA-Z0-9_]*)(\\))|(^\\$|[^\\$]\\$)([a-zA-Z]+[a-zA-Z0-9_]*)|(^%|[^%]%)([a-zA-Z]+[a-zA-Z0-9_]*)(%[^%]|%$)" );
-
- int pos = rx.indexIn( str, start );
- if ( pos != -1 )
- {
- int i = 1;
- while ( i <= rx.numCaptures() && varName.isEmpty() )
- {
- QString capStr = rx.cap( i );
- if ( !capStr.contains( "%" ) && !capStr.contains( "$" ) )
- varName = capStr;
- i++;
- }
-
- if ( !varName.isEmpty() )
- {
- int capIdx = i - 1;
- start = rx.pos( capIdx );
- int end = start + varName.length();
- if ( capIdx > 1 && rx.cap( capIdx - 1 ).contains( QRegExp( "\\$|%" ) ) )
- start = rx.pos( capIdx - 1 ) + rx.cap( capIdx - 1 ).indexOf( QRegExp( "\\$|%" ) );
- if ( capIdx < rx.numCaptures() && !rx.cap( capIdx - 1 ).isEmpty() )
- end++;
- len = end - start;
- }
- }
- return varName;
-}
-
/*!
\brief Substitute variables by their values.
int start( 0 ), len( 0 );
while ( true )
{
- QString envName = environmentVariable( res, start, len );
+ QString envName = Qtx::findEnvVar( res, start, len );
if ( envName.isNull() )
break;