.plist management

Hi community,

Does somebody give me a short introduction on how to create, modify, etc. .plist files on OS X?

A good tutorial link would be also appreciated.

Thank you in advance!

Gabriel

Hello,

I did such a class to manage plist back in 2009. (You’ll find the code bellow :-))
That said, I’ve not used it for quite some times (now using Juce’s property file, so it’s always up to date with latest git and Cross platform) and you may need to edit here and there to make it work according to your needs.
If I had to redo it now, I may do it differently, but at least it worked and it should give you some ideas…

Salvator

[code]/**

  • plistManager.h
  • Created by TriTone Digital on 21/09/09.
  • Copyright 2009 EDITIONS IHS. All rights reserved.

@todo : check if file exist before reading/writing
@use :
//declare an object :
plistManager plists;

// add some key entry
plists.addKey(“key11”, “val1”);
plists.addKey(“anotherkey”, “another val”);

//read some keys from name
plists.getKey(“key4”);

plists.readFile("~/Library/Preferences/com.yourCompany.yourProduct.plist");
plists.writeFile("~/Library/Preferences/com.yourCompany.yourProduct.plist");
*/

//------------------------------------------------------------

#ifndef plistManager_H
#define plistManager_H
class plistManager
{
public:
ScopedPointer plist;
int whichEntry;
String plistFilePath;

public:
//------------------------------------------------------------
plistManager ()
{
plist = new XmlElement (“dict”);
}

//------------------------------------------------------------	
~plistManager()
{

// if (plist !=nullptr)
// delete plist;
}
//------------------------------------------------------------

//------------------------------------------------------------	
void writeFile (String path_)

{
	File myxml (path_);
    
    //		print (String(" getTagName  : "),plist->getTagName ());
    //		print (String(" getNumChildElements  : "),plist->getNumChildElements ());
    
	plist->writeToFile(myxml,"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
}

//------------------------------------------------------------	
bool readFile (String path_)

{	
    plistFilePath = path_;
	File myxml (path_);
    
	if (myxml.exists())
	{	XmlDocument document (myxml);
		ScopedPointer<XmlElement> temp;
        temp = document.getDocumentElement();
        if (temp->hasTagName ("dict"))
        {
            plist = new XmlElement (*temp);
            return true;
        }
        
        else
        {
            plist = new XmlElement (*temp->getChildByName ("dict"));
            return true;
        }
        
            return false;            

// print (String(" getTagName : “),plist->getTagName ());
// print (String(” getNumChildElements : "),plist->getNumChildElements ());
}
}
//------------------------------------------------------------
bool isValid ()
{
if (plist !=nullptr) {
return true;
}
else
return false;
}

//------------------------------------------------------------	
void addKey (String name_, String value_)
{	//printf("addKey\n");
	
	ScopedPointer<XmlElement>  name;
    name = new XmlElement ("key"); 
	name->addTextElement(name_);
	plist->addChildElement (name); 	
    
	
	ScopedPointer<XmlElement> value;
    value = new XmlElement ("string"); 
	value->addTextElement(value_);
	plist->addChildElement (value); 	
    
}
//----------------------
void addKey (String name_, int value_)
{	//printf("addKey\n");
	
	ScopedPointer<XmlElement>  name;
    name = new XmlElement ("key"); 
	name->addTextElement(name_);
	plist->addChildElement (name); 	
	
	
	ScopedPointer<XmlElement> value;
    value = new XmlElement ("integer"); 
	value->addTextElement(String (value_));
	plist->addChildElement (value); 	
	
}

//------------------------------------------------------------	
String getKey (int whichEntry_)
{	//printf("addKey\n");
	
	if (whichEntry_ < plist->getNumChildElements()/2-2)
	{String data = plist->getChildElement (2*whichEntry_+1)->getAllSubText();			//whichEntry 1 = KEY , so *2+1
        return data;
	}
    return String::empty;
}
//------------------------------------------------------------		
String getKey (String name_)
{	//printf("getKey string\n");
	for (int i=0; i<plist->getNumChildElements();i++)
	{	// print(i);
        //			int which =0;
        //			print (plist->getChildElement (i)->getAllSubText ());
		if (name_==	plist->getChildElement (i)->getAllSubText ())
            return plist->getChildElement (i+1)->getAllSubText ();
	}
	
	return String::empty ;
}

//------------------------------------------------------------	

};
#endif
[/code]

Hi Salvator,

Thank you so much for take your time in searching and put it in the forum. I apreciate it! Take you into account that you have saved me a lot of time.

All my best

Gabriel

Thanks for this code!

There's a bug however:

In AddKey(), you should remove the ScopedPointers and use XmlElement pointers, otherwise you're going to have bad memory accesses.

The main plist pointer should remain a ScopedPointer though.