Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
fopen.h
Go to the documentation of this file.
1/*
2 * fopen.h
3 *
4 * adapted from https://curl.haxx.se/libcurl/c/fopen.html.
5 *
6 *
7 * Modifications to read/write wide character streams by
8 * Simon Brooke.
9 *
10 * NOTE THAT: for my purposes, I'm only interested in wide characters,
11 * and I always read them one character at a time.
12 *
13 * Copyright (c) 2003, 2017 Simtec Electronics
14 * Some portions (c) 2019 Simon Brooke <simon@journeyman.cc>
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * This example requires libcurl 7.9.7 or later.
39 */
40
41#ifndef __fopen_h
42#define __fopen_h
43#include <curl/curl.h>
44/*
45 * wide characters
46 */
47#include <wchar.h>
48#include <wctype.h>
49
50#define url_fwprintf(f, ...) ((f->type = CFTYPE_FILE) ? fwprintf( f->handle.file, __VA_ARGS__) : -1)
51#define url_fputws(ws, f) ((f->type = CFTYPE_FILE) ? fputws(ws, f->handle.file) : 0)
52#define url_fputwc(wc, f) ((f->type = CFTYPE_FILE) ? fputwc(wc, f->handle.file) : 0)
53
59
60struct fcurl_data {
61 enum fcurl_type_e type; /* type of handle */
62 union {
63 CURL *curl;
64 FILE *file;
65 } handle; /* handle */
66
67 char *buffer; /* buffer to store cached data */
68 size_t buffer_len; /* currently allocated buffer's length */
69 size_t buffer_pos; /* cursor into in buffer */
70 int still_running; /* Is background url fetch still in progress */
71};
72
73typedef struct fcurl_data URL_FILE;
74
75/* exported functions */
76URL_FILE *url_fopen( const char *url, const char *operation );
77int url_fclose( URL_FILE * file );
78int url_feof( URL_FILE * file );
79size_t url_fread( void *ptr, size_t size, size_t nmemb, URL_FILE * file );
80char *url_fgets( char *ptr, size_t size, URL_FILE * file );
81void url_rewind( URL_FILE * file );
82
83#endif
int still_running
Definition fopen.h:70
void url_rewind(URL_FILE *file)
Definition fopen.c:393
int url_feof(URL_FILE *file)
Definition fopen.c:286
URL_FILE * url_fopen(const char *url, const char *operation)
Definition fopen.c:202
enum fcurl_type_e type
Definition fopen.h:61
fcurl_type_e
Definition fopen.h:54
@ CFTYPE_CURL
Definition fopen.h:57
@ CFTYPE_FILE
Definition fopen.h:56
@ CFTYPE_NONE
Definition fopen.h:55
char * url_fgets(char *ptr, size_t size, URL_FILE *file)
Definition fopen.c:346
union fcurl_data::@0 handle
size_t buffer_len
Definition fopen.h:68
size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
Definition fopen.c:307
size_t buffer_pos
Definition fopen.h:69
int url_fclose(URL_FILE *file)
Definition fopen.c:258
char * buffer
Definition fopen.h:67