]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/numfmt/util.cpp
ICU-461.12.tar.gz
[apple/icu.git] / icuSources / samples / numfmt / util.cpp
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
729e4ab9 3 * Copyright (c) 1999-2003,2009, International Business Machines Corporation and
b75a7d8f
A
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) {
729e4ab9 75 delete[] buf;
b75a7d8f
A
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:
374ca955 92 case Formattable::kInt64:
b75a7d8f
A
93 {
94 char buf[256];
95 sprintf(buf, "%ldL", f.getLong());
96 return UnicodeString(buf, "");
97 }
98 case Formattable::kString:
99 return UnicodeString((UChar)U_DQUOTE).append(f.getString()).append((UChar)U_DQUOTE);
100 case Formattable::kArray:
101 {
102 int32_t i, count;
103 const Formattable* array = f.getArray(count);
104 UnicodeString result((UChar)U_LEFT_SQUARE_BRACKET);
105 for (i=0; i<count; ++i) {
106 if (i > 0) {
107 (result += (UChar)U_COMMA) += (UChar)U_SPACE;
108 }
109 result += formattableToString(array[i]);
110 }
111 result += (UChar)U_RIGHT_SQUARE_BRACKET;
112 return result;
113 }
114 default:
115 return UNICODE_STRING_SIMPLE("INVALID_Formattable");
116 }
117}