summaryrefslogtreecommitdiff
path: root/dmake/dbug/malloc/tostring.c
blob: e3bc9990271d074772f1daf624996f3e763484d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
 * You may copy, distribute, and use this software as long as this
 * copyright statement is not removed.
 */
#include "tostring.h"

/*
 * Function:    tostring()
 *
 * Purpose: to convert an integer to an ascii display string
 *
 * Arguments:   buf - place to put the
 *      val - integer to convert
 *      len - length of output field (0 if just enough to hold data)
 *      base    - base for number conversion (only works for base <= 16)
 *      fill    - fill char when len > # digits
 *
 * Returns: length of string
 *
 * Narrative:   IF fill character is non-blank
 *          Determine base
 *              If base is HEX
 *                  add "0x" to begining of string
 *              IF base is OCTAL
 *                  add "0" to begining of string
 *
 *      While value is greater than zero
 *          use val % base as index into xlation str to get cur char
 *          divide val by base
 *
 *      Determine fill-in length
 *
 *      Fill in fill chars
 *
 *      Copy in number
 *
 *
 * Mod History:
 *   90/01/24   cpcahil     Initial revision.
 */

#ifndef lint
static
char rcs_hdr[] = "$Id: tostring.c,v 1.2 2006-07-25 10:10:17 rt Exp $";
#endif

#define T_LEN 10

int
tostring(buf,val,len,base,fill)
    int   base;
    char    * buf;
    char      fill;
    int   len;
    int   val;

{
    char    * bufstart = buf;
    int   i = T_LEN;
    char    * xbuf = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char      tbuf[T_LEN];

    /*
     * if we are filling with non-blanks, make sure the
     * proper start string is added
     */
    if( fill != ' ' )
    {
        switch(base)
        {
            case B_HEX:
                *(buf++) = '0';
                *(buf++) = 'x';
                if( len )
                {
                    len -= 2;
                }
                break;
            case B_OCTAL:
                *(buf++) = fill;
                if( len )
                {
                    len--;
                }
                break;
            default:
                break;
        }
    }

    while( val > 0 )
    {
        tbuf[--i] = xbuf[val % base];
        val = val / base;
    }

    if( len )
    {
        len -= (T_LEN - i);

        if( len > 0 )
        {
            while(len-- > 0)
            {
                *(buf++) = fill;
            }
        }
        else
        {
            /*
             * string is too long so we must truncate
             * off some characters.  We do this the easiest
             * way by just incrementing i.  This means the
             * most significant digits are lost.
             */
            while( len++ < 0 )
            {
                i++;
            }
        }
    }

    while( i < T_LEN )
    {
        *(buf++) = tbuf[i++];
    }

    return( (int) (buf - bufstart) );

} /* tostring(... */