PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-1-3-24/SWIG/Tools/WAD/Wad/io.c

#
C | 107 lines | 71 code | 8 blank | 28 comment | 12 complexity | 14e942147fa021935f6163aba7a7e00c MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * io.c
  3. *
  4. * This file provides some I/O routines so that WAD can produce
  5. * debugging output without using buffered I/O.
  6. *
  7. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  8. *
  9. * Copyright (C) 2000. The University of Chicago.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * See the file COPYING for a complete copy of the LGPL.
  26. * ----------------------------------------------------------------------------- */
  27. #include "wad.h"
  28. #include <stdarg.h>
  29. static char cvs[] = "$Header$";
  30. /* Utility functions used to generate strings that are guaranteed not to
  31. rely upon malloc() and related functions */
  32. char *wad_format_hex(unsigned long u, int leading) {
  33. static char result[64];
  34. int i;
  35. char *c;
  36. c = &result[63];
  37. *c = 0;
  38. for (i = 0; i < (sizeof(unsigned long)*2); i++) {
  39. int d;
  40. d = (int) (u & 0xf);
  41. c--;
  42. if (d < 10) {
  43. *c = '0' + d;
  44. } else {
  45. *c = 'a' + (d-10);
  46. }
  47. if (!u && !leading) break;
  48. u = u >> 4;
  49. }
  50. return c;
  51. }
  52. char *wad_format_unsigned(unsigned long u, int width) {
  53. static char result[128];
  54. static char digits[] = "0123456789";
  55. char *c, *w;
  56. int count = 0;
  57. int i;
  58. c = &result[64];
  59. while (u) {
  60. int digit = u % 10;
  61. *(--c) = digits[digit];
  62. count++;
  63. u = u / 10;
  64. }
  65. if (!count) {
  66. *(--c) = '0';
  67. count++;
  68. }
  69. w = &result[64];
  70. for (i = count; i < width; i++) {
  71. *(w++) = ' ';
  72. }
  73. *w = 0;
  74. return c;
  75. }
  76. char *wad_format_signed(signed long s, int width) {
  77. static char result[128];
  78. unsigned long u;
  79. char *c = result;
  80. if (s < 0) {
  81. *(c++) = '-';
  82. width--;
  83. u = (unsigned long) (-s);
  84. if (u == 0) {
  85. u = (unsigned long) s;
  86. }
  87. } else {
  88. u = (unsigned long) s;
  89. }
  90. *c = 0;
  91. wad_strcat(result, wad_format_unsigned(u,width));
  92. return result;
  93. }
  94. void wad_printf(const char *fmt, ...) {
  95. va_list ap;
  96. va_start(ap, fmt);
  97. vfprintf(stderr,fmt,ap);
  98. va_end(ap);
  99. }