Scippy

SCIP

Solving Constraint Integer Programs

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-2017 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 email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file fileio.c
17  * @brief wrapper functions to map file i/o to standard or zlib file i/o
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <stdio.h>
24 #include <stdarg.h>
25 
26 #include "scip/pub_fileio.h"
27 
28 
29 #define BUFFER_LEN 8192
30 
31 #ifdef WITH_ZLIB
32 
33 /* file i/o using zlib */
34 #include <zlib.h>
35 
36 SCIP_FILE* SCIPfopen(const char *path, const char *mode)
37 {
38  return (SCIP_FILE*)gzopen(path, mode);
39 }
40 
41 SCIP_FILE* SCIPfdopen(int fildes, const char *mode)
42 {
43  return (SCIP_FILE*)gzdopen(fildes, mode);
44 }
45 
46 size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
47 {
48 #ifndef NDEBUG
49  int nbytesread = gzread((gzFile)stream, ptr, (unsigned int) (size * nmemb));
50  assert(nbytesread >= 0);
51 
52  return (size_t) nbytesread; /*lint !e571*/
53 #else
54  return (size_t) gzread((gzFile)stream, ptr, (unsigned int) (size * nmemb));
55 #endif
56 }
57 
58 size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
59 {
60  return (size_t) gzwrite((gzFile)stream, ptr, (unsigned int) (size * nmemb)); /*lint !e571*/
61 }
62 
63 int SCIPfprintf(SCIP_FILE *stream, const char *format, ...)
64 {
65  char buffer[BUFFER_LEN];
66  va_list ap;
67  int n;
68 
69  va_start(ap, format); /*lint !e826*/
70 #if defined(_WIN32) || defined(_WIN64)
71  n = _vsnprintf(buffer, BUFFER_LEN, format, ap);
72 #else
73  n = vsnprintf(buffer, BUFFER_LEN, format, ap);
74 #endif
75  va_end(ap);
76  if( n < 0 || n > BUFFER_LEN)
77  buffer[BUFFER_LEN-1] = '\0';
78 
79  return gzputs((gzFile)stream, buffer);
80 }
81 
82 int SCIPfputc(int c, SCIP_FILE *stream)
83 {
84  return gzputc((gzFile)stream, c);
85 }
86 
87 int SCIPfputs(const char *s, SCIP_FILE *stream)
88 {
89  return gzputs((gzFile)stream, s);
90 }
91 
92 int SCIPfgetc(SCIP_FILE *stream)
93 {
94  return gzgetc((gzFile)stream);
95 }
96 
97 char* SCIPfgets(char *s, int size, SCIP_FILE *stream)
98 {
99  if( size > 0 )
100  s[0] = '\0';
101  return gzgets((gzFile)stream, s, size);
102 }
103 
104 int SCIPfflush(SCIP_FILE *stream)
105 {
106  return gzflush((gzFile)stream, Z_SYNC_FLUSH);
107 }
108 
109 int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
110 {
111  return (int) gzseek((gzFile)stream, offset, whence);
112 }
113 
114 void SCIPrewind(SCIP_FILE *stream)
115 {
116  (void) gzrewind((gzFile)stream);
117 }
118 
119 long SCIPftell(SCIP_FILE *stream)
120 {
121  return gztell((gzFile)stream);
122 }
123 
124 int SCIPfeof(SCIP_FILE *stream)
125 {
126  return gzeof((gzFile)stream);
127 }
128 
129 int SCIPfclose(SCIP_FILE *fp)
130 {
131  return gzclose((gzFile)fp);
132 }
133 
134 
135 #else
136 
137 
138 /* file i/o using standard i/o */
139 
140 SCIP_FILE* SCIPfopen(const char *path, const char *mode)
141 {
142  return (SCIP_FILE*)fopen(path, mode);
143 }
144 
145 SCIP_FILE* SCIPfdopen(int fildes, const char *mode)
146 {
147  return (SCIP_FILE*)fdopen(fildes, mode);
148 }
149 
150 size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
151 {
152  return fread(ptr, size, nmemb, (FILE*)stream);
153 }
154 
155 size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
156 {
157  return fwrite(ptr, size, nmemb, (FILE*)stream);
158 }
159 
160 int SCIPfprintf(SCIP_FILE *stream, const char *format, ...)
161 {
162  va_list ap;
163  int retval;
164 
165  va_start(ap, format); /*lint !e826*/
166  retval = vfprintf((FILE*)stream, format, ap);
167  va_end(ap);
168 
169  return retval;
170 }
171 
172 int SCIPfputc(int c, SCIP_FILE *stream)
173 {
174  return fputc(c, (FILE*)stream);
175 }
176 
177 int SCIPfputs(const char *s, SCIP_FILE *stream)
178 {
179  return fputs(s, (FILE*)stream);
180 }
181 
182 int SCIPfgetc(SCIP_FILE *stream)
183 {
184  return fgetc((FILE*)stream);
185 }
186 
187 char* SCIPfgets(char *s, int size, SCIP_FILE *stream)
188 {
189  if( size > 0 )
190  s[0] = '\0';
191  return fgets(s, size, (FILE*)stream);
192 }
193 
194 int SCIPfflush(SCIP_FILE *stream)
195 {
196  return fflush((FILE*)stream);
197 }
198 
199 int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
200 {
201  return fseek((FILE*)stream, offset, whence);
202 }
203 
204 void SCIPrewind(SCIP_FILE *stream)
205 {
206  rewind((FILE*)stream);
207 }
208 
209 long SCIPftell(SCIP_FILE *stream)
210 {
211  return ftell((FILE*)stream);
212 }
213 
214 int SCIPfeof(SCIP_FILE *stream)
215 {
216  return feof((FILE*)stream);
217 }
218 
220 {
221  return fclose((FILE*)fp);
222 }
223 
224 
225 #endif
size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
Definition: fileio.c:150
#define BUFFER_LEN
Definition: fileio.c:29
int SCIPfgetc(SCIP_FILE *stream)
Definition: fileio.c:182
int SCIPfputs(const char *s, SCIP_FILE *stream)
Definition: fileio.c:177
int SCIPfflush(SCIP_FILE *stream)
Definition: fileio.c:194
int SCIPfseek(SCIP_FILE *stream, long offset, int whence)
Definition: fileio.c:199
int SCIPfputc(int c, SCIP_FILE *stream)
Definition: fileio.c:172
size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream)
Definition: fileio.c:155
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:140
int SCIPfprintf(SCIP_FILE *stream, const char *format,...)
Definition: fileio.c:160
int SCIPfeof(SCIP_FILE *stream)
Definition: fileio.c:214
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:34
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:187
wrapper functions to map file i/o to standard or zlib file i/o
SCIP_FILE * SCIPfdopen(int fildes, const char *mode)
Definition: fileio.c:145
void SCIPrewind(SCIP_FILE *stream)
Definition: fileio.c:204
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
long SCIPftell(SCIP_FILE *stream)
Definition: fileio.c:209