xref: /Universal-ctags/main/mio.h (revision ad1e690f6250cb47d5e54c7fbad6fb56f4a56ddb)
1509a47dbSJiří Techet /*
2509a47dbSJiří Techet  *  MIO, an I/O abstraction layer replicating C file I/O API.
3509a47dbSJiří Techet  *  Copyright (C) 2010  Colomban Wendling <ban@herbesfolles.org>
4509a47dbSJiří Techet  *
5509a47dbSJiří Techet  *  This program is free software; you can redistribute it and/or modify
6509a47dbSJiří Techet  *  it under the terms of the GNU General Public License as published by
7509a47dbSJiří Techet  *  the Free Software Foundation; either version 2 of the License, or
8509a47dbSJiří Techet  *  (at your option) any later version.
9509a47dbSJiří Techet  *
10509a47dbSJiří Techet  *  This program is distributed in the hope that it will be useful,
11509a47dbSJiří Techet  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12509a47dbSJiří Techet  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13509a47dbSJiří Techet  *  GNU General Public License for more details.
14509a47dbSJiří Techet  *
15509a47dbSJiří Techet  *  You should have received a copy of the GNU General Public License along
16509a47dbSJiří Techet  *  with this program; if not, write to the Free Software Foundation, Inc.,
17509a47dbSJiří Techet  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18509a47dbSJiří Techet  *
19509a47dbSJiří Techet  */
20509a47dbSJiří Techet 
21509a47dbSJiří Techet #ifndef MIO_H
22509a47dbSJiří Techet #define MIO_H
23509a47dbSJiří Techet 
2464ae08e5SMasatake YAMATO #ifndef QUALIFIER
25509a47dbSJiří Techet #include "general.h"  /* must always come first */
2664ae08e5SMasatake YAMATO #else
2764ae08e5SMasatake YAMATO #include "gcc-attr.h"
2864ae08e5SMasatake YAMATO #endif
29509a47dbSJiří Techet 
30509a47dbSJiří Techet #include <stdio.h>
31509a47dbSJiří Techet #include <stdarg.h>
32509a47dbSJiří Techet 
33509a47dbSJiří Techet 
34509a47dbSJiří Techet /**
35509a47dbSJiří Techet  * MIOType:
36509a47dbSJiří Techet  * @MIO_TYPE_FILE: #MIO object works on a file
37509a47dbSJiří Techet  * @MIO_TYPE_MEMORY: #MIO object works in-memory
38509a47dbSJiří Techet  *
39509a47dbSJiří Techet  * Existing implementations.
40509a47dbSJiří Techet  */
41509a47dbSJiří Techet enum _MIOType {
42509a47dbSJiří Techet 	MIO_TYPE_FILE,
43509a47dbSJiří Techet 	MIO_TYPE_MEMORY
44509a47dbSJiří Techet };
45509a47dbSJiří Techet 
46509a47dbSJiří Techet typedef enum _MIOType   MIOType;
47509a47dbSJiří Techet typedef struct _MIO     MIO;
48509a47dbSJiří Techet typedef struct _MIOPos  MIOPos;
49509a47dbSJiří Techet 
50509a47dbSJiří Techet /**
51509a47dbSJiří Techet  * MIOReallocFunc:
52509a47dbSJiří Techet  * @ptr: Pointer to the memory to resize
53509a47dbSJiří Techet  * @size: New size of the memory pointed by @ptr
54509a47dbSJiří Techet  *
55509a47dbSJiří Techet  * A function following the realloc() semantic.
56509a47dbSJiří Techet  *
57509a47dbSJiří Techet  * Returns: A pointer to the start of the new memory, or %NULL on failure.
58509a47dbSJiří Techet  */
59509a47dbSJiří Techet typedef void *(* MIOReallocFunc) (void * ptr, size_t size);
60509a47dbSJiří Techet 
61509a47dbSJiří Techet /**
62509a47dbSJiří Techet  * MIOFOpenFunc:
63509a47dbSJiří Techet  * @filename: The filename to open
64509a47dbSJiří Techet  * @mode: fopen() modes for opening @filename
65509a47dbSJiří Techet  *
66509a47dbSJiří Techet  * A function following the fclose() semantic, used to close a #FILE
67509a47dbSJiří Techet  * object.
68509a47dbSJiří Techet  *
69509a47dbSJiří Techet  * Returns: A new #FILE object, or %NULL on failure
70509a47dbSJiří Techet  */
71509a47dbSJiří Techet typedef FILE *(* MIOFOpenFunc) (const char *filename, const char *mode);
72509a47dbSJiří Techet 
73509a47dbSJiří Techet /**
74509a47dbSJiří Techet  * MIOFCloseFunc:
75509a47dbSJiří Techet  * @fp: An opened #FILE object
76509a47dbSJiří Techet  *
77509a47dbSJiří Techet  * A function following the fclose() semantic, used to close a #FILE
78509a47dbSJiří Techet  * object.
79509a47dbSJiří Techet  *
80509a47dbSJiří Techet  * Returns: 0 on success, EOF otherwise.
81509a47dbSJiří Techet  */
82509a47dbSJiří Techet typedef int (* MIOFCloseFunc) (FILE *fp);
83509a47dbSJiří Techet 
84509a47dbSJiří Techet /**
85509a47dbSJiří Techet  * MIODestroyNotify:
86509a47dbSJiří Techet  * @data: Data element being destroyed
87509a47dbSJiří Techet  *
88509a47dbSJiří Techet  * Specifies the type of function which is called when a data element is
89509a47dbSJiří Techet  * destroyed. It is passed the pointer to the data element and should free any
90509a47dbSJiří Techet  * memory and resources allocated for it.
91509a47dbSJiří Techet  */
92509a47dbSJiří Techet typedef void (*MIODestroyNotify) (void *data);
93509a47dbSJiří Techet 
94509a47dbSJiří Techet /**
95509a47dbSJiří Techet  * MIOPos:
96509a47dbSJiří Techet  *
97509a47dbSJiří Techet  * An object representing the state of a #MIO stream. This object can be
98509a47dbSJiří Techet  * statically allocated but all its fields are private and should not be
99509a47dbSJiří Techet  * accessed directly.
100509a47dbSJiří Techet  */
101509a47dbSJiří Techet struct _MIOPos {
102509a47dbSJiří Techet 	/*< private >*/
1033199a5c0SMasatake YAMATO 	MIOType type;
104509a47dbSJiří Techet #ifdef MIO_DEBUG
105509a47dbSJiří Techet 	void *tag;
106509a47dbSJiří Techet #endif
107509a47dbSJiří Techet 	union {
108509a47dbSJiří Techet 		fpos_t file;
109509a47dbSJiří Techet 		size_t mem;
110509a47dbSJiří Techet 	} impl;
111509a47dbSJiří Techet };
112509a47dbSJiří Techet 
113509a47dbSJiří Techet 
114509a47dbSJiří Techet 
115509a47dbSJiří Techet MIO *mio_new_file (const char *filename, const char *mode);
116509a47dbSJiří Techet MIO *mio_new_file_full (const char *filename,
117509a47dbSJiří Techet 						const char *mode,
118509a47dbSJiří Techet 						MIOFOpenFunc open_func,
119509a47dbSJiří Techet 						MIOFCloseFunc close_func);
120509a47dbSJiří Techet MIO *mio_new_fp (FILE *fp, MIOFCloseFunc close_func);
121509a47dbSJiří Techet MIO *mio_new_memory (unsigned char *data,
122509a47dbSJiří Techet 					 size_t size,
123509a47dbSJiří Techet 					 MIOReallocFunc realloc_func,
124509a47dbSJiří Techet 					 MIODestroyNotify free_func);
125e1ef8f28SMasatake YAMATO 
126915d6979SJiří Techet MIO *mio_new_mio    (MIO *base, long start, long size);
12739ef3738SMasatake YAMATO MIO *mio_ref        (MIO *mio);
128e1ef8f28SMasatake YAMATO 
129b978efd6SMasatake YAMATO int mio_unref (MIO *mio);
130509a47dbSJiří Techet FILE *mio_file_get_fp (MIO *mio);
131509a47dbSJiří Techet unsigned char *mio_memory_get_data (MIO *mio, size_t *size);
132509a47dbSJiří Techet size_t mio_read (MIO *mio,
133509a47dbSJiří Techet 				 void *ptr,
134509a47dbSJiří Techet 				 size_t size,
135509a47dbSJiří Techet 				 size_t nmemb);
136509a47dbSJiří Techet size_t mio_write (MIO *mio,
137509a47dbSJiří Techet 				  const void *ptr,
138509a47dbSJiří Techet 				  size_t size,
139509a47dbSJiří Techet 				  size_t nmemb);
140509a47dbSJiří Techet int mio_getc (MIO *mio);
141509a47dbSJiří Techet char *mio_gets (MIO *mio, char *s, size_t size);
142509a47dbSJiří Techet int mio_ungetc (MIO *mio, int ch);
143509a47dbSJiří Techet int mio_putc (MIO *mio, int c);
144509a47dbSJiří Techet int mio_puts (MIO *mio, const char *s);
145509a47dbSJiří Techet 
1468ccb7ee9SJiří Techet int mio_vprintf (MIO *mio, const char *format, va_list ap) CTAGS_ATTR_PRINTF (2, 0);
1478ccb7ee9SJiří Techet int mio_printf (MIO *mio, const char *format, ...) CTAGS_ATTR_PRINTF (2, 3);
148509a47dbSJiří Techet 
149509a47dbSJiří Techet void mio_clearerr (MIO *mio);
150509a47dbSJiří Techet int mio_eof (MIO *mio);
151509a47dbSJiří Techet int mio_error (MIO *mio);
152509a47dbSJiří Techet int mio_seek (MIO *mio, long offset, int whence);
153509a47dbSJiří Techet long mio_tell (MIO *mio);
154509a47dbSJiří Techet void mio_rewind (MIO *mio);
155509a47dbSJiří Techet int mio_getpos (MIO *mio, MIOPos *pos);
156509a47dbSJiří Techet int mio_setpos (MIO *mio, MIOPos *pos);
157509a47dbSJiří Techet int mio_flush (MIO *mio);
158509a47dbSJiří Techet 
159bf6da614SMasatake YAMATO void  mio_attach_user_data (MIO *mio, void *user_data, MIODestroyNotify user_data_free_func);
160bf6da614SMasatake YAMATO void *mio_get_user_data (MIO *mio);
161bf6da614SMasatake YAMATO 
162*ad1e690fSMasatake YAMATO int mio_try_resize (MIO *mio, size_t new_size);
163*ad1e690fSMasatake YAMATO 
164509a47dbSJiří Techet #endif /* MIO_H */
165