Subversion Repositories shark

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/* Project:     OSLib
2
 * Description: The OS Construction Kit
3
 * Date:                1.6.2000
4
 * Idea by:             Luca Abeni & Gerardo Lamastra
5
 *
6
 * OSLib is an SO project aimed at developing a common, easy-to-use
7
 * low-level infrastructure for developing OS kernels and Embedded
8
 * Applications; it partially derives from the HARTIK project but it
9
 * currently is independently developed.
10
 *
11
 * OSLib is distributed under GPL License, and some of its code has
12
 * been derived from the Linux kernel source; also some important
13
 * ideas come from studying the DJGPP go32 extender.
14
 *
15
 * We acknowledge the Linux Community, Free Software Foundation,
16
 * D.J. Delorie and all the other developers who believe in the
17
 * freedom of software and ideas.
18
 *
19
 * For legalese, check out the included GPL license.
20
 */
21
 
22
#include <ll/i386/stdlib.h>
23
#include <ll/i386/string.h>
24
#include <ll/i386/limits.h>
25
#include <ll/i386/float.h>
26
#include <ll/i386/mem.h>
27
#include <ll/stdarg.h>
28
#include <ll/ctype.h>
29
#include "sprintf.h"
30
 
31
FILE(ucvt);
32
 
33
unsigned ucvt(unsigned long v,char *buffer,int base,int width,int flag)
34
{
35
    register int i = 0,j;
36
    unsigned ret = 0,abs_base;
37
    unsigned long abs_v;
38
    char tmp[12];
39
    /* Extract the less significant digit */
40
    /* Put it into temporary buffer       */
41
    /* It has to be local to have         */
42
    /* reentrant functions                */
43
    /*
44
    MG: fix to handle zero correctly
45
    if (v == 0) {
46
        *buffer++ = '0';
47
        *buffer = 0;
48
        return(1);
49
    }
50
    */
51
 
52
    /* MG: is this required? */
53
    /* the vsprintf() function seems to call this procedure with */
54
    /* this flag inverted */
55
    flag ^= LEFT_PAD;
56
 
57
    abs_base = (base >= 0) ? base : -base;
58
    if (base < 0) abs_v = ((long)(v) < 0) ? -v : v;
59
    else abs_v = v;
60
    /* Process left space-padding flags */
61
    if (flag & ADD_PLUS || ((base < 0) && ((long)(v) < 0))) {
62
        ret++;
63
    }
64
    /* MG: fix to handle zero correctly */
65
    if (v == 0)
66
        tmp[i++]='0';
67
    else
68
        while (abs_v > 0) {
69
            tmp[i++] = todigit(abs_v % abs_base);
70
            abs_v = abs_v / abs_base;
71
        }
72
    ret += i;
73
    if ((flag & LEFT_PAD) && (flag & SPACE_PAD)) {
74
        j = ret;
75
        while (j < width) {
76
            *buffer++ = ' ';
77
            ret++;
78
            j++;
79
        }
80
    }
81
    /* Add plus if wanted */
82
    if (base < 0) {
83
        if (((long)(v)) < 0) *buffer++ = '-';
84
        else if (flag & ADD_PLUS) *buffer++ = '+';
85
    } else if (flag & ADD_PLUS) *buffer++ = '+';
86
    /* Process left zero-padding flags */
87
    if ((flag & LEFT_PAD) && (flag & ZERO_PAD)) {
88
        j = ret;
89
        while (j < width) {
90
            *buffer++ = '0';
91
            ret++;
92
            j++;
93
        }
94
    }
95
    /* Reverse temporary buffer into output buffer */
96
    /* If wanted left pad with zero/space; anyway only one at once is ok */
97
    for (j = i-1; j >= 0; j--) *buffer++ = tmp[j];
98
    if ((flag & (SPACE_PAD)) && !(flag & LEFT_PAD)) {
99
        /* If wanted, pad with space to specified width */
100
        j = ret;
101
        while (j < width) {
102
            *buffer++ = ' ';
103
            ret++;
104
            j++;
105
        }
106
    }
107
    /* Null terminate the output buffer */
108
    *buffer = 0;
109
    return(ret);
110
}
111
 
112
unsigned dcvt(long v,char *buffer,int base,int width,int flag)
113
{
114
    return(ucvt((unsigned long)(v),buffer,-base,width,flag));
115
}