PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/libreoffice-3.6.0.2/dmake/dbug/malloc/tostring.c

#
C | 132 lines | 70 code | 13 blank | 49 comment | 6 complexity | f48033a96cb51abffc01c2e1ffeac42f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, AGPL-1.0, BSD-3-Clause-No-Nuclear-License-2014, GPL-3.0, LGPL-3.0
  1. /*
  2. * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
  3. * You may copy, distribute, and use this software as long as this
  4. * copyright statement is not removed.
  5. */
  6. #include "tostring.h"
  7. /*
  8. * Function: tostring()
  9. *
  10. * Purpose: to convert an integer to an ascii display string
  11. *
  12. * Arguments: buf - place to put the
  13. * val - integer to convert
  14. * len - length of output field (0 if just enough to hold data)
  15. * base - base for number conversion (only works for base <= 16)
  16. * fill - fill char when len > # digits
  17. *
  18. * Returns: length of string
  19. *
  20. * Narrative: IF fill character is non-blank
  21. * Determine base
  22. * If base is HEX
  23. * add "0x" to begining of string
  24. * IF base is OCTAL
  25. * add "0" to begining of string
  26. *
  27. * While value is greater than zero
  28. * use val % base as index into xlation str to get cur char
  29. * divide val by base
  30. *
  31. * Determine fill-in length
  32. *
  33. * Fill in fill chars
  34. *
  35. * Copy in number
  36. *
  37. *
  38. * Mod History:
  39. * 90/01/24 cpcahil Initial revision.
  40. */
  41. #ifndef lint
  42. static
  43. char rcs_hdr[] = "$Id: tostring.c,v 1.2 2006-07-25 10:10:17 rt Exp $";
  44. #endif
  45. #define T_LEN 10
  46. int
  47. tostring(buf,val,len,base,fill)
  48. int base;
  49. char * buf;
  50. char fill;
  51. int len;
  52. int val;
  53. {
  54. char * bufstart = buf;
  55. int i = T_LEN;
  56. char * xbuf = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  57. char tbuf[T_LEN];
  58. /*
  59. * if we are filling with non-blanks, make sure the
  60. * proper start string is added
  61. */
  62. if( fill != ' ' )
  63. {
  64. switch(base)
  65. {
  66. case B_HEX:
  67. *(buf++) = '0';
  68. *(buf++) = 'x';
  69. if( len )
  70. {
  71. len -= 2;
  72. }
  73. break;
  74. case B_OCTAL:
  75. *(buf++) = fill;
  76. if( len )
  77. {
  78. len--;
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. while( val > 0 )
  86. {
  87. tbuf[--i] = xbuf[val % base];
  88. val = val / base;
  89. }
  90. if( len )
  91. {
  92. len -= (T_LEN - i);
  93. if( len > 0 )
  94. {
  95. while(len-- > 0)
  96. {
  97. *(buf++) = fill;
  98. }
  99. }
  100. else
  101. {
  102. /*
  103. * string is too long so we must truncate
  104. * off some characters. We do this the easiest
  105. * way by just incrementing i. This means the
  106. * most significant digits are lost.
  107. */
  108. while( len++ < 0 )
  109. {
  110. i++;
  111. }
  112. }
  113. }
  114. while( i < T_LEN )
  115. {
  116. *(buf++) = tbuf[i++];
  117. }
  118. return( (int) (buf - bufstart) );
  119. } /* tostring(... */