]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/numfmt/util.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / samples / numfmt / util.cpp
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1999-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "unicode/unistr.h"
8#include "unicode/fmtable.h"
9#include <stdio.h>
10#include <stdlib.h>
11
12enum {
13 U_SPACE=0x20,
14 U_DQUOTE=0x22,
15 U_COMMA=0x2c,
16 U_LEFT_SQUARE_BRACKET=0x5b,
17 U_BACKSLASH=0x5c,
18 U_RIGHT_SQUARE_BRACKET=0x5d,
19 U_SMALL_U=0x75
20};
21
22// Verify that a UErrorCode is successful; exit(1) if not
23void check(UErrorCode& status, const char* msg) {
24 if (U_FAILURE(status)) {
25 printf("ERROR: %s (%s)\n", u_errorName(status), msg);
26 exit(1);
27 }
28 // printf("Ok: %s\n", msg);
29}
30
31// Append a hex string to the target
32static UnicodeString& appendHex(uint32_t number,
33 int8_t digits,
34 UnicodeString& target) {
35 uint32_t digit;
36 while (digits > 0) {
37 digit = (number >> ((--digits) * 4)) & 0xF;
38 target += (UChar)(digit < 10 ? 0x30 + digit : 0x41 - 10 + digit);
39 }
40 return target;
41}
42
43// Replace nonprintable characters with unicode escapes
44UnicodeString escape(const UnicodeString &source) {
45 int32_t i;
46 UnicodeString target;
47 target += (UChar)U_DQUOTE;
48 for (i=0; i<source.length(); ++i) {
49 UChar ch = source[i];
50 if (ch < 0x09 || (ch > 0x0D && ch < 0x20) || ch > 0x7E) {
51 (target += (UChar)U_BACKSLASH) += (UChar)U_SMALL_U;
52 appendHex(ch, 4, target);
53 } else {
54 target += ch;
55 }
56 }
57 target += (UChar)U_DQUOTE;
58 return target;
59}
60
61// Print the given string to stdout using the UTF-8 converter
62void uprintf(const UnicodeString &str) {
63 char stackBuffer[100];
64 char *buf = 0;
65
66 int32_t bufLen = str.extract(0, 0x7fffffff, stackBuffer, sizeof(stackBuffer), "UTF-8");
67 if(bufLen < sizeof(stackBuffer)) {
68 buf = stackBuffer;
69 } else {
70 buf = new char[bufLen + 1];
71 bufLen = str.extract(0, 0x7fffffff, buf, bufLen + 1, "UTF-8");
72 }
73 printf("%s", buf);
74 if(buf != stackBuffer) {
75 delete buf;
76 }
77}
78
79// Create a display string for a formattable
80UnicodeString formattableToString(const Formattable& f) {
81 switch (f.getType()) {
82 case Formattable::kDate:
83 // TODO: Finish implementing this
84 return UNICODE_STRING_SIMPLE("Formattable_DATE_TBD");
85 case Formattable::kDouble:
86 {
87 char buf[256];
88 sprintf(buf, "%gD", f.getDouble());
89 return UnicodeString(buf, "");
90 }
91 case Formattable::kLong:
92 {
93 char buf[256];
94 sprintf(buf, "%ldL", f.getLong());
95 return UnicodeString(buf, "");
96 }
97 case Formattable::kString:
98 return UnicodeString((UChar)U_DQUOTE).append(f.getString()).append((UChar)U_DQUOTE);
99 case Formattable::kArray:
100 {
101 int32_t i, count;
102 const Formattable* array = f.getArray(count);
103 UnicodeString result((UChar)U_LEFT_SQUARE_BRACKET);
104 for (i=0; i<count; ++i) {
105 if (i > 0) {
106 (result += (UChar)U_COMMA) += (UChar)U_SPACE;
107 }
108 result += formattableToString(array[i]);
109 }
110 result += (UChar)U_RIGHT_SQUARE_BRACKET;
111 return result;
112 }
113 default:
114 return UNICODE_STRING_SIMPLE("INVALID_Formattable");
115 }
116}