Configuration file solutions


#1

Guys, for Wusik Station I created my own dirty wINI file processor. I needed something that I could add and edit parameters. So I used a simple solution, but from what I see its Windows only, and I don’t think its a good solution. So I wonder if maybe Juce has a better solution for this? A way to edit parameters in a file? Or should I just port this code to Juce instead? Below follows the code:

[code]//------------------------------------------------------------------------
char *WusikMain::IniFileProcess(char *nKey, int aType, char *nSave, float iSave, bool isLong, bool isMainIni, char aFile, int Slot, char Alternate) // W_SAVE, W_READ
{
static char charRINI[2][1028] = {0};
memset(charRINI[Slot],0,sizeof(charRINI[Slot]));

char nKey2[42] = {0};
strcpy(nKey2,nKey);
for (int x=0; x<fabs(39-strlen(nKey)); x++) strcat(nKey2," ");

char Buffer[1026] = {0};
char pathDLL[9024] = {0};
bool Found = false;
long pSize;
DWORD pathSize = sizeof(pathDLL);

if (aFile)
{
	strcpy(pathDLL,WusikMain::getPathOnly(aFile));
	strcat(pathDLL,WusikMain::getFileOnlyNoExt(aFile));
	strcat(pathDLL,".wIni");
	ConvertSlashes(pathDLL);
}
else
{
	if (isMainIni)
	{
		#if WINDOWS
			pSize = GetModuleFileName(hModule,pathDLL, pathSize);
		#endif

		if (Alternate)
		{
			char aC[4096] = {0};
			strcpy(aC,WusikMain::getPathOnly(pathDLL));
			strcat(aC,"\\");
			strcat(aC,Alternate);
			strcat(aC,".wIni");
			strcpy(pathDLL,aC);
			WusikMain::removeDupSlashs(pathDLL);
		}
		else
		{
			int x=strlen(pathDLL);
			pathDLL[x-3] = 'w';
			pathDLL[x-2] = 'I';
			pathDLL[x-1] = 'n';
			pathDLL[x]   = 'i';
			pathDLL[x+1] =  0;
		}
	}
	else
	{
		strcpy(pathDLL,IniFileProcess("Data Path", W_READ, 0, 0, true, true,0,1));
		strcat(pathDLL,"\\Configurations\\");
		char pathDLL2[1024] = {0};
		DWORD pathSize2 = sizeof(pathDLL2);
		#if WINDOWS
			pSize = GetModuleFileName(hModule,pathDLL2, pathSize2);
		#endif
		strcat(pathDLL,getLastDirectory(pathDLL2));
		int x=strlen(pathDLL);
		pathDLL[x-4] = 0;
		pathDLL[x-3] = 0;
		pathDLL[x-2] = 0;
		pathDLL[x-1] = 0;
		pathDLL[x]   = 0;
		pathDLL[x+1] =  0;
		strcat(pathDLL," Extra Config.wIni");
		ConvertSlashes(pathDLL);
	}
}

FILE *fd = 0;
fd = fopen(pathDLL, "r+");

if (!fd)
{
	if (isMainIni) 
	{
		if (!aFile) WusikMain::showMessage(pathDLL,"Fatal Error: missing .wIni file!");
	}
	else
	{
		if (aType == W_SAVE)
		{
			fd = fopen(pathDLL, "a");
			if (!fd) WusikMain::showMessage(pathDLL,"Fatal Error: can't create Extra Config.wIni file!");
			if (fd)
			{
				fwrite("[Configuration File]                   \n",sizeof(char),41,fd);
				fwrite("                                       ",sizeof(char),40,fd);
				fclose(fd);
				fd = fopen(pathDLL, "r+");
			}
		}
	}
}

if (fd)
{
	if (isMainIni)
	{
		fgets(Buffer,1026,fd);
		fgets(Buffer,1026,fd); // Skip first 2 lines
	}

	while (fgets(Buffer,1026,fd))
	{		
		if (!strncmp(nKey2,Buffer,strlen(nKey2))) // Found
		{
			Found = true;
			if (aType == W_READ)
			{
				fgets(Buffer,1026,fd);
				strcpy(charRINI[Slot],Buffer);
				for (int xx=1027; xx>=(strlen(Buffer)-4); xx--) charRINI[Slot][xx] = 0;
				for (INT_DEC xx=1027; xx>=0; xx--) 
				{
					if (charRINI[Slot][xx] != ' ' && charRINI[Slot][xx] != 0) break;
					charRINI[Slot][xx] = 0;
				}
				fclose(fd);
				return charRINI[Slot];
			}
			break;
		}
		else
		{
			fgets(Buffer,1026,fd); // Skip Line //
		}
	}

	if (aType == W_SAVE)
	{
		if (Found)
		{
			fseek(fd,0,SEEK_CUR);
		}
		else
		{
			fclose(fd);
			fd = fopen(pathDLL, "a");
			if (!fd) return 0;

			fwrite("\n",sizeof(char),1,fd);
			fwrite(nKey2,sizeof(char)*39,1,fd);
			fwrite("\n",sizeof(char),1,fd);
		}

		char nSave2[1028] = {0};
		if (nSave)
		{
			strcpy(nSave2,nSave);
		}
		else
		{
			sprintf(nSave2,"%f",iSave);
		}

		int Size = strlen(nSave2);
		fwrite(nSave2,sizeof(char)*Size,1,fd);
		int Max = fabs(((isLong) ? 1023 : 39)-Size);
		for (int x=0; x<Max; x++) { fwrite(" ",sizeof(char),1,fd); }// Clear the rest of the line //
		
		if (fd) fclose(fd);
		return charRINI[Slot];
	}

	if (fd) fclose(fd);
}

if (fd) fclose(fd);

return 0;

}[/code]

