aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/env/stream.c
blob: 906e5b04e11c3c2847dfb42c6c409d130d507929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* stream.c (stream input/output) */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2008-2017 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "env.h"
#include "zlib.h"

struct glp_file
{     /* sequential stream descriptor */
      char *base;
      /* pointer to buffer */
      int size;
      /* size of buffer, in bytes */
      char *ptr;
      /* pointer to next byte in buffer */
      int cnt;
      /* count of bytes in buffer */
      int flag;
      /* stream flags: */
#define IONULL 0x01 /* null file */
#define IOSTD  0x02 /* standard stream */
#define IOGZIP 0x04 /* gzipped file */
#define IOWRT  0x08 /* output stream */
#define IOEOF  0x10 /* end of file */
#define IOERR  0x20 /* input/output error */
      void *file;
      /* pointer to underlying control object */
};

/***********************************************************************
*  NAME
*
*  glp_open - open stream
*
*  SYNOPSIS
*
*  glp_file *glp_open(const char *name, const char *mode);
*
*  DESCRIPTION
*
*  The routine glp_open opens a file whose name is a string pointed to
*  by name and associates a stream with it.
*
*  The following special filenames are recognized by the routine (this
*  feature is platform independent):
*
*  "/dev/null"    empty (null) file;
*  "/dev/stdin"   standard input stream;
*  "/dev/stdout"  standard output stream;
*  "/dev/stderr"  standard error stream.
*
*  If the specified filename is ended with ".gz", it is assumed that
*  the file is in gzipped format. In this case the file is compressed
*  or decompressed by the I/O routines "on the fly".
*
*  The parameter mode points to a string, which indicates the open mode
*  and should be one of the following:
*
*  "r"   open text file for reading;
*  "w"   truncate to zero length or create text file for writing;
*  "a"   append, open or create text file for writing at end-of-file;
*  "rb"  open binary file for reading;
*  "wb"  truncate to zero length or create binary file for writing;
*  "ab"  append, open or create binary file for writing at end-of-file.
*
*  RETURNS
*
*  The routine glp_open returns a pointer to the object controlling the
*  stream. If the operation fails, the routine returns NULL. */

glp_file *glp_open(const char *name, const char *mode)
{     glp_file *f;
      int flag;
      void *file;
      if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0)
         flag = 0;
      else if (strcmp(mode, "w") == 0 || strcmp(mode, "wb") == 0)
         flag = IOWRT;
#if 1 /* 08/V-2014 */
      else if (strcmp(mode, "a") == 0 || strcmp(mode, "ab") == 0)
         flag = IOWRT;
#endif
      else
         xerror("glp_open: invalid mode string\n");
      if (strcmp(name, "/dev/null") == 0)
      {  flag |= IONULL;
         file = NULL;
      }
      else if (strcmp(name, "/dev/stdin") == 0)
      {  flag |= IOSTD;
         file = stdin;
      }
      else if (strcmp(name, "/dev/stdout") == 0)
      {  flag |= IOSTD;
         file = stdout;
      }
      else if (strcmp(name, "/dev/stderr") == 0)
      {  flag |= IOSTD;
         file = stderr;
      }
      else
      {  char *ext = strrchr(name, '.');
         if (ext == NULL || strcmp(ext, ".gz") != 0)
         {  file = fopen(name, mode);
            if (file == NULL)
#if 0 /* 29/I-2017 */
            {  put_err_msg(strerror(errno));
#else
            {  put_err_msg(xstrerr(errno));
#endif
               return NULL;
            }
         }
         else
         {  flag |= IOGZIP;
            if (strcmp(mode, "r") == 0)
               mode = "rb";
            else if (strcmp(mode, "w") == 0)
               mode = "wb";
#if 1 /* 08/V-2014; this mode seems not to work */
            else if (strcmp(mode, "a") == 0)
               mode = "ab";
#endif
            file = gzopen(name, mode);
            if (file == NULL)
#if 0 /* 29/I-2017 */
            {  put_err_msg(strerror(errno));
#else
            {  put_err_msg(xstrerr(errno));
#endif
               return NULL;
            }
         }
      }
      f = talloc(1, glp_file);
      f->base = talloc(BUFSIZ, char);
      f->size = BUFSIZ;
      f->ptr = f->base;
      f->cnt = 0;
      f->flag = flag;
      f->file = file;
      return f;
}

/***********************************************************************
*  NAME
*
*  glp_eof - test end-of-file indicator
*
*  SYNOPSIS
*
*  int glp_eof(glp_file *f);
*
*  DESCRIPTION
*
*  The routine glp_eof tests the end-of-file indicator for the stream
*  pointed to by f.
*
*  RETURNS
*
*  The routine glp_eof returns non-zero if and only if the end-of-file
*  indicator is set for the specified stream. */

int glp_eof(glp_file *f)
{     return
         f->flag & IOEOF;
}

/***********************************************************************
*  NAME
*
*  glp_ioerr - test I/O error indicator
*
*  SYNOPSIS
*
*  int glp_ioerr(glp_file *f);
*
*  DESCRIPTION
*
*  The routine glp_ioerr tests the I/O error indicator for the stream
*  pointed to by f.
*
*  RETURNS
*
*  The routine glp_ioerr returns non-zero if and only if the I/O error
*  indicator is set for the specified stream. */

int glp_ioerr(glp_file *f)
{     return
         f->flag & IOERR;
}

/***********************************************************************
*  NAME
*
*  glp_read - read data from stream
*
*  SYNOPSIS
*
*  int glp_read(glp_file *f, void *buf, int nnn);
*
*  DESCRIPTION
*
*  The routine glp_read reads, into the buffer pointed to by buf, up to
*  nnn bytes, from the stream pointed to by f.
*
*  RETURNS
*
*  The routine glp_read returns the number of bytes successfully read
*  (which may be less than nnn). If an end-of-file is encountered, the
*  end-of-file indicator for the stream is set and glp_read returns
*  zero. If a read error occurs, the error indicator for the stream is
*  set and glp_read returns a negative value. */

int glp_read(glp_file *f, void *buf, int nnn)
{     int nrd, cnt;
      if (f->flag & IOWRT)
         xerror("glp_read: attempt to read from output stream\n");
      if (nnn < 1)
         xerror("glp_read: nnn = %d; invalid parameter\n", nnn);
      for (nrd = 0; nrd < nnn; nrd += cnt)
      {  if (f->cnt == 0)
         {  /* buffer is empty; fill it */
            if (f->flag & IONULL)
               cnt = 0;
            else if (!(f->flag & IOGZIP))
            {  cnt = fread(f->base, 1, f->size, (FILE *)(f->file));
               if (ferror((FILE *)(f->file)))
               {  f->flag |= IOERR;
#if 0 /* 29/I-2017 */
                  put_err_msg(strerror(errno));
#else
                  put_err_msg(xstrerr(errno));
#endif
                  return EOF;
               }
            }
            else
            {  int errnum;
               const char *msg;
               cnt = gzread((gzFile)(f->file), f->base, f->size);
               if (cnt < 0)
               {  f->flag |= IOERR;
                  msg = gzerror((gzFile)(f->file), &errnum);
                  if (errnum == Z_ERRNO)
#if 0 /* 29/I-2017 */
                     put_err_msg(strerror(errno));
#else
                     put_err_msg(xstrerr(errno));
#endif
                  else
                     put_err_msg(msg);
                  return EOF;
               }
            }
            if (cnt == 0)
            {  if (nrd == 0)
                  f->flag |= IOEOF;
               break;
            }
            f->ptr = f->base;
            f->cnt = cnt;
         }
         cnt = nnn - nrd;
         if (cnt > f->cnt)
            cnt = f->cnt;
         memcpy((char *)buf + nrd, f->ptr, cnt);
         f->ptr += cnt;
         f->cnt -= cnt;
      }
      return nrd;
}

/***********************************************************************
*  NAME
*
*  glp_getc - read character from stream
*
*  SYNOPSIS
*
*  int glp_getc(glp_file *f);
*
*  DESCRIPTION
*
*  The routine glp_getc obtains a next character as an unsigned char
*  converted to an int from the input stream pointed to by f.
*
*  RETURNS
*
*  The routine glp_getc returns the next character obtained. However,
*  if an end-of-file is encountered or a read error occurs, the routine
*  returns EOF. (An end-of-file and a read error can be distinguished
*  by use of the routines glp_eof and glp_ioerr.) */

int glp_getc(glp_file *f)
{     unsigned char buf[1];
      if (f->flag & IOWRT)
         xerror("glp_getc: attempt to read from output stream\n");
      if (glp_read(f, buf, 1) != 1)
         return EOF;
      return buf[0];
}

/***********************************************************************
*  do_flush - flush output stream
*
*  This routine causes buffered data for the specified output stream to
*  be written to the associated file.
*
*  If the operation was successful, the routine returns zero, otherwise
*  non-zero. */

static int do_flush(glp_file *f)
{     xassert(f->flag & IOWRT);
      if (f->cnt > 0)
      {  if (f->flag & IONULL)
            ;
         else if (!(f->flag & IOGZIP))
         {  if ((int)fwrite(f->base, 1, f->cnt, (FILE *)(f->file))
               != f->cnt)
            {  f->flag |= IOERR;
#if 0 /* 29/I-2017 */
               put_err_msg(strerror(errno));
#else
               put_err_msg(xstrerr(errno));
#endif
               return EOF;
            }
         }
         else
         {  int errnum;
            const char *msg;
            if (gzwrite((gzFile)(f->file), f->base, f->cnt) != f->cnt)
            {  f->flag |= IOERR;
               msg = gzerror((gzFile)(f->file), &errnum);
               if (errnum == Z_ERRNO)
#if 0 /* 29/I-2017 */
                  put_err_msg(strerror(errno));
#else
                  put_err_msg(xstrerr(errno));
#endif
               else
                  put_err_msg(msg);
               return EOF;
            }
         }
      }
      f->ptr = f->base;
      f->cnt = 0;
      return 0;
}

/***********************************************************************
*  NAME
*
*  glp_write - write data to stream
*
*  SYNOPSIS
*
*  int glp_write(glp_file *f, const void *buf, int nnn);
*
*  DESCRIPTION
*
*  The routine glp_write writes, from the buffer pointed to by buf, up
*  to nnn bytes, to the stream pointed to by f.
*
*  RETURNS
*
*  The routine glp_write returns the number of bytes successfully
*  written (which is equal to nnn). If a write error occurs, the error
*  indicator for the stream is set and glp_write returns a negative
*  value. */

int glp_write(glp_file *f, const void *buf, int nnn)
{     int nwr, cnt;
      if (!(f->flag & IOWRT))
         xerror("glp_write: attempt to write to input stream\n");
      if (nnn < 1)
         xerror("glp_write: nnn = %d; invalid parameter\n", nnn);
      for (nwr = 0; nwr < nnn; nwr += cnt)
      {  cnt = nnn - nwr;
         if (cnt > f->size - f->cnt)
            cnt = f->size - f->cnt;
         memcpy(f->ptr, (const char *)buf + nwr, cnt);
         f->ptr += cnt;
         f->cnt += cnt;
         if (f->cnt == f->size)
         {  /* buffer is full; flush it */
            if (do_flush(f) != 0)
               return EOF;
         }
      }
      return nwr;
}

/***********************************************************************
*  NAME
*
*  glp_format - write formatted data to stream
*
*  SYNOPSIS
*
*  int glp_format(glp_file *f, const char *fmt, ...);
*
*  DESCRIPTION
*
*  The routine glp_format writes formatted data to the stream pointed
*  to by f. The format control string pointed to by fmt specifies how
*  subsequent arguments are converted for output.
*
*  RETURNS
*
*  The routine glp_format returns the number of characters written, or
*  a negative value if an output error occurs. */

int glp_format(glp_file *f, const char *fmt, ...)
{     ENV *env = get_env_ptr();
      va_list arg;
      int nnn;
      if (!(f->flag & IOWRT))
         xerror("glp_format: attempt to write to input stream\n");
      va_start(arg, fmt);
      nnn = vsprintf(env->term_buf, fmt, arg);
      xassert(0 <= nnn && nnn < TBUF_SIZE);
      va_end(arg);
      return nnn == 0 ? 0 : glp_write(f, env->term_buf, nnn);
}

/***********************************************************************
*  NAME
*
*  glp_close - close stream
*
*  SYNOPSIS
*
*  int glp_close(glp_file *f);
*
*  DESCRIPTION
*
*  The routine glp_close closes the stream pointed to by f.
*
*  RETURNS
*
*  If the operation was successful, the routine returns zero, otherwise
*  non-zero. */

int glp_close(glp_file *f)
{     int ret = 0;
      if (f->flag & IOWRT)
      {  if (do_flush(f) != 0)
            ret = EOF;
      }
      if (f->flag & (IONULL | IOSTD))
         ;
      else if (!(f->flag & IOGZIP))
      {  if (fclose((FILE *)(f->file)) != 0)
         {  if (ret == 0)
#if 0 /* 29/I-2017 */
            {  put_err_msg(strerror(errno));
#else
            {  put_err_msg(xstrerr(errno));
#endif
               ret = EOF;
            }
         }
      }
      else
      {  int errnum;
         errnum = gzclose((gzFile)(f->file));
         if (errnum == Z_OK)
            ;
         else if (errnum == Z_ERRNO)
         {  if (ret == 0)
#if 0 /* 29/I-2017 */
            {  put_err_msg(strerror(errno));
#else
            {  put_err_msg(xstrerr(errno));
#endif
               ret = EOF;
            }
         }
#if 1 /* FIXME */
         else
         {  if (ret == 0)
            {  ENV *env = get_env_ptr();
               sprintf(env->term_buf, "gzclose returned %d", errnum);
               put_err_msg(env->term_buf);
               ret = EOF;
            }
         }
#endif
      }
      tfree(f->base);
      tfree(f);
      return ret;
}

/* eof */