source: gaycko/trunk/src/io/io.c @ 4748

Last change on this file since 4748 was 4748, checked in by Jean-Yves Lamoureux, 9 years ago
  • Added copyright notices
File size: 2.0 KB
Line 
1/*
2 *  Gaycko        Text mode web browser
3 *  Copyright (c) 2011 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12#include "io.h"
13#include <stdlib.h>
14#include <string.h>
15
16
17gIO *gaycko_open(char *uri) {
18        gIO *ret = NULL;   
19    //TODO handle URI scheme
20   
21    ret = malloc(sizeof(gIO));
22   
23    if(strlen(uri)>7 && !strncmp(uri, "http://", 7)) {
24        ret->type = IO_HTTP;
25    } else {
26        ret->type = IO_FILE;
27    }
28   
29retry:
30    switch(ret->type) {
31        default:
32            ret->stream.http = http_open(uri);
33            if(!ret->stream.http) {
34                free(ret);
35                return NULL;
36            }
37            break;
38        case IO_FILE:
39            ret->stream.file = file_open(uri);
40            if(!ret->stream.file) {
41                ret->type = IO_HTTP;
42                goto retry;
43                return NULL;
44            }
45            break;
46    }
47   
48    return ret;
49}
50
51void gaycko_close(gIO *io) {
52        if(!io) return;
53   
54    switch(io->type) {
55        default:
56            http_close(io->stream.http);
57            break;
58        case IO_FILE:
59            file_close(io->stream.file);
60            break;
61    }
62    free(io);
63   
64}
65
66char *gaycko_get_data(gIO *io) {
67        if(!io) return NULL;
68   
69    switch(io->type) {
70        default:
71            return io->stream.http->data;
72            break;
73        case IO_FILE:
74            return io->stream.file->data;
75            break;
76    }
77}
78
79unsigned int gaycko_get_size(gIO *io) {
80    if(!io) return 0;
81   
82    switch(io->type) {
83        default:
84            return io->stream.http->size;
85            break;
86        case IO_FILE:
87            return io->stream.file->size;
88            break;
89    }
90   
91}
Note: See TracBrowser for help on using the repository browser.