~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/samples/msgfmt/util.cpp

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "unicode/unistr.h"
2
 
#include <stdio.h>
3
 
#include <stdlib.h>
4
 
 
5
 
// Verify that a UErrorCode is successful; exit(1) if not
6
 
void check(UErrorCode& status, const char* msg) {
7
 
    if (U_FAILURE(status)) {
8
 
        printf("ERROR: %s (%s)\n", u_errorName(status), msg);
9
 
        exit(1);
10
 
    }
11
 
    // printf("Ok: %s\n", msg);
12
 
}
13
 
                                                      
14
 
// Append a hex string to the target
15
 
static UnicodeString& appendHex(uint32_t number, 
16
 
                         int8_t digits, 
17
 
                         UnicodeString& target) {
18
 
    static const UnicodeString DIGIT_STRING("0123456789ABCDEF");
19
 
    while (digits > 0) {
20
 
        target += DIGIT_STRING[(number >> ((--digits) * 4)) & 0xF];
21
 
    }
22
 
    return target;
23
 
}
24
 
 
25
 
// Replace nonprintable characters with unicode escapes
26
 
UnicodeString escape(const UnicodeString &source) {
27
 
    int32_t i;
28
 
    UnicodeString target;
29
 
    target += "\"";
30
 
    for (i=0; i<source.length(); ++i) {
31
 
        UChar ch = source[i];
32
 
        if (ch < 0x09 || (ch > 0x0A && ch < 0x20) || ch > 0x7E) {
33
 
            target += "\\u";
34
 
            appendHex(ch, 4, target);
35
 
        } else {
36
 
            target += ch;
37
 
        }
38
 
    }
39
 
    target += "\"";
40
 
    return target;
41
 
}
42
 
 
43
 
// Print the given string to stdout
44
 
void uprintf(const UnicodeString &str) {
45
 
    char *buf = 0;
46
 
    int32_t len = str.length();
47
 
    // int32_t bufLen = str.extract(0, len, buf); // Preflight
48
 
    /* Preflighting seems to be broken now, so assume 1-1 conversion,
49
 
       plus some slop. */
50
 
    int32_t bufLen = len + 16;
51
 
        int32_t actualLen;
52
 
    buf = new char[bufLen + 1];
53
 
    actualLen = str.extract(0, len, buf/*, bufLen*/); // Default codepage conversion
54
 
    buf[actualLen] = 0;
55
 
    printf("%s", buf);
56
 
    delete buf;
57
 
}