mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-14 17:24:01 +00:00
ICU-434 add basic NumberFormat sample, C and C++ APIs
X-SVN-Rev: 2799
This commit is contained in:
parent
78bef52bec
commit
f64f312b4f
6 changed files with 374 additions and 0 deletions
65
icu4c/source/samples/numfmt/capi.c
Normal file
65
icu4c/source/samples/numfmt/capi.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include "unicode/unum.h"
|
||||
#include "unicode/ustring.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void uprintf(const UChar* str) {
|
||||
char buf[256];
|
||||
u_austrcpy(buf, str);
|
||||
printf("%s", buf);
|
||||
}
|
||||
|
||||
void capi() {
|
||||
UNumberFormat *fmt;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
/* The string "987654321.123" as UChars */
|
||||
UChar str[] = { 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33,
|
||||
0x32, 0x31, 0x30, 0x2E, 0x31, 0x32, 0x33, 0 };
|
||||
UChar buf[256];
|
||||
int32_t needed;
|
||||
double a;
|
||||
|
||||
/* Create a formatter for the US locale */
|
||||
fmt = unum_open(UNUM_DECIMAL, "en_US", &status);
|
||||
if (U_FAILURE(status)) {
|
||||
printf("FAIL: unum_open\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Use the formatter to parse a number. When using the C API,
|
||||
we have to specify whether we want a double or a long in advance.
|
||||
|
||||
We pass in NULL for the position pointer in order to get the
|
||||
default behavior which is to parse from the start. */
|
||||
a = unum_parseDouble(fmt, str, u_strlen(str), NULL, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
printf("FAIL: unum_parseDouble\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Show the result */
|
||||
printf("unum_parseDouble(\"");
|
||||
uprintf(str);
|
||||
printf("\") => %g\n", a);
|
||||
|
||||
/* Use the formatter to format the same number back into a string
|
||||
in the US locale. The return value is the buffer size needed.
|
||||
We're pretty sure we have enough space, but in a production
|
||||
application one would check this value.
|
||||
|
||||
We pass in NULL for the UFieldPosition pointer because we don't
|
||||
care to receive that data. */
|
||||
needed = unum_formatDouble(fmt, a, buf, 256, NULL, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
printf("FAIL: format_parseDouble\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Show the result */
|
||||
printf("unum_formatDouble(%g) => \"", a);
|
||||
uprintf(buf);
|
||||
printf("\"\n");
|
||||
|
||||
/* Release the storage used by the formatter */
|
||||
unum_close(fmt);
|
||||
}
|
59
icu4c/source/samples/numfmt/main.cpp
Normal file
59
icu4c/source/samples/numfmt/main.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include "unicode/unistr.h"
|
||||
#include "unicode/numfmt.h"
|
||||
#include "unicode/locid.h"
|
||||
#include "util.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern "C" void capi();
|
||||
void cppapi();
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
printf("C++ API\n");
|
||||
cppapi();
|
||||
|
||||
printf("C API\n");
|
||||
capi();
|
||||
|
||||
printf("Exiting successfully\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sample code for the C++ API to NumberFormat.
|
||||
*/
|
||||
void cppapi() {
|
||||
Locale us("en", "US");
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
|
||||
// Create a number formatter for the US locale
|
||||
NumberFormat *fmt = NumberFormat::createInstance(us, status);
|
||||
check(status, "NumberFormat::createInstance");
|
||||
|
||||
// Parse a string. The string uses the digits '0' through '9'
|
||||
// and the decimal separator '.', standard in the US locale
|
||||
UnicodeString str("9876543210.123");
|
||||
Formattable result;
|
||||
fmt->parse(str, result, status);
|
||||
check(status, "NumberFormat::parse");
|
||||
|
||||
printf("NumberFormat::parse(\""); // Display the result
|
||||
uprintf(str);
|
||||
printf("\") => ");
|
||||
uprintf(formattableToString(result));
|
||||
printf("\n");
|
||||
|
||||
// Take the number parsed above, and use the formatter to
|
||||
// format it.
|
||||
str.remove(); // format() will APPEND to this string
|
||||
fmt->format(result, str, status);
|
||||
check(status, "NumberFormat::format");
|
||||
|
||||
printf("NumberFormat::format("); // Display the result
|
||||
uprintf(formattableToString(result));
|
||||
printf(") => \"");
|
||||
uprintf(str);
|
||||
printf("\"\n");
|
||||
|
||||
delete fmt; // Release the storage used by the formatter
|
||||
}
|
110
icu4c/source/samples/numfmt/numfmt.dsp
Normal file
110
icu4c/source/samples/numfmt/numfmt.dsp
Normal file
|
@ -0,0 +1,110 @@
|
|||
# Microsoft Developer Studio Project File - Name="numfmt" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Console Application" 0x0103
|
||||
|
||||
CFG=numfmt - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "numfmt.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "numfmt.mak" CFG="numfmt - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "numfmt - Win32 Release" (based on "Win32 (x86) Console Application")
|
||||
!MESSAGE "numfmt - Win32 Debug" (based on "Win32 (x86) Console Application")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "numfmt - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
|
||||
# ADD CPP /nologo /W3 /GX /O2 /I "../../../include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
|
||||
# ADD BASE RSC /l 0x409 /d "NDEBUG"
|
||||
# ADD RSC /l 0x409 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
|
||||
# ADD LINK32 icuuc.lib icui18n.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /libpath:"../../../lib/Release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "numfmt - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../../../include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
|
||||
# ADD BASE RSC /l 0x409 /d "_DEBUG"
|
||||
# ADD RSC /l 0x409 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 icuuc.lib icui18n.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"../../../lib/Debug"
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "numfmt - Win32 Release"
|
||||
# Name "numfmt - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\capi.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\main.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\util.cpp
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
29
icu4c/source/samples/numfmt/numfmt.dsw
Normal file
29
icu4c/source/samples/numfmt/numfmt.dsw
Normal file
|
@ -0,0 +1,29 @@
|
|||
Microsoft Developer Studio Workspace File, Format Version 6.00
|
||||
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
|
||||
|
||||
###############################################################################
|
||||
|
||||
Project: "numfmt"=.\numfmt.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<4>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
||||
Global:
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<3>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
97
icu4c/source/samples/numfmt/util.cpp
Normal file
97
icu4c/source/samples/numfmt/util.cpp
Normal file
|
@ -0,0 +1,97 @@
|
|||
#include "unicode/unistr.h"
|
||||
#include "unicode/fmtable.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Verify that a UErrorCode is successful; exit(1) if not
|
||||
void check(UErrorCode& status, const char* msg) {
|
||||
if (U_FAILURE(status)) {
|
||||
printf("ERROR: %s (%s)\n", u_errorName(status), msg);
|
||||
exit(1);
|
||||
}
|
||||
// printf("Ok: %s\n", msg);
|
||||
}
|
||||
|
||||
// Append a hex string to the target
|
||||
static UnicodeString& appendHex(uint32_t number,
|
||||
int8_t digits,
|
||||
UnicodeString& target) {
|
||||
static const UnicodeString DIGIT_STRING("0123456789ABCDEF");
|
||||
while (digits > 0) {
|
||||
target += DIGIT_STRING[(number >> ((--digits) * 4)) & 0xF];
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
// Replace nonprintable characters with unicode escapes
|
||||
UnicodeString escape(const UnicodeString &source) {
|
||||
int32_t i;
|
||||
UnicodeString target;
|
||||
target += "\"";
|
||||
for (i=0; i<source.length(); ++i) {
|
||||
UChar ch = source[i];
|
||||
if (ch < 0x09 || (ch > 0x0A && ch < 0x20) || ch > 0x7E) {
|
||||
target += "\\u";
|
||||
appendHex(ch, 4, target);
|
||||
} else {
|
||||
target += ch;
|
||||
}
|
||||
}
|
||||
target += "\"";
|
||||
return target;
|
||||
}
|
||||
|
||||
// Print the given string to stdout
|
||||
void uprintf(const UnicodeString &str) {
|
||||
char *buf = 0;
|
||||
int32_t len = str.length();
|
||||
// int32_t bufLen = str.extract(0, len, buf); // Preflight
|
||||
/* Preflighting seems to be broken now, so assume 1-1 conversion,
|
||||
plus some slop. */
|
||||
int32_t bufLen = len + 16;
|
||||
int32_t actualLen;
|
||||
buf = new char[bufLen + 1];
|
||||
actualLen = str.extract(0, len, buf/*, bufLen*/); // Default codepage conversion
|
||||
buf[actualLen] = 0;
|
||||
printf("%s", buf);
|
||||
delete buf;
|
||||
}
|
||||
|
||||
// Create a display string for a formattable
|
||||
UnicodeString formattableToString(const Formattable& f) {
|
||||
switch (f.getType()) {
|
||||
case Formattable::kDate:
|
||||
// TODO: Finish implementing this
|
||||
return UnicodeString("Formattable_DATE_TBD");
|
||||
case Formattable::kDouble:
|
||||
{
|
||||
char buf[256];
|
||||
sprintf(buf, "%gD", f.getDouble());
|
||||
return UnicodeString(buf);
|
||||
}
|
||||
case Formattable::kLong:
|
||||
{
|
||||
char buf[256];
|
||||
sprintf(buf, "%ldL", f.getLong());
|
||||
return UnicodeString(buf);
|
||||
}
|
||||
case Formattable::kString:
|
||||
return UnicodeString("\"").append(f.getString()).append("\"");
|
||||
case Formattable::kArray:
|
||||
{
|
||||
int32_t i, count;
|
||||
const Formattable* array = f.getArray(count);
|
||||
UnicodeString result("[");
|
||||
for (i=0; i<count; ++i) {
|
||||
if (i > 0) {
|
||||
result += ", ";
|
||||
}
|
||||
result += formattableToString(array[i]);
|
||||
}
|
||||
result += "]";
|
||||
return result;
|
||||
}
|
||||
default:
|
||||
return UnicodeString("INVALID_Formattable");
|
||||
}
|
||||
}
|
14
icu4c/source/samples/numfmt/util.h
Normal file
14
icu4c/source/samples/numfmt/util.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "unicode/unistr.h"
|
||||
#include "unicode/fmtable.h"
|
||||
|
||||
// Verify that a UErrorCode is successful; exit(1) if not
|
||||
void check(UErrorCode& status, const char* msg);
|
||||
|
||||
// Replace nonprintable characters with unicode escapes
|
||||
UnicodeString escape(const UnicodeString &source);
|
||||
|
||||
// Print the given string to stdout
|
||||
void uprintf(const UnicodeString &str);
|
||||
|
||||
// Create a display string for a formattable
|
||||
UnicodeString formattableToString(const Formattable& f);
|
Loading…
Add table
Reference in a new issue