1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2022 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15
16 /**@file fileio.c
17 * @ingroup OTHER_CFILES
18 * @brief wrapper functions to map file i/o to standard or zlib file i/o
19 * @author Tobias Achterberg
20 */
21
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "scip/pub_fileio.h"
28
29
30 #define BUFFER_LEN 8192
31
32 #ifdef SCIP_WITH_ZLIB
33
34 /* file i/o using zlib */
35 #include <zlib.h>
36
37 SCIP_FILE* SCIPfopen(const char *path, const char *mode)
38 {
39 return (SCIP_FILE*)gzopen(path, mode);
40 }
41
42 SCIP_FILE* SCIPfdopen(int fildes, const char *mode)
43 {
44 return (SCIP_FILE*)gzdopen(fildes, mode);
45 }
46
47 size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
48 {
49 int nbytesread;
50
51 nbytesread = gzread((gzFile)stream, ptr, (unsigned int) (size * nmemb));
52 /* An error occured if nbytesread < 0. To be compatible with fread(), we return 0, which signifies an error there. */
53 if ( nbytesread < 0 )
54 return 0;
55
56 return (size_t) nbytesread; /*lint !e571*/
57 }
58
59 size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
60 {
61 return (size_t) gzwrite((gzFile)stream, ptr, (unsigned int) (size * nmemb)); /*lint !e571*/
62 }
63
64 int SCIPfprintf(SCIP_FILE *stream, const char *format, ...)
65 {
66 char buffer[BUFFER_LEN];
67 va_list ap;
68 int n;
69
70 va_start(ap, format); /*lint !e826*/
71 #if defined(_WIN32) || defined(_WIN64)
72 n = _vsnprintf(buffer, BUFFER_LEN, format, ap);
73 #else
74 n = vsnprintf(buffer, BUFFER_LEN, format, ap);
75 #endif
76 va_end(ap);
77 if( n < 0 || n > BUFFER_LEN)
78 buffer[BUFFER_LEN-1] = '\0';
79
80 return gzputs((gzFile)stream, buffer);
81 }
82
83 int SCIPfputc(int c, SCIP_FILE *stream)
84 {
85 return gzputc((gzFile)stream, c);
86 }
87
88 int SCIPfputs(const char *s, SCIP_FILE *stream)
89 {
90 return gzputs((gzFile)stream, s);
91 }
92
93 int SCIPfgetc(SCIP_FILE *stream)
94 {
95 return gzgetc((gzFile)stream);
96 }
97
98 char* SCIPfgets(char *s, int size, SCIP_FILE *stream)
99 {
(1) Event cond_true: |
Condition "size > 0", taking true branch. |
100 if( size > 0 )
101 s[0] = '\0';
(2) Event identity_transfer: |
Passing "s" as argument 2 to function "gzgets", which returns that argument. |
(3) Event tainted_data_return: |
"gzgets" returns tainted data. |
102 return gzgets((gzFile)stream, s, size);
103 }
104
105 int SCIPfflush(SCIP_FILE *stream)
106 {
107 return gzflush((gzFile)stream, Z_SYNC_FLUSH);
108 }
109
110 int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
111 {
112 return (int) gzseek((gzFile)stream, offset, whence);
113 }
114
115 void SCIPrewind(SCIP_FILE *stream)
116 {
117 (void) gzrewind((gzFile)stream);
118 }
119
120 long SCIPftell(SCIP_FILE *stream)
121 {
122 return gztell((gzFile)stream);
123 }
124
125 int SCIPfeof(SCIP_FILE *stream)
126 {
127 return gzeof((gzFile)stream);
128 }
129
130 int SCIPfclose(SCIP_FILE *fp)
131 {
132 return gzclose((gzFile)fp);
133 }
134
135
136 #else
137
138 #ifdef _MSC_VER
139 #define fdopen _fdopen
140 #endif
141
142 /* file i/o using standard i/o */
143
144 SCIP_FILE* SCIPfopen(const char *path, const char *mode)
145 {
146 return (SCIP_FILE*)fopen(path, mode);
147 }
148
149 SCIP_FILE* SCIPfdopen(int fildes, const char *mode)
150 {
151 return (SCIP_FILE*)fdopen(fildes, mode);
152 }
153
154 size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
155 {
156 return fread(ptr, size, nmemb, (FILE*)stream);
157 }
158
159 size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
160 {
161 return fwrite(ptr, size, nmemb, (FILE*)stream);
162 }
163
164 int SCIPfprintf(SCIP_FILE *stream, const char *format, ...)
165 {
166 va_list ap;
167 int retval;
168
169 va_start(ap, format); /*lint !e826*/
170 retval = vfprintf((FILE*)stream, format, ap);
171 va_end(ap);
172
173 return retval;
174 }
175
176 int SCIPfputc(int c, SCIP_FILE *stream)
177 {
178 return fputc(c, (FILE*)stream);
179 }
180
181 int SCIPfputs(const char *s, SCIP_FILE *stream)
182 {
183 return fputs(s, (FILE*)stream);
184 }
185
186 int SCIPfgetc(SCIP_FILE *stream)
187 {
188 return fgetc((FILE*)stream);
189 }
190
191 char* SCIPfgets(char *s, int size, SCIP_FILE *stream)
192 {
193 if( size > 0 )
194 s[0] = '\0';
195 return fgets(s, size, (FILE*)stream);
196 }
197
198 int SCIPfflush(SCIP_FILE *stream)
199 {
200 return fflush((FILE*)stream);
201 }
202
203 int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
204 {
205 return fseek((FILE*)stream, offset, whence);
206 }
207
208 void SCIPrewind(SCIP_FILE *stream)
209 {
210 rewind((FILE*)stream);
211 }
212
213 long SCIPftell(SCIP_FILE *stream)
214 {
215 return ftell((FILE*)stream);
216 }
217
218 int SCIPfeof(SCIP_FILE *stream)
219 {
220 return feof((FILE*)stream);
221 }
222
223 int SCIPfclose(SCIP_FILE *fp)
224 {
225 return fclose((FILE*)fp);
226 }
227
228
229 #endif
230