Тест 1

This commit is contained in:
Andrei Astafev 2021-06-04 16:03:01 +03:00
parent feda5be3ba
commit 5d2098b4fc
3 changed files with 187 additions and 96 deletions

View File

@ -18,9 +18,9 @@ int main( int argc, char** argv )
QCoreApplication::setApplicationName( QStringLiteral( CMLIB_PROJECT_NAME ) ); QCoreApplication::setApplicationName( QStringLiteral( CMLIB_PROJECT_NAME ) );
MF::Paths& paths = MF::Paths::instance(); MF::Paths& paths = MF::Paths::instance();
paths.init( QStringLiteral( CMLIB_PROJECT_NAME ), QStringLiteral( "conf" ) ); paths.init();
qDebug() << "prefixDirectory : " << paths.prefixDirectory(); qDebug() << "prefixDirectory : " << paths.projectDirectory();
qDebug() << "executableName : " << paths.executableName(); qDebug() << "executableName : " << paths.executableName();
qDebug() << "executableFilePath : " << paths.executableFilePath(); qDebug() << "executableFilePath : " << paths.executableFilePath();

View File

@ -8,6 +8,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QString> #include <QString>
#include <QDebug>
namespace myx { namespace myx {
@ -23,20 +24,20 @@ MYXLIB_INLINE Paths::Paths()
} }
MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultPrefixDirectory, MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultProjectDirectory,
const QString& defaultEtcDirectory, const QString& defaultEtcDirectory,
const QString& defaultConstDataDirectory, const QString& defaultConstDataDirectory,
const QString& defaultVarDataDirectory, const QString& defaultVarDataDirectory,
const QString& defaultLogDirectory ) const QString& defaultLogDirectory )
{ {
QFileInfo prefixDirInfo { defaultPrefixDirectory }; QFileInfo prefixDirInfo { defaultProjectDirectory };
if ( prefixDirInfo.isDir() && prefixDirInfo.isReadable() ) if ( prefixDirInfo.isDir() && prefixDirInfo.isReadable() )
{ {
m_prefixDirectory = defaultPrefixDirectory; m_projectDirectory = defaultProjectDirectory;
} }
else else
{ {
m_prefixDirectory = QStringLiteral( "." ); m_projectDirectory = QStringLiteral( "." );
} }
QFileInfo etcDirInfo { defaultEtcDirectory }; QFileInfo etcDirInfo { defaultEtcDirectory };
@ -84,100 +85,141 @@ MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultPrefixDi
MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType()
{ {
QRegExp binUnityRegexp( "/s*bin/unity$" ); QRegExp binUnityRegexp( "/s*bin/unity$" );
auto binaryDir = m_executableDirectory; if ( binUnityRegexp.indexIn( m_executableDirectory ) >= 0 )
if ( binUnityRegexp.indexIn( binaryDir ) >= 0 )
{ {
binaryDir.remove( binUnityRegexp );
setupSystemDirectories( binaryDir,
binaryDir + "/etc",
binaryDir + "/share",
binaryDir + "/var",
binaryDir + "/log" );
return ( HierarchyType::kDevelopment ); return ( HierarchyType::kDevelopment );
} }
QRegExp binRegexp( "/s*bin$" ); QRegExp binRegexp( "/s*bin$" );
if ( binRegexp.indexIn( binaryDir ) == -1 ) if ( binRegexp.indexIn( m_executableDirectory ) == -1 )
{ {
m_prefixDirectory = m_executableDirectory; return ( HierarchyType::kFlat );
}
QRegExp optRegexp( "^/opt(/|/.+/)" );
if ( optRegexp.indexIn( m_executableDirectory ) >= 0 )
{
return ( HierarchyType::kOpt );
}
if ( m_executableDirectory.startsWith( QStringLiteral( "/usr/local/bin" ) ) )
{
return ( HierarchyType::kUsrLocal );
}
if ( m_executableDirectory.startsWith( QStringLiteral( "/usr/local" ) ) )
{
return ( HierarchyType::kUsrLocalOrg );
}
if ( m_executableDirectory.startsWith( QStringLiteral( "/usr/bin" ) ) )
{
return ( HierarchyType::kUsr );
}
if ( m_executableDirectory.startsWith( m_homeDirectory + "/.local/bin" ) ||
m_executableDirectory.startsWith( m_homeDirectory + "/bin" ) )
{
return( HierarchyType::kHome );
}
return ( HierarchyType::kDevelopment );
} // Paths::getHierarchyType
MYXLIB_INLINE void Paths::calculatePaths( HierarchyType hType )
{
QRegExp binUnityRegexp( "/s*bin/unity$" );
QRegExp binRegexp( "/s*bin$" );
auto directory = m_executableDirectory;
switch ( hType )
{
case HierarchyType::kFlat:
m_projectDirectory = m_executableDirectory;
m_systemConstDataDirectory = m_executableDirectory; m_systemConstDataDirectory = m_executableDirectory;
m_systemVarDataDirectory = m_executableDirectory; m_systemVarDataDirectory = m_executableDirectory;
m_systemConfigDirectory = m_executableDirectory; m_systemConfigDirectory = m_executableDirectory;
m_systemLogDirectory = m_executableDirectory; m_systemLogDirectory = m_executableDirectory;
break;
case HierarchyType::kOpt:
if ( m_organizationName.isEmpty() || m_themeName.isEmpty() )
{
QRegExp parse( "opt/(.+)-(.+)/" );
parse.setMinimal( true );
if ( parse.indexIn( m_executableDirectory ) )
{
m_organizationName = parse.cap( 1 );
m_themeName = parse.cap( 2 );
}
}
return ( HierarchyType::kFlat ); if ( m_projectName.isEmpty() )
} {
QRegExp parse( "opt/.+/(.+)/" );
parse.setMinimal( true );
if ( parse.indexIn( m_executableDirectory ) )
{
m_projectName = parse.cap( 1 );
}
}
qDebug() << m_organizationName;
qDebug() << m_themeName;
qDebug() << m_projectName;
directory.remove( binRegexp );
QRegExp optRegexp( "^/opt(/|/.+/)" + m_projectName + "/" ); setupSystemDirectories( directory,
if ( optRegexp.indexIn( binaryDir ) >= 0 ) directory + "/etc",
{ directory + "/share",
binaryDir.remove( binRegexp ); directory + "/var",
setupSystemDirectories( binaryDir, directory + "/log" );
binaryDir + "/etc",
binaryDir + "/share",
binaryDir + "/var",
binaryDir + "/log" );
return ( HierarchyType::kOpt ); break;
} case HierarchyType::kUsr:
setupSystemDirectories( QStringLiteral( "/usr" ),
if ( binaryDir.startsWith( QStringLiteral( "/usr/local/bin" ) ) ) "/etc/" + m_projectName,
{ "/usr/share/" + m_projectName,
"/var/lib/" + m_projectName,
"/var/log/" + m_projectName );
break;
case HierarchyType::kUsrLocal:
setupSystemDirectories( QStringLiteral( "/usr/local" ), setupSystemDirectories( QStringLiteral( "/usr/local" ),
"/usr/local/etc/" + m_projectName, "/usr/local/etc/" + m_projectName,
"/usr/local/share/" + m_projectName, "/usr/local/share/" + m_projectName,
"/var/lib/" + m_projectName, "/var/lib/" + m_projectName,
"/var/log/" + m_projectName ); "/var/log/" + m_projectName );
return ( HierarchyType::kUsrLocal ); break;
} case HierarchyType::kUsrLocalOrg:
directory.remove( QRegExp( "/bin$" ) );
setupSystemDirectories( directory,
directory + "/etc",
directory + "/share",
directory + "/var",
directory + "/log" );
if ( binaryDir.startsWith( QStringLiteral( "/usr/local" ) ) ) break;
{ case HierarchyType::kHome:
binaryDir.remove( QRegExp( "/bin$" ) ); m_projectDirectory = m_homeDirectory;
setupSystemDirectories( binaryDir,
binaryDir + "/etc",
binaryDir + "/share",
binaryDir + "/var",
binaryDir + "/log" );
return ( HierarchyType::kUsrLocalOrg );
}
if ( binaryDir.startsWith( QStringLiteral( "/usr" ) ) )
{
setupSystemDirectories( QStringLiteral( "/usr" ),
"/etc/" + m_projectName,
"/usr/share/" + m_projectName,
"/var/lib/" + m_projectName,
"/var/log/" + m_projectName );
return ( HierarchyType::kUsr );
}
if ( binaryDir.startsWith( m_homeDirectory + "/.local/bin" ) ||
binaryDir.startsWith( m_homeDirectory + "/bin" ) )
{
m_prefixDirectory = m_homeDirectory;
m_systemConfigDirectory = m_userConfigDirectory; m_systemConfigDirectory = m_userConfigDirectory;
m_systemConstDataDirectory = m_userConstDataDirectory; m_systemConstDataDirectory = m_userConstDataDirectory;
m_systemVarDataDirectory = m_userVarDataDirectory; m_systemVarDataDirectory = m_userVarDataDirectory;
m_systemLogDirectory = m_userLogDirectory; m_systemLogDirectory = m_userLogDirectory;
break;
case HierarchyType::kDevelopment:
directory.remove( binUnityRegexp );
directory.remove( binRegexp );
setupSystemDirectories( directory,
directory + "/etc",
directory + "/share",
directory + "/var",
directory + "/log" );
return( HierarchyType::kHome ); break;
} case HierarchyType::kUndefined:
;
binaryDir.remove( binRegexp ); } // switch
setupSystemDirectories( binaryDir, } // Paths::calculatePaths
binaryDir + "/etc",
binaryDir + "/share",
binaryDir + "/var",
binaryDir + "/log" );
return ( HierarchyType::kDevelopment );
} // Paths::getHierarchyType
MYXLIB_INLINE bool Paths::initCommon() MYXLIB_INLINE bool Paths::initCommon()
@ -185,24 +227,20 @@ MYXLIB_INLINE bool Paths::initCommon()
m_homeDirectory = QDir::homePath(); m_homeDirectory = QDir::homePath();
m_tempDirectory = QDir::tempPath(); m_tempDirectory = QDir::tempPath();
auto configHome = QString::fromLocal8Bit( qgetenv( "XDG_CONFIG_HOME" ) ); m_configDirectory = QString::fromLocal8Bit( qgetenv( "XDG_CONFIG_HOME" ) );
if ( configHome.isEmpty() ) if ( m_configDirectory.isEmpty() )
{ {
configHome = m_homeDirectory + "/.config"; m_configDirectory = m_homeDirectory + "/.config";
} }
m_userConfigDirectory = configHome + "/" + m_projectName;
auto dataHome = QString::fromLocal8Bit( qgetenv( "XDG_DATA_HOME" ) ); m_dataDirectory = QString::fromLocal8Bit( qgetenv( "XDG_DATA_HOME" ) );
if ( dataHome.isEmpty() ) if ( m_dataDirectory.isEmpty() )
{ {
dataHome = m_homeDirectory + "/.local/share"; m_dataDirectory = m_homeDirectory + "/.local/share";
} }
dataHome += "/" + m_projectName;
m_userConstDataDirectory = dataHome + "/share";
m_userVarDataDirectory = dataHome + "/var";
m_userLogDirectory = dataHome + "/log";
m_hierarchyType = getHierarchyType(); m_hierarchyType = getHierarchyType();
calculatePaths( m_hierarchyType );
m_configFilePath = m_systemConfigDirectory + "/" + m_configFileName; m_configFilePath = m_systemConfigDirectory + "/" + m_configFileName;
@ -212,7 +250,6 @@ MYXLIB_INLINE bool Paths::initCommon()
MYXLIB_INLINE bool Paths::init() MYXLIB_INLINE bool Paths::init()
{ {
m_projectName = m_executableName;
m_configFileName = m_executableName + ".conf"; m_configFileName = m_executableName + ".conf";
return( initCommon() ); return( initCommon() );
} }
@ -220,7 +257,6 @@ MYXLIB_INLINE bool Paths::init()
MYXLIB_INLINE bool Paths::init( const QString& configFileName ) MYXLIB_INLINE bool Paths::init( const QString& configFileName )
{ {
m_projectName = m_executableName;
m_configFileName = configFileName; m_configFileName = configFileName;
return( initCommon() ); return( initCommon() );
} }
@ -374,6 +410,30 @@ MYXLIB_INLINE const QString& Paths::projectName() const
} }
MYXLIB_INLINE const QString& Paths::organizationName() const
{
return( m_organizationName );
}
MYXLIB_INLINE void Paths::setOrganizationName( QString name )
{
m_organizationName = name;
}
MYXLIB_INLINE const QString& Paths::themeName() const
{
return( m_themeName );
}
MYXLIB_INLINE void Paths::setThemeName( QString name )
{
m_themeName = name;
}
MYXLIB_INLINE const QString& Paths::executableName() const MYXLIB_INLINE const QString& Paths::executableName() const
{ {
return( m_executableName ); return( m_executableName );
@ -392,9 +452,9 @@ MYXLIB_INLINE const QString& Paths::executableDirectory() const
} }
MYXLIB_INLINE const QString& Paths::prefixDirectory() const MYXLIB_INLINE const QString& Paths::projectDirectory() const
{ {
return( m_prefixDirectory ); return( m_projectDirectory );
} }
} // namespace filesystem } // namespace filesystem

View File

@ -98,7 +98,7 @@ public:
/** /**
* @brief Полный путь к базовому каталогу * @brief Полный путь к базовому каталогу
*/ */
const QString& prefixDirectory() const; const QString& projectName() const;
/** /**
* @brief Имя исполняемого файла * @brief Имя исполняемого файла
@ -181,9 +181,26 @@ public:
const QString& homeDirectory() const; const QString& homeDirectory() const;
/** /**
* @brief Имя подкаталога для проекта * @brief Имя каталога для работы (темы)
*/ */
const QString& projectName() const; const QString& themeDirectory() const;
/**
* @brief Имя каталога для проекта
*/
const QString& projectDirectory() const;
/**
* @brief Название организации
*/
const QString& organizationName() const;
void setOrganizationName( QString name );
/**
* @brief Название работы (темы)
*/
const QString& themeName() const;
void setThemeName( QString name );
protected: protected:
Paths(); Paths();
@ -193,6 +210,12 @@ private:
/// @brief Тип расположения файлов по каталогам /// @brief Тип расположения файлов по каталогам
HierarchyType m_hierarchyType { HierarchyType::kFlat }; HierarchyType m_hierarchyType { HierarchyType::kFlat };
/// @brief Название организации
QString m_organizationName;
/// @brief Название работы (темы)
QString m_themeName;
/// @brief Имя проекта, которое используется при формировании имён файлов и каталогов /// @brief Имя проекта, которое используется при формировании имён файлов и каталогов
QString m_projectName; QString m_projectName;
@ -200,13 +223,20 @@ private:
QString m_executableFilePath; QString m_executableFilePath;
QString m_executableDirectory; QString m_executableDirectory;
/// @brief Общий префикс для файлов проекта /// @brief Общий каталог для файлов работы (темы)
QString m_prefixDirectory; QString m_themeDirectory;
/// @brief Общий каталог для файлов проекта
QString m_projectDirectory;
/// @brief Путь к каталогу с временными файлами /// @brief Путь к каталогу с временными файлами
QString m_tempDirectory; QString m_tempDirectory;
/// @brief Путь к домашнему каталогу текущего пользователя /// @brief Путь к домашнему каталогу текущего пользователя
QString m_homeDirectory; QString m_homeDirectory;
/// @brief Путь к общему пользовательскому каталогу настроек
QString m_configDirectory;
/// @brief Путь к общему пользовательскому каталогу данных
QString m_dataDirectory;
/// @brief Путь к пользовательскому каталогу с изменяемыми файлами /// @brief Путь к пользовательскому каталогу с изменяемыми файлами
QString m_userVarDataDirectory; QString m_userVarDataDirectory;
@ -233,13 +263,14 @@ private:
/// @brief Имя файла настройки /// @brief Имя файла настройки
QString m_configFileName; QString m_configFileName;
void setupSystemDirectories( const QString& defaultPrefixDirectory, void setupSystemDirectories( const QString& defaultProjectDirectory,
const QString& defaultEtcDirectory, const QString& defaultEtcDirectory,
const QString& defaultConstDataDirectory, const QString& defaultConstDataDirectory,
const QString& defaultVarDataDirectory, const QString& defaultVarDataDirectory,
const QString& defaultLogDirectory ); const QString& defaultLogDirectory );
bool initCommon(); bool initCommon();
HierarchyType getHierarchyType(); HierarchyType getHierarchyType();
void calculatePaths( HierarchyType hType );
}; // class Paths }; // class Paths
} // namespace filesystem } // namespace filesystem