1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/**********************************************************************
Audacity: A Digital Audio Editor
FileConfig.cpp
Leland Lucius
**********************************************************************/
#include <errno.h>
#include <wx/wfstream.h>
#include "FileConfig.h"
#include <cerrno> // for ENOENT
#if !defined(F_OK)
#define F_OK 0x00
#endif
#if !defined(W_OK)
#define W_OK 0x02
#endif
#if !defined(R_OK)
#define R_OK 0x04
#endif
FileConfig::FileConfig(const wxString& appName,
const wxString& vendorName,
const wxString& localFilename,
const wxString& globalFilename,
long style,
const wxMBConv& conv)
: wxConfigBase(appName, vendorName, localFilename, globalFilename, style),
mAppName(appName),
mVendorName(vendorName),
mLocalFilename(localFilename),
mGlobalFilename(globalFilename),
mStyle(style),
mConv(conv.Clone()),//pass to Init() instead?
mDirty(false)
{
}
void FileConfig::Init()
{
while (true)
{
mConfig = std::make_unique<wxFileConfig>
(mAppName, mVendorName, mLocalFilename, mGlobalFilename, mStyle, *mConv);
// Prevent wxFileConfig from attempting a Flush() during object deletion. This happens
// because we don't use the wxFileConfig::Flush() method and so the wxFileConfig dirty
// flag never gets reset. During deletion, the dirty flag is checked and a Flush()
// performed. This can (and probably will) create bogus temporary files.
mConfig->DisableAutoSave();
bool canRead = false;
bool canWrite = false;
int fd;
fd = wxOpen(mLocalFilename, O_RDONLY, S_IREAD);
if (fd != -1 || errno == ENOENT)
{
canRead = true;
if (fd != -1)
{
wxClose(fd);
}
}
fd = wxOpen(mLocalFilename, O_WRONLY | O_CREAT, S_IWRITE);
if (fd != -1)
{
canWrite = true;
wxClose(fd);
}
if (canRead && canWrite)
{
break;
}
Warn();
}
}
FileConfig::~FileConfig()
{
wxASSERT(mDirty == false);
}
void FileConfig::SetPath(const wxString& strPath)
{
mConfig->SetPath(strPath);
}
const wxString& FileConfig::GetPath() const
{
return mConfig->GetPath();
}
bool FileConfig::GetFirstGroup(wxString& str, long& lIndex) const
{
return mConfig->GetFirstGroup(str, lIndex);
}
bool FileConfig::GetNextGroup(wxString& str, long& lIndex) const
{
return mConfig->GetNextGroup(str, lIndex);
}
bool FileConfig::GetFirstEntry(wxString& str, long& lIndex) const
{
return mConfig->GetFirstEntry(str, lIndex);
}
bool FileConfig::GetNextEntry(wxString& str, long& lIndex) const
{
return mConfig->GetNextEntry(str, lIndex);
}
size_t FileConfig::GetNumberOfEntries(bool bRecursive) const
{
return mConfig->GetNumberOfEntries(bRecursive);
}
size_t FileConfig::GetNumberOfGroups(bool bRecursive) const
{
return mConfig->GetNumberOfGroups(bRecursive);
}
bool FileConfig::HasGroup(const wxString& strName) const
{
return mConfig->HasGroup(strName);
}
bool FileConfig::HasEntry(const wxString& strName) const
{
return mConfig->HasEntry(strName);
}
bool FileConfig::Flush(bool WXUNUSED(bCurrentOnly))
{
if (!mDirty)
{
return true;
}
while (true)
{
FilePath backup = mLocalFilename + ".bkp";
if (!wxFileExists(backup) || (wxRemove(backup) == 0))
{
if (!wxFileExists(mLocalFilename) || (wxRename(mLocalFilename, backup) == 0))
{
wxFileOutputStream stream(mLocalFilename);
if (stream.IsOk())
{
if (mConfig->Save(stream))
{
stream.Sync();
if (stream.IsOk() && stream.Close())
{
if (!wxFileExists(backup) || (wxRemove(backup) == 0))
{
mDirty = false;
return true;
}
}
}
}
if (wxFileExists(backup))
{
wxRemove(mLocalFilename);
wxRename(backup, mLocalFilename);
}
}
}
Warn();
}
return false;
}
bool FileConfig::RenameEntry(const wxString& oldName, const wxString& newName)
{
auto res = mConfig->RenameEntry(oldName, newName);
if (res)
{
mDirty = true;
}
return res;
}
bool FileConfig::RenameGroup(const wxString& oldName, const wxString& newName)
{
auto res = mConfig->RenameGroup(oldName, newName);
if (res)
{
mDirty = true;
}
return res;
}
bool FileConfig::DeleteEntry(const wxString& key, bool bDeleteGroupIfEmpty)
{
auto res = mConfig->DeleteEntry(key, bDeleteGroupIfEmpty);
if (res)
{
mDirty = true;
}
return res;
}
bool FileConfig::DeleteGroup(const wxString& key)
{
auto res = mConfig->DeleteGroup(key);
if (res)
{
mDirty = true;
}
return res;
}
bool FileConfig::DeleteAll()
{
auto res = mConfig->DeleteAll();
if (res)
{
mDirty = true;
}
return res;
}
bool FileConfig::DoReadString(const wxString& key, wxString *pStr) const
{
return mConfig->Read(key, pStr);
}
bool FileConfig::DoReadLong(const wxString& key, long *pl) const
{
return mConfig->Read(key, pl);
}
#if wxUSE_BASE64
bool FileConfig::DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const
{
return mConfig->Read(key, buf);
}
#endif // wxUSE_BASE64
bool FileConfig::DoWriteString(const wxString& key, const wxString& szValue)
{
bool res = mConfig->Write(key, szValue);
if (res)
{
mDirty = true;
}
return res;
}
bool FileConfig::DoWriteLong(const wxString& key, long lValue)
{
bool res = mConfig->Write(key, lValue);
if (res)
{
mDirty = true;
}
return res;
}
#if wxUSE_BASE64
bool FileConfig::DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf)
{
bool res = mConfig->Write(key, buf);
if (res)
{
mDirty = true;
}
return res;
}
#endif // wxUSE_BASE64
void FileConfig::Warn()
{
}
|