plover/fileio.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Jun 14 12:39:53 2016 +0100 (2016-06-14)
changeset 41 bd50a4b7ab68
child 42 419a02fa70db
permissions -rw-r--r--
First tentative step in using razor_file_set_vtable
     1 /*
     2  * Copyright (C) 2016  J. Ali Harlow <ali@juiblex.co.uk>
     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 #include <stdlib.h>
    20 #include <razor.h>
    21 #include <glib.h>
    22 #include <gio/gio.h>
    23 #include "config.h"
    24 #include "plover/plover.h"
    25 
    26 static GList *open_razor_files;
    27 
    28 static void *file_get_contents(const char *uri,size_t *length,int private,
    29   struct razor_error **error)
    30 {
    31     GFile *file;
    32     gchar *path;
    33     void *addr;
    34     char *contents;
    35     gsize len;
    36     GError *err=NULL;
    37     g_message("file_get_contents(%s)",uri);
    38     file=g_file_new_for_uri(uri);
    39     path=g_file_get_path(file);
    40     if (path)
    41     {
    42 	g_object_unref(file);
    43 	addr=razor_file_default_get_contents(path,length,private,error);
    44 	if (addr)
    45 	    open_razor_files=g_list_prepend(open_razor_files,addr);
    46 	g_free(path);
    47     }
    48     else if (!g_file_load_contents(file,NULL,&contents,&len,NULL,&err))
    49     {
    50 	plover_propagate_g_error(error,err);
    51 	g_object_unref(file);
    52 	addr=NULL;
    53     }
    54     else
    55     {
    56 	g_object_unref(file);
    57 	addr=contents;
    58 	if (length)
    59 	    *length=len;
    60     }
    61     return addr;
    62 }
    63 
    64 static int file_free_contents(void *addr,size_t length)
    65 {
    66     int retval;
    67     GList *lnk;
    68     g_message("file_free_contents(%p)",addr);
    69     lnk=g_list_find(open_razor_files,addr);
    70     if (lnk)
    71     {
    72 	open_razor_files=g_list_delete_link(open_razor_files,lnk);
    73 	retval=razor_file_default_free_contents(addr,length);
    74     }
    75     else
    76     {
    77 	g_free(addr);
    78 	retval=0;
    79     }
    80     return retval;
    81 }
    82 
    83 void plover__file_io_init(void)
    84 {
    85     static gsize init=0;
    86     struct razor_file_vtable file_vtable;
    87     g_message("plover__file_io_init()");
    88     if (g_once_init_enter(&init))
    89     {
    90 	file_vtable.get_contents=file_get_contents;
    91 	file_vtable.free_contents=file_free_contents;
    92 	razor_file_set_vtable(&file_vtable);
    93 	g_once_init_leave(&init,1);
    94     }
    95 }