]>
Commit | Line | Data |
---|---|---|
3eb9473e | 1 | /*++\r |
2 | \r | |
4ea9375a HT |
3 | Copyright (c) 2004, Intel Corporation. All rights reserved.<BR>\r |
4 | This program and the accompanying materials \r | |
3eb9473e | 5 | are licensed and made available under the terms and conditions of the BSD License \r |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | ValueToString.c\r | |
15 | \r | |
16 | Abstract:\r | |
17 | \r | |
18 | Routines changing value to Hex or Dec string\r | |
19 | \r | |
20 | --*/\r | |
21 | \r | |
22 | #include "Tiano.h"\r | |
23 | #include "EfiDriverLib.h"\r | |
24 | \r | |
25 | static CHAR16 mHexStr[] = { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7',\r | |
26 | L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };\r | |
27 | \r | |
28 | UINTN\r | |
29 | EfiValueToHexStr (\r | |
30 | IN OUT CHAR16 *Buffer, \r | |
31 | IN UINT64 Value, \r | |
32 | IN UINTN Flags, \r | |
33 | IN UINTN Width\r | |
34 | )\r | |
35 | /*++\r | |
36 | \r | |
37 | Routine Description:\r | |
38 | \r | |
39 | VSPrint worker function that prints a Value as a hex number in Buffer\r | |
40 | \r | |
41 | Arguments:\r | |
42 | \r | |
43 | Buffer - Location to place ascii hex string of Value.\r | |
44 | \r | |
45 | Value - Hex value to convert to a string in Buffer.\r | |
46 | \r | |
47 | Flags - Flags to use in printing Hex string, see file header for details.\r | |
48 | \r | |
49 | Width - Width of hex value.\r | |
50 | \r | |
51 | Returns: \r | |
52 | \r | |
53 | Number of characters printed. \r | |
54 | \r | |
55 | --*/\r | |
56 | {\r | |
57 | CHAR16 TempBuffer[CHARACTER_NUMBER_FOR_VALUE];\r | |
58 | CHAR16 *TempStr;\r | |
59 | CHAR16 Prefix;\r | |
60 | CHAR16 *BufferPtr;\r | |
61 | UINTN Count;\r | |
62 | UINTN Index;\r | |
63 | \r | |
64 | TempStr = TempBuffer;\r | |
65 | BufferPtr = Buffer;\r | |
66 | \r | |
67 | //\r | |
68 | // Count starts at one since we will null terminate. Each iteration of the\r | |
69 | // loop picks off one nibble. Oh yea TempStr ends up backwards\r | |
70 | //\r | |
71 | Count = 0;\r | |
72 | \r | |
73 | if (Width > CHARACTER_NUMBER_FOR_VALUE - 1) {\r | |
74 | Width = CHARACTER_NUMBER_FOR_VALUE - 1;\r | |
75 | }\r | |
76 | \r | |
77 | do {\r | |
78 | Index = ((UINTN)Value & 0xf);\r | |
79 | *(TempStr++) = mHexStr[Index];\r | |
80 | Value = RShiftU64 (Value, 4);\r | |
81 | Count++;\r | |
82 | } while (Value != 0);\r | |
83 | \r | |
84 | if (Flags & PREFIX_ZERO) {\r | |
85 | Prefix = '0';\r | |
86 | } else { \r | |
87 | Prefix = ' ';\r | |
88 | }\r | |
89 | \r | |
90 | Index = Count;\r | |
91 | if (!(Flags & LEFT_JUSTIFY)) {\r | |
92 | for (; Index < Width; Index++) {\r | |
93 | *(TempStr++) = Prefix;\r | |
94 | }\r | |
95 | }\r | |
96 | \r | |
97 | //\r | |
98 | // Reverse temp string into Buffer.\r | |
99 | //\r | |
100 | if (Width > 0 && (UINTN) (TempStr - TempBuffer) > Width) {\r | |
101 | TempStr = TempBuffer + Width;\r | |
102 | }\r | |
103 | Index = 0;\r | |
104 | while (TempStr != TempBuffer) {\r | |
105 | *(BufferPtr++) = *(--TempStr);\r | |
106 | Index++;\r | |
107 | }\r | |
108 | \r | |
109 | *BufferPtr = 0;\r | |
110 | return Index;\r | |
111 | }\r | |
112 | \r | |
113 | \r | |
114 | UINTN\r | |
115 | EfiValueToString (\r | |
116 | IN OUT CHAR16 *Buffer, \r | |
117 | IN INT64 Value, \r | |
118 | IN UINTN Flags,\r | |
119 | IN UINTN Width\r | |
120 | )\r | |
121 | /*++\r | |
122 | \r | |
123 | Routine Description:\r | |
124 | \r | |
125 | VSPrint worker function that prints a Value as a decimal number in Buffer\r | |
126 | \r | |
127 | Arguments:\r | |
128 | \r | |
129 | Buffer - Location to place ascii decimal number string of Value.\r | |
130 | \r | |
131 | Value - Decimal value to convert to a string in Buffer.\r | |
132 | \r | |
133 | Flags - Flags to use in printing decimal string, see file header for details.\r | |
134 | \r | |
135 | Width - Width of hex value.\r | |
136 | \r | |
137 | Returns: \r | |
138 | \r | |
139 | Number of characters printed. \r | |
140 | \r | |
141 | --*/\r | |
142 | {\r | |
143 | CHAR16 TempBuffer[CHARACTER_NUMBER_FOR_VALUE];\r | |
144 | CHAR16 *TempStr;\r | |
145 | CHAR16 *BufferPtr;\r | |
146 | UINTN Count;\r | |
147 | UINTN ValueCharNum;\r | |
148 | UINTN Remainder;\r | |
149 | CHAR16 Prefix;\r | |
150 | UINTN Index;\r | |
151 | BOOLEAN ValueIsNegative;\r | |
152 | \r | |
153 | TempStr = TempBuffer;\r | |
154 | BufferPtr = Buffer;\r | |
155 | Count = 0;\r | |
156 | ValueCharNum = 0;\r | |
157 | ValueIsNegative = FALSE;\r | |
158 | \r | |
159 | if (Width > CHARACTER_NUMBER_FOR_VALUE - 1) {\r | |
160 | Width = CHARACTER_NUMBER_FOR_VALUE - 1;\r | |
161 | }\r | |
162 | \r | |
163 | if (Value < 0) {\r | |
164 | Value = -Value;\r | |
165 | ValueIsNegative = TRUE;\r | |
166 | }\r | |
167 | \r | |
168 | do {\r | |
169 | Value = (INT64)DivU64x32 ((UINT64)Value, 10, &Remainder);\r | |
170 | *(TempStr++) = (CHAR16)(Remainder + '0');\r | |
171 | ValueCharNum++;\r | |
172 | Count++;\r | |
173 | if ((Flags & COMMA_TYPE) == COMMA_TYPE) {\r | |
174 | if (ValueCharNum % 3 == 0 && Value != 0) {\r | |
175 | *(TempStr++) = ',';\r | |
176 | Count++;\r | |
177 | }\r | |
178 | }\r | |
179 | } while (Value != 0);\r | |
180 | \r | |
181 | if (ValueIsNegative) {\r | |
182 | *(TempStr++) = '-';\r | |
183 | Count++;\r | |
184 | }\r | |
185 | \r | |
186 | if ((Flags & PREFIX_ZERO) && !ValueIsNegative) {\r | |
187 | Prefix = '0';\r | |
188 | } else { \r | |
189 | Prefix = ' ';\r | |
190 | } \r | |
191 | \r | |
192 | Index = Count;\r | |
193 | if (!(Flags & LEFT_JUSTIFY)) {\r | |
194 | for (; Index < Width; Index++) {\r | |
195 | *(TempStr++) = Prefix;\r | |
196 | }\r | |
197 | }\r | |
198 | \r | |
199 | //\r | |
200 | // Reverse temp string into Buffer.\r | |
201 | //\r | |
202 | if (Width > 0 && (UINTN) (TempStr - TempBuffer) > Width) {\r | |
203 | TempStr = TempBuffer + Width;\r | |
204 | }\r | |
205 | Index = 0;\r | |
206 | while (TempStr != TempBuffer) {\r | |
207 | *(BufferPtr++) = *(--TempStr);\r | |
208 | Index++;\r | |
209 | }\r | |
210 | \r | |
211 | *BufferPtr = 0;\r | |
212 | return Index;\r | |
213 | }\r |