Add wxStandardPaths::ConfigFileConv enum for clarity and safety

Use an enum instead of type-unsafe "int" for the second parameter of the
recently added wxStandardPaths::MakeConfigFileName().

This also avoids unnatural dependency of wxStandardPaths on
wxCONFIG_USE_SUBDIR constant defined in a higher level wxFileConfig class.

No real changes, but just make things a bit more robust and hopefully more
clear.
This commit is contained in:
Vadim Zeitlin
2017-03-09 17:48:42 +01:00
parent 4bf6ae8d7c
commit 5cf9fcbb1a
9 changed files with 103 additions and 30 deletions

View File

@@ -29,7 +29,8 @@ public:
virtual wxString GetUserLocalDataDir() const wxOVERRIDE; virtual wxString GetUserLocalDataDir() const wxOVERRIDE;
virtual wxString GetPluginsDir() const wxOVERRIDE; virtual wxString GetPluginsDir() const wxOVERRIDE;
virtual wxString GetUserDir(Dir userDir) const wxOVERRIDE; virtual wxString GetUserDir(Dir userDir) const wxOVERRIDE;
virtual wxString MakeConfigFileName(const wxString& basename, int style) const wxOVERRIDE; virtual wxString MakeConfigFileName(const wxString& basename,
ConfigFileConv conv) const wxOVERRIDE;
// MSW-specific methods // MSW-specific methods

View File

@@ -32,7 +32,8 @@ public:
GetLocalizedResourcesDir(const wxString& lang, GetLocalizedResourcesDir(const wxString& lang,
ResourceCat category = ResourceCat_None) const wxOVERRIDE; ResourceCat category = ResourceCat_None) const wxOVERRIDE;
virtual wxString GetUserDir(Dir userDir) const wxOVERRIDE; virtual wxString GetUserDir(Dir userDir) const wxOVERRIDE;
virtual wxString MakeConfigFileName(const wxString& basename, int style) const wxOVERRIDE; virtual wxString MakeConfigFileName(const wxString& basename,
ConfigFileConv conv) const wxOVERRIDE;
protected: protected:
// Ctor is protected, use wxStandardPaths::Get() instead of instantiating // Ctor is protected, use wxStandardPaths::Get() instead of instantiating

View File

@@ -67,6 +67,14 @@ public:
FileLayout_XDG // Recommended: use XDG specification. FileLayout_XDG // Recommended: use XDG specification.
}; };
// Naming convention for the config files under Unix.
enum ConfigFileConv
{
ConfigFileConv_Dot, // Classic Unix dot-file convention.
ConfigFileConv_Ext // Use .conf extension.
};
// return the global standard paths object // return the global standard paths object
static wxStandardPaths& Get(); static wxStandardPaths& Get();
@@ -162,7 +170,9 @@ public:
virtual wxString GetUserDir(Dir userDir) const; virtual wxString GetUserDir(Dir userDir) const;
virtual wxString MakeConfigFileName(const wxString& basename, int style) const = 0; virtual wxString
MakeConfigFileName(const wxString& basename,
ConfigFileConv conv = ConfigFileConv_Ext) const = 0;
// virtual dtor for the base class // virtual dtor for the base class
virtual ~wxStandardPathsBase(); virtual ~wxStandardPathsBase();

View File

@@ -49,7 +49,8 @@ public:
#ifndef __VMS #ifndef __VMS
virtual wxString GetUserDir(Dir userDir) const wxOVERRIDE; virtual wxString GetUserDir(Dir userDir) const wxOVERRIDE;
#endif #endif
virtual wxString MakeConfigFileName(const wxString& basename, int style) const wxOVERRIDE; virtual wxString MakeConfigFileName(const wxString& basename,
ConfigFileConv conv) const wxOVERRIDE;
protected: protected:
// Ctor is protected, use wxStandardPaths::Get() instead of instantiating // Ctor is protected, use wxStandardPaths::Get() instead of instantiating

View File

