QOF iteration (was QOF won't link)

Derek Atkins warlord at MIT.EDU
Wed Jun 16 23:22:52 EDT 2004


Neil Williams <linux at codehelp.co.uk> writes:

> I was following the QOF example and trying to use the qofsql.c code as I keep 
> getting segmentation faults if I use iterators and callback functions. 
> However, it's looking like that is the only way to get it to work. I find the 
> whole (QofCollection *coll, QofEntityForeachCB cb, gpointer ud) VERY 
> confusing. 

You cannot use the qofsql code -- it's not built in GnuCash.

> Once I've got qof_collection_get_data(coll) how do I read data contained 
> within the object?

Jesus, how many fricking times do I have to explain this?  You don't
need to use a collection, you can just use:

   qof_object_foreach(QOF_TYPE_XXX, book, my_foreach_cb, my_cb_arg);

If you _have_ a collection you could use qof_collection_foreach(),
but you don't need to do so...

> e.g. my current callback doesn't do any data query, it just prints a debug 
> line so that I know it is being executed and then obtains the data type 
> directly from QofCollection. (Not very useful, I know.)
> void gnc_book_merge_foreach (QofCollection *coll, QofEntityForeachCB cb, 
> gpointer ud) {
> 	printf("foreach running \n");
> 	printf("Foreach Coll data type: %s\n",qof_collection_get_type(coll));
> }
>
> foreach running
> Foreach Coll data type: gncInvoice
> foreach running
> Foreach Coll data type: gncInvoice
>
> Whatever I do at that stage to be able to use a get_whatever() call for the 
> gncInvoice-type data results in a seg. fault, so I've obviously got it wrong.
> (I'm calling the foreach using 	
> qof_collection_foreach(coll, (QofEntityForeachCB)gnc_book_merge_foreach, ud);
>>From main())

Well, DUH, gnc_book_merge_foreach doesn't match the QofEntityForeachCB
type!  Of course this is going to segv, you're passing completely
bogus data into your function.

> This maybe isn't ideal (as I understand it) for my needs anyway, it appears to 
> rely too much on knowing the type of data in advance. If this is to be 
> generic, it needs to handle any registered type without major surgery.
>
> When I use the alternative:
> 	qof_object_foreach(GNC_ID_INVOICE, testbook, (QofEntityForeachCB) 
> gnc_book_merge_foreach, ud);
> I get:
> ** CRITICAL **: Error: qof_object_foreach(): No object of type gncInvoice
> and no debug "foreach running" lines.

See above about the function type being incorrect.  Don't cast it.  The
cast it biting you in the ass.

> Using QOF within Gnucash. I want to be able to scroll through the contents of 
> a GNCBook by using QOF to query all registered objects with full tracking of 
> exactly what type of data is being scrolled.

Don't use the QOFSQL code.  It's not built.  It will eventually be
added in, but not yet.

> I'm sorry this is taking so long, you did say it wasn't easy but it is taking 
> more effort than I expected to get under the skin of QOF. You might be 
> relieved to hear that the doxygen formatting and the comparison and collision 
> code is not hitting the same problems, but then until I can throw some real 
> data at the code, I can't test it properly either!

You're thinking about it too hard.  You're using the right apis but you're
not using them correctly.  Write your callback function properly.

-derek

-- 
       Derek Atkins, SB '93 MIT EE, SM '95 MIT Media Laboratory
       Member, MIT Student Information Processing Board  (SIPB)
       URL: http://web.mit.edu/warlord/    PP-ASEL-IA     N1NWH
       warlord at MIT.EDU                        PGP key available


More information about the gnucash-devel mailing list