Return to Formatter.cpp CVS log | Up to [Pegasus] / pegasus / src / Pegasus / Common |
1 martin 1.30 //%LICENSE//////////////////////////////////////////////////////////////// | ||
2 martin 1.31 // | ||
3 martin 1.30 // Licensed to The Open Group (TOG) under one or more contributor license 4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with 5 // this work for additional information regarding copyright ownership. 6 // Each contributor licenses this file to you under the OpenPegasus Open 7 // Source License; you may not use this file except in compliance with the 8 // License. | ||
9 martin 1.31 // | ||
10 martin 1.30 // Permission is hereby granted, free of charge, to any person obtaining a 11 // copy of this software and associated documentation files (the "Software"), 12 // to deal in the Software without restriction, including without limitation 13 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 // and/or sell copies of the Software, and to permit persons to whom the 15 // Software is furnished to do so, subject to the following conditions: | ||
16 martin 1.31 // | ||
17 martin 1.30 // The above copyright notice and this permission notice shall be included 18 // in all copies or substantial portions of the Software. | ||
19 martin 1.31 // | ||
20 martin 1.30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
21 martin 1.31 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
22 martin 1.30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
27 martin 1.31 // | ||
28 martin 1.30 ////////////////////////////////////////////////////////////////////////// | ||
29 mike 1.13 // 30 //%///////////////////////////////////////////////////////////////////////////// 31 32 #include <iostream> 33 #include <cstdio> 34 #include "Formatter.h" | ||
35 mike 1.23 #include "StrLit.h" | ||
36 mike 1.13 37 PEGASUS_NAMESPACE_BEGIN 38 | ||
39 karl 1.20 const Formatter::Arg Formatter::DEFAULT_ARG; 40 | ||
41 mike 1.13 String Formatter::Arg::toString() const 42 { 43 switch (_type) 44 { | ||
45 david.dillard 1.24 case INTEGER: 46 { 47 char buffer[32]; 48 sprintf(buffer, "%d", _integer); 49 return buffer; 50 } 51 52 case UINTEGER: 53 { 54 char buffer[32]; | ||
55 kumpf 1.29 sprintf(buffer, "%u", _uinteger); | ||
56 david.dillard 1.24 return buffer; 57 } 58 59 case BOOLEAN: 60 { 61 //char buffer[32]; 62 //buffer = (_boolean ? "true": "false"); 63 //return buffer; 64 return (_boolean ? "true": "false"); 65 } 66 67 case REAL: 68 { 69 char buffer[32]; 70 sprintf(buffer, "%f", _real); 71 return buffer; 72 } | ||
73 mike 1.13 | ||
74 kumpf 1.22 case LINTEGER: 75 { 76 char buffer[32]; 77 sprintf(buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "d", _lInteger); 78 return buffer; 79 } 80 81 case ULINTEGER: 82 { 83 char buffer[32]; 84 sprintf(buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", _lUInteger); 85 return buffer; 86 } | ||
87 mike 1.13 | ||
88 david.dillard 1.24 case STRING: 89 return _string; | ||
90 mike 1.13 | ||
91 david.dillard 1.24 case CSTRLIT: | ||
92 kumpf 1.28 return String(_cstrlit->str, _cstrlit->size); | ||
93 david.dillard 1.24 break; 94 95 case VOIDT: 96 default: 97 return String(); | ||
98 mike 1.13 } 99 } 100 | ||
101 mike 1.23 struct StrRef 102 { 103 const char* str; 104 size_t size; 105 }; 106 107 static const StrRef _num_strings[] = 108 { 109 { STRLIT_ARGS("0") }, 110 { STRLIT_ARGS("1") }, 111 { STRLIT_ARGS("2") }, 112 { STRLIT_ARGS("3") }, 113 { STRLIT_ARGS("4") }, 114 { STRLIT_ARGS("5") }, 115 { STRLIT_ARGS("6") }, 116 { STRLIT_ARGS("7") }, 117 { STRLIT_ARGS("8") }, 118 { STRLIT_ARGS("9") }, 119 { STRLIT_ARGS("10") }, 120 { STRLIT_ARGS("11") }, 121 { STRLIT_ARGS("12") }, 122 mike 1.23 { STRLIT_ARGS("13") }, 123 { STRLIT_ARGS("14") }, 124 { STRLIT_ARGS("15") }, 125 { STRLIT_ARGS("16") }, 126 { STRLIT_ARGS("17") }, 127 { STRLIT_ARGS("18") }, 128 { STRLIT_ARGS("19") }, 129 { STRLIT_ARGS("20") }, 130 { STRLIT_ARGS("21") }, 131 { STRLIT_ARGS("22") }, 132 { STRLIT_ARGS("23") }, 133 { STRLIT_ARGS("24") }, 134 { STRLIT_ARGS("25") }, 135 { STRLIT_ARGS("26") }, 136 { STRLIT_ARGS("27") }, 137 { STRLIT_ARGS("28") }, 138 { STRLIT_ARGS("29") }, 139 { STRLIT_ARGS("30") }, 140 { STRLIT_ARGS("31") }, 141 { STRLIT_ARGS("32") }, 142 { STRLIT_ARGS("33") }, 143 mike 1.23 { STRLIT_ARGS("34") }, 144 { STRLIT_ARGS("35") }, 145 { STRLIT_ARGS("36") }, 146 { STRLIT_ARGS("37") }, 147 { STRLIT_ARGS("38") }, 148 { STRLIT_ARGS("39") }, 149 { STRLIT_ARGS("40") }, 150 { STRLIT_ARGS("41") }, 151 { STRLIT_ARGS("42") }, 152 { STRLIT_ARGS("43") }, 153 { STRLIT_ARGS("44") }, 154 { STRLIT_ARGS("45") }, 155 { STRLIT_ARGS("46") }, 156 { STRLIT_ARGS("47") }, 157 { STRLIT_ARGS("48") }, 158 { STRLIT_ARGS("49") }, 159 { STRLIT_ARGS("50") }, 160 { STRLIT_ARGS("51") }, 161 { STRLIT_ARGS("52") }, 162 { STRLIT_ARGS("53") }, 163 { STRLIT_ARGS("54") }, 164 mike 1.23 { STRLIT_ARGS("55") }, 165 { STRLIT_ARGS("56") }, 166 { STRLIT_ARGS("57") }, 167 { STRLIT_ARGS("58") }, 168 { STRLIT_ARGS("59") }, 169 { STRLIT_ARGS("60") }, 170 { STRLIT_ARGS("61") }, 171 { STRLIT_ARGS("62") }, 172 { STRLIT_ARGS("63") }, 173 { STRLIT_ARGS("64") }, 174 { STRLIT_ARGS("65") }, 175 { STRLIT_ARGS("66") }, 176 { STRLIT_ARGS("67") }, 177 { STRLIT_ARGS("68") }, 178 { STRLIT_ARGS("69") }, 179 { STRLIT_ARGS("70") }, 180 { STRLIT_ARGS("71") }, 181 { STRLIT_ARGS("72") }, 182 { STRLIT_ARGS("73") }, 183 { STRLIT_ARGS("74") }, 184 { STRLIT_ARGS("75") }, 185 mike 1.23 { STRLIT_ARGS("76") }, 186 { STRLIT_ARGS("77") }, 187 { STRLIT_ARGS("78") }, 188 { STRLIT_ARGS("79") }, 189 { STRLIT_ARGS("80") }, 190 { STRLIT_ARGS("81") }, 191 { STRLIT_ARGS("82") }, 192 { STRLIT_ARGS("83") }, 193 { STRLIT_ARGS("84") }, 194 { STRLIT_ARGS("85") }, 195 { STRLIT_ARGS("86") }, 196 { STRLIT_ARGS("87") }, 197 { STRLIT_ARGS("88") }, 198 { STRLIT_ARGS("89") }, 199 { STRLIT_ARGS("90") }, 200 { STRLIT_ARGS("91") }, 201 { STRLIT_ARGS("92") }, 202 { STRLIT_ARGS("93") }, 203 { STRLIT_ARGS("94") }, 204 { STRLIT_ARGS("95") }, 205 { STRLIT_ARGS("96") }, 206 mike 1.23 { STRLIT_ARGS("97") }, 207 { STRLIT_ARGS("98") }, 208 { STRLIT_ARGS("99") }, 209 { STRLIT_ARGS("100") }, 210 { STRLIT_ARGS("101") }, 211 { STRLIT_ARGS("102") }, 212 { STRLIT_ARGS("103") }, 213 { STRLIT_ARGS("104") }, 214 { STRLIT_ARGS("105") }, 215 { STRLIT_ARGS("106") }, 216 { STRLIT_ARGS("107") }, 217 { STRLIT_ARGS("108") }, 218 { STRLIT_ARGS("109") }, 219 { STRLIT_ARGS("110") }, 220 { STRLIT_ARGS("111") }, 221 { STRLIT_ARGS("112") }, 222 { STRLIT_ARGS("113") }, 223 { STRLIT_ARGS("114") }, 224 { STRLIT_ARGS("115") }, 225 { STRLIT_ARGS("116") }, 226 { STRLIT_ARGS("117") }, 227 mike 1.23 { STRLIT_ARGS("118") }, 228 { STRLIT_ARGS("119") }, 229 { STRLIT_ARGS("120") }, 230 { STRLIT_ARGS("121") }, 231 { STRLIT_ARGS("122") }, 232 { STRLIT_ARGS("123") }, 233 { STRLIT_ARGS("124") }, 234 { STRLIT_ARGS("125") }, 235 { STRLIT_ARGS("126") }, 236 { STRLIT_ARGS("127") }, 237 }; 238 239 template<class T> 240 static inline char* _append_unsigned(char* end, T x) 241 { 242 char* p = end; 243 244 do 245 { | ||
246 david.dillard 1.24 *--p = '0' + static_cast<char>(x % 10); | ||
247 mike 1.23 } 248 while ((x /= 10) != 0); 249 250 return p; 251 } 252 253 void Formatter::Arg::appendToString(String& out) const 254 { 255 switch (_type) 256 { | ||
257 david.dillard 1.24 case INTEGER: 258 { 259 Sint32 x = _integer; 260 261 if (x >= 0 && x < 128) | ||
262 kumpf 1.28 out.append(_num_strings[x].str, _num_strings[x].size); | ||
263 david.dillard 1.24 else 264 { 265 char buffer[32]; 266 int n = sprintf(buffer, "%d", x); 267 out.append(buffer, n); 268 } 269 break; 270 } 271 272 case UINTEGER: 273 { 274 Uint32 x = _uinteger; 275 276 if (x < 128) | ||
277 a.dunfey 1.27 { | ||
278 kumpf 1.28 out.append(_num_strings[x].str, _num_strings[x].size); | ||
279 a.dunfey 1.27 } | ||
280 david.dillard 1.24 else 281 { 282 char buffer[32]; 283 int n = sprintf(buffer, "%u", x); 284 out.append(buffer, n); 285 } 286 break; 287 } 288 289 case BOOLEAN: 290 { 291 if (_boolean) 292 out.append("true", 4); 293 else 294 out.append("false", 5); 295 break; 296 } 297 298 case REAL: 299 { 300 char buffer[32]; 301 david.dillard 1.24 int n = sprintf(buffer, "%f", _real); 302 out.append(buffer, n); 303 break; 304 } | ||
305 mike 1.23 306 case LINTEGER: 307 { 308 char buffer[32]; 309 int n = sprintf( | ||
310 david.dillard 1.24 buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "d", _lInteger); | ||
311 mike 1.23 out.append(buffer, n); | ||
312 david.dillard 1.24 break; | ||
313 mike 1.23 } 314 315 case ULINTEGER: 316 { | ||
317 david.dillard 1.24 Uint64 x = _lUInteger; 318 319 if (x < 128) | ||
320 a.dunfey 1.27 { | ||
321 kumpf 1.28 out.append(_num_strings[x].str, _num_strings[x].size); | ||
322 a.dunfey 1.27 } | ||
323 david.dillard 1.24 else 324 { 325 char buffer[32]; 326 char* end = &buffer[32]; 327 char* p = _append_unsigned(end, x); | ||
328 a.dunfey 1.27 out.append(p, (Uint32)(end - p)); | ||
329 david.dillard 1.24 } 330 break; 331 } 332 333 case STRING: 334 { 335 out.append(_string); 336 break; 337 } 338 339 case CSTRLIT: 340 { | ||
341 kumpf 1.28 out.append(_cstrlit->str, _cstrlit->size); | ||
342 david.dillard 1.24 break; 343 } | ||
344 mike 1.23 | ||
345 david.dillard 1.24 case VOIDT: 346 default: 347 break; | ||
348 mike 1.23 } 349 } 350 351 // 352 // Non-special characters (any but these: '$', '\0', '\\') 353 // 354 static const Uint8 _isNonSpecial[256] = 355 { 356 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 357 1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 358 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1, 359 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 360 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 361 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 362 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 363 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 364 }; 365 366 PEGASUS_USING_STD; 367 | ||
368 mike 1.13 String Formatter::format( | ||
369 mike 1.23 const String& format, | ||
370 mike 1.13 const Arg& arg0, 371 const Arg& arg1, 372 const Arg& arg2, 373 const Arg& arg3, 374 const Arg& arg4, 375 const Arg& arg5, 376 const Arg& arg6, 377 const Arg& arg7, 378 const Arg& arg8, 379 const Arg& arg9) 380 { 381 String result; 382 | ||
383 mike 1.23 #if 0 384 cout << format << endl; 385 #endif 386 387 result.reserveCapacity(256); 388 389 const Uint16* p = (const Uint16*)format.getChar16Data(); 390 391 for (;;) | ||
392 mike 1.13 { | ||
393 david.dillard 1.24 //// Skip over non-special characters: | ||
394 mike 1.23 | ||
395 david.dillard 1.24 const Uint16* start = p; | ||
396 mike 1.23 | ||
397 david.dillard 1.24 while (*p < 128 && _isNonSpecial[*p]) 398 p++; | ||
399 mike 1.23 | ||
400 david.dillard 1.24 //// Append any non-special characters. | ||
401 mike 1.23 | ||
402 david.dillard 1.24 size_t r = p - start; 403 404 if (r) | ||
405 a.dunfey 1.27 result.append((const Char16*)start, (Uint32)r); | ||
406 david.dillard 1.24 407 //// Process next special character: 408 409 if (*p == '$') 410 { 411 Uint16 c = p[1]; 412 413 switch (c - '0') 414 { 415 case 0: arg0.appendToString(result); break; 416 case 1: arg1.appendToString(result); break; 417 case 2: arg2.appendToString(result); break; 418 case 3: arg3.appendToString(result); break; 419 case 4: arg4.appendToString(result); break; 420 case 5: arg5.appendToString(result); break; 421 case 6: arg6.appendToString(result); break; 422 case 7: arg7.appendToString(result); break; 423 case 8: arg8.appendToString(result); break; 424 case 9: arg9.appendToString(result); break; 425 default: break; 426 } 427 david.dillard 1.24 428 p += 2; 429 } 430 else if (*p == '\\') 431 { 432 result.append(p[1]); 433 p += 2; 434 } 435 else if (*p == '\0') 436 { 437 break; 438 } 439 else 440 { 441 result.append(p[0]); 442 p++; 443 } | ||
444 mike 1.13 } 445 | ||
446 mike 1.23 #if 0 447 cout << result << endl; 448 #endif 449 | ||
450 mike 1.13 return result; 451 } 452 453 PEGASUS_NAMESPACE_END |
No CVS admin address has been configured |
Powered by ViewCVS 0.9.2 |