ICU-2369 move sample code out of usersguide
X-SVN-Rev: 10857
This commit is contained in:
parent
2cfa2ab7bb
commit
2370e664c8
24
icu4c/source/samples/case/Makefile
Normal file
24
icu4c/source/samples/case/Makefile
Normal file
@ -0,0 +1,24 @@
|
||||
# Copyright (c) 2003 IBM, Inc. and others
|
||||
# sample code makefile
|
||||
|
||||
# Usage:
|
||||
# - configure, build, install ICU (make install)
|
||||
# - make sure "icu-config" (in the ICU installed bin directory) is on
|
||||
# the path
|
||||
# - do 'make' in this directory
|
||||
|
||||
#### definitions
|
||||
# Name of your target
|
||||
TARGET=case
|
||||
|
||||
# All object files (C or C++)
|
||||
OBJECTS=case.o ucase.o
|
||||
|
||||
#### rules
|
||||
# Load in standard makefile definitions
|
||||
include ../defs.mk
|
||||
|
||||
LDFLAGS += $(LDFLAGS_USTDIO)
|
||||
|
||||
# the actual rules (this is a simple sample)
|
||||
include ../rules.mk
|
85
icu4c/source/samples/case/case.cpp
Normal file
85
icu4c/source/samples/case/case.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
*
|
||||
* Copyright (C) 2003, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
#include <unicode/unistr.h>
|
||||
#include <unicode/ustdio.h>
|
||||
#include <iostream.h>
|
||||
#include <unicode/brkiter.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
U_CFUNC int c_main(UFILE *out);
|
||||
|
||||
void printUnicodeString(UFILE *out, const UnicodeString &s) {
|
||||
UnicodeString other = s;
|
||||
u_fprintf(out, "\"%U\"", other.getTerminatedBuffer());
|
||||
}
|
||||
|
||||
|
||||
int main( void )
|
||||
{
|
||||
UFILE *out;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
out = u_finit(stdout, NULL, NULL);
|
||||
if(!out) {
|
||||
fprintf(stderr, "Could not initialize (finit()) over stdout! \n");
|
||||
return 1;
|
||||
}
|
||||
ucnv_setFromUCallBack(u_fgetConverter(out), UCNV_FROM_U_CALLBACK_ESCAPE,
|
||||
NULL, NULL, NULL, &status);
|
||||
if(U_FAILURE(status)) {
|
||||
u_fprintf(out, "Warning- couldn't set the substitute callback - err %s\n", u_errorName(status));
|
||||
}
|
||||
|
||||
/* End Demo boilerplate */
|
||||
|
||||
u_fprintf(out,"ICU Case Mapping Sample Program\n\n");
|
||||
u_fprintf(out, "C++ Case Mapping\n\n");
|
||||
|
||||
UnicodeString string("This is a test");
|
||||
/* lowercase = "istanbul" */
|
||||
UChar lowercase[] = {0x69, 0x73, 0x74, 0x61, 0x6e, 0x62, 0x75, 0x6c, 0};
|
||||
/* uppercase = "LATIN CAPITAL I WITH DOT ABOVE STANBUL" */
|
||||
UChar uppercase[] = {0x0130, 0x53, 0x54, 0x41, 0x4e, 0x42, 0x55, 0x4C, 0};
|
||||
|
||||
UnicodeString upper(uppercase);
|
||||
UnicodeString lower(lowercase);
|
||||
|
||||
u_fprintf(out, "\nstring: ");
|
||||
printUnicodeString(out, string);
|
||||
string.toUpper(); /* string = "THIS IS A TEST" */
|
||||
u_fprintf(out, "\ntoUpper(): ");
|
||||
printUnicodeString(out, string);
|
||||
string.toLower(); /* string = "this is a test" */
|
||||
u_fprintf(out, "\ntoLower(): ");
|
||||
printUnicodeString(out, string);
|
||||
|
||||
u_fprintf(out, "\n\nlowercase=%U, uppercase=%U\n", lowercase, uppercase);
|
||||
|
||||
|
||||
string = upper;
|
||||
string.toLower(Locale("tr", "TR")); /* Turkish lower case map string =
|
||||
lowercase */
|
||||
u_fprintf(out, "\nupper.toLower: ");
|
||||
printUnicodeString(out, string);
|
||||
|
||||
string = lower;
|
||||
string.toUpper(Locale("tr", "TR")); /* Turkish upper case map string =
|
||||
uppercase */
|
||||
u_fprintf(out, "\nlower.toUpper: ");
|
||||
printUnicodeString(out, string);
|
||||
|
||||
|
||||
|
||||
u_fprintf(out, "\nEnd C++ sample\n\n");
|
||||
|
||||
// Call the C version
|
||||
int rc = c_main(out);
|
||||
u_fclose(out);
|
||||
return rc;
|
||||
}
|
60
icu4c/source/samples/case/readme.txt
Normal file
60
icu4c/source/samples/case/readme.txt
Normal file
@ -0,0 +1,60 @@
|
||||
Copyright (c) 2003, International Business Machines Corporation and others. All Rights Reserved.
|
||||
case: case mapping
|
||||
|
||||
This sample demonstrates
|
||||
Using ICU to convert between different cases
|
||||
|
||||
|
||||
Files:
|
||||
case.cpp Main source file in C++
|
||||
ucase.c Main source file in C
|
||||
case.dsp Windows MSVC project file
|
||||
|
||||
To Build case on Windows
|
||||
1. Install and build ICU
|
||||
2. In MSVC, open the project file icu\samples\case\case.dsp
|
||||
(or, use the workspace All, in icu\samples\all\all.dsw )
|
||||
3. Choose a Debug or Release build.
|
||||
4. Build.
|
||||
|
||||
To Run on Windows
|
||||
1. Start a command shell window
|
||||
2. Add ICU's bin directory to the path, e.g.
|
||||
set PATH=c:\icu\bin;%PATH%
|
||||
(Use the path to where ever ICU is on your system.)
|
||||
3. cd into the case directory, e.g.
|
||||
cd c:\icu\source\samples\case\debug
|
||||
4. Run it
|
||||
case
|
||||
|
||||
To Build on Unixes
|
||||
1. Build ICU.
|
||||
Specify an ICU install directory when running configure,
|
||||
using the --prefix option. The steps to build ICU will look something
|
||||
like this:
|
||||
cd <icu directory>/source
|
||||
runConfigureICU <platform-name> --prefix <icu install directory> [other options]
|
||||
gmake all
|
||||
|
||||
2. Install ICU,
|
||||
gmake install
|
||||
|
||||
3. Compile
|
||||
cd <icu directory>/source/samples/case
|
||||
gmake ICU_PREFIX=<icu install directory)
|
||||
|
||||
To Run on Unixes
|
||||
cd <icu directory>/source/samples/case
|
||||
|
||||
gmake ICU_PREFIX=<icu install directory> check
|
||||
-or-
|
||||
|
||||
export LD_LIBRARY_PATH=<icu install directory>/lib:.:$LD_LIBRARY_PATH
|
||||
case
|
||||
|
||||
|
||||
Note: The name of the LD_LIBRARY_PATH variable is different on some systems.
|
||||
If in doubt, run the sample using "gmake check", and note the name of
|
||||
the variable that is used there. LD_LIBRARY_PATH is the correct name
|
||||
for Linux and Solaris.
|
||||
|
82
icu4c/source/samples/case/ucase.c
Normal file
82
icu4c/source/samples/case/ucase.c
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
*
|
||||
* Copyright (C) 2003, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
*
|
||||
****
|
||||
*
|
||||
* Case folding examples, in C
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
#include "unicode/uchar.h"
|
||||
#include "unicode/ustring.h"
|
||||
#include "unicode/utypes.h"
|
||||
#include "unicode/ustdio.h"
|
||||
|
||||
/* Note: don't use 'k' or 'K' because we might be on EBCDIC */
|
||||
|
||||
int c_main(UFILE *out)
|
||||
{
|
||||
printf("** C Case Mapping Sample\n");
|
||||
UChar ch;
|
||||
UErrorCode errorCode = U_ZERO_ERROR;
|
||||
static const UChar upper[] = {0x61, 0x42, 0x49, 0}; /* upper = "aBI" */
|
||||
static const UChar lower[] = {0x61, 0x42, 0x69, 0}; /* lower = "abi" */
|
||||
/* unfold = "aB LATIN SMALL LETTER DOTLESS I" */
|
||||
static const UChar unfold[] = {0x61, 0x42, 0x131, 0} ;
|
||||
UChar buffer[32];
|
||||
const UChar char_k = 0x006b; /* 'k' */
|
||||
const UChar char_K = 0x004b; /* 'K' */
|
||||
|
||||
int length;
|
||||
|
||||
/* uchar.h APIs, single character case mapping */
|
||||
ch = u_toupper(char_k); /* ch = 'K' */
|
||||
u_fprintf(out, "toupper(%K) = %K\n", char_k, ch);
|
||||
ch = u_tolower(ch); /* ch = 'k' */
|
||||
u_fprintf(out, "tolower() = %K\n", ch);
|
||||
ch = u_totitle(char_k); /* ch = 'K' */
|
||||
u_fprintf(out, "totitle(%K) = %K\n", char_k, ch);
|
||||
ch = u_foldCase(char_K, U_FOLD_CASE_DEFAULT); /* ch = 'k' */
|
||||
u_fprintf(out, "u_foldCase(%K, U_FOLD_CASE_DEFAULT) = %K\n", char_K, ch);
|
||||
|
||||
/* ustring.h APIs, UChar * string case mapping with a Turkish locale */
|
||||
/* result buffer = "ab?" latin small letter a, latin small letter b, latin
|
||||
small letter dotless i */
|
||||
length = u_strToLower(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
|
||||
sizeof(upper)/sizeof(upper[0]), "tr", &errorCode);
|
||||
if(U_FAILURE(errorCode) || buffer[length]!=0) {
|
||||
u_fprintf(out, "error in u_strToLower(Turkish locale)=%ld error=%s\n", length,
|
||||
u_errorName(errorCode));
|
||||
}
|
||||
|
||||
u_fprintf(out, "u_strToLower(%U, turkish) -> %U\n", upper, buffer);
|
||||
|
||||
|
||||
/* ustring.h APIs, UChar * string case mapping with a Engish locale */
|
||||
/* result buffer = "ABI" latin CAPITAL letter A, latin capital letter B,
|
||||
latin capital letter I */
|
||||
length = u_strToUpper(buffer, sizeof(buffer)/sizeof(buffer[0]), upper,
|
||||
sizeof(upper)/sizeof(upper[0]), "en", &errorCode);
|
||||
if(U_FAILURE(errorCode) || buffer[length]!=0) {
|
||||
u_fprintf(out, "error in u_strToLower(English locale)=%ld error=%s\n", length,
|
||||
u_errorName(errorCode));
|
||||
}
|
||||
u_fprintf(out, "u_strToUpper(%U, english) -> %U\n", lower, buffer);
|
||||
|
||||
|
||||
/* ustring.h APIs, UChar * string case folding */
|
||||
/* result buffer = "abi" */
|
||||
length = u_strFoldCase(buffer, sizeof(buffer)/sizeof(buffer[0]), unfold,
|
||||
sizeof(unfold)/sizeof(unfold[0]), U_FOLD_CASE_DEFAULT,
|
||||
&errorCode);
|
||||
if(U_FAILURE(errorCode) || buffer[length]!=0) {
|
||||
u_fprintf(out, "error in u_strFoldCase()=%ld error=%s\n", length,
|
||||
u_errorName(errorCode));
|
||||
}
|
||||
u_fprintf(out, "u_strFoldCase(%U, U_FOLD_CASE_DEFAULT) -> %U\n", unfold, buffer);
|
||||
u_fprintf(out, "\n** end of C sample\n");
|
||||
}
|
Loading…
Reference in New Issue
Block a user