Details | 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 <ll/math.h> |
||
30 | #include "sprintf.h" |
||
31 | |||
32 | FILE(ucvt); |
||
33 | |||
34 | unsigned ucvt(unsigned long v,char *buffer,int base,int width,int flag) |
||
35 | { |
||
36 | register int i = 0,j; |
||
37 | unsigned ret = 0,abs_base; |
||
38 | unsigned long abs_v; |
||
39 | char tmp[12]; |
||
40 | /* Extract the less significant digit */ |
||
41 | /* Put it into temporary buffer */ |
||
42 | /* It has to be local to have */ |
||
43 | /* reentrant functions */ |
||
44 | /* |
||
45 | MG: fix to handle zero correctly |
||
46 | if (v == 0) { |
||
47 | *buffer++ = '0'; |
||
48 | *buffer = 0; |
||
49 | return(1); |
||
50 | } |
||
51 | */ |
||
52 | |||
53 | /* MG: is this required? */ |
||
54 | /* the vsprintf() function seems to call this procedure with */ |
||
55 | /* this flag inverted */ |
||
56 | flag ^= LEFT_PAD; |
||
57 | |||
58 | abs_base = (base >= 0) ? base : -base; |
||
59 | if (base < 0) abs_v = ((long)(v) < 0) ? -v : v; |
||
60 | else abs_v = v; |
||
61 | /* Process left space-padding flags */ |
||
62 | if (flag & ADD_PLUS || ((base < 0) && ((long)(v) < 0))) { |
||
63 | ret++; |
||
64 | } |
||
65 | /* MG: fix to handle zero correctly */ |
||
66 | if (v == 0) |
||
67 | tmp[i++]='0'; |
||
68 | else |
||
69 | while (abs_v > 0) { |
||
70 | tmp[i++] = todigit(abs_v % abs_base); |
||
71 | abs_v = abs_v / abs_base; |
||
72 | } |
||
73 | ret += i; |
||
74 | if ((flag & LEFT_PAD) && (flag & SPACE_PAD)) { |
||
75 | j = ret; |
||
76 | while (j < width) { |
||
77 | *buffer++ = ' '; |
||
78 | ret++; |
||
79 | j++; |
||
80 | } |
||
81 | } |
||
82 | /* Add plus if wanted */ |
||
83 | if (base < 0) { |
||
84 | if (((long)(v)) < 0) *buffer++ = '-'; |
||
85 | else if (flag & ADD_PLUS) *buffer++ = '+'; |
||
86 | } else if (flag & ADD_PLUS) *buffer++ = '+'; |
||
87 | /* Process left zero-padding flags */ |
||
88 | if ((flag & LEFT_PAD) && (flag & ZERO_PAD)) { |
||
89 | j = ret; |
||
90 | while (j < width) { |
||
91 | *buffer++ = '0'; |
||
92 | ret++; |
||
93 | j++; |
||
94 | } |
||
95 | } |
||
96 | /* Reverse temporary buffer into output buffer */ |
||
97 | /* If wanted left pad with zero/space; anyway only one at once is ok */ |
||
98 | for (j = i-1; j >= 0; j--) *buffer++ = tmp[j]; |
||
99 | if ((flag & (SPACE_PAD)) && !(flag & LEFT_PAD)) { |
||
100 | /* If wanted, pad with space to specified width */ |
||
101 | j = ret; |
||
102 | while (j < width) { |
||
103 | *buffer++ = ' '; |
||
104 | ret++; |
||
105 | j++; |
||
106 | } |
||
107 | } |
||
108 | /* Null terminate the output buffer */ |
||
109 | *buffer = 0; |
||
110 | return(ret); |
||
111 | } |
||
112 | |||
113 | unsigned dcvt(long v,char *buffer,int base,int width,int flag) |
||
114 | { |
||
115 | return(ucvt((unsigned long)(v),buffer,-base,width,flag)); |
||
116 | } |