/farmR/src/glplib09.c

https://code.google.com/p/javawfm/ · C · 95 lines · 37 code · 8 blank · 50 comment · 4 complexity · 377be40e9eb09d6038860c06fef217e8 MD5 · raw file

  1. /* glplib09.c (formatted input/output) */
  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. /***********************************************************************
  25. * NAME
  26. *
  27. * lib_doprnt - perform formatted output (basic routine)
  28. *
  29. * SYNOPSIS
  30. *
  31. * #include "glplib.h"
  32. * int lib_doprnt(int (*func)(void *info, int c), void *info,
  33. * const char *fmt, va_list arg);
  34. *
  35. * DESCRIPTION
  36. *
  37. * The routine xdoprnt is a basic routine to perform formatted output.
  38. * It is equivalent to the standard routine vfprintf, except that the
  39. * output is passed to the formal routine func rather than to an output
  40. * stream.
  41. *
  42. * The formal routine func is called every time a next character should
  43. * be written. If there is an output error, it should return a negative
  44. * value, in which case the output is terminated.
  45. *
  46. * RETURNS
  47. *
  48. * The routine xdoprnt returns the number of characters passed to the
  49. * formal routine func. However, if there was an output error (detected
  50. * by the formal routine func), a negative value is returned. */
  51. int lib_doprnt(int (*func)(void *info, int c), void *info,
  52. const char *fmt, va_list arg)
  53. { int cnt, j;
  54. char buf[4000+1];
  55. #ifdef HAVE_VSNPRINTF
  56. cnt = vsnprintf(buf, sizeof(buf), fmt, arg);
  57. #else
  58. cnt = vsprintf(buf, fmt, arg);
  59. #endif
  60. xassert(0 <= cnt && cnt < sizeof(buf));
  61. xassert((int)strlen(buf) == cnt);
  62. for (j = 0; j < cnt; j++)
  63. { if (func(info, (unsigned char)buf[j]) < 0)
  64. { cnt = -1;
  65. break;
  66. }
  67. }
  68. return cnt;
  69. }
  70. /**********************************************************************/
  71. static int my_put(void *file, int c)
  72. { return
  73. xfputc(c, file);
  74. }
  75. int xfprintf(XFILE *file, const char *fmt, ...)
  76. { va_list arg;
  77. int ret;
  78. va_start(arg, fmt);
  79. #if 0
  80. ret = vfprintf(file->fp, fmt, arg);
  81. #else
  82. ret = lib_doprnt(my_put, file, fmt, arg);
  83. #endif
  84. va_end(arg);
  85. return ret;
  86. }
  87. /* eof */