1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 2 /* */ 3 /* This file is part of the program and library */ 4 /* SCIP --- Solving Constraint Integer Programs */ 5 /* */ 6 /* Copyright (c) 2002-2023 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 #include <zlib.h> 45 46 SCIP_FILE* SCIPfopen(const char *path, const char *mode) 47 { 48 return (SCIP_FILE*)gzopen(path, mode); 49 } 50 51 SCIP_FILE* SCIPfdopen(int fildes, const char *mode) 52 { 53 return (SCIP_FILE*)gzdopen(fildes, mode); 54 } 55 56 size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream) 57 { 58 int nbytesread; 59 60 nbytesread = gzread((gzFile)stream, ptr, (unsigned int) (size * nmemb)); 61 /* An error occured if nbytesread < 0. To be compatible with fread(), we return 0, which signifies an error there. */ 62 if ( nbytesread < 0 ) 63 return 0; 64 65 return (size_t) nbytesread; /*lint !e571*/ 66 } 67 68 size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream) 69 { 70 return (size_t) gzwrite((gzFile)stream, ptr, (unsigned int) (size * nmemb)); /*lint !e571*/ 71 } 72 73 int SCIPfprintf(SCIP_FILE *stream, const char *format, ...) 74 { 75 char buffer[BUFFER_LEN]; 76 va_list ap; 77 int n; 78 79 va_start(ap, format); /*lint !e826*/ 80 #if defined(_WIN32) || defined(_WIN64) 81 n = _vsnprintf(buffer, BUFFER_LEN, format, ap); 82 #else 83 n = vsnprintf(buffer, BUFFER_LEN, format, ap); 84 #endif 85 va_end(ap); 86 if( n < 0 || n > BUFFER_LEN) 87 buffer[BUFFER_LEN-1] = '\0'; 88 89 return gzputs((gzFile)stream, buffer); 90 } 91 92 int SCIPfputc(int c, SCIP_FILE *stream) 93 { 94 return gzputc((gzFile)stream, c); 95 } 96 97 int SCIPfputs(const char *s, SCIP_FILE *stream) 98 { 99 return gzputs((gzFile)stream, s); 100 } 101 102 int SCIPfgetc(SCIP_FILE *stream) 103 { 104 return gzgetc((gzFile)stream); 105 } 106 107 char* SCIPfgets(char *s, int size, SCIP_FILE *stream) 108 { 109 if( size > 0 ) 110 s[0] = '\0'; 111 return gzgets((gzFile)stream, s, size); 112 } 113 114 int SCIPfflush(SCIP_FILE *stream) 115 { 116 return gzflush((gzFile)stream, Z_SYNC_FLUSH); 117 } 118 119 int SCIPfseek(SCIP_FILE *stream, long offset, int whence) 120 { 121 return (int) gzseek((gzFile)stream, offset, whence); 122 } 123 124 void SCIPrewind(SCIP_FILE *stream) 125 { 126 (void) gzrewind((gzFile)stream); 127 } 128 129 long SCIPftell(SCIP_FILE *stream) 130 { 131 return gztell((gzFile)stream); 132 } 133 134 int SCIPfeof(SCIP_FILE *stream) 135 { 136 return gzeof((gzFile)stream); 137 } 138 139 int SCIPfclose(SCIP_FILE *fp) 140 { 141 return gzclose((gzFile)fp); 142 } 143 144 145 #else 146 147 #ifdef _MSC_VER 148 #define fdopen _fdopen 149 #endif 150 151 /* file i/o using standard i/o */ 152 153 SCIP_FILE* SCIPfopen(const char *path, const char *mode) 154 { 155 return (SCIP_FILE*)fopen(path, mode); 156 } 157 158 SCIP_FILE* SCIPfdopen(int fildes, const char *mode) 159 { 160 return (SCIP_FILE*)fdopen(fildes, mode); 161 } 162 163 size_t SCIPfread(void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream) 164 { 165 return fread(ptr, size, nmemb, (FILE*)stream); 166 } 167 168 size_t SCIPfwrite(const void *ptr, size_t size, size_t nmemb, SCIP_FILE *stream) 169 { 170 return fwrite(ptr, size, nmemb, (FILE*)stream); 171 } 172 173 int SCIPfprintf(SCIP_FILE *stream, const char *format, ...) 174 { 175 va_list ap; 176 int retval; 177 178 va_start(ap, format); /*lint !e826*/ 179 retval = vfprintf((FILE*)stream, format, ap); 180 va_end(ap); 181 182 return retval; 183 } 184 185 int SCIPfputc(int c, SCIP_FILE *stream) 186 { 187 return fputc(c, (FILE*)stream); 188 } 189 190 int SCIPfputs(const char *s, SCIP_FILE *stream) 191 { 192 return fputs(s, (FILE*)stream); 193 } 194 195 int SCIPfgetc(SCIP_FILE *stream) 196 { 197 return fgetc((FILE*)stream); 198 } 199 200 char* SCIPfgets(char *s, int size, SCIP_FILE *stream) 201 { 202 if( size > 0 ) 203 s[0] = '\0'; 204 return fgets(s, size, (FILE*)stream); 205 } 206 207 int SCIPfflush(SCIP_FILE *stream) 208 { 209 return fflush((FILE*)stream); 210 } 211 212 int SCIPfseek(SCIP_FILE *stream, long offset, int whence) 213 { 214 return fseek((FILE*)stream, offset, whence); 215 } 216 217 void SCIPrewind(SCIP_FILE *stream) 218 { 219 rewind((FILE*)stream); 220 } 221 222 long SCIPftell(SCIP_FILE *stream) 223 { 224 return ftell((FILE*)stream); 225 } 226 227 int SCIPfeof(SCIP_FILE *stream) 228 { 229 return feof((FILE*)stream); 230 } 231 232 int SCIPfclose(SCIP_FILE *fp) 233 { 234 return fclose((FILE*)fp); 235 } 236 237 238 #endif 239