1 /*
2  * FILE stream functions
3  *
4  * Copyright (C) 2010-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #if !defined( _FILE_STREAM_H )
23 #define _FILE_STREAM_H
24 
25 #include "common.h"
26 
27 #if defined( HAVE_GLIB_H )
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #endif
31 
32 #include <stdio.h>
33 
34 #if defined( __cplusplus )
35 extern "C" {
36 #endif
37 
38 #define FILE_STREAM_OPEN_APPEND			"a"
39 #define FILE_STREAM_OPEN_READ			"r"
40 #define FILE_STREAM_OPEN_WRITE			"w"
41 
42 #if defined( WINAPI )
43 #define FILE_STREAM_BINARY_OPEN_APPEND		"ab"
44 #define FILE_STREAM_BINARY_OPEN_READ		"rb"
45 #define FILE_STREAM_BINARY_OPEN_WRITE		"wb"
46 
47 #else
48 #define FILE_STREAM_BINARY_OPEN_APPEND		"a"
49 #define FILE_STREAM_BINARY_OPEN_READ		"r"
50 #define FILE_STREAM_BINARY_OPEN_WRITE		"w"
51 
52 #endif
53 
54 /* narrow character FILE stream open
55  */
56 #if defined( HAVE_GLIB_H )
57 #define file_stream_open( filename, mode ) \
58 	g_fopen( filename, mode )
59 
60 #elif defined( HAVE_FOPEN ) || defined( WINAPI )
61 #define file_stream_open( filename, mode ) \
62 	fopen( filename, mode )
63 #endif
64 
65 /* wide character FILE stream open
66  */
67 #if defined( WINAPI )
68 #define file_stream_open_wide( filename, mode ) \
69 	_wfopen( filename, mode )
70 #endif
71 
72 /* FILE stream close
73  */
74 #if defined( HAVE_FCLOSE ) || defined( WINAPI )
75 #define file_stream_close( stream ) \
76 	fclose( stream )
77 #endif
78 
79 /* FILE stream read
80  */
81 #if defined( HAVE_FREAD ) || defined( WINAPI )
82 #define file_stream_read( stream, data, size ) \
83 	fread( data, 1, size, stream )
84 #endif
85 
86 /* FILE stream write
87  */
88 #if defined( HAVE_FWRITE ) || defined( WINAPI )
89 #define file_stream_write( stream, data, size ) \
90 	fwrite( data, 1, size, stream )
91 #endif
92 
93 /* FILE stream seek
94  */
95 #if defined( WINAPI )
96 #define file_stream_seek_offset( stream, offset, whence ) \
97 	fseek( stream, offset, whence )
98 
99 #elif defined( HAVE_FSEEKO )
100 #define file_stream_seek_offset( stream, offset, whence ) \
101 	fseeko( stream, offset, whence )
102 
103 #elif defined( HAVE_FSEEKO64 )
104 #define file_stream_seek_offset( stream, offset, whence ) \
105 	fseeko64( stream, offset, whence )
106 #endif
107 
108 /* End of FILE stream
109  */
110 #if defined( HAVE_FEOF ) || defined( WINAPI )
111 #define file_stream_at_end( stream ) \
112         feof( stream )
113 #endif
114 
115 /* Get narrow character string from FILE stream
116  */
117 #if defined( HAVE_FGETS ) || defined( WINAPI )
118 #define file_stream_get_string( stream, string, size ) \
119         fgets( string, size, stream )
120 #endif
121 
122 /* Get wide characters string from FILE stream
123  */
124 #if defined( HAVE_FGETWS ) || defined( WINAPI )
125 #define file_stream_get_string_wide( stream, string, size ) \
126         fgetws( string, size, stream )
127 #endif
128 
129 /* Variable arguments formatted print to stream function
130  */
131 #if defined( HAVE_GLIB_H )
132 #define file_stream_vfprintf( stream, format, ... ) \
133 	g_vfprintf( stream, format, __VA_ARGS__ )
134 
135 /* Borland BCC previous to version 5.6.0 cannot handle the macro form: MACRO( ... )
136  */
137 #elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
138 #define file_stream_vfprintf \
139 	vfprintf
140 
141 #elif defined( HAVE_VFPRINTF ) || defined( WINAPI )
142 #define file_stream_vfprintf( stream, format, ... ) \
143 	vfprintf( stream, format, __VA_ARGS__ )
144 #endif
145 
146 #if defined( __cplusplus )
147 }
148 #endif
149 
150 #endif /* !defined( _FILE_STREAM_H ) */
151 
152