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:
		| @@ -29,7 +29,8 @@ public: | ||||
|     virtual wxString GetUserLocalDataDir() const wxOVERRIDE; | ||||
|     virtual wxString GetPluginsDir() 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 | ||||
|   | ||||
| @@ -32,7 +32,8 @@ public: | ||||
|     GetLocalizedResourcesDir(const wxString& lang, | ||||
|                              ResourceCat category = ResourceCat_None) 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: | ||||
|     // Ctor is protected, use wxStandardPaths::Get() instead of instantiating | ||||
|   | ||||
| @@ -67,6 +67,14 @@ public: | ||||
|         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 | ||||
|     static wxStandardPaths& Get(); | ||||
|  | ||||
| @@ -162,7 +170,9 @@ public: | ||||
|  | ||||
|     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 ~wxStandardPathsBase(); | ||||
|   | ||||
| @@ -49,7 +49,8 @@ public: | ||||
| #ifndef __VMS | ||||
|     virtual wxString GetUserDir(Dir userDir) const wxOVERRIDE; | ||||
| #endif | ||||
|     virtual wxString MakeConfigFileName(const wxString& basename, int style) const wxOVERRIDE; | ||||
|     virtual wxString MakeConfigFileName(const wxString& basename, | ||||
|                                         ConfigFileConv conv) const wxOVERRIDE; | ||||
|  | ||||
| protected: | ||||
|     // Ctor is protected, use wxStandardPaths::Get() instead of instantiating | ||||
|   | ||||
| @@ -166,6 +166,35 @@ public: | ||||
|         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. | ||||
|  | ||||
| @@ -500,16 +529,22 @@ public: | ||||
|     FileLayout GetFileLayout() const; | ||||
|  | ||||
|     /** | ||||
|         Return the file name which would be used by wxFileConfig as local, | ||||
|         user-specific, file if it were constructed with @a basename. | ||||
|         Return the file name which would be used by wxFileConfig if it were | ||||
|         constructed with @a basename. | ||||
|  | ||||
|         @a style has the same meaning as in @ref wxConfigBase::wxConfigBase "wxConfig constructor" | ||||
|         and can contain any combination of styles but only wxCONFIG_USE_SUBDIR bit is | ||||
|         examined by this function. | ||||
|         @a conv is used to construct the name of the file under Unix and only | ||||
|         matters when using the class file layout, i.e. if SetFileLayout() had | ||||
|         @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: | ||||
|     /** | ||||
|   | ||||
| @@ -266,14 +266,28 @@ wxString wxFileConfig::GetLocalDir(int style) | ||||
|  | ||||
| wxFileName wxFileConfig::GetGlobalFile(const wxString& szFile) | ||||
| { | ||||
|     wxFileName fn(GetGlobalDir(), wxStandardPaths::Get().MakeConfigFileName(szFile, wxCONFIG_USE_SUBDIR)); | ||||
|     return fn; | ||||
|     wxStandardPathsBase& stdp = wxStandardPaths::Get(); | ||||
|  | ||||
|     return wxFileName(GetGlobalDir(), stdp.MakeConfigFileName(szFile)); | ||||
| } | ||||
|  | ||||
| wxFileName wxFileConfig::GetLocalFile(const wxString& szFile, int style) | ||||
| { | ||||
|     wxFileName fn(GetLocalDir(style), wxStandardPaths::Get().MakeConfigFileName(szFile, style)); | ||||
|     return fn; | ||||
|     wxStandardPathsBase& stdp = wxStandardPaths::Get(); | ||||
|  | ||||
|     // 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)); | ||||
| } | ||||
|  | ||||
| // ---------------------------------------------------------------------------- | ||||
|   | ||||
| @@ -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); | ||||
|     fn.SetExt(wxT("ini")); | ||||
|   | ||||
| @@ -130,7 +130,9 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const | ||||
|     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); | ||||
|     fn.SetName(fn.GetName() + wxT(" Preferences")); | ||||
|   | ||||
| @@ -33,7 +33,6 @@ | ||||
|     #include "wx/utils.h" | ||||
| #endif //WX_PRECOMP | ||||
|  | ||||
| #include "wx/fileconf.h" | ||||
| #include "wx/filename.h" | ||||
| #include "wx/log.h" | ||||
| #include "wx/textfile.h" | ||||
| @@ -334,29 +333,37 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const | ||||
|  | ||||
| #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); | ||||
|  | ||||
|     bool addExt = false; | ||||
|  | ||||
|     switch ( GetFileLayout() ) | ||||
|     { | ||||
|         case FileLayout_Classic: | ||||
|             if ( !(style & wxCONFIG_USE_SUBDIR) ) | ||||
|             switch ( conv ) | ||||
|             { | ||||
|                 // The standard convention is to not use the extensions for the | ||||
|                 // config files in the home directory and just prepend a dot to | ||||
|                 // them instead. | ||||
|                 case ConfigFileConv_Dot: | ||||
|                     fn.SetName(wxT('.') + fn.GetName()); | ||||
|                     break; | ||||
|             } | ||||
|             //else: fall through to add the extension | ||||
|             wxFALLTHROUGH; | ||||
|  | ||||
|         case FileLayout_XDG: | ||||
|             // We always use the extension for the config files when using XDG | ||||
|             // layout as they don't go to the home directory anyhow. | ||||
|             fn.SetExt(wxS("conf")); | ||||
|                 case ConfigFileConv_Ext: | ||||
|                     addExt = true; | ||||
|                     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(); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user