/farmR/src/glplib03.c

https://code.google.com/p/javawfm/ · C · 141 lines · 44 code · 13 blank · 84 comment · 6 complexity · 9133491e911ad0d8d38558380713f899 MD5 · raw file

  1. /* glplib03.c (error handling) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000,01,02,03,04,05,06,07,08,2009 Andrew Makhorin,
  6. * Department for Applied Informatics, Moscow Aviation Institute,
  7. * Moscow, Russia. All rights reserved. E-mail: <mao@mai2.rcnet.ru>.
  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. #define _GLPSTD_STDIO
  23. #include "glplib.h"
  24. #include "R.h"
  25. /***********************************************************************
  26. * NAME
  27. *
  28. * xassert - check for logical condition
  29. *
  30. * SYNOPSIS
  31. *
  32. * #include "glplib.h"
  33. * void xassert(int expr);
  34. *
  35. * DESCRIPTION
  36. *
  37. * The routine xassert (implemented as a macro) checks for a logical
  38. * condition specified by the parameter expr. If the condition is false
  39. * (i.e. the value of expr is zero), the routine writes a message to
  40. * the terminal and abnormally terminates the program. */
  41. void lib_xassert(const char *expr, const char *file, int line)
  42. { lib_xerror1(file, line)("Assertion failed: %s\n", expr);
  43. /* no return */
  44. }
  45. /***********************************************************************
  46. * NAME
  47. *
  48. * xerror - display error message and terminate execution
  49. *
  50. * SYNOPSIS
  51. *
  52. * #include "glplib.h"
  53. * void xerror(const char *fmt, ...);
  54. *
  55. * DESCRIPTION
  56. *
  57. * The routine xerror (implemented as a macro) formats its parameters
  58. * under the format control string fmt, writes the formatted message to
  59. * the terminal, and abnormally terminates the program. */
  60. xerror_t lib_xerror1(const char *file, int line)
  61. { LIBENV *env = lib_link_env();
  62. env->err_file = file;
  63. env->err_line = line;
  64. return lib_xerror2;
  65. }
  66. void lib_xerror2(const char *fmt, ...)
  67. { LIBENV *env = lib_link_env();
  68. va_list arg;
  69. va_start(arg, fmt);
  70. xvprintf(fmt, arg);
  71. va_end(arg);
  72. xprintf("Error detected in file %s at line %d\n",
  73. env->err_file, env->err_line);
  74. fflush(stdout);
  75. fflush(stderr);
  76. error("Execution aborted.");
  77. /* no return */
  78. }
  79. void lib_fault_hook(int (*func)(void *info, char *buf), void *info)
  80. { /* (obsolete) */
  81. xassert(func == func);
  82. xassert(info == info);
  83. return;
  84. }
  85. /***********************************************************************
  86. * NAME
  87. *
  88. * lib_err_msg - save error message string
  89. *
  90. * SYNOPSIS
  91. *
  92. * #include "glplib.h"
  93. * void lib_err_msg(const char *msg);
  94. *
  95. * DESCRIPTION
  96. *
  97. * The routine lib_err_msg saves an error message string specified by
  98. * the parameter msg. The message is obtained by some library routines
  99. * with a call to strerror(errno). */
  100. void lib_err_msg(const char *msg)
  101. { LIBENV *env = lib_link_env();
  102. int len = strlen(msg);
  103. if (len >= sizeof(env->err_msg))
  104. len = sizeof(env->err_msg) - 1;
  105. memcpy(env->err_msg, msg, len);
  106. if (len > 0 && env->err_msg[len-1] == '\n') len--;
  107. env->err_msg[len] = '\0';
  108. return;
  109. }
  110. /***********************************************************************
  111. * NAME
  112. *
  113. * xerrmsg - retrieve error message string
  114. *
  115. * SYNOPSIS
  116. *
  117. * #include "glplib.h"
  118. * const char *xerrmsg(void);
  119. *
  120. * RETURNS
  121. *
  122. * The routine xerrmsg returns a pointer to an error message string
  123. * previously set by some library routine to indicate an error. */
  124. const char *xerrmsg(void)
  125. { LIBENV *env = lib_link_env();
  126. return env->err_msg;
  127. }
  128. /* eof */