49
|
1 |
/*
|
|
2 |
* Copyright (c) 1999 - 2001 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
/*****************************************************************
|
|
20 |
** File name: nwx_sprintf.c
|
|
21 |
** Part of: string (NW)
|
|
22 |
** Description: Provides interfaces to (NW) string API.
|
|
23 |
******************************************************************/
|
|
24 |
|
|
25 |
/*
|
|
26 |
**-------------------------------------------------------------------------
|
|
27 |
** Include Files
|
|
28 |
**-------------------------------------------------------------------------
|
|
29 |
*/
|
|
30 |
|
|
31 |
#include "nwx_string.h"
|
|
32 |
#include "nwx_math.h"
|
|
33 |
#include "nwx_url_utils.h"
|
|
34 |
|
|
35 |
#include <stdarg.h>
|
|
36 |
#include "float.h" /* FLT_DIG */
|
|
37 |
|
|
38 |
/*lint -save -e794, -e661 Conceivable use of null pointer, Possible access of out-of-bounds pointer*/
|
|
39 |
|
|
40 |
/*
|
|
41 |
**-------------------------------------------------------------------------
|
|
42 |
** Constants
|
|
43 |
**-------------------------------------------------------------------------
|
|
44 |
*/
|
|
45 |
|
|
46 |
#define FLAG_LEFT_JUSTIFIED (1 << 0)
|
|
47 |
#define FLAG_USE_PLUS (1 << 1)
|
|
48 |
#define FLAG_USE_SPACE (1 << 2)
|
|
49 |
#define FLAG_ALTERNATE (1 << 3)
|
|
50 |
|
|
51 |
#define FIELD_FLAGS (1 << 0)
|
|
52 |
#define FIELD_WIDTH (1 << 1)
|
|
53 |
#define FIELD_PREC (1 << 2)
|
|
54 |
#define FIELD_TYPE (1 << 3)
|
|
55 |
#define MAX_FLOAT_STRING 20
|
|
56 |
#define MAX_TEMP_BUF 50
|
|
57 |
/*
|
|
58 |
**-------------------------------------------------------------------------
|
|
59 |
** Internal Types
|
|
60 |
**-------------------------------------------------------------------------
|
|
61 |
*/
|
|
62 |
|
|
63 |
typedef enum {
|
|
64 |
TYPE_INVALID,
|
|
65 |
TYPE_SHORT,
|
|
66 |
TYPE_LONG_INT,
|
|
67 |
TYPE_LONG_DOUBLE
|
|
68 |
} Type;
|
|
69 |
|
|
70 |
typedef enum {
|
|
71 |
FORMAT_INVALID,
|
|
72 |
FORMAT_SIGNED,
|
|
73 |
FORMAT_OCTAL,
|
|
74 |
FORMAT_UNSIGNED,
|
|
75 |
FORMAT_HEX,
|
|
76 |
FORMAT_BIGHEX,
|
|
77 |
FORMAT_FLOAT,
|
|
78 |
FORMAT_EXP,
|
|
79 |
FORMAT_BIGEXP,
|
|
80 |
FORMAT_CHOOSE,
|
|
81 |
FORMAT_BIGCHOOSE,
|
|
82 |
FORMAT_CHAR,
|
|
83 |
FORMAT_STRING,
|
|
84 |
FORMAT_POINTER,
|
|
85 |
FORMAT_OUTPUT_LEN
|
|
86 |
} Format;
|
|
87 |
|
|
88 |
typedef enum {
|
|
89 |
STATE_NORMAL,
|
|
90 |
STATE_DIRECTIVE
|
|
91 |
} State;
|
|
92 |
|
|
93 |
/*
|
|
94 |
**-------------------------------------------------------------------------
|
|
95 |
** Macros
|
|
96 |
**-------------------------------------------------------------------------
|
|
97 |
*/
|
|
98 |
|
|
99 |
/*
|
|
100 |
**-------------------------------------------------------------------------
|
|
101 |
** Internal Prototypes
|
|
102 |
**-------------------------------------------------------------------------
|
|
103 |
*/
|
|
104 |
|
|
105 |
/*
|
|
106 |
**-------------------------------------------------------------------------
|
|
107 |
** File Scoped Static Variables
|
|
108 |
**-------------------------------------------------------------------------
|
|
109 |
*/
|
|
110 |
|
|
111 |
/*
|
|
112 |
**-------------------------------------------------------------------------
|
|
113 |
** Global Variable Definitions
|
|
114 |
**-------------------------------------------------------------------------
|
|
115 |
*/
|
|
116 |
|
|
117 |
/*
|
|
118 |
**-------------------------------------------------------------------------
|
|
119 |
** Internal Functions
|
|
120 |
**-------------------------------------------------------------------------
|
|
121 |
*/
|
|
122 |
|
|
123 |
/*
|
|
124 |
** this function rounds an integer (val) to a specified number of
|
|
125 |
** significant digits (prec). If the digit at the place to be rounded is 5
|
|
126 |
** or greater we round AWAY from 0 (e.g. -15 gets rounded to -20). The
|
|
127 |
** resulting value is converted to a string and returned in digString. On
|
|
128 |
** input, digString must contain a string representation of the value to be
|
|
129 |
** rounded. The rounded integer value is returned as the value of the
|
|
130 |
** function. Rounding is accomplished by adding 5Ex to val, where x
|
|
131 |
** specifies the position of the digit where rounding will occur. The result
|
|
132 |
** is divided by 1E(x+1) (integer division) to eliminate the (x+1) low order
|
|
133 |
** digits, then multiplied by 1E(x+1) to refill the (x+1) low order digits
|
|
134 |
** with zeros. ex. val = 12345678 prec = 5, x = 2, val += 500 -> 12346178,
|
|
135 |
** 12346178/1000 -> 12346, 12346*1000 -> 12346000 which is then converted to
|
|
136 |
** the string "12346000"
|
|
137 |
*/
|
|
138 |
static NW_Int32 roundInt(NW_Int32 val, NW_Ucs2 *digString, NW_Int32 prec)
|
|
139 |
{
|
|
140 |
NW_Int32 roundFactor; /* 5Ex */
|
|
141 |
NW_Int32 roundLoc; /* roundLoc = x+1 */
|
|
142 |
NW_Int32 roundMult; /* 1E(x+1) */
|
|
143 |
|
|
144 |
/*
|
|
145 |
** calculate x - for the example above, x will be 2, roundLoc is 3.
|
|
146 |
** NOTE: digString must start out as the string representation of val!
|
|
147 |
*/
|
|
148 |
roundLoc = NW_Str_Strlen(digString) - prec;
|
|
149 |
/*
|
|
150 |
** if we've got a negative number then we need to account for the '-' in
|
|
151 |
** the string representation, so subtract one from roundLoc. Our example
|
|
152 |
** is positive so roundLoc remains 3.
|
|
153 |
*/
|
|
154 |
if (val < 0) {
|
|
155 |
roundLoc--;
|
|
156 |
}
|
|
157 |
/* now - if we need to do some rounding */
|
|
158 |
if (roundLoc > 0) {
|
|
159 |
roundFactor = 5;
|
|
160 |
roundMult = 10;
|
|
161 |
/*
|
|
162 |
** in our example roundLoc is 3, so we execute the following loop twice,
|
|
163 |
** roundFactor ends up as 500, roundMult ends up as 1000.
|
|
164 |
*/
|
|
165 |
while (roundLoc > 1) {
|
|
166 |
roundFactor *= 10;
|
|
167 |
roundMult *= 10;
|
|
168 |
roundLoc--;
|
|
169 |
}
|
|
170 |
/* if we're rounding negative numbers sub rather than add */
|
|
171 |
if (val > 0) {
|
|
172 |
val += roundFactor;
|
|
173 |
} else {
|
|
174 |
val -= roundFactor;
|
|
175 |
}
|
|
176 |
/* in our example 12346178/1000 => 12346*/
|
|
177 |
val /= roundMult;
|
|
178 |
|
|
179 |
/* in our example 12346178*1000 => 12346000 */
|
|
180 |
val *= roundMult;
|
|
181 |
|
|
182 |
/* finally, convert the result back to a string */
|
|
183 |
NW_Str_Itoa(val, digString);
|
|
184 |
}
|
|
185 |
/* return the rounded value */
|
|
186 |
return val;
|
|
187 |
}
|
|
188 |
|
|
189 |
static NW_Int32 gFormat(NW_Float64 x, NW_Int32 prec, NW_Ucs2 *output,NW_Int32 outSize)
|
|
190 |
{
|
|
191 |
NW_Int32 base;
|
|
192 |
NW_Int32 nDigits = 0;
|
|
193 |
NW_Int32 exp;
|
|
194 |
NW_Int32 count = 0;
|
|
195 |
NW_Ucs2 expString[MAX_FLOAT_STRING];
|
|
196 |
NW_Ucs2 baseString[MAX_FLOAT_STRING];
|
|
197 |
NW_Ucs2 *ptrOut;
|
|
198 |
NW_Ucs2 *ptrIn;
|
|
199 |
|
|
200 |
NW_ASSERT(output != NULL);
|
|
201 |
|
|
202 |
NW_Math_BaseandExp64(x, &base, &exp);
|
|
203 |
NW_Str_Itoa(base, baseString);
|
|
204 |
|
|
205 |
ptrOut = output;
|
|
206 |
ptrIn = baseString;
|
|
207 |
|
|
208 |
nDigits = NW_Str_Strlen(baseString);
|
|
209 |
|
|
210 |
if (prec > nDigits){
|
|
211 |
nDigits = prec;
|
|
212 |
/* 8: '-','0','.','E','-','1','5','\0' as a ex. -0.3E-15 */
|
|
213 |
if ((nDigits + 8) > outSize) {
|
|
214 |
return 0;
|
|
215 |
}
|
|
216 |
}
|
|
217 |
/**copy '-' into start of output**/
|
|
218 |
if (x < 0) {
|
|
219 |
*ptrOut++ = *ptrIn++;
|
|
220 |
}
|
|
221 |
*ptrOut++ = '0';
|
|
222 |
*ptrOut++ = WAE_URL_DOT_CHAR;
|
|
223 |
base = roundInt(base, baseString, prec);
|
|
224 |
|
|
225 |
if ((NW_Str_Strlen(baseString) - nDigits) == 1) {
|
|
226 |
exp ++;
|
|
227 |
}
|
|
228 |
|
|
229 |
while ((count < prec) && (*ptrIn != 0)) {
|
|
230 |
*ptrOut++ = *ptrIn++;
|
|
231 |
count++;
|
|
232 |
}
|
|
233 |
while (count < prec) {
|
|
234 |
*ptrOut++ = '0';
|
|
235 |
count++;
|
|
236 |
}
|
|
237 |
|
|
238 |
NW_Str_Itoa(exp, expString);
|
|
239 |
|
|
240 |
|
|
241 |
if (exp != 0) {
|
|
242 |
*ptrOut++ = 'E';
|
|
243 |
ptrIn = expString;
|
|
244 |
while (*ptrIn != 0) {
|
|
245 |
*ptrOut++ = *ptrIn++;
|
|
246 |
}
|
|
247 |
}
|
|
248 |
*ptrOut = 0;
|
|
249 |
return (ptrOut - output);
|
|
250 |
}
|
|
251 |
|
|
252 |
/*
|
|
253 |
**this function is converting a float to a string by the precision input,and return
|
|
254 |
**the length of it.First convert it to a mantissa and exponent(exp),then do a
|
|
255 |
**rounding as a suitable position. To get the output,the string is made by 5 steps:
|
|
256 |
**1)if negative, add a '-' at the beginning. 2)if exp > 0, output those bits in
|
|
257 |
**baseString;otherwise add a '0' before '.'. 3)'.'. 4)if exp < 0, add '0'(s) as the bit(s)
|
|
258 |
**of precision;otherwise output the precision part of baseString. 5)if baseString is
|
|
259 |
**not as precision as needed,fill '0'(s) in tail.ex. x = -1.2345678,prec = 6,->exp = 1,
|
|
260 |
**'-12345678' -> '-1234570' -> '-1.234570' ( len = 9)
|
|
261 |
*/
|
|
262 |
static NW_Int32 fFormat(NW_Float64 x, NW_Int32 prec, NW_Ucs2 *output, NW_Int32 outSize)
|
|
263 |
{
|
|
264 |
NW_Int32 count = 0; /* length of output string */
|
|
265 |
NW_Int32 nDigits = 0; /* length of baseString */
|
|
266 |
NW_Int32 base; /* integer form */
|
|
267 |
NW_Int32 exp; /* exponent */
|
|
268 |
NW_Ucs2 baseString[MAX_FLOAT_STRING];
|
|
269 |
NW_Int32 j,precCount = 0; /* if it is smaller than the precision asked */
|
|
270 |
NW_Int32 expCount = 0; /* how many '0' should set before and after '.' */
|
|
271 |
NW_Ucs2 *ptrIn; /* baseString */
|
|
272 |
NW_Int32 roundSpot; /* suitable position of rounding */
|
|
273 |
|
|
274 |
NW_ASSERT(output != NULL);
|
|
275 |
NW_ASSERT(prec < outSize);
|
|
276 |
/* convert x to a interger and exp,in our example, exp = 1 */
|
|
277 |
NW_Math_BaseandExp64(x, &base, &exp);
|
|
278 |
/* convert base to a string */
|
|
279 |
NW_Str_Itoa(base, baseString);
|
|
280 |
|
|
281 |
/* suitable position of rounding,in our example,roundSpot = 7 */
|
|
282 |
roundSpot = prec + exp;
|
|
283 |
/* now,roundSpot = 6 */
|
|
284 |
if (roundSpot > FLT_DIG){
|
|
285 |
roundSpot = FLT_DIG;
|
|
286 |
}
|
|
287 |
/* in our example,base -> 1234570 */
|
|
288 |
base = roundInt(base, baseString, roundSpot);
|
|
289 |
|
|
290 |
/* string length,in our example nDigits = 7 */
|
|
291 |
nDigits = NW_Str_Strlen(baseString);
|
|
292 |
NW_ASSERT(nDigits < outSize);
|
|
293 |
/* To fix TI compiler warning */
|
|
294 |
(void) nDigits;
|
|
295 |
/* 4: '-', '0', '.', '\0' */
|
|
296 |
if (((exp + prec + 4 ) > outSize) || ((prec + 4) > outSize)){
|
|
297 |
return 0;
|
|
298 |
}
|
|
299 |
ptrIn = baseString;
|
|
300 |
|
|
301 |
/* copy '-' into start of output */
|
|
302 |
if (x < 0) {
|
|
303 |
*output++ = *ptrIn++;
|
|
304 |
count++;
|
|
305 |
}
|
|
306 |
NW_ASSERT(count < outSize);
|
|
307 |
/* is there any '0' before and after '.' */
|
|
308 |
/* if exp > 0,expCount = -1; if exp = 0,expCount = 0; if exp < 0;expCount = -exp */
|
|
309 |
if ( exp < 0) {
|
|
310 |
expCount = -exp ;
|
|
311 |
}
|
|
312 |
else{
|
|
313 |
if (exp == 0){
|
|
314 |
expCount = 0;
|
|
315 |
}
|
|
316 |
else{
|
|
317 |
expCount = -1;
|
|
318 |
}
|
|
319 |
}
|
|
320 |
NW_ASSERT((ptrIn-baseString) < MAX_FLOAT_STRING);
|
|
321 |
/* in our example,output '1',j -> 1*/
|
|
322 |
for(j=0;j < exp;j++){
|
|
323 |
if (( j < exp ) && ( *ptrIn != '\0')){
|
|
324 |
*output++ = *ptrIn++;
|
|
325 |
}
|
|
326 |
}
|
|
327 |
for (;j< exp; j++){
|
|
328 |
*output++ = '0';
|
|
329 |
}
|
|
330 |
/*in our example,count -> 2 */
|
|
331 |
count += j;
|
|
332 |
NW_ASSERT(count < outSize);
|
|
333 |
/* exp > 0, in our example, count -> 3 */
|
|
334 |
if ( expCount == -1 ){
|
|
335 |
*output++ = WAE_URL_DOT_CHAR;
|
|
336 |
count++;
|
|
337 |
}
|
|
338 |
else{
|
|
339 |
/* exp < = 0 */
|
|
340 |
*output++ = '0';
|
|
341 |
*output++ = WAE_URL_DOT_CHAR;
|
|
342 |
count += 2;
|
|
343 |
/* output '0' when exp < 0 */
|
|
344 |
while ( expCount > 0) {
|
|
345 |
*output++ = '0';
|
|
346 |
expCount--;
|
|
347 |
precCount++;
|
|
348 |
count++;
|
|
349 |
}
|
|
350 |
}
|
|
351 |
NW_ASSERT(count < outSize);
|
|
352 |
/*in our example,output '234570',count -> 9,precCount -> 6*/
|
|
353 |
while (( precCount < prec ) && (*ptrIn != '\0')){
|
|
354 |
*output++ = *ptrIn++;
|
|
355 |
count++;
|
|
356 |
precCount++;
|
|
357 |
}
|
|
358 |
/* fill '0'(s) at the end if not precision enough */
|
|
359 |
while (( precCount < prec ) && (prec > 0)){
|
|
360 |
*output++ = '0';
|
|
361 |
count++;
|
|
362 |
precCount++;
|
|
363 |
}
|
|
364 |
NW_ASSERT(count < outSize);
|
|
365 |
*output = 0;
|
|
366 |
/* in our example count -> 9 */
|
|
367 |
return (count) ;
|
|
368 |
}
|
|
369 |
|
|
370 |
static const NW_Ucs2 *parse_flags ( const NW_Ucs2 *format, NW_Uint32 *fields, NW_Uint32 *retFlags )
|
|
371 |
{
|
|
372 |
const NW_Ucs2 *traverse = format;
|
|
373 |
NW_Bool done = NW_FALSE;
|
|
374 |
NW_Ucs2 ch;
|
|
375 |
|
|
376 |
NW_ASSERT ( traverse != NULL );
|
|
377 |
NW_ASSERT ( fields != NULL );
|
|
378 |
NW_ASSERT ( retFlags != NULL );
|
|
379 |
|
|
380 |
while ((( ch = *traverse ) != '\0') && !done) {
|
|
381 |
switch (ch) {
|
|
382 |
case '-' :
|
|
383 |
*retFlags |= FLAG_LEFT_JUSTIFIED;
|
|
384 |
traverse++;
|
|
385 |
break;
|
|
386 |
case '+' :
|
|
387 |
*retFlags |= FLAG_USE_PLUS;
|
|
388 |
traverse++;
|
|
389 |
break;
|
|
390 |
case ' ' :
|
|
391 |
*retFlags |= FLAG_USE_SPACE;
|
|
392 |
traverse++;
|
|
393 |
break;
|
|
394 |
case '#' :
|
|
395 |
*retFlags |= FLAG_ALTERNATE;
|
|
396 |
traverse++;
|
|
397 |
break;
|
|
398 |
default:
|
|
399 |
/* end of flags */
|
|
400 |
done = NW_TRUE;
|
|
401 |
}
|
|
402 |
}
|
|
403 |
|
|
404 |
if ( traverse != format )
|
|
405 |
*fields |= FIELD_FLAGS;
|
|
406 |
|
|
407 |
return traverse;
|
|
408 |
}
|
|
409 |
|
|
410 |
static const NW_Ucs2 *parse_width ( const NW_Ucs2 *format, NW_Uint32 *fields, NW_Uint32 *retWidth )
|
|
411 |
{
|
|
412 |
NW_Uint32 width;
|
|
413 |
NW_Ucs2 *endPtr = NULL;
|
|
414 |
|
|
415 |
NW_ASSERT ( format != NULL );
|
|
416 |
NW_ASSERT ( fields != NULL );
|
|
417 |
NW_ASSERT ( retWidth != NULL );
|
|
418 |
|
|
419 |
width = NW_Str_Strtoul ( format, &endPtr, 10 );
|
|
420 |
NW_ASSERT ( endPtr != NULL );
|
|
421 |
if ( format != endPtr ) {
|
|
422 |
*fields |= FIELD_WIDTH;
|
|
423 |
*retWidth = width;
|
|
424 |
}
|
|
425 |
|
|
426 |
return endPtr;
|
|
427 |
}
|
|
428 |
|
|
429 |
static const NW_Ucs2 *parse_prec ( const NW_Ucs2 *format, NW_Uint32 *fields, NW_Uint32 *retPrec )
|
|
430 |
{
|
|
431 |
NW_Uint32 prec;
|
|
432 |
|
|
433 |
NW_ASSERT ( format != NULL );
|
|
434 |
NW_ASSERT ( fields != NULL );
|
|
435 |
NW_ASSERT ( retPrec != NULL );
|
|
436 |
|
|
437 |
if ( *format == '.' ) {
|
|
438 |
NW_Ucs2 *endPtr = NULL;
|
|
439 |
format++;
|
|
440 |
prec = NW_Str_Strtoul ( format, &endPtr, 10 );
|
|
441 |
*fields |= FIELD_PREC;
|
|
442 |
if ( format != endPtr ) {
|
|
443 |
*retPrec = prec;
|
|
444 |
} else {
|
|
445 |
*retPrec = 0;
|
|
446 |
}
|
|
447 |
return endPtr;
|
|
448 |
} else {
|
|
449 |
return format;
|
|
450 |
}
|
|
451 |
}
|
|
452 |
|
|
453 |
static const NW_Ucs2 *parse_type ( const NW_Ucs2 *format, NW_Uint32 *fields, Type *retType )
|
|
454 |
{
|
|
455 |
NW_ASSERT ( format != NULL );
|
|
456 |
NW_ASSERT ( fields != NULL );
|
|
457 |
NW_ASSERT ( retType != NULL );
|
|
458 |
|
|
459 |
switch ( *format ) {
|
|
460 |
case 'h':
|
|
461 |
*retType = TYPE_SHORT;
|
|
462 |
*fields |= FIELD_TYPE;
|
|
463 |
format++;
|
|
464 |
break;
|
|
465 |
case 'l':
|
|
466 |
*retType = TYPE_LONG_INT;
|
|
467 |
*fields |= FIELD_TYPE;
|
|
468 |
format++;
|
|
469 |
break;
|
|
470 |
case 'L':
|
|
471 |
*retType = TYPE_LONG_DOUBLE;
|
|
472 |
*fields |= FIELD_TYPE;
|
|
473 |
format++;
|
|
474 |
break;
|
|
475 |
default:
|
|
476 |
/* no type */
|
|
477 |
;
|
|
478 |
}
|
|
479 |
return format;
|
|
480 |
}
|
|
481 |
|
|
482 |
static const NW_Ucs2 *parse_format ( const NW_Ucs2 *format, Format *retFormat )
|
|
483 |
{
|
|
484 |
NW_ASSERT ( format != NULL );
|
|
485 |
NW_ASSERT ( retFormat != NULL );
|
|
486 |
|
|
487 |
switch ( *format ) {
|
|
488 |
case 'i':
|
|
489 |
case 'd':
|
|
490 |
*retFormat = FORMAT_SIGNED;
|
|
491 |
format++;
|
|
492 |
break;
|
|
493 |
case 'o':
|
|
494 |
*retFormat = FORMAT_OCTAL;
|
|
495 |
format++;
|
|
496 |
break;
|
|
497 |
case 'u':
|
|
498 |
*retFormat = FORMAT_UNSIGNED;
|
|
499 |
format++;
|
|
500 |
break;
|
|
501 |
case 'x':
|
|
502 |
*retFormat = FORMAT_HEX;
|
|
503 |
format++;
|
|
504 |
break;
|
|
505 |
case 'X':
|
|
506 |
*retFormat = FORMAT_BIGHEX;
|
|
507 |
format++;
|
|
508 |
break;
|
|
509 |
case 'f':
|
|
510 |
*retFormat = FORMAT_FLOAT;
|
|
511 |
format++;
|
|
512 |
break;
|
|
513 |
case 'e':
|
|
514 |
*retFormat = FORMAT_EXP;
|
|
515 |
format++;
|
|
516 |
break;
|
|
517 |
case 'E':
|
|
518 |
*retFormat = FORMAT_BIGEXP;
|
|
519 |
format++;
|
|
520 |
break;
|
|
521 |
case 'g':
|
|
522 |
*retFormat = FORMAT_CHOOSE;
|
|
523 |
format++;
|
|
524 |
break;
|
|
525 |
case 'G':
|
|
526 |
*retFormat = FORMAT_BIGCHOOSE;
|
|
527 |
format++;
|
|
528 |
break;
|
|
529 |
case 'c':
|
|
530 |
*retFormat = FORMAT_CHAR;
|
|
531 |
format++;
|
|
532 |
break;
|
|
533 |
case 's':
|
|
534 |
*retFormat = FORMAT_STRING;
|
|
535 |
format++;
|
|
536 |
break;
|
|
537 |
case 'p':
|
|
538 |
*retFormat = FORMAT_POINTER;
|
|
539 |
format++;
|
|
540 |
break;
|
|
541 |
case 'n':
|
|
542 |
*retFormat = FORMAT_OUTPUT_LEN;
|
|
543 |
format++;
|
|
544 |
break;
|
|
545 |
default:
|
|
546 |
/* bad format string */
|
|
547 |
NW_ASSERT ( 0 );
|
|
548 |
format = NULL;
|
|
549 |
}
|
|
550 |
return format;
|
|
551 |
}
|
|
552 |
|
|
553 |
static NW_Int32 execute_hex ( NW_Ucs2 *output, NW_Int32 count, NW_Int32 fields,
|
|
554 |
NW_Int32 width, NW_Int32 prec, Type type, va_list *argptr )
|
|
555 |
{
|
|
556 |
NW_Uint32 val = 0;
|
|
557 |
NW_Int32 numWritten = 0;
|
|
558 |
|
|
559 |
NW_ASSERT ( output != NULL );
|
|
560 |
NW_ASSERT ( count >= 0 );
|
|
561 |
NW_ASSERT ( fields >= 0 );
|
|
562 |
NW_ASSERT ( width >= 0 );
|
|
563 |
NW_ASSERT ( prec >= 0 );
|
|
564 |
|
|
565 |
NW_REQUIRED_PARAM(type);
|
|
566 |
|
|
567 |
val = va_arg ( *argptr, NW_Uint32 );
|
|
568 |
|
|
569 |
if (! ( fields & FIELD_WIDTH )) {
|
|
570 |
width = 1;
|
|
571 |
}
|
|
572 |
if (! ( fields & FIELD_PREC )) {
|
|
573 |
prec = 1;
|
|
574 |
}
|
|
575 |
|
|
576 |
/* perhaps I should through in val == 0 and width == 0 */
|
|
577 |
if (! (( prec == 0 ) && (val == 0))) {
|
|
578 |
NW_Ucs2 temp_buf[40];
|
|
579 |
NW_Int32 len;
|
|
580 |
NW_Int32 zeroes = 0;
|
|
581 |
NW_Int32 spaces = 0;
|
|
582 |
NW_Int32 i;
|
|
583 |
|
|
584 |
/* Platform-specific */
|
|
585 |
NW_Str_UltoBase(val, temp_buf, 16);
|
|
586 |
|
|
587 |
len = NW_Str_Strlen ( temp_buf );
|
|
588 |
|
|
589 |
if ( len < prec ) {
|
|
590 |
zeroes = prec - len;
|
|
591 |
}
|
|
592 |
if (( len + zeroes ) < width ) {
|
|
593 |
spaces = width - ( len + zeroes );
|
|
594 |
}
|
|
595 |
|
|
596 |
for ( i = 0; i < spaces; i++ ) {
|
|
597 |
if ( count > (NW_Uint16) numWritten ) {
|
|
598 |
*output++ = ' ';
|
|
599 |
numWritten++;
|
|
600 |
}
|
|
601 |
}
|
|
602 |
|
|
603 |
for ( i = 0; i < zeroes; i++ ) {
|
|
604 |
if ( count > (NW_Uint16) numWritten ) {
|
|
605 |
*output++ = '0';
|
|
606 |
numWritten++;
|
|
607 |
}
|
|
608 |
}
|
|
609 |
|
|
610 |
if ( count > (NW_Uint16) numWritten ) {
|
|
611 |
NW_Int32 toWrite = len;
|
|
612 |
if ( len > (count - (NW_Uint16) numWritten )) {
|
|
613 |
toWrite = count - numWritten;
|
|
614 |
}
|
|
615 |
NW_Str_Strncpy ( output, temp_buf, toWrite );
|
|
616 |
numWritten = numWritten + toWrite;
|
|
617 |
}
|
|
618 |
}
|
|
619 |
|
|
620 |
return numWritten;
|
|
621 |
}
|
|
622 |
|
|
623 |
static NW_Int32 execute_signed ( NW_Ucs2 *output, NW_Int32 count, NW_Int32 fields,
|
|
624 |
NW_Int32 width, NW_Int32 prec, Type type, va_list *argptr )
|
|
625 |
{
|
|
626 |
long val = 0;
|
|
627 |
NW_Int32 numWritten = 0;
|
|
628 |
NW_Bool negative = NW_FALSE;
|
|
629 |
|
|
630 |
NW_ASSERT ( output != NULL );
|
|
631 |
NW_ASSERT ( count >= 0 );
|
|
632 |
NW_ASSERT ( fields >= 0 );
|
|
633 |
NW_ASSERT ( width >= 0 );
|
|
634 |
NW_ASSERT ( prec >= 0 );
|
|
635 |
|
|
636 |
if ( fields & FIELD_TYPE ) {
|
|
637 |
switch ( type ) {
|
|
638 |
case TYPE_SHORT :
|
|
639 |
val = (NW_Int16) va_arg ( *argptr, NW_Int32 );
|
|
640 |
break;
|
|
641 |
case TYPE_LONG_INT :
|
|
642 |
val = va_arg ( *argptr, long );
|
|
643 |
break;
|
|
644 |
case TYPE_LONG_DOUBLE:
|
|
645 |
NW_ASSERT ( 0 ); /* should not happen with a %d */
|
|
646 |
numWritten = -1;
|
|
647 |
break;
|
|
648 |
default:
|
|
649 |
NW_ASSERT ( 0 );
|
|
650 |
}
|
|
651 |
} else {
|
|
652 |
val = va_arg ( *argptr, NW_Int32 );
|
|
653 |
}
|
|
654 |
|
|
655 |
if (! ( fields & FIELD_WIDTH )) {
|
|
656 |
width = 1;
|
|
657 |
}
|
|
658 |
if (! ( fields & FIELD_PREC )) {
|
|
659 |
prec = 1;
|
|
660 |
}
|
|
661 |
|
|
662 |
/* perhaps I should through in val == 0 and width == 0 */
|
|
663 |
if ((numWritten != -1) && (! (( prec == 0 ) && (val == 0)))) {
|
|
664 |
NW_Ucs2 temp_buf[40];
|
|
665 |
NW_Int32 len;
|
|
666 |
NW_Int32 zeroes = 0;
|
|
667 |
NW_Int32 spaces = 0;
|
|
668 |
NW_Int32 i;
|
|
669 |
|
|
670 |
/* Platform-specific */
|
|
671 |
NW_Str_Itoa(val, temp_buf);
|
|
672 |
if (*temp_buf == '-') negative = NW_TRUE;
|
|
673 |
|
|
674 |
len = NW_Str_Strlen ( temp_buf );
|
|
675 |
if ( negative ) len--;
|
|
676 |
|
|
677 |
if ( len < prec ) {
|
|
678 |
zeroes = prec - len;
|
|
679 |
}
|
|
680 |
if (( len + zeroes ) < width ) {
|
|
681 |
spaces = width - ( len + zeroes );
|
|
682 |
if ( negative ) spaces--;
|
|
683 |
}
|
|
684 |
|
|
685 |
for ( i = 0; i < spaces; i++ ) {
|
|
686 |
if ( count > numWritten ) {
|
|
687 |
*output++ = ' ';
|
|
688 |
numWritten++;
|
|
689 |
}
|
|
690 |
}
|
|
691 |
|
|
692 |
if ( negative ) {
|
|
693 |
if ( count > numWritten ) {
|
|
694 |
*output++ = '-';
|
|
695 |
numWritten++;
|
|
696 |
}
|
|
697 |
}
|
|
698 |
|
|
699 |
for ( i = 0; i < zeroes; i++ ) {
|
|
700 |
if ( count > numWritten ) {
|
|
701 |
*output++ = '0';
|
|
702 |
numWritten++;
|
|
703 |
}
|
|
704 |
}
|
|
705 |
|
|
706 |
if ( count > numWritten ) {
|
|
707 |
NW_Int32 toWrite = len;
|
|
708 |
if ( len > (count - numWritten )) {
|
|
709 |
toWrite = count - numWritten;
|
|
710 |
}
|
|
711 |
if ( negative ) {
|
|
712 |
NW_Str_Strncpy ( output, temp_buf + 1, toWrite);
|
|
713 |
} else {
|
|
714 |
NW_Str_Strncpy ( output, temp_buf, toWrite );
|
|
715 |
}
|
|
716 |
numWritten = numWritten + toWrite;
|
|
717 |
}
|
|
718 |
}
|
|
719 |
|
|
720 |
return numWritten;
|
|
721 |
}
|
|
722 |
/*
|
|
723 |
**The function handle the output of float by precision asked.When the precison
|
|
724 |
**input is zero,set it to '6' as a default value.If fmt is FORMAT_FLOAT,output a
|
|
725 |
**float string as input. If is FORMAT_CHOOSE, output a string being written in
|
|
726 |
**"0.2E8" format if the input val is not in 0.0001 <= |val| < 1000000. And in
|
|
727 |
**the second situation, we skip useless zero(s) at the tail.
|
|
728 |
*/
|
|
729 |
static NW_Int32 execute_choose (NW_Ucs2 *buf, NW_Int32 count, NW_Int32 fields,
|
|
730 |
NW_Int32 width, NW_Int32 prec, Format fmt,
|
|
731 |
va_list *argptr)
|
|
732 |
{
|
|
733 |
NW_Float64 val;
|
|
734 |
NW_Ucs2 temp_buf[MAX_TEMP_BUF];
|
|
735 |
NW_Int32 i, j;
|
|
736 |
NW_Int32 len = 0;
|
|
737 |
NW_Int32 spaces = 0;
|
|
738 |
NW_Int32 numWritten = 0;
|
|
739 |
NW_Ucs2 *output = buf;
|
|
740 |
NW_Bool localPrec = NW_FALSE; /* Is the precision set by input, or by local function */
|
|
741 |
NW_Bool decDot = NW_FALSE; /* Is there a decimal pot in the string */
|
|
742 |
NW_Bool eFormat = NW_FALSE; /* Is there a "E" in the string */
|
|
743 |
|
|
744 |
NW_ASSERT ( buf != NULL );
|
|
745 |
NW_ASSERT ( output != NULL );
|
|
746 |
|
|
747 |
val = va_arg ( *argptr, NW_Float64 );
|
|
748 |
/*
|
|
749 |
* if no precision asked, set it as a default value, localPrec -> NW_TRUE
|
|
750 |
* if the precision is more then the buffer then set it to efault value.
|
|
751 |
*/
|
|
752 |
if (((!(fields & FIELD_PREC)) && (prec == 0)) || (prec >= MAX_TEMP_BUF))
|
|
753 |
{
|
|
754 |
prec = NW_DEFAULT_FLOAT_PREC;
|
|
755 |
localPrec = NW_TRUE;
|
|
756 |
}
|
|
757 |
if (! ( fields & FIELD_WIDTH)) {
|
|
758 |
width = 1;
|
|
759 |
}
|
|
760 |
NW_ASSERT((fmt == FORMAT_FLOAT) || (fmt == FORMAT_CHOOSE));
|
|
761 |
if (fmt == FORMAT_FLOAT){
|
|
762 |
len = fFormat( val, prec, temp_buf, MAX_TEMP_BUF );/* just ouput float string */
|
|
763 |
}
|
|
764 |
else {
|
|
765 |
/* different formats as different float values input, for example, 20000 -> 20000.0,
|
|
766 |
len = 8 by fFormat;20000000 -> 0.2E8 ,len = 5 by gFormat as prec = 1 */
|
|
767 |
if ((val < 1000000.00 && val >= 0.0001) || (val > -1000000.00 && val <= -0.0001)){
|
|
768 |
len = fFormat( val, prec, temp_buf, MAX_TEMP_BUF );
|
|
769 |
}
|
|
770 |
else {
|
|
771 |
len = gFormat( val, prec, temp_buf, MAX_TEMP_BUF );
|
|
772 |
}
|
|
773 |
/* calculate trailing '0',if percision was set by local function; else output string
|
|
774 |
as percision as asked*/
|
|
775 |
if (localPrec) {
|
|
776 |
i = 0;
|
|
777 |
while ((temp_buf[len-i-1] == '0')&&((len-i-1) > 0)){
|
|
778 |
i++;
|
|
779 |
}
|
|
780 |
for (j = 0 ; j <= (len - i - 1); j++){
|
|
781 |
if (temp_buf[j] == '.'){
|
|
782 |
decDot = NW_TRUE;
|
|
783 |
}
|
|
784 |
if (temp_buf[j] == 'E'){
|
|
785 |
eFormat = NW_TRUE;
|
|
786 |
}
|
|
787 |
}
|
|
788 |
/* if there isn't a decimal pot in the string, no need to skip '0'(s) at the tail */
|
|
789 |
/* undo it if the val was processed by gFomat.for example "0.1E10" we shouldn't skip the tail "0"*/
|
|
790 |
if ((decDot) && (!eFormat)){
|
|
791 |
if (i == prec){
|
|
792 |
len -= (i-1);
|
|
793 |
}
|
|
794 |
else {
|
|
795 |
len -= i;
|
|
796 |
}
|
|
797 |
}
|
|
798 |
}
|
|
799 |
}
|
|
800 |
/* if the last bit of the string is '.',skip it */
|
|
801 |
if (temp_buf[len-1] == '.'){
|
|
802 |
len -= 1;
|
|
803 |
}
|
|
804 |
/* is there any empty spaces need before the string */
|
|
805 |
if ( width > len ){
|
|
806 |
spaces = width - len;
|
|
807 |
}
|
|
808 |
for ( i = 0; (i < spaces) && (numWritten < count); i++ ) {
|
|
809 |
*output++ = ' ';
|
|
810 |
numWritten++;
|
|
811 |
}
|
|
812 |
i = 0;
|
|
813 |
while((i < len) && (temp_buf[i] != '\0') && (numWritten < count)){
|
|
814 |
*output++ = temp_buf[i];
|
|
815 |
numWritten++;
|
|
816 |
i++;
|
|
817 |
}
|
|
818 |
return numWritten;
|
|
819 |
}
|
|
820 |
|
|
821 |
static NW_Int32 execute_string ( NW_Ucs2 *output, NW_Int32 count, NW_Int32 fields,
|
|
822 |
NW_Int32 width, NW_Int32 prec, va_list *argptr )
|
|
823 |
{
|
|
824 |
const NW_Ucs2 *val;
|
|
825 |
NW_Int32 numWritten = 0;
|
|
826 |
NW_Int32 len;
|
|
827 |
NW_Int32 toWrite;
|
|
828 |
NW_Int32 i;
|
|
829 |
|
|
830 |
NW_ASSERT ( output != NULL );
|
|
831 |
NW_ASSERT ( count >= 0 );
|
|
832 |
NW_ASSERT ( fields >= 0 );
|
|
833 |
NW_ASSERT ( width >= 0 );
|
|
834 |
NW_ASSERT ( prec >= 0 );
|
|
835 |
|
|
836 |
if ( ! ( fields & FIELD_WIDTH )) {
|
|
837 |
width = 1;
|
|
838 |
/* To fix TI compiler warning */
|
|
839 |
(void) width;
|
|
840 |
}
|
|
841 |
|
|
842 |
val = va_arg ( *argptr, const NW_Ucs2 *);
|
|
843 |
if (val == NULL) {
|
|
844 |
return 0;
|
|
845 |
}
|
|
846 |
len = NW_Str_Strlen ( val );
|
|
847 |
|
|
848 |
/*
|
|
849 |
** WMLScript Std Lib 9.16 says for strings: "When the width is larger than
|
|
850 |
** precision, the width should be ignored." and in general: "If the number
|
|
851 |
** of characters in the output value is greater than the specified width
|
|
852 |
** or, if width is not given, all characters of the value are printed."
|
|
853 |
** Sooo, for strings, we always ignore width
|
|
854 |
*/
|
|
855 |
toWrite = len;
|
|
856 |
|
|
857 |
if ( fields & FIELD_PREC ) {
|
|
858 |
if ( prec < toWrite ) toWrite = prec;
|
|
859 |
}
|
|
860 |
|
|
861 |
for ( i = 0; i < toWrite; i++) {
|
|
862 |
if ( numWritten < count ) {
|
|
863 |
*output++ = *val++;
|
|
864 |
numWritten++;
|
|
865 |
}
|
|
866 |
}
|
|
867 |
|
|
868 |
return numWritten;
|
|
869 |
}
|
|
870 |
|
|
871 |
static NW_Int32 execute_directive ( NW_Ucs2 *buf, NW_Int32 count, NW_Int32 fields,
|
|
872 |
NW_Int32 width, NW_Int32 prec, Type type,
|
|
873 |
Format fmt, va_list *argptr )
|
|
874 |
{
|
|
875 |
NW_Int32 numWritten = 0;
|
|
876 |
|
|
877 |
NW_ASSERT ( buf != NULL );
|
|
878 |
NW_ASSERT ( count >= 0 );
|
|
879 |
NW_ASSERT ( fields >= 0 );
|
|
880 |
NW_ASSERT ( width >= 0 );
|
|
881 |
NW_ASSERT ( prec >= 0 );
|
|
882 |
|
|
883 |
switch ( fmt ) {
|
|
884 |
case FORMAT_SIGNED:
|
|
885 |
numWritten = execute_signed ( buf, count, fields, width, prec, type, argptr );
|
|
886 |
break;
|
|
887 |
case FORMAT_FLOAT:
|
|
888 |
case FORMAT_CHOOSE:
|
|
889 |
numWritten = execute_choose ( buf, count, fields, width, prec, fmt, argptr );
|
|
890 |
break;
|
|
891 |
case FORMAT_STRING:
|
|
892 |
numWritten = execute_string ( buf, count, fields, width, prec, argptr );
|
|
893 |
break;
|
|
894 |
case FORMAT_HEX:
|
|
895 |
case FORMAT_BIGHEX:
|
|
896 |
numWritten = execute_hex ( buf, count, fields, width, prec, type, argptr );
|
|
897 |
break;
|
|
898 |
default:
|
|
899 |
NW_ASSERT (0);
|
|
900 |
/* not implemented */
|
|
901 |
numWritten = -1;
|
|
902 |
}
|
|
903 |
return numWritten;
|
|
904 |
}
|
|
905 |
|
|
906 |
/*
|
|
907 |
**-------------------------------------------------------------------------
|
|
908 |
** External Public (Exported) Functions
|
|
909 |
**-------------------------------------------------------------------------
|
|
910 |
*/
|
|
911 |
|
|
912 |
NW_Int32 NW_Str_Vsnprintf (NW_Ucs2 *buf, const NW_Int32 count, const NW_Ucs2 *format, va_list *argptr)
|
|
913 |
{
|
|
914 |
NW_Ucs2 ch;
|
|
915 |
NW_Int32 numWritten = 0;
|
|
916 |
NW_Ucs2 *output;
|
|
917 |
const NW_Ucs2 *input;
|
|
918 |
State state = STATE_NORMAL;
|
|
919 |
|
|
920 |
NW_ASSERT ( buf != NULL );
|
|
921 |
NW_ASSERT ( count >= 0 );
|
|
922 |
NW_ASSERT ( format != NULL );
|
|
923 |
|
|
924 |
input = format;
|
|
925 |
|
|
926 |
output = buf;
|
|
927 |
|
|
928 |
|
|
929 |
while ((( ch = *input ) != '\0') && ( numWritten >= 0 )) {
|
|
930 |
switch (state) {
|
|
931 |
case STATE_NORMAL:
|
|
932 |
if ( ch == '%' ) {
|
|
933 |
if (*(input + 1) == '%') {
|
|
934 |
/* it's a %% */
|
|
935 |
input++;
|
|
936 |
} else {
|
|
937 |
/* starting directive */
|
|
938 |
state = STATE_DIRECTIVE;
|
|
939 |
}
|
|
940 |
}
|
|
941 |
if ( state == STATE_NORMAL ) {
|
|
942 |
/* regular character, just write it */
|
|
943 |
NW_ASSERT(0 <= count);
|
|
944 |
if ( numWritten < count ) {
|
|
945 |
*output++ = ch;
|
|
946 |
numWritten++;
|
|
947 |
}
|
|
948 |
}
|
|
949 |
input++;
|
|
950 |
break;
|
|
951 |
case STATE_DIRECTIVE:
|
|
952 |
{
|
|
953 |
NW_Uint32 fields = 0;
|
|
954 |
NW_Uint32 flags = 0;
|
|
955 |
NW_Uint32 width = 0;
|
|
956 |
NW_Uint32 prec = 0;
|
|
957 |
Type type = TYPE_INVALID;
|
|
958 |
Format fmt = FORMAT_INVALID;
|
|
959 |
|
|
960 |
if (( input = parse_flags ( input, &fields, &flags )) != NULL ) {
|
|
961 |
if (( input = parse_width ( input, &fields, &width )) != NULL ) {
|
|
962 |
/* If width is more than allowed then set width to the max allowed */
|
|
963 |
if (width > NW_UINT32_CAST(count))
|
|
964 |
{
|
|
965 |
width = NW_UINT32_CAST(count); /* Last byte is to hold null char */
|
|
966 |
}
|
|
967 |
if (( input = parse_prec ( input, &fields, &prec )) != NULL ) {
|
|
968 |
if (( input = parse_type ( input, &fields, &type )) != NULL ) {
|
|
969 |
if (( input = parse_format ( input, &fmt )) != NULL ) {
|
|
970 |
NW_Int32 retVal;
|
|
971 |
retVal = execute_directive ( output, count - numWritten,
|
|
972 |
fields, width, prec, type,
|
|
973 |
fmt, argptr );
|
|
974 |
if ( retVal >= 0 ) {
|
|
975 |
numWritten += retVal;
|
|
976 |
output += retVal;
|
|
977 |
state = STATE_NORMAL;
|
|
978 |
}
|
|
979 |
} else {
|
|
980 |
numWritten = -1;
|
|
981 |
}
|
|
982 |
} else {
|
|
983 |
numWritten = -1;
|
|
984 |
}
|
|
985 |
} else {
|
|
986 |
numWritten = -1;
|
|
987 |
}
|
|
988 |
} else {
|
|
989 |
numWritten = -1;
|
|
990 |
}
|
|
991 |
}
|
|
992 |
}
|
|
993 |
break;
|
|
994 |
default:
|
|
995 |
NW_ASSERT ( 0 );
|
|
996 |
}
|
|
997 |
}
|
|
998 |
|
|
999 |
NW_ASSERT(0 <= count);
|
|
1000 |
if (numWritten <= count) {
|
|
1001 |
*output = '\0';
|
|
1002 |
}
|
|
1003 |
return numWritten;
|
|
1004 |
}
|
|
1005 |
|
|
1006 |
NW_Int32 NW_Str_Sprintf (NW_Ucs2 *buf, const NW_Ucs2 *format, ... )
|
|
1007 |
{
|
|
1008 |
va_list args;
|
|
1009 |
NW_Int32 status;
|
|
1010 |
|
|
1011 |
va_start (args, format );
|
|
1012 |
/* TODO: 9999999 should be some kind of MAX_INT */
|
|
1013 |
status = NW_Str_Vsnprintf ( buf, 9999999, format, &args );
|
|
1014 |
va_end (args);
|
|
1015 |
|
|
1016 |
return status;
|
|
1017 |
}
|
|
1018 |
|
|
1019 |
/*lint -restore*/
|