Exemplo n.º 1
0
/**
 * news_parser_buffered_fill_cb:
 * @object: (in): A #GBufferedInputStream.
 * @result: (in): A #GAsyncResult.
 * @user_data: (in): A #NewsParser.
 *
 * Completes an asynchronous request to fill the buffer of a
 * #GBufferedInputStream. If the buffer was successfully filled, then
 * the feed format will be discovered and the stream parsed.
 *
 * Returns: None.
 */
static void
news_parser_buffered_fill_cb (GObject      *object,
                              GAsyncResult *result,
                              gpointer      user_data)
{
   GBufferedInputStream *buffered = (GBufferedInputStream *)object;
   NewsParserPrivate *priv;
   NewsParser *parser = user_data;
   NewsFormat *format;
   guint8 buffer[BUFFER_FILL_SIZE];
   gsize length = 0;
   GType format_type = G_TYPE_INVALID;

   ENTRY;

   g_assert(G_IS_BUFFERED_INPUT_STREAM(buffered));
   g_assert(G_IS_ASYNC_RESULT(result));
   g_assert(NEWS_IS_PARSER(parser));

   priv = parser->priv;

   length = g_buffered_input_stream_peek(buffered, buffer, 0, sizeof buffer);

   if (length <= 0) {
      g_simple_async_result_set_error(priv->simple, NEWS_PARSER_ERROR,
                                      NEWS_PARSER_ERROR_NO_DATA,
                                      _("No data was available to parse."));
      g_simple_async_result_complete_in_idle(priv->simple);
   } else {
      format_type = news_parser_discover_format(parser, buffer, length);
      if (format_type) {
         format = g_object_new(format_type,
                               "adapter", priv->adapter,
                          "source", priv->source,
                               NULL);
         news_format_read_async(format, G_INPUT_STREAM(buffered),
                                priv->cancellable,
                                news_parser_format_read_cb,
                                g_object_ref(parser));
         g_object_unref(format);
      } else {
         g_simple_async_result_set_error(priv->simple, NEWS_PARSER_ERROR,
                                         NEWS_PARSER_ERROR_NO_FORMAT,
                                         _("Failed to discover format."));
         g_simple_async_result_complete_in_idle(priv->simple);
#if 0
         {
            gchar *uri;
            g_object_get(priv->source, "uri", &uri, NULL);
            g_printerr("%s\n", uri);
            g_free(uri);
         }
#endif
      }
   }

   g_object_unref(parser);

   EXIT;
}
Exemplo n.º 2
0
static VALUE
rg_peek(VALUE self, VALUE rboffset, VALUE rbcount)
{
        gsize offset = RVAL2GSIZE(rboffset);
        gsize count = RVAL2GSIZE(rbcount);
        VALUE result = rb_str_new(NULL, count);
        gsize bytes_peeked = g_buffered_input_stream_peek(_SELF(self),
                                                          RSTRING_PTR(result),
                                                          offset,
                                                          count);

        rb_str_set_len(result, bytes_peeked);
        rb_str_resize(result, bytes_peeked);
        OBJ_TAINT(result);

        return result;
}