|
2
|
1 //======================================================================== |
|
|
2 // |
|
|
3 // itoa.cxx |
|
|
4 // |
|
|
5 // FIXME |
|
|
6 // |
|
|
7 //======================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //======================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour |
|
|
34 // Date: 1999-03-17 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //======================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libc.h> // C library configuration |
|
|
46 |
|
|
47 // INCLUDES |
|
|
48 |
|
|
49 #include <cyg/infra/cyg_type.h> // Common type definitions and support |
|
|
50 #include <cyg/infra/cyg_ass.h> // Assertion infrastructure |
|
|
51 #include <cyg/infra/cyg_trac.h> // Tracing infrastructure |
|
|
52 #include <stdlib.h> // Header for this file |
|
|
53 #include <string.h> // For memcpy() |
|
|
54 |
|
|
55 // FUNCTIONS |
|
|
56 |
|
|
57 ///////////////////// |
|
|
58 // cyg_libc_itoa() // |
|
|
59 ///////////////////// |
|
|
60 // |
|
|
61 // This converts num to a string and puts it into s padding with |
|
|
62 // "0"'s if padzero is set, or spaces otherwise if necessary. |
|
|
63 // The number of chars written to s is returned |
|
|
64 // |
|
|
65 |
|
|
66 // This implementation is probably suboptimal in terms of performance |
|
|
67 // but there wouldn't be much in it with only 11 chars max to convert :-/. |
|
|
68 // Actually FIXME: what if someone passes a width >11 |
|
|
69 |
|
|
70 externC cyg_uint8 |
|
|
71 cyg_libc_itoa( cyg_uint8 *s, cyg_int32 num, cyg_uint8 width, cyg_bool padzero ) |
|
|
72 { |
|
|
73 CYG_REPORT_FUNCNAMETYPE("cyg_libc_itoa", "returning %d"); |
|
|
74 CYG_REPORT_FUNCARG4( "s=%08x, num=%d, width=%d, padzero=%d", |
|
|
75 s, num, width, padzero ); |
|
|
76 |
|
|
77 CYG_CHECK_DATA_PTR(s, "input string not a valid pointer"); |
|
|
78 |
|
|
79 // special case for zero otherwise we'd have to treat it specially later |
|
|
80 // on anyway |
|
|
81 |
|
|
82 if (num==0) { |
|
|
83 cyg_ucount8 i; |
|
|
84 |
|
|
85 for (i=0; i<width; ++i) |
|
|
86 s[i] = padzero ? '0' : ' '; |
|
|
87 CYG_REPORT_RETVAL(i); |
|
|
88 return i; |
|
|
89 } |
|
|
90 |
|
|
91 // return value |
|
|
92 cyg_uint8 ret=0; |
|
|
93 |
|
|
94 // Pre-fiddle for negative numbers |
|
|
95 if ((num < 0) && (width > 0)) { |
|
|
96 *s++ = '-'; |
|
|
97 --width; |
|
|
98 num = abs(num); |
|
|
99 ++ret; |
|
|
100 } |
|
|
101 |
|
|
102 cyg_ucount32 i; |
|
|
103 cyg_bool reachednum = false; |
|
|
104 cyg_uint8 c, j; |
|
|
105 |
|
|
106 // i starts off with factor of 10 digits - which is the string length |
|
|
107 // of a positive 32-bit number |
|
|
108 for (i=1000000000, j=10; i>0; i/=10, --j) { |
|
|
109 c = (num / i); |
|
|
110 |
|
|
111 if (!reachednum && c==0) { |
|
|
112 if (j <= width) { |
|
|
113 *s++ = padzero ? '0' : ' '; |
|
|
114 ++ret; |
|
|
115 } |
|
|
116 } // if |
|
|
117 else { |
|
|
118 *s++ = c + '0'; |
|
|
119 ++ret; |
|
|
120 reachednum = true; |
|
|
121 } |
|
|
122 num %= i; |
|
|
123 } // for |
|
|
124 |
|
|
125 CYG_POSTCONDITION(ret >= width, "Didn't output enough chars!"); |
|
|
126 |
|
|
127 CYG_REPORT_RETVAL(ret); |
|
|
128 return ret; |
|
|
129 } // cyg_libc_itoa() |
|
|
130 |
|
|
131 // EOF itoa.cxx |