Get string from AtomFeed
[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   GList *entries;
29 };
30
31 AtomFeed *
32 atom_feed_new (void)
33 {
34   AtomFeed *feed;
35   feed = g_slice_new (AtomFeed);
36   feed->entries = NULL;
37   return feed;
38 }
39
40 void
41 atom_feed_delete (AtomFeed *feed)
42 {
43   GList *l;
44   for (l = g_list_first (feed->entries); l != NULL; l = l->next)
45     {
46       atom_entry_delete (l->data);
47     }
48   g_slice_free (AtomFeed, feed);
49 }
50
51 void
52 atom_feed_entry_append (AtomFeed *feed, AtomEntry *entry)
53 {
54   feed->entries = g_list_prepend (feed->entries, entry);
55 }
56
57 void
58 atom_feed_entry_append_array (AtomFeed *feed, AtomEntry **entries, size_t len)
59 {
60   int i;
61   for (i = 0; i < len; i++)
62     {
63       feed->entries = g_list_prepend (feed->entries, entries[i]);
64     }
65 }
66
67 xmlNodePtr
68 atom_feed_to_xmlnode (AtomFeed *feed)
69 {
70   xmlNodePtr node;
71   xmlNodePtr entry;
72   GList *l;
73   node = xmlNewNode (NULL, "feed");
74   for (l = g_list_last (feed->entries); l != NULL; l = l->prev)
75     {
76       entry = atom_entry_to_xmlnode (l->data);
77       xmlAddChild (node, entry);
78     }
79   return node;
80 }
81
82 void
83 atom_feed_string (AtomFeed *feed, char **buffer, size_t *len)
84 {
85   xmlDocPtr doc;
86   xmlNodePtr node;
87   doc = xmlNewDoc ("1.0");
88   node = atom_feed_to_xmlnode (feed);
89   xmlNewNs (node, ATOM_NAMESPACE, NULL);
90   xmlDocSetRootElement (doc, node);
91   xmlDocDumpMemory (doc, buffer, len);
92   xmlFreeDoc (doc);
93 }