PrefsObject

This forum is for general developer support questions.
User avatar
mritter0
Posts: 214
Joined: Mon Aug 25, 2014 9:41 pm
Location: Bettendorf, IA, USA

Re: PrefsObject

Post by mritter0 »

Is it possible to read multiple sub-items in an array?

Code: Select all

<array key="Tabs">
	<item key="Tab0">
		<integer>2</integer>
		<string>"dh0:"</string>
	</iem>
	<item key="Tab1">
		<integer>4</integer>
		<string>"dh2:"</string>
	</iem>
</array>

-- or --

<array key="Tabs">
	<item key="Tab0">
		<item key="Location">
			<integer>2</integer>
		</item>
		<item key="Path">
			<string>"dh0:"</string>
		</item>
	</item>
	<item key="Tab1">
		<item key="Location">
			<integer>4</integer>
		</item>
		<item key="Path">
			<string>"dh2:"</string>
		</item>
	</item>
</array>
In like my previous post, not use a loop, but find the tag, go in and find that tag's tags, etc. Not the greatest since will have several extra PrefsObjects for each sub-item class.
Workbench Explorer - A better way to browse drawers
User avatar
mritter0
Posts: 214
Joined: Mon Aug 25, 2014 9:41 pm
Location: Bettendorf, IA, USA

Re: PrefsObject

Post by mritter0 »

What if you have an array of variable length? Have to be able to loop through somehow adding each item, then save it.

Code: Select all

uint32 NumItems=10;    // or any number

....program fills in array with data...

....save it....

for (x=0; x<NumItems; x++)
{
	WindowObj=IPrefsObjects->PrefsArray(NULL,NULL,
		ALPO_Alloc,							0,
		ALPOARR_AddObj,
			IPrefsObjects->PrefsNumber(NULL,NULL,
				ALPONUM_AllocSetLong,				myArray[x],
			TAG_DONE),
	TAG_DONE);
}
IPrefsObjects->DictSetObjectForKey(myPrefsDict,WindowObj,"MyArray");
Workbench Explorer - A better way to browse drawers
User avatar
Cyborg
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 59
Joined: Wed Feb 16, 2011 1:29 pm

Re: PrefsObject

Post by Cyborg »

mritter0 wrote:Is it possible to read multiple sub-items in an array?.
Sure, but your example would be wrong. All Tab# members of the Tabs array would need to be arrays themselves as well to hold multiple child items.
Please contact support@hyperion-entertainment.com rather than trying to send PMs to me. Thanks! :)
User avatar
Cyborg
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 59
Joined: Wed Feb 16, 2011 1:29 pm

Re: PrefsObject

Post by Cyborg »

mritter0 wrote:What if you have an array of variable length? Have to be able to loop through somehow adding each item, then save it.
Of course you can do this, but not with the code you posted, because it is (again) utterly wrong. In the loop you are creating NumItems PrefsArrays, but you are saving them in the very same WindowObj variable, which means that after the loop WindowObj contains only the very last created PrefsArray.. the others are lost in space and only leaking memory.
Please contact support@hyperion-entertainment.com rather than trying to send PMs to me. Thanks! :)
User avatar
mritter0
Posts: 214
Joined: Mon Aug 25, 2014 9:41 pm
Location: Bettendorf, IA, USA

Re: PrefsObject

Post by mritter0 »

So, Cyborg, why don't you post some utterly correct code so I can do it? I know my code is wrong, just supplying a basic example of what I want to do.

PrefsObject code is pretty non-existent. There are no good examples out there other than simple lists of single items. If you have some better examples, please post them.
Workbench Explorer - A better way to browse drawers
User avatar
Cyborg
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 59
Joined: Wed Feb 16, 2011 1:29 pm

Re: PrefsObject

Post by Cyborg »

Huh?

1) I told you in both posts exactly, what is wrong.

2) A "basic" example is in the autodoc for application.library/prefsobjects, node -prefsobjects-. You are asking questions specific to your use-case.

3a) With regard to your first question "multiple sub-items in an array": In order to understand PrefsObjects, you need a basic knowledge about XML, which is beyond the scope of our documentation, because it is an industry standard. There are plenty of sources for that out there in the web. In your example, Tab0 and Tab1 would obviously need to be arrays as well to be able to hold multiple sub-items.

3b) The error in the code of your second post is what I described: you are overwriting WindowObj in any iteration of the loop with a new array pointer containing exactly ONE number, because you don't tell PrefsArray() to which array it is supposed to add the newly created number objects. Admittedly the autodocs for PrefsObjects aren't the best we have, but this is clearly described in them. So your loop would have to look like this:

Code: Select all

...
/* Warning: this code does no error checking */

/* Create the the initially empty array object */
PrefsObject *WindowObj = IPrefsObjects->PrefsArray(NULL, NULL, ALPO_Alloc, 0, TAG_DONE);

for (x = 0; x < NumItems; x++)
{
   /* Access the old WindowObj array (created above for 1st iteration, returned by the loop's PrefsArray()
    * for any following iteration), add a newly created number object to it and save the returned array object
    * for the next iteration, which might or might not be the same as before.
    * That way we are telling PrefsArray() to which array it has to add the newly created numbers.
    */
   WindowObj = IPrefsObjects->PrefsArray(WindowObj, NULL,
      ALPOARR_AddObj,
         IPrefsObjects->PrefsNumber(NULL, NULL,
            ALPONUM_AllocSetLong, myArray[x],
         TAG_DONE),
   TAG_DONE);
}

IPrefsObjects->DictSetObjectForKey(myPrefsDict, WindowObj, "MyArray");
Please contact support@hyperion-entertainment.com rather than trying to send PMs to me. Thanks! :)
User avatar
mritter0
Posts: 214
Joined: Mon Aug 25, 2014 9:41 pm
Location: Bettendorf, IA, USA

Re: PrefsObject

Post by mritter0 »

Thank you, Cyborg. Little code snippets go a long way in clearing things up. Seeing the PrefsArray alloc outside the loop made it make sense. Maybe I am a more visual learner....
Workbench Explorer - A better way to browse drawers
Post Reply