xref: /Universal-ctags/main/mio.h (revision ad1e690f6250cb47d5e54c7fbad6fb56f4a56ddb)
1 /*
2  *  MIO, an I/O abstraction layer replicating C file I/O API.
3  *  Copyright (C) 2010  Colomban Wendling <ban@herbesfolles.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  */
20 
21 #ifndef MIO_H
22 #define MIO_H
23 
24 #ifndef QUALIFIER
25 #include "general.h"  /* must always come first */
26 #else
27 #include "gcc-attr.h"
28 #endif
29 
30 #include <stdio.h>
31 #include <stdarg.h>
32 
33 
34 /**
35  * MIOType:
36  * @MIO_TYPE_FILE: #MIO object works on a file
37  * @MIO_TYPE_MEMORY: #MIO object works in-memory
38  *
39  * Existing implementations.
40  */
41 enum _MIOType {
42 	MIO_TYPE_FILE,
43 	MIO_TYPE_MEMORY
44 };
45 
46 typedef enum _MIOType   MIOType;
47 typedef struct _MIO     MIO;
48 typedef struct _MIOPos  MIOPos;
49 
50 /**
51  * MIOReallocFunc:
52  * @ptr: Pointer to the memory to resize
53  * @size: New size of the memory pointed by @ptr
54  *
55  * A function following the realloc() semantic.
56  *
57  * Returns: A pointer to the start of the new memory, or %NULL on failure.
58  */
59 typedef void *(* MIOReallocFunc) (void * ptr, size_t size);
60 
61 /**
62  * MIOFOpenFunc:
63  * @filename: The filename to open
64  * @mode: fopen() modes for opening @filename
65  *
66  * A function following the fclose() semantic, used to close a #FILE
67  * object.
68  *
69  * Returns: A new #FILE object, or %NULL on failure
70  */
71 typedef FILE *(* MIOFOpenFunc) (const char *filename, const char *mode);
72 
73 /**
74  * MIOFCloseFunc:
75  * @fp: An opened #FILE object
76  *
77  * A function following the fclose() semantic, used to close a #FILE
78  * object.
79  *
80  * Returns: 0 on success, EOF otherwise.
81  */
82 typedef int (* MIOFCloseFunc) (FILE *fp);
83 
84 /**
85  * MIODestroyNotify:
86  * @data: Data element being destroyed
87  *
88  * Specifies the type of function which is called when a data element is
89  * destroyed. It is passed the pointer to the data element and should free any
90  * memory and resources allocated for it.
91  */
92 typedef void (*MIODestroyNotify) (void *data);
93 
94 /**
95  * MIOPos:
96  *
97  * An object representing the state of a #MIO stream. This object can be
98  * statically allocated but all its fields are private and should not be
99  * accessed directly.
100  */
101 struct _MIOPos {
102 	/*< private >*/
103 	MIOType type;
104 #ifdef MIO_DEBUG
105 	void *tag;
106 #endif
107 	union {
108 		fpos_t file;
109 		size_t mem;
110 	} impl;
111 };
112 
113 
114 
115 MIO *mio_new_file (const char *filename, const char *mode);
116 MIO *mio_new_file_full (const char *filename,
117 						const char *mode,
118 						MIOFOpenFunc open_func,
119 						MIOFCloseFunc close_func);
120 MIO *mio_new_fp (FILE *fp, MIOFCloseFunc close_func);
121 MIO *mio_new_memory (unsigned char *data,
122 					 size_t size,
123 					 MIOReallocFunc realloc_func,
124 					 MIODestroyNotify free_func);
125 
126 MIO *mio_new_mio    (MIO *base, long start, long size);
127 MIO *mio_ref        (MIO *mio);
128 
129 int mio_unref (MIO *mio);
130 FILE *mio_file_get_fp (MIO *mio);
131 unsigned char *mio_memory_get_data (MIO *mio, size_t *size);
132 size_t mio_read (MIO *mio,
133 				 void *ptr,
134 				 size_t size,
135 				 size_t nmemb);
136 size_t mio_write (MIO *mio,
137 				  const void *ptr,
138 				  size_t size,
139 				  size_t nmemb);
140 int mio_getc (MIO *mio);
141 char *mio_gets (MIO *mio, char *s, size_t size);
142 int mio_ungetc (MIO *mio, int ch);
143 int mio_putc (MIO *mio, int c);
144 int mio_puts (MIO *mio, const char *s);
145 
146 int mio_vprintf (MIO *mio, const char *format, va_list ap) CTAGS_ATTR_PRINTF (2, 0);
147 int mio_printf (MIO *mio, const char *format, ...) CTAGS_ATTR_PRINTF (2, 3);
148 
149 void mio_clearerr (MIO *mio);
150 int mio_eof (MIO *mio);
151 int mio_error (MIO *mio);
152 int mio_seek (MIO *mio, long offset, int whence);
153 long mio_tell (MIO *mio);
154 void mio_rewind (MIO *mio);
155 int mio_getpos (MIO *mio, MIOPos *pos);
156 int mio_setpos (MIO *mio, MIOPos *pos);
157 int mio_flush (MIO *mio);
158 
159 void  mio_attach_user_data (MIO *mio, void *user_data, MIODestroyNotify user_data_free_func);
160 void *mio_get_user_data (MIO *mio);
161 
162 int mio_try_resize (MIO *mio, size_t new_size);
163 
164 #endif /* MIO_H */
165