src/glpenv03.c
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/glpenv03.c	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,228 @@
     1.4 +/* glpenv03.c (terminal output) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.10 +*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
    1.11 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.12 +*  E-mail: <mao@gnu.org>.
    1.13 +*
    1.14 +*  GLPK is free software: you can redistribute it and/or modify it
    1.15 +*  under the terms of the GNU General Public License as published by
    1.16 +*  the Free Software Foundation, either version 3 of the License, or
    1.17 +*  (at your option) any later version.
    1.18 +*
    1.19 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.20 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.21 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.22 +*  License for more details.
    1.23 +*
    1.24 +*  You should have received a copy of the GNU General Public License
    1.25 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.26 +***********************************************************************/
    1.27 +
    1.28 +#include "glpapi.h"
    1.29 +
    1.30 +/***********************************************************************
    1.31 +*  NAME
    1.32 +*
    1.33 +*  glp_printf - write formatted output to terminal
    1.34 +*
    1.35 +*  SYNOPSIS
    1.36 +*
    1.37 +*  void glp_printf(const char *fmt, ...);
    1.38 +*
    1.39 +*  DESCRIPTION
    1.40 +*
    1.41 +*  The routine glp_printf uses the format control string fmt to format
    1.42 +*  its parameters and writes the formatted output to the terminal. */
    1.43 +
    1.44 +void glp_printf(const char *fmt, ...)
    1.45 +{     va_list arg;
    1.46 +      va_start(arg, fmt);
    1.47 +      xvprintf(fmt, arg);
    1.48 +      va_end(arg);
    1.49 +      return;
    1.50 +}
    1.51 +
    1.52 +/***********************************************************************
    1.53 +*  NAME
    1.54 +*
    1.55 +*  glp_vprintf - write formatted output to terminal
    1.56 +*
    1.57 +*  SYNOPSIS
    1.58 +*
    1.59 +*  void glp_vprintf(const char *fmt, va_list arg);
    1.60 +*
    1.61 +*  DESCRIPTION
    1.62 +*
    1.63 +*  The routine glp_vprintf uses the format control string fmt to format
    1.64 +*  its parameters specified by the list arg and writes the formatted
    1.65 +*  output to the terminal. */
    1.66 +
    1.67 +void glp_vprintf(const char *fmt, va_list arg)
    1.68 +{     ENV *env = get_env_ptr();
    1.69 +      /* if terminal output is disabled, do nothing */
    1.70 +      if (!env->term_out) goto skip;
    1.71 +      /* format the output */
    1.72 +      vsprintf(env->term_buf, fmt, arg);
    1.73 +      /* pass the output to the user-defined routine */
    1.74 +      if (env->term_hook != NULL)
    1.75 +      {  if (env->term_hook(env->term_info, env->term_buf) != 0)
    1.76 +            goto skip;
    1.77 +      }
    1.78 +      /* send the output to the terminal */
    1.79 +      fputs(env->term_buf, stdout);
    1.80 +      fflush(stdout);
    1.81 +      /* copy the output to the text file */
    1.82 +      if (env->tee_file != NULL)
    1.83 +      {  fputs(env->term_buf, env->tee_file);
    1.84 +         fflush(env->tee_file);
    1.85 +      }
    1.86 +skip: return;
    1.87 +}
    1.88 +
    1.89 +/***********************************************************************
    1.90 +*  NAME
    1.91 +*
    1.92 +*  glp_term_out - enable/disable terminal output
    1.93 +*
    1.94 +*  SYNOPSIS
    1.95 +*
    1.96 +*  int glp_term_out(int flag);
    1.97 +*
    1.98 +*  DESCRIPTION
    1.99 +*
   1.100 +*  Depending on the parameter flag the routine glp_term_out enables or
   1.101 +*  disables terminal output performed by glpk routines:
   1.102 +*
   1.103 +*  GLP_ON  - enable terminal output;
   1.104 +*  GLP_OFF - disable terminal output.
   1.105 +*
   1.106 +*  RETURNS
   1.107 +*
   1.108 +*  The routine glp_term_out returns the previous value of the terminal
   1.109 +*  output flag. */
   1.110 +
   1.111 +int glp_term_out(int flag)
   1.112 +{     ENV *env = get_env_ptr();
   1.113 +      int old = env->term_out;
   1.114 +      if (!(flag == GLP_ON || flag == GLP_OFF))
   1.115 +         xerror("glp_term_out: flag = %d; invalid value\n", flag);
   1.116 +      env->term_out = flag;
   1.117 +      return old;
   1.118 +}
   1.119 +
   1.120 +/***********************************************************************
   1.121 +*  NAME
   1.122 +*
   1.123 +*  glp_term_hook - install hook to intercept terminal output
   1.124 +*
   1.125 +*  SYNOPSIS
   1.126 +*
   1.127 +*  void glp_term_hook(int (*func)(void *info, const char *s),
   1.128 +*     void *info);
   1.129 +*
   1.130 +*  DESCRIPTION
   1.131 +*
   1.132 +*  The routine glp_term_hook installs a user-defined hook routine to
   1.133 +*  intercept all terminal output performed by glpk routines.
   1.134 +*
   1.135 +*  This feature can be used to redirect the terminal output to other
   1.136 +*  destination, for example to a file or a text window.
   1.137 +*
   1.138 +*  The parameter func specifies the user-defined hook routine. It is
   1.139 +*  called from an internal printing routine, which passes to it two
   1.140 +*  parameters: info and s. The parameter info is a transit pointer,
   1.141 +*  specified in the corresponding call to the routine glp_term_hook;
   1.142 +*  it may be used to pass some information to the hook routine. The
   1.143 +*  parameter s is a pointer to the null terminated character string,
   1.144 +*  which is intended to be written to the terminal. If the hook routine
   1.145 +*  returns zero, the printing routine writes the string s to the
   1.146 +*  terminal in a usual way; otherwise, if the hook routine returns
   1.147 +*  non-zero, no terminal output is performed.
   1.148 +*
   1.149 +*  To uninstall the hook routine the parameters func and info should be
   1.150 +*  specified as NULL. */
   1.151 +
   1.152 +void glp_term_hook(int (*func)(void *info, const char *s), void *info)
   1.153 +{     ENV *env = get_env_ptr();
   1.154 +      if (func == NULL)
   1.155 +      {  env->term_hook = NULL;
   1.156 +         env->term_info = NULL;
   1.157 +      }
   1.158 +      else
   1.159 +      {  env->term_hook = func;
   1.160 +         env->term_info = info;
   1.161 +      }
   1.162 +      return;
   1.163 +}
   1.164 +
   1.165 +/***********************************************************************
   1.166 +*  NAME
   1.167 +*
   1.168 +*  glp_open_tee - start copying terminal output to text file
   1.169 +*
   1.170 +*  SYNOPSIS
   1.171 +*
   1.172 +*  int glp_open_tee(const char *fname);
   1.173 +*
   1.174 +*  DESCRIPTION
   1.175 +*
   1.176 +*  The routine glp_open_tee starts copying all the terminal output to
   1.177 +*  an output text file, whose name is specified by the character string
   1.178 +*  fname.
   1.179 +*
   1.180 +*  RETURNS
   1.181 +*
   1.182 +*  0 - operation successful
   1.183 +*  1 - copying terminal output is already active
   1.184 +*  2 - unable to create output file */
   1.185 +
   1.186 +int glp_open_tee(const char *fname)
   1.187 +{     ENV *env = get_env_ptr();
   1.188 +      if (env->tee_file != NULL)
   1.189 +      {  /* copying terminal output is already active */
   1.190 +         return 1;
   1.191 +      }
   1.192 +      env->tee_file = fopen(fname, "w");
   1.193 +      if (env->tee_file == NULL)
   1.194 +      {  /* unable to create output file */
   1.195 +         return 2;
   1.196 +      }
   1.197 +      return 0;
   1.198 +}
   1.199 +
   1.200 +/***********************************************************************
   1.201 +*  NAME
   1.202 +*
   1.203 +*  glp_close_tee - stop copying terminal output to text file
   1.204 +*
   1.205 +*  SYNOPSIS
   1.206 +*
   1.207 +*  int glp_close_tee(void);
   1.208 +*
   1.209 +*  DESCRIPTION
   1.210 +*
   1.211 +*  The routine glp_close_tee stops copying the terminal output to the
   1.212 +*  output text file previously open by the routine glp_open_tee closing
   1.213 +*  that file.
   1.214 +*
   1.215 +*  RETURNS
   1.216 +*
   1.217 +*  0 - operation successful
   1.218 +*  1 - copying terminal output was not started */
   1.219 +
   1.220 +int glp_close_tee(void)
   1.221 +{     ENV *env = get_env_ptr();
   1.222 +      if (env->tee_file == NULL)
   1.223 +      {  /* copying terminal output was not started */
   1.224 +         return 1;
   1.225 +      }
   1.226 +      fclose(env->tee_file);
   1.227 +      env->tee_file = NULL;
   1.228 +      return 0;
   1.229 +}
   1.230 +
   1.231 +/* eof */