X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1824493628261b2cb7ceceb431d85c427f1c976b..e29b83a455bead3dae44300f631d07aa9635d3e9:/src/common/fileconf.cpp diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index bdf11498cb..5366c66d4a 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -14,31 +14,31 @@ #pragma implementation "fileconf.h" #endif -// ============================================================================ -// declarations -// ============================================================================ - // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- #include "wx/wxprec.h" +#include "wx/fileconf.h" + +#ifdef wxUSE_CONFIG + #ifdef __BORLANDC__ #pragma hdrstop #endif //__BORLANDC__ #ifndef WX_PRECOMP - #include - #include + #include "wx/wx.h" #endif //WX_PRECOMP -#include -#include -#include -#include -#include -#include -#include +#include "wx/app.h" +#include "wx/dynarray.h" +#include "wx/file.h" +#include "wx/log.h" +#include "wx/textfile.h" +#include "wx/config.h" + +#include "wx/utils.h" // for wxGetHomeDir // _WINDOWS_ is defined when windows.h is included, // __WXMSW__ is defined for MS Windows compilation @@ -54,6 +54,13 @@ // ---------------------------------------------------------------------------- #define CONST_CAST ((wxFileConfig *)this)-> +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- +#ifndef MAX_PATH + #define MAX_PATH 512 +#endif + // ---------------------------------------------------------------------------- // global functions declarations // ---------------------------------------------------------------------------- @@ -87,15 +94,12 @@ wxString wxFileConfig::GetGlobalDir() #ifdef __UNIX__ strDir = "/etc/"; #elif defined(__WXSTUBS__) - // TODO - wxASSERT( TRUE ) ; + wxASSERT_MSG( FALSE, "TODO" ) ; + #elif defined(__WXMAC__) + wxASSERT_MSG( FALSE, "TODO" ) ; #else // Windows - #ifndef _MAX_PATH - #define _MAX_PATH 512 - #endif - - char szWinDir[_MAX_PATH]; - ::GetWindowsDirectory(szWinDir, _MAX_PATH); + char szWinDir[MAX_PATH]; + ::GetWindowsDirectory(szWinDir, MAX_PATH); strDir = szWinDir; strDir << '\\'; @@ -108,29 +112,13 @@ wxString wxFileConfig::GetLocalDir() { wxString strDir; - #ifdef __UNIX__ - const char *szHome = getenv("HOME"); - if ( szHome == NULL ) { - // we're homeless... - wxLogWarning(_("can't find user's HOME, using current directory.")); - strDir = "."; - } - else - strDir = szHome; - strDir << '/'; // a double slash is no problem, a missin one yes - #else // Windows - #ifdef __WIN32__ - const char *szHome = getenv("HOMEDRIVE"); - if ( szHome != NULL ) - strDir << szHome; - szHome = getenv("HOMEPATH"); - if ( szHome != NULL ) - strDir << szHome; - #else // Win16 - // Win16 has no idea about home, so use the current directory instead - strDir = ".\\"; - #endif // WIN16/32 - #endif // UNIX/Win + wxGetHomeDir(&strDir); + +#ifdef __UNIX__ + if (strDir.Last() != '/') strDir << '/'; +#else + if (strDir.Last() != '\\') strDir << '\\'; +#endif return strDir; } @@ -208,82 +196,51 @@ void wxFileConfig::Init() } } -#if 0 -wxFileConfig::wxFileConfig(const char *szAppName, bool bLocalOnly) -{ - wxASSERT( !IsEmpty(szAppName) ); // invent a name for your application! - - m_strLocalFile = GetLocalFileName(szAppName); - if ( !bLocalOnly ) - m_strGlobalFile = GetGlobalFileName(szAppName); - //else: it's going to be empty and we won't use the global file - - Init(); -} - -wxFileConfig::wxFileConfig(const wxString& strLocal, const wxString& strGlobal) - : m_strLocalFile(strLocal), m_strGlobalFile(strGlobal) -{ - // if the path is not absolute, prepend the standard directory to it - if ( !strLocal.IsEmpty() && !wxIsAbsolutePath(strLocal) ) - { - m_strLocalFile = GetLocalDir(); - m_strLocalFile << strLocal; - } - - if ( !strGlobal.IsEmpty() && !wxIsAbsolutePath(strGlobal) ) - { - m_strGlobalFile = GetGlobalDir(); - m_strGlobalFile << strGlobal; - } - - Init(); -} -#endif - -// New-style constructor +// constructor supports creation of wxFileConfig objects of any type wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, - const wxString& strLocal, const wxString& strGlobal, long style) + const wxString& strLocal, const wxString& strGlobal, + long style) : wxConfigBase(appName, vendorName, strLocal, strGlobal, style), m_strLocalFile(strLocal), m_strGlobalFile(strGlobal) { - // Make up an application name if not supplied - if (appName.IsEmpty() && wxTheApp) - { - SetAppName(wxTheApp->GetAppName()); - } + // Make up an application name if not supplied + if (appName.IsEmpty() && wxTheApp) + { + SetAppName(wxTheApp->GetAppName()); + } - // Make up names for files if empty - if (m_strLocalFile.IsEmpty() && (style & wxCONFIG_USE_LOCAL_FILE) && wxTheApp) - { - m_strLocalFile = wxTheApp->GetAppName(); - } + // Make up names for files if empty + if ( m_strLocalFile.IsEmpty() && (style & wxCONFIG_USE_LOCAL_FILE) ) + { + m_strLocalFile = GetLocalFileName(GetAppName()); + } - if (m_strGlobalFile.IsEmpty() && (style & wxCONFIG_USE_GLOBAL_FILE)) - { - // TODO: What should the default global filename be? - m_strGlobalFile = "global"; - } + if ( m_strGlobalFile.IsEmpty() && (style & wxCONFIG_USE_GLOBAL_FILE) ) + { + m_strGlobalFile = GetGlobalFileName(GetAppName()); + } - // Check if styles are not supplied, but filenames are, in which case - // add the correct styles. - if (!m_strLocalFile.IsEmpty() && ((style & wxCONFIG_USE_LOCAL_FILE) != wxCONFIG_USE_LOCAL_FILE)) - SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE); + // Check if styles are not supplied, but filenames are, in which case + // add the correct styles. + if ( !m_strLocalFile.IsEmpty() ) + SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE); - if (!m_strGlobalFile.IsEmpty() && ((style & wxCONFIG_USE_GLOBAL_FILE) != wxCONFIG_USE_GLOBAL_FILE)) - SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE); + if ( !m_strGlobalFile.IsEmpty() ) + SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE); // if the path is not absolute, prepend the standard directory to it - if ( !strLocal.IsEmpty() && !wxIsAbsolutePath(strLocal) ) + if ( !m_strLocalFile.IsEmpty() && !wxIsAbsolutePath(m_strLocalFile) ) { - m_strLocalFile = GetLocalDir(); - m_strLocalFile << strLocal; + wxString strLocal = m_strLocalFile; + m_strLocalFile = GetLocalDir(); + m_strLocalFile << strLocal; } - - if ( !strGlobal.IsEmpty() && !wxIsAbsolutePath(strGlobal) ) + + if ( !m_strGlobalFile.IsEmpty() && !wxIsAbsolutePath(m_strGlobalFile) ) { - m_strGlobalFile = GetGlobalDir(); - m_strGlobalFile << strGlobal; + wxString strGlobal = m_strGlobalFile; + m_strGlobalFile = GetGlobalDir(); + m_strGlobalFile << strGlobal; } Init(); @@ -669,9 +626,9 @@ bool wxFileConfig::Write(const wxString& key, const wxString& szValue) bool wxFileConfig::Write(const wxString& key, long lValue) { // ltoa() is not ANSI :-( - char szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits) - sprintf(szBuf, "%ld", lValue); - return Write(key, szBuf); + wxString buf; + buf.Printf("%ld", lValue); + return Write(key, buf); } bool wxFileConfig::Flush(bool /* bCurrentOnly */) @@ -805,7 +762,7 @@ void wxFileConfig::LineListRemove(LineList *pLine) // last entry? if ( pNext == NULL ) m_linesTail = pPrev; - else + else pNext->SetPrev(pPrev); delete pLine; @@ -908,7 +865,7 @@ wxFileConfig::LineList *wxFileConfig::ConfigGroup::GetGroupLine() // this group wasn't present in local config file, add it now if ( pParent != NULL ) { wxString strFullName; - strFullName << "[" << GetFullName().c_str() + 1 << "]"; // +1: no '/' + strFullName << "[" << (GetFullName().c_str() + 1) << "]"; // +1: no '/' m_pLine = m_pConfig->LineListInsert(strFullName, pParent->GetLastGroupLine()); pParent->SetLastGroup(this); // we're surely after all the others @@ -1371,7 +1328,7 @@ wxString FilterOut(const wxString& str) { if(str.IsEmpty()) return str; - + wxString strResult; strResult.Alloc(str.Len()); @@ -1421,3 +1378,7 @@ wxString FilterOut(const wxString& str) return strResult; } + +#endif + + // wxUSE_CONFIG