Example of reading something:

[code] static char SelectedSkin[1240] = {0};
memset(SelectedSkin,0,sizeof(SelectedSkin));

strcpy(SelectedSkin,WusikMain::IniFileProcess(“Default Skin”,W_READ,0,0,true,true));[/code]


#2

Is PropertiesFile what you’re after:

http://www.rawmaterialsoftware.com/juce/api/classPropertiesFile.html

?


#3

Ahhh, thank you so much, I didn’t see that yet. I guess I should read all classes before I ask something… :oops:

None the less, thanks, it sure helps me out. 8)

Wk


#4

Juce is simple great, I’m loving it. I just got the following working! 8)

[code]ApplicationProperties::getInstance()
->setStorageParameters (T(“Settings”),
T(“xml”), “Wusik Fuse Box”, 1000,
PropertiesFile::storeAsXML);

defaultSettings = new PropertySet();

// Add Default Settings Here //
File fPath = File(File::getSpecialLocation (File::userHomeDirectory));
while (fPath.getParentDirectory() != fPath)  fPath = fPath.getParentDirectory();
defaultSettings->setValue(T("TreeViewDefPath"),fPath.getFullPathName());
defaultSettings->setValue(T("TreeViewBookName0"),fPath.getFullPathName());
defaultSettings->setValue(T("TreeViewBook0"),fPath.getFullPathName());
defaultSettings->setValue(T("TreeViewShowExt"),true);

userSettings->setFallbackPropertySet(defaultSettings);

[/code]

Don’t forget to call

ApplicationProperties::getInstance()->closeFiles();

at the destructor.

Now, to load and save parameters, its very easy:

#define userSettings ApplicationProperties::getInstance()->getUserSettings()

userSettings->getValue

and

userSettings->setValue

I’m also using it to load the skin files:

#define SkinFile(ID) PropertiesFile::getDefaultAppSettingsFile(ID,T("xml"),T("Wusik Fuse Box Skin"),false)
#define SkinPng(ID) PropertiesFile::getDefaultAppSettingsFile(ID,T("png"),T("Wusik Fuse Box Skin"),false)

File dPath(SkinPng(T("Background"))); 
iBackground = ImageFileFormat::loadFrom(dPath);

Wk

Wk


#5

hey williamk

You have explained it well but still I don’t get how exactly you are using PropertiesFile and PropertiesSet to save configuration options … Can you please provide detailed example? Thanks…