summaryrefslogtreecommitdiff
path: root/src/yelp-page.c
blob: 00fce62dce990995c69b0674298c74c4a1d8dae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2006 Brent Smith  <gnome@nextreality.net>
 * Copyright (C) 2007 Shaun McCance  <shaunm@gnome.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Brent Smith  <gnome@nextreality.net>
 *          Shaun McCance  <shaunm@gnome.org>
 */

#include <glib.h>
#include <string.h>

#include "yelp-page.h"

static GIOStatus   page_read_string  (YelpPage    *page,
				      gchar       *buffer,
				      gsize        count,
				      gsize       *bytes_read,
				      YelpError  **error);
static GIOStatus   page_read_file    (YelpPage    *page,
				      gchar       *buffer,
				      gsize        count,
				      gsize       *bytes_read,
				      YelpError  **error);

YelpPage *
yelp_page_new_string (YelpDocument  *document,
		      gchar         *id,
		      const gchar   *content,
		      YelpPageMime   mime)
{
    YelpPage *page;

    page = g_slice_new0 (YelpPage);

    page->mime = mime;

    if (document)
	page->document = g_object_ref (document);
    page->source = YELP_PAGE_SOURCE_STRING;
    page->id = g_strdup (id);

    page->content = (gchar *) content;
    page->content_len = strlen (content);

    return page;
}

GIOStatus
yelp_page_read (YelpPage    *page,
		gchar       *buffer,
		gsize        count,
		gsize       *bytes_read,
		YelpError  **error)
{
    /* FIXME: set error */
    g_return_val_if_fail (page != NULL, G_IO_STATUS_ERROR);

    if (page->source == YELP_PAGE_SOURCE_STRING)
	return page_read_string (page, buffer, count, bytes_read, error);
    else
	return page_read_file (page, buffer, count, bytes_read, error);
}

static GIOStatus
page_read_string (YelpPage    *page,
		  gchar       *buffer,
		  gsize        count,
		  gsize       *bytes_read,
		  YelpError  **error)
{
    g_return_val_if_fail (page != NULL, G_IO_STATUS_ERROR);


    if (page->content_offset == page->content_len) {
	/* FIXME: set the error */
	return G_IO_STATUS_EOF;
    }
    else if (page->content_offset > page->content_len) {
	/* FIXME: set the error */
	return G_IO_STATUS_ERROR;
    }
    else if (page->content_offset + count <= page->content_len) {
	strncpy (buffer, page->content + page->content_offset, count);
	page->content_offset += count;
	*bytes_read = count;
	return G_IO_STATUS_NORMAL;
    }
    else {
	strcpy (buffer, page->content + page->content_offset);
	*bytes_read = strlen (buffer);
	page->content_offset += *bytes_read;
	return G_IO_STATUS_NORMAL;
    }
}

static GIOStatus
page_read_file (YelpPage    *page,
		gchar       *buffer,
		gsize        count,
		gsize       *bytes_read,
		YelpError  **error)
{
    g_return_val_if_fail (page != NULL, G_IO_STATUS_ERROR);
    /* FIXME: just use yelp-io-channel? */
    return G_IO_STATUS_ERROR;
}

void
yelp_page_free (YelpPage *page)
{
    g_return_if_fail (page != NULL);

    if (page->document) {
	yelp_document_release_page (page->document, page);
	g_object_unref (page->document);
    }

    if (page->title)
	g_free (page->title);

    if (page->id)
	g_free (page->id);
    if (page->prev_id)
	g_free (page->prev_id);
    if (page->next_id)
	g_free (page->next_id);
    if (page->up_id)
	g_free (page->up_id);
    if (page->root_id)
	g_free (page->root_id);

    g_slice_free (YelpPage, page);
}