30 namespace PropertyFileConstants
32 JUCE_CONSTEXPR
static const int magicNumber = (int)
ByteOrder::makeInt (
'P',
'R',
'O',
'P');
33 JUCE_CONSTEXPR
static const int magicNumberCompressed = (int)
ByteOrder::makeInt (
'C',
'P',
'R',
'P');
35 JUCE_CONSTEXPR
static const char*
const fileTag =
"PROPERTIES";
36 JUCE_CONSTEXPR
static const char*
const valueTag =
"VALUE";
37 JUCE_CONSTEXPR
static const char*
const nameAttribute =
"name";
38 JUCE_CONSTEXPR
static const char*
const valueAttribute =
"val";
43 : commonToAllUsers (false),
44 ignoreCaseOfKeyNames (false),
46 millisecondsBeforeSaving (3000),
57 #if JUCE_MAC || JUCE_IOS 93 #elif JUCE_LINUX || JUCE_ANDROID 118 file (f), options (o)
125 file (o.getDefaultFile()), options (o)
132 ProcessScopedLock pl (createProcessLock());
134 if (pl !=
nullptr && ! pl->isLocked())
137 loadedOk = (! file.
exists()) || loadAsBinary() || loadAsXml();
154 return (! needsWriting) ||
save();
166 needsWriting = needsToBeSaved_;
184 return saveAsBinary();
187 bool PropertiesFile::loadAsXml()
189 if (
auto doc = parseXMLIfTagMatches (file, PropertyFileConstants::fileTag))
191 forEachXmlChildElementWithTagName (*doc, e, PropertyFileConstants::valueTag)
193 auto name = e->getStringAttribute (PropertyFileConstants::nameAttribute);
195 if (name.isNotEmpty())
197 e->getFirstChildElement() !=
nullptr 199 : e->getStringAttribute (PropertyFileConstants::valueAttribute));
208 bool PropertiesFile::saveAsXml()
210 XmlElement doc (PropertyFileConstants::fileTag);
213 for (
int i = 0; i < props.size(); ++i)
216 e->
setAttribute (PropertyFileConstants::nameAttribute, props.getAllKeys() [i]);
219 if (
auto childElement = parseXML (props.getAllValues() [i]))
220 e->addChildElement (childElement.release());
222 e->setAttribute (PropertyFileConstants::valueAttribute, props.getAllValues() [i]);
225 ProcessScopedLock pl (createProcessLock());
227 if (pl !=
nullptr && ! pl->isLocked())
232 needsWriting =
false;
239 bool PropertiesFile::loadAsBinary()
245 auto magicNumber = fileStream.
readInt();
247 if (magicNumber == PropertyFileConstants::magicNumberCompressed)
251 return loadAsBinary (gzip);
254 if (magicNumber == PropertyFileConstants::magicNumber)
255 return loadAsBinary (fileStream);
261 bool PropertiesFile::loadAsBinary (
InputStream& input)
271 jassert (key.isNotEmpty());
273 if (key.isNotEmpty())
280 bool PropertiesFile::saveAsBinary()
282 ProcessScopedLock pl (createProcessLock());
284 if (pl !=
nullptr && ! pl->isLocked())
292 if (! out.openedOk())
297 out.
writeInt (PropertyFileConstants::magicNumberCompressed);
302 if (! writeToStream (zipped))
310 out.writeInt (PropertyFileConstants::magicNumber);
312 if (! writeToStream (out))
320 needsWriting =
false;
327 auto& keys = props.getAllKeys();
328 auto& values = props.getAllValues();
329 auto numProperties = props.size();
334 for (
int i = 0; i < numProperties; ++i)
343 void PropertiesFile::timerCallback()
void startTimer(int intervalInMilliseconds) noexcept
StringPairArray & getAllProperties() noexcept
String osxLibrarySubFolder
void stopTimer() noexcept
bool overwriteTargetFileWithTemporary() const
bool isNotEmpty() const noexcept
static JUCE_CONSTEXPR uint16 makeInt(uint8 leastSig, uint8 mostSig) noexcept
const File & getFile() const noexcept
~PropertiesFile() override
File getParentDirectory() const
Result createDirectory() const
void writeTo(OutputStream &output, const TextFormat &format={}) const
InterProcessLock * processLock
StorageFormat storageFormat
File getChildFile(StringRef relativeOrAbsolutePath) const
PropertiesFile(const Options &options)
static String createLegalFileName(const String &fileNameToFix)
XmlElement * createNewChildElement(StringRef tagName)
int millisecondsBeforeSaving
File withFileExtension(StringRef newExtension) const
void propertyChanged() override
void setAttribute(const Identifier &attributeName, const String &newValue)
void setNeedsToBeSaved(bool needsToBeSaved)
bool ignoreCaseOfKeyNames
bool needsToBeSaved() const
void set(const String &key, const String &value)
bool startsWithChar(juce_wchar character) const noexcept
File getDefaultFile() const
virtual bool writeInt(int value)
virtual bool writeString(const String &text)
static File JUCE_CALLTYPE getSpecialLocation(const SpecialLocationType type)
const CriticalSection & getLock() const noexcept
bool startsWith(StringRef text) const noexcept