SCIP Doxygen Documentation
Loading...
Searching...
No Matches
fileio.c
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* SCIP --- Solving Constraint Integer Programs */
5/* */
6/* Copyright (c) 2002-2025 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file fileio.c
26 * @ingroup OTHER_CFILES
27 * @brief wrapper functions to map file i/o to standard or zlib file i/o
28 * @author Tobias Achterberg
29 */
30
31/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32
33#include <stdio.h>
34#include <stdarg.h>
35
36#include "scip/pub_fileio.h"
37
38
39#define BUFFER_LEN 8192
40
41#ifdef SCIP_WITH_ZLIB
42
43/* file i/o using zlib */
44#define WITH_GZFILEOP
45#include <zlib-ng.h>
46
47SCIP_FILE* SCIPfopen(const char *path, const char *mode)
48{
49 return (SCIP_FILE*)zng_gzopen(path, mode);
50}
51
52SCIP_FILE* SCIPfdopen(int fildes, const char *mode)
53{
54 return (SCIP_FILE*)zng_gzdopen(fildes, mode);
55}
56
57size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
58{
59 int nbytesread;
60
61 nbytesread = zng_gzread((gzFile)stream, ptr, (unsigned int) (size * nmemb));
62 /* An error occured if nbytesread < 0. To be compatible with fread(), we return 0, which signifies an error there. */
63 if ( nbytesread < 0 )
64 return 0;
65
66 return (size_t) nbytesread; /*lint !e571*/
67}
68
69size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
70{
71 return (size_t) zng_gzwrite((gzFile)stream, ptr, (unsigned int) (size * nmemb)); /*lint !e571*/
72}
73
74int SCIPfprintf(SCIP_FILE *stream, const char *format, ...)
75{
76 char buffer[BUFFER_LEN];
77 va_list ap;
78 int n;
79
80 va_start(ap, format); /*lint !e826*/
81#if defined(_MSC_VER) && _MSC_VER < 1900
82 n = _vsnprintf(buffer, BUFFER_LEN, format, ap);
83#else
84 n = vsnprintf(buffer, BUFFER_LEN, format, ap);
85#endif
86 va_end(ap);
87 if( n < 0 || n > BUFFER_LEN)
88 buffer[BUFFER_LEN-1] = '\0';
89
90 return zng_gzputs((gzFile)stream, buffer);
91}
92
93int SCIPfputc(int c, SCIP_FILE *stream)
94{
95 return zng_gzputc((gzFile)stream, c);
96}
97
98int SCIPfputs(const char *s, SCIP_FILE *stream)
99{
100 return zng_gzputs((gzFile)stream, s);
101}
102
103int SCIPfgetc(SCIP_FILE *stream)
104{
105 return zng_gzgetc((gzFile)stream);
106}
107
108char* SCIPfgets(char *s, int size, SCIP_FILE *stream)
109{
110 if( size > 0 )
111 s[0] = '\0';
112 return zng_gzgets((gzFile)stream, s, size);
113}
114
115int SCIPfflush(SCIP_FILE *stream)
116{
117 return zng_gzflush((gzFile)stream, Z_SYNC_FLUSH);
118}
119
120int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
121{
122 return (int) zng_gzseek((gzFile)stream, offset, whence);
123}
124
125void SCIPrewind(SCIP_FILE *stream)
126{
127 (void) zng_gzrewind((gzFile)stream);
128}
129
130long SCIPftell(SCIP_FILE *stream)
131{
132 return zng_gztell((gzFile)stream);
133}
134
135int SCIPfeof(SCIP_FILE *stream)
136{
137 return zng_gzeof((gzFile)stream);
138}
139
140int SCIPfclose(SCIP_FILE *fp)
141{
142 return zng_gzclose((gzFile)fp);
143}
144
145
146#else
147
148#ifdef _MSC_VER
149#define fdopen _fdopen
150#endif
151
152/* file i/o using standard i/o */
153
154SCIP_FILE* SCIPfopen(const char *path, const char *mode)
155{
156 return (SCIP_FILE*)fopen(path, mode);
157}
158
159SCIP_FILE* SCIPfdopen(int fildes, const char *mode)
160{
161 return (SCIP_FILE*)fdopen(fildes, mode);
162}
163
164size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
165{
166 return fread(ptr, size, nmemb, (FILE*)stream);
167}
168
169size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
170{
171 return fwrite(ptr, size, nmemb, (FILE*)stream);
172}
173
174int SCIPfprintf(SCIP_FILE *stream, const char *format, ...)
175{
176 va_list ap;
177 int retval;
178
179 va_start(ap, format); /*lint !e826*/
180 retval = vfprintf((FILE*)stream, format, ap);
181 va_end(ap);
182
183 return retval;
184}
185
186int SCIPfputc(int c, SCIP_FILE *stream)
187{
188 return fputc(c, (FILE*)stream);
189}
190
191int SCIPfputs(const char *s, SCIP_FILE *stream)
192{
193 return fputs(s, (FILE*)stream);
194}
195
197{
198 return fgetc((FILE*)stream);
199}
200
201char* SCIPfgets(char *s, int size, SCIP_FILE *stream)
202{
203 if( size > 0 )
204 s[0] = '\0';
205 return fgets(s, size, (FILE*)stream);
206}
207
209{
210 return fflush((FILE*)stream);
211}
212
213int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
214{
215 return fseek((FILE*)stream, offset, whence);
216}
217
219{
220 rewind((FILE*)stream);
221}
222
223long SCIPftell(SCIP_FILE *stream)
224{
225 return ftell((FILE*)stream);
226}
227
228int SCIPfeof(SCIP_FILE *stream)
229{
230 return feof((FILE*)stream);
231}
232
234{
235 return fclose((FILE*)fp);
236}
237
238
239#endif
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition fileio.c:154
size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
Definition fileio.c:169
int SCIPfgetc(SCIP_FILE *stream)
Definition fileio.c:196
int SCIPfprintf(SCIP_FILE *stream, const char *format,...)
Definition fileio.c:174
int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
Definition fileio.c:213
#define BUFFER_LEN
Definition fileio.c:39
int SCIPfeof(SCIP_FILE *stream)
Definition fileio.c:228
void SCIPrewind(SCIP_FILE *stream)
Definition fileio.c:218
size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
Definition fileio.c:164
long SCIPftell(SCIP_FILE *stream)
Definition fileio.c:223
int SCIPfputc(int c, SCIP_FILE *stream)
Definition fileio.c:186
int SCIPfclose(SCIP_FILE *fp)
Definition fileio.c:233
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition fileio.c:201
int SCIPfputs(const char *s, SCIP_FILE *stream)
Definition fileio.c:191
SCIP_FILE * SCIPfdopen(int fildes, const char *mode)
Definition fileio.c:159
int SCIPfflush(SCIP_FILE *stream)
Definition fileio.c:208
int c
wrapper functions to map file i/o to standard or zlib file i/o
struct SCIP_File SCIP_FILE
Definition pub_fileio.h:43