]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/iniconf.cpp
   1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/os2/iniconf.cpp 
   3 // Purpose:     implementation of wxIniConfig class 
   4 // Author:      David Webster 
   7 // Copyright:   David Webster 
   8 // Licence:     wxWindows license 
   9 /////////////////////////////////////////////////////////////////////////////// 
  11 // For compilers that support precompilation, includes "wx.h". 
  12 #include "wx/wxprec.h" 
  15   #include  <wx/string.h> 
  22 #include  <wx/dynarray.h> 
  25 #include  <wx/config.h> 
  28 #include  <wx/os2/iniconf.h> 
  33 // ---------------------------------------------------------------------------- 
  35 // ---------------------------------------------------------------------------- 
  37 // we replace all path separators with this character 
  38 #define PATH_SEP_REPLACE  '_' 
  40 // ============================================================================ 
  42 // ============================================================================ 
  44 // ---------------------------------------------------------------------------- 
  46 // ---------------------------------------------------------------------------- 
  48 wxIniConfig::wxIniConfig(const wxString
& strAppName
, 
  49                          const wxString
& strVendor
, 
  50                          const wxString
& localFilename
, 
  51                          const wxString
& globalFilename
, 
  53            : wxConfigBase(!strAppName 
&& wxTheApp 
? wxTheApp
->GetAppName() 
  55                           !strVendor 
? (wxTheApp 
? wxTheApp
->GetVendorName() 
  58                           localFilename
, globalFilename
, style
) 
  60     m_strLocalFilename 
= localFilename
; 
  61     if (m_strLocalFilename
.IsEmpty()) 
  63         m_strLocalFilename 
= GetAppName() + ".ini"; 
  66     // append the extension if none given and it's not an absolute file name 
  67     // (otherwise we assume that they know what they're doing) 
  68     if ( !wxIsPathSeparator(m_strLocalFilename
[(size_t) 0]) && 
  69         m_strLocalFilename
.Find('.') == wxNOT_FOUND 
) 
  71         m_strLocalFilename 
<< ".ini"; 
  78 wxIniConfig::~wxIniConfig() 
  82 // ---------------------------------------------------------------------------- 
  84 // ---------------------------------------------------------------------------- 
  86 void wxIniConfig::SetPath(const wxString
& strPath
) 
  90   if ( strPath
.IsEmpty() ) { 
  93   else if ( strPath
[(size_t) 0] == wxCONFIG_PATH_SEPARATOR 
) { 
  95     wxSplitPath(aParts
, strPath
); 
  98     // relative path, combine with current one 
  99     wxString strFullPath 
= GetPath(); 
 100     strFullPath 
<< wxCONFIG_PATH_SEPARATOR 
<< strPath
; 
 101     wxSplitPath(aParts
, strFullPath
); 
 104   size_t nPartsCount 
= aParts
.Count(); 
 106   if ( nPartsCount 
== 0 ) { 
 108     m_strGroup 
= PATH_SEP_REPLACE
; 
 112     m_strGroup 
= aParts
[(size_t) 0]; 
 113     for ( size_t nPart 
= 1; nPart 
< nPartsCount
; nPart
++ ) { 
 115         m_strPath 
<< PATH_SEP_REPLACE
; 
 116       m_strPath 
<< aParts
[nPart
]; 
 120   // other functions assume that all this is true, i.e. there are no trailing 
 121   // underscores at the end except if the group is the root one 
 122   wxASSERT( (m_strPath
.IsEmpty() || m_strPath
.Last() != PATH_SEP_REPLACE
) && 
 123             (m_strGroup 
== wxString(PATH_SEP_REPLACE
) || 
 124              m_strGroup
.Last() != PATH_SEP_REPLACE
) ); 
 127 const wxString
& wxIniConfig::GetPath() const 
 129   static wxString s_str
; 
 131   // always return abs path 
 132   s_str 
= wxCONFIG_PATH_SEPARATOR
; 
 134   if ( m_strGroup 
== wxString(PATH_SEP_REPLACE
) ) { 
 135     // we're at the root level, nothing to do 
 139     if ( !m_strPath
.IsEmpty() ) 
 140       s_str 
<< wxCONFIG_PATH_SEPARATOR
; 
 141     for ( const char *p 
= m_strPath
; *p 
!= '\0'; p
++ ) { 
 142       s_str 
<< (*p 
== PATH_SEP_REPLACE 
? wxCONFIG_PATH_SEPARATOR 
: *p
); 
 149 wxString 
wxIniConfig::GetPrivateKeyName(const wxString
& szKey
) const 
 153   if ( !m_strPath
.IsEmpty() ) 
 154     strKey 
<< m_strPath 
<< PATH_SEP_REPLACE
; 
 161 wxString 
wxIniConfig::GetKeyName(const wxString
& szKey
) const 
 165   if ( m_strGroup 
!= wxString(PATH_SEP_REPLACE
) ) 
 166     strKey 
<< m_strGroup 
<< PATH_SEP_REPLACE
; 
 167   if ( !m_strPath
.IsEmpty() ) 
 168     strKey 
<< m_strPath 
<< PATH_SEP_REPLACE
; 
 175 // ---------------------------------------------------------------------------- 
 177 // ---------------------------------------------------------------------------- 
 180 bool wxIniConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const 
 182   wxFAIL_MSG("not implemented"); 
 187 bool wxIniConfig::GetNextGroup (wxString
& str
, long& lIndex
) const 
 189   wxFAIL_MSG("not implemented"); 
 194 bool wxIniConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const 
 196   wxFAIL_MSG("not implemented"); 
 201 bool wxIniConfig::GetNextEntry (wxString
& str
, long& lIndex
) const 
 203   wxFAIL_MSG("not implemented"); 
 208 // ---------------------------------------------------------------------------- 
 210 // ---------------------------------------------------------------------------- 
 213 size_t wxIniConfig::GetNumberOfEntries(bool bRecursive
) const 
 215   wxFAIL_MSG("not implemented"); 
 220 size_t wxIniConfig::GetNumberOfGroups(bool bRecursive
) const 
 222   wxFAIL_MSG("not implemented"); 
 227 bool wxIniConfig::HasGroup(const wxString
& strName
) const 
 229   wxFAIL_MSG("not implemented"); 
 234 bool wxIniConfig::HasEntry(const wxString
& strName
) const 
 236   wxFAIL_MSG("not implemented"); 
 241 // is current group empty? 
 242 bool wxIniConfig::IsEmpty() const 
 246 //  GetPrivateProfileString(m_strGroup, NULL, "", 
 247 //                          szBuf, WXSIZEOF(szBuf), m_strLocalFilename); 
 248   if ( !::IsEmpty(szBuf
) ) 
 251 //  GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf)); 
 252 //  if ( !::IsEmpty(szBuf) ) 
 258 // ---------------------------------------------------------------------------- 
 260 // ---------------------------------------------------------------------------- 
 262 bool wxIniConfig::Read(const wxString
& szKey
, wxString 
*pstr
) const 
 264   wxConfigPathChanger 
path(this, szKey
); 
 265   wxString strKey 
= GetPrivateKeyName(path
.Name()); 
 267   char szBuf
[1024]; // @@ should dynamically allocate memory... 
 269   // first look in the private INI file 
 271   // NB: the lpDefault param to GetPrivateProfileString can't be NULL 
 272 //  GetPrivateProfileString(m_strGroup, strKey, "", 
 273 //                          szBuf, WXSIZEOF(szBuf), m_strLocalFilename); 
 274   if ( ::IsEmpty(szBuf
) ) { 
 275     // now look in win.ini 
 276     wxString strKey 
= GetKeyName(path
.Name()); 
 277 //    GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf)); 
 280   if ( ::IsEmpty(szBuf
) ) { 
 289 bool wxIniConfig::Read(const wxString
& szKey
, wxString 
*pstr
, 
 290                        const wxString
& szDefault
) const 
 292   wxConfigPathChanger 
path(this, szKey
); 
 293   wxString strKey 
= GetPrivateKeyName(path
.Name()); 
 295   char szBuf
[1024]; // @@ should dynamically allocate memory... 
 297   // first look in the private INI file 
 299   // NB: the lpDefault param to GetPrivateProfileString can't be NULL 
 300 //  GetPrivateProfileString(m_strGroup, strKey, "", 
 301 //                          szBuf, WXSIZEOF(szBuf), m_strLocalFilename); 
 302   if ( ::IsEmpty(szBuf
) ) { 
 303     // now look in win.ini 
 304     wxString strKey 
= GetKeyName(path
.Name()); 
 305 //    GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf)); 
 308   if ( ::IsEmpty(szBuf
) ) { 
 318 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const 
 320   wxConfigPathChanger 
path(this, szKey
); 
 321   wxString strKey 
= GetPrivateKeyName(path
.Name()); 
 323   // hack: we have no mean to know if it really found the default value or 
 324   // didn't find anything, so we call it twice 
 326   static const int nMagic  
= 17; // 17 is some "rare" number 
 327   static const int nMagic2 
= 28; // arbitrary number != nMagic 
 328   long lVal 
= 0; // = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename); 
 329   if ( lVal 
!= nMagic 
) { 
 330     // the value was read from the file 
 335   // is it really nMagic? 
 336 //  lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename); 
 337   if ( lVal 
== nMagic2 
) { 
 338     // the nMagic it returned was indeed read from the file 
 343   // no, it was just returning the default value, so now look in win.ini 
 344 //  *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl); 
 349 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& szValue
) 
 351   wxConfigPathChanger 
path(this, szKey
); 
 352   wxString strKey 
= GetPrivateKeyName(path
.Name()); 
 354   bool bOk 
= FALSE
; // = WritePrivateProfileString(m_strGroup, strKey, 
 355 //                                       szValue, m_strLocalFilename) != 0; 
 358     wxLogLastError("WritePrivateProfileString"); 
 363 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
) 
 365   // ltoa() is not ANSI :-( 
 366   char szBuf
[40];   // should be good for sizeof(long) <= 16 (128 bits) 
 367   sprintf(szBuf
, "%ld", lValue
); 
 369   return Write(szKey
, szBuf
); 
 372 bool wxIniConfig::Flush(bool /* bCurrentOnly */) 
 374   // this is just the way it works 
 375 //  return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0; 
 379 // ---------------------------------------------------------------------------- 
 381 // ---------------------------------------------------------------------------- 
 383 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
) 
 385   // passing NULL as value to WritePrivateProfileString deletes the key 
 386 //  if ( !Write(szKey, (const char *)NULL) ) 
 388   wxConfigPathChanger 
path(this, szKey
); 
 389   wxString strKey 
= GetPrivateKeyName(path
.Name()); 
 391 //  if (WritePrivateProfileString(m_strGroup, szKey, 
 392 //                                         (const char*) NULL, m_strLocalFilename) == 0) 
 395   if ( !bGroupIfEmptyAlso 
|| !IsEmpty() ) 
 398   // delete the current group too 
 399   bool bOk 
= FALSE
; // = WritePrivateProfileString(m_strGroup, NULL, 
 400 //                                       NULL, m_strLocalFilename) != 0; 
 403     wxLogLastError("WritePrivateProfileString"); 
 408 bool wxIniConfig::DeleteGroup(const wxString
& szKey
) 
 410   wxConfigPathChanger 
path(this, szKey
); 
 412   // passing NULL as section name to WritePrivateProfileString deletes the 
 413   // whole section according to the docs 
 414   bool bOk 
= FALSE
; // = WritePrivateProfileString(path.Name(), NULL, 
 415   //                                     NULL, m_strLocalFilename) != 0; 
 418     wxLogLastError("WritePrivateProfileString"); 
 427 bool wxIniConfig::DeleteAll() 
 429   // first delete our group in win.ini 
 430 //  WriteProfileString(GetVendorName(), NULL, NULL); 
 432   // then delete our own ini file 
 433   char szBuf
[MAX_PATH
]; 
 434   size_t nRc 
= 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf)); 
 437     wxLogLastError("GetWindowsDirectory"); 
 439   else if ( nRc 
> WXSIZEOF(szBuf
) ) 
 441     wxFAIL_MSG("buffer is too small for Windows directory."); 
 444   wxString strFile 
= szBuf
; 
 445   strFile 
<< '\\' << m_strLocalFilename
; 
 447   if ( !wxRemoveFile(strFile
) ) { 
 448     wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str()); 
 455 bool wxIniConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
) 
 461 bool wxIniConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
) 
 467 #endif //wxUSE_CONFIG