I've been continuing to make improvements to the API over the past few days and have just made a 2.3.1 release.
The main focus of this release has been a redesign of the header API which has been nagging at me for quite a while now. To fix it, I've done the following:
- Renamed GMimeHeader to GMimeHeaderList, GMimeHeader felt more like it should be a single header (name/value pair).
- Introduced a new GMimeHeader, altho it is internal only at the moment. I had originally planned on making it public (hence the rename of the old GMimeHeader struct).
- Introduced a GMimeHeaderIter which can be used to iterate forward and backward over the headers, allowing you to change header values (but not names). It also allows removal of headers which makes it possible to remove any header you want, whereas the old API would only let you remove a header by name (which was a problem if there was more than 1 by the same name).
- Got rid of g_mime_header_foreach() because that API sucked and has been replaced by the far more useful GMimeHeaderIter API.
The GMimeHeaderIter API looks like this:
GMimeHeaderIter *g_mime_header_iter_copy (GMimeHeaderIter *iter); void g_mime_header_iter_free (GMimeHeaderIter *iter); bool g_mime_header_iter_equal (GMimeHeaderIter *iter1, GMimeHeaderIter *iter2); bool g_mime_header_iter_is_valid (GMimeHeaderIter *iter); bool g_mime_header_iter_first (GMimeHeaderIter *iter); bool g_mime_header_iter_last (GMimeHeaderIter *iter); bool g_mime_header_iter_next (GMimeHeaderIter *iter); bool g_mime_header_iter_prev (GMimeHeaderIter *iter); gint64 g_mime_header_iter_get_offset (GMimeHeaderIter *iter); const char *g_mime_header_iter_get_name (GMimeHeaderIter *iter); bool g_mime_header_iter_set_value (GMimeHeaderIter *iter, const char *value); const char *g_mime_header_iter_get_value (GMimeHeaderIter *iter); /* if the iter is valid, removes the current header and advances to the next - returns TRUE on success or FALSE otherwise */ bool g_mime_header_iter_remove (GMimeHeaderIter *iter);
Currently, the way to get a GMimeHeaderIter is to call g_mime_header_list_get_iter() which returns a newly allocated iter. I'm not sure if this is the best API or not tho... some other thoughts I've had are:
GMimeHeaderIter *g_mime_header_iter_new (void); bool g_mime_header_list_get_iter (GMimeHeaderList *list, GMimeHeaderIter *iter);
The second function would initialize iter and return TRUE, or, if list was empty, it could return FALSE.
Another option would be to just have:
GMimeHeaderIter *g_mime_header_iter_new (GMimeHeaderList *list);
Then, if list is empty you'd just get back an invalidated iter. If, later, headers were added to list, then perhaps the iter could auto-magically become valid again. This would more-or-less work the same as the current code - except that the way to instantiate a GMimeHeaderIter is different.
To be honest, now that I've written these 2 ideas down, I think I prefer the first idea.
So... iterators. Since you can have multiple iterators for the same GMimeHeaderList, it is important to invalidate them on at least two occasions:
- when the GMimeHeaderList is destroyed
- when the header that an iter points to is removed; either by a call to g_mime_header_list_remove() or via a call to g_mime_header_iter_remove() on another iter instance.
You'll be pleased to note that only iters that reference a header that got removed are invalidated. This fact seems to be an argument in favor of my first idea above, as it would allow re-use of iters once they get invalidated.