Use an array instead of a list for the entries in a feed
[cascardo/atompub.git] / atom / feed.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <atompub/atom.h>
21 #include <atompub/atom-xml.h>
22
23 #include <glib.h>
24 #include <libxml/tree.h>
25
26 struct _atom_feed
27 {
28   GPtrArray *entries;
29 };
30
31 AtomFeed *
32 atom_feed_new (void)
33 {
34   AtomFeed *feed;
35   feed = g_slice_new (AtomFeed);
36   feed->entries = g_ptr_array_new ();;
37   return feed;
38 }
39
40 void
41 atom_feed_delete (AtomFeed *feed)
42 {
43   int i;
44   for (i = 0; i < feed->entries->len; i++)
45     {
46       atom_entry_delete (g_ptr_array_index (feed->entries, i));
47     }
48   g_ptr_array_free (feed->entries, TRUE);
49   g_slice_free (AtomFeed, feed);
50 }
51
52 void
53 atom_feed_entry_append (AtomFeed *feed, AtomEntry *entry)
54 {
55   g_ptr_array_add (feed->entries, entry);
56 }
57
58 void
59 atom_feed_entry_append_array (AtomFeed *feed, AtomEntry **entries, size_t len)
60 {
61   int i;
62   for (i = 0; i < len; i++)
63     {
64       g_ptr_array_add (feed->entries, entries[i]);
65     }
66 }
67
68 xmlNodePtr
69 atom_feed_to_xmlnode (AtomFeed *feed)
70 {
71   xmlNodePtr node;
72   xmlNodePtr entry;
73   int i;
74   node = xmlNewNode (NULL, "feed");
75   xmlNewNs (node, ATOM_NAMESPACE, NULL);
76   for (i = feed->entries->len - 1; i >= 0; i--)
77     {
78       entry = atom_entry_to_xmlnode (g_ptr_array_index (feed->entries, i));
79       xmlAddChild (node, entry);
80     }
81   return node;
82 }
83
84 void
85 atom_feed_string (AtomFeed *feed, char **buffer, size_t *len)
86 {
87   xmlDocPtr doc;
88   xmlNodePtr node;
89   doc = xmlNewDoc ("1.0");
90   node = atom_feed_to_xmlnode (feed);
91   xmlDocSetRootElement (doc, node);
92   xmlDocDumpMemory (doc, buffer, len);
93   xmlFreeDoc (doc);
94 }