PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/glpk/src/env/error.c

https://bitbucket.org/alpar/lemon-project-template-with-glpk
C | 170 lines | 52 code | 14 blank | 104 comment | 8 complexity | 6004da7d4768fa283ca25095271297a9 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. /* error.c (error handling) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2013 Andrew Makhorin, Department for Applied
  6. * Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
  7. * reserved. E-mail: <mao@gnu.org>.
  8. *
  9. * GLPK is free software: you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  21. ***********************************************************************/
  22. #include "env.h"
  23. /***********************************************************************
  24. * NAME
  25. *
  26. * glp_error - display fatal error message and terminate execution
  27. *
  28. * SYNOPSIS
  29. *
  30. * void glp_error(const char *fmt, ...);
  31. *
  32. * DESCRIPTION
  33. *
  34. * The routine glp_error (implemented as a macro) formats its
  35. * parameters using the format control string fmt, writes the formatted
  36. * message on the terminal, and abnormally terminates the program. */
  37. static void errfunc(const char *fmt, ...)
  38. { ENV *env = get_env_ptr();
  39. va_list arg;
  40. env->term_out = GLP_ON;
  41. va_start(arg, fmt);
  42. xvprintf(fmt, arg);
  43. va_end(arg);
  44. xprintf("Error detected in file %s at line %d\n",
  45. env->err_file, env->err_line);
  46. if (env->err_hook != NULL)
  47. env->err_hook(env->err_info);
  48. abort();
  49. exit(EXIT_FAILURE);
  50. /* no return */
  51. }
  52. glp_errfunc glp_error_(const char *file, int line)
  53. { ENV *env = get_env_ptr();
  54. env->err_file = file;
  55. env->err_line = line;
  56. return errfunc;
  57. }
  58. /***********************************************************************
  59. * NAME
  60. *
  61. * glp_assert - check for logical condition
  62. *
  63. * SYNOPSIS
  64. *
  65. * void glp_assert(int expr);
  66. *
  67. * DESCRIPTION
  68. *
  69. * The routine glp_assert (implemented as a macro) checks for a logical
  70. * condition specified by the parameter expr. If the condition is false
  71. * (i.e. the value of expr is zero), the routine writes a message on
  72. * the terminal and abnormally terminates the program. */
  73. void glp_assert_(const char *expr, const char *file, int line)
  74. { glp_error_(file, line)("Assertion failed: %s\n", expr);
  75. /* no return */
  76. }
  77. /***********************************************************************
  78. * NAME
  79. *
  80. * glp_error_hook - install hook to intercept abnormal termination
  81. *
  82. * SYNOPSIS
  83. *
  84. * void glp_error_hook(void (*func)(void *info), void *info);
  85. *
  86. * DESCRIPTION
  87. *
  88. * The routine glp_error_hook installs a user-defined hook routine to
  89. * intercept abnormal termination.
  90. *
  91. * The parameter func specifies the user-defined hook routine. It is
  92. * called from the routine glp_error before the latter calls the abort
  93. * function to abnormally terminate the application program because of
  94. * fatal error. The parameter info is a transit pointer, specified in
  95. * the corresponding call to the routine glp_error_hook; it may be used
  96. * to pass some information to the hook routine.
  97. *
  98. * To uninstall the hook routine the parameters func and info should be
  99. * both specified as NULL. */
  100. void glp_error_hook(void (*func)(void *info), void *info)
  101. { ENV *env = get_env_ptr();
  102. if (func == NULL)
  103. { env->err_hook = NULL;
  104. env->err_info = NULL;
  105. }
  106. else
  107. { env->err_hook = func;
  108. env->err_info = info;
  109. }
  110. return;
  111. }
  112. /***********************************************************************
  113. * NAME
  114. *
  115. * put_err_msg - provide error message string
  116. *
  117. * SYNOPSIS
  118. *
  119. * #include "env.h"
  120. * void put_err_msg(const char *msg);
  121. *
  122. * DESCRIPTION
  123. *
  124. * The routine put_err_msg stores an error message string pointed to by
  125. * msg to the environment block. */
  126. void put_err_msg(const char *msg)
  127. { ENV *env = get_env_ptr();
  128. int len;
  129. len = strlen(msg);
  130. if (len >= EBUF_SIZE)
  131. len = EBUF_SIZE - 1;
  132. memcpy(env->err_buf, msg, len);
  133. if (len > 0 && env->err_buf[len-1] == '\n')
  134. len--;
  135. env->err_buf[len] = '\0';
  136. return;
  137. }
  138. /***********************************************************************
  139. * NAME
  140. *
  141. * get_err_msg - obtain error message string
  142. *
  143. * SYNOPSIS
  144. *
  145. * #include "env.h"
  146. * const char *get_err_msg(void);
  147. *
  148. * RETURNS
  149. *
  150. * The routine get_err_msg returns a pointer to an error message string
  151. * previously stored by the routine put_err_msg. */
  152. const char *get_err_msg(void)
  153. { ENV *env = get_env_ptr();
  154. return env->err_buf;
  155. }
  156. /* eof */