@@ -166,6 +166,35 @@ public:
FileLayout_XDG FileLayout_XDG
}; };
/**
Possible values for MakeConfigFileName() naming convention argument.
The values in this enum are only used under Unix and only when using
the classic Unix convention for file layout, in XDG mode, XDG naming
convention is used unconditionally.
@since 3.1.1
*/
enum ConfigFileConv
{
/**
Use the class Unix dot-file convention.
Prepend the dot to the file base name.
This value is ignored when in XDG mode, where MakeConfigFileName()
always behaves as if ConfigFileConv_Ext was specified.
*/
ConfigFileConv_Dot,
/**
Use @c .conf extension for the file names.
This convention is always used in XDG mode.
*/
ConfigFileConv_Ext
};
/** /**
MSW-specific function undoing the effect of IgnoreAppSubDir() calls. MSW-specific function undoing the effect of IgnoreAppSubDir() calls.
@@ -500,16 +529,22 @@ public:
FileLayout GetFileLayout() const; FileLayout GetFileLayout() const;
/** /**
Return the file name which would be used by wxFileConfig as local, Return the file name which would be used by wxFileConfig if it were
user-specific, file if it were constructed with @a basename. constructed with @a basename.
@a style has the same meaning as in @ref wxConfigBase::wxConfigBase "wxConfig constructor" @a conv is used to construct the name of the file under Unix and only
and can contain any combination of styles but only wxCONFIG_USE_SUBDIR bit is matters when using the class file layout, i.e. if SetFileLayout() had
examined by this function. @e not been called with @c FileLayout_XDG argument. In this case, this
argument is used to determine whether to use an extension or a leading
dot. When following XDG specification, the function always appends the
extension, regardless of @a conv value. Finally, this argument is not
used at all under non-Unix platforms.
Notice that this function cannot be used if @a basename is already a full path name. @since 3.1.1
*/ */
virtual wxString MakeConfigFileName(const wxString& basename, int style) const; virtual wxString
MakeConfigFileName(const wxString& basename,
ConfigFileConv conv = ConfigFileConv_Ext) const;
protected: protected:
/** /**

View File

@@ -266,14 +266,28 @@ wxString wxFileConfig::GetLocalDir(int style)
wxFileName wxFileConfig::GetGlobalFile(const wxString& szFile) wxFileName wxFileConfig::GetGlobalFile(const wxString& szFile)
{ {
wxFileName fn(GetGlobalDir(), wxStandardPaths::Get().MakeConfigFileName(szFile, wxCONFIG_USE_SUBDIR)); wxStandardPathsBase& stdp = wxStandardPaths::Get();
return fn;
return wxFileName(GetGlobalDir(), stdp.MakeConfigFileName(szFile));
} }
wxFileName wxFileConfig::GetLocalFile(const wxString& szFile, int style) wxFileName wxFileConfig::GetLocalFile(const wxString& szFile, int style)
{ {
wxFileName fn(GetLocalDir(style), wxStandardPaths::Get().MakeConfigFileName(szFile, style)); wxStandardPathsBase& stdp = wxStandardPaths::Get();
return fn;
// If the config file is located in a subdirectory, we always use an
// extension for it, but we use just the leading dot if it is located
// directly in the home directory. Note that if wxStandardPaths is
// configured to follow XDG specification, all config files go to a
// subdirectory of XDG_CONFIG_HOME anyhow, so in this case we'll still end
// up using the extension even if wxCONFIG_USE_SUBDIR is not set, but this
// is the correct and expected (if a little confusing) behaviour.
const wxStandardPaths::ConfigFileConv
conv = style & wxCONFIG_USE_SUBDIR
? wxStandardPaths::ConfigFileConv_Ext
: wxStandardPaths::ConfigFileConv_Dot;
return wxFileName(GetLocalDir(style), stdp.MakeConfigFileName(szFile, conv));
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -370,7 +370,9 @@ wxString wxStandardPaths::GetPluginsDir() const
} }
wxString wxStandardPaths::MakeConfigFileName(const wxString& basename, int WXUNUSED(style)) const wxString
wxStandardPaths::MakeConfigFileName(const wxString& basename,
ConfigFileConv WXUNUSED(conv)) const
{ {
wxFileName fn(wxEmptyString, basename); wxFileName fn(wxEmptyString, basename);
fn.SetExt(wxT("ini")); fn.SetExt(wxT("ini"));

View File

@@ -130,7 +130,9 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const
return GetFMDirectory(dirType, NSUserDomainMask); return GetFMDirectory(dirType, NSUserDomainMask);
} }
wxString wxStandardPaths::MakeConfigFileName(const wxString& basename, int WXUNUSED(style)) const wxString
wxStandardPaths::MakeConfigFileName(const wxString& basename,
ConfigFileConv WXUNUSED(conv)) const
{ {
wxFileName fn(wxEmptyString, basename); wxFileName fn(wxEmptyString, basename);
fn.SetName(fn.GetName() + wxT(" Preferences")); fn.SetName(fn.GetName() + wxT(" Preferences"));

View File

@@ -33,7 +33,6 @@
#include "wx/utils.h" #include "wx/utils.h"
#endif //WX_PRECOMP #endif //WX_PRECOMP
#include "wx/fileconf.h"
#include "wx/filename.h" #include "wx/filename.h"
#include "wx/log.h" #include "wx/log.h"
#include "wx/textfile.h" #include "wx/textfile.h"
@@ -334,29 +333,37 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const
#endif // __VMS/!__VMS #endif // __VMS/!__VMS
wxString wxStandardPaths::MakeConfigFileName(const wxString& basename, int style) const wxString
wxStandardPaths::MakeConfigFileName(const wxString& basename,
ConfigFileConv conv) const
{ {
wxFileName fn(wxEmptyString, basename); wxFileName fn(wxEmptyString, basename);
bool addExt = false;
switch ( GetFileLayout() ) switch ( GetFileLayout() )
{ {
case FileLayout_Classic: case FileLayout_Classic:
if ( !(style & wxCONFIG_USE_SUBDIR) ) switch ( conv )
{ {
// The standard convention is to not use the extensions for the case ConfigFileConv_Dot:
// config files in the home directory and just prepend a dot to
// them instead.
fn.SetName(wxT('.') + fn.GetName()); fn.SetName(wxT('.') + fn.GetName());
break; break;
}
//else: fall through to add the extension
wxFALLTHROUGH;
case FileLayout_XDG: case ConfigFileConv_Ext:
// We always use the extension for the config files when using XDG addExt = true;
// layout as they don't go to the home directory anyhow.
fn.SetExt(wxS("conf"));
break; break;
} }
break;
case FileLayout_XDG:
// Dot files are never used in XDG mode.
addExt = true;
break;
}
if ( addExt )
fn.SetExt(wxS("conf"));
return fn.GetFullName(); return fn.GetFullName();
} }