1 : /* glplib07.c (stream input/output) */
2 :
3 : /***********************************************************************
4 : * This code is part of GLPK (GNU Linear Programming Kit).
5 : *
6 : * Copyright (C) 2000, 01, 02, 03, 04, 05, 06, 07 Andrew Makhorin,
7 : * Department for Applied Informatics, Moscow Aviation Institute,
8 : * Moscow, Russia. All rights reserved. E-mail: <mao@mai2.rcnet.ru>.
9 : *
10 : * GLPK is free software: you can redistribute it and/or modify it
11 : * under the terms of the GNU General Public License as published by
12 : * the Free Software Foundation, either version 3 of the License, or
13 : * (at your option) any later version.
14 : *
15 : * GLPK is distributed in the hope that it will be useful, but WITHOUT
16 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 : * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 : * License for more details.
19 : *
20 : * You should have received a copy of the GNU General Public License
21 : * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
22 : ***********************************************************************/
23 :
24 : #include "glplib.h"
25 :
26 : /***********************************************************************
27 : * NAME
28 : *
29 : * xfopen - open file
30 : *
31 : * SYNOPSIS
32 : *
33 : * #include "glplib.h"
34 : * FILE *xfopen(const char *fname, const char *mode);
35 : *
36 : * DESCRIPTION
37 : *
38 : * The routine xfopen opens a file using the character string fname as
39 : * the file name and the character string mode as the open mode.
40 : *
41 : * RETURNS
42 : *
43 : * If the file has been open successfully, the routine xfopen returns a
44 : * pointer to an i/o stream associated with the file (i.e. a pointer to
45 : * an object of the FILE type). Otherwise the routine return NULL. */
46 :
47 : FILE *xfopen(const char *fname, const char *mode)
48 8 : { LIBENV *env = lib_link_env();
49 : int k;
50 : /* find free slot */
51 8 : for (k = 0; k < LIB_MAX_OPEN; k++)
52 8 : if (env->file_slot[k] == NULL) break;
53 8 : if (k == LIB_MAX_OPEN)
54 0 : xfault("xfopen: too many open files\n");
55 : /* open a file and store a pointer to the i/o stream */
56 8 : env->file_slot[k] = fopen(fname, mode);
57 8 : return env->file_slot[k];
58 : }
59 :
60 : /***********************************************************************
61 : * NAME
62 : *
63 : * xfclose - close file
64 : *
65 : * SYNOPSIS
66 : *
67 : * #include "glplib.h"
68 : * void xfclose(FILE *fp);
69 : *
70 : * DESCRIPTION
71 : *
72 : * The routine xfclose closes a file associated with i/o stream, which
73 : * the parameter fp points to. It is assumed that the file was open by
74 : * the routine xfopen. */
75 :
76 : void xfclose(FILE *fp)
77 8 : { LIBENV *env = lib_link_env();
78 : int k;
79 : /* check if the i/o stream pointer is valid */
80 8 : if (fp == NULL)
81 0 : xfault("xfclose: fp = %p; null i/o stream\n", fp);
82 8 : for (k = 0; k < LIB_MAX_OPEN; k++)
83 8 : if (env->file_slot[k] == fp) break;
84 8 : if (k == LIB_MAX_OPEN)
85 0 : xfault("xfclose: fp = %p; invalid i/o stream\n", fp);
86 : /* close a file and free the corresponding slot */
87 8 : fclose(fp);
88 8 : env->file_slot[k] = NULL;
89 : return;
90 : }
91 :
92 : /**********************************************************************/
93 :
94 : int lib_open_log(const char *fname)
95 0 : { /* open hardcopy file */
96 0 : LIBENV *env = lib_link_env();
97 0 : if (env->log_file != NULL)
98 : { /* hardcopy file is already open */
99 0 : return 1;
100 : }
101 0 : env->log_file = xfopen(fname, "w");
102 0 : if (env->log_file == NULL)
103 : { /* cannot create hardcopy file */
104 0 : return 2;
105 : }
106 0 : setvbuf(env->log_file, NULL, _IOLBF, BUFSIZ);
107 0 : return 0;
108 : }
109 :
110 : /**********************************************************************/
111 :
112 : int lib_close_log(void)
113 0 : { /* close hardcopy file */
114 0 : LIBENV *env = lib_link_env();
115 0 : if (env->log_file == NULL)
116 : { /* hardcopy file is already closed */
117 0 : return 1;
118 : }
119 0 : xfclose(env->log_file);
120 0 : env->log_file = NULL;
121 0 : return 0;
122 : }
123 :
124 : /* eof */
|