From 475703cd402acc09fd0d19d9cf2bf04006e124f0 Mon Sep 17 00:00:00 2001 From: Craig Cornelius Date: Fri, 25 Feb 2022 02:51:44 +0000 Subject: [PATCH] ICU-21900 Fix ICU4C sample build and execution This fixes several of the sample C++ programs. --- icu4c/source/samples/Makefile.in | 2 +- icu4c/source/samples/case/ucase.c | 3 + icu4c/source/samples/citer/citer.cpp | 8 +- icu4c/source/samples/coll/coll.cpp | 8 +- icu4c/source/samples/datecal/cal.cpp | 2 + icu4c/source/samples/dtitvfmtsample/Makefile | 27 +++ .../samples/dtitvfmtsample/dtitvfmtsample.cpp | 180 +++++++------- icu4c/source/samples/dtptngsample/Makefile | 27 +++ .../samples/dtptngsample/dtptngsample.cpp | 220 +++++++++--------- icu4c/source/samples/plurfmtsample/Makefile | 27 +++ .../samples/plurfmtsample/plurfmtsample.cpp | 60 ++--- icu4c/source/samples/readme.txt | 45 ++-- icu4c/source/samples/strsrch/strsrch.cpp | 6 +- 13 files changed, 355 insertions(+), 260 deletions(-) create mode 100644 icu4c/source/samples/dtitvfmtsample/Makefile create mode 100644 icu4c/source/samples/dtptngsample/Makefile create mode 100644 icu4c/source/samples/plurfmtsample/Makefile diff --git a/icu4c/source/samples/Makefile.in b/icu4c/source/samples/Makefile.in index 374a70f3525..8f2514bfd00 100644 --- a/icu4c/source/samples/Makefile.in +++ b/icu4c/source/samples/Makefile.in @@ -23,7 +23,7 @@ include @platform_make_fragment@ CLEANFILES = *~ SUBDIRS = date cal -ALLSUBDIRS = break case csdet datefmt msgfmt numfmt props translit ucnv udata ufortune uresb ustring citer uciter8 ugrep +ALLSUBDIRS = cal citer coll csdet date datecal ufortune uresb datefmt msgfmt numfmt props strsrch translit uciter8 ucnv udata ustring dtitvfmtsample dtptngsample plurfmtsample ## List of phony targets .PHONY : all all-local all-recursive install install-local \ diff --git a/icu4c/source/samples/case/ucase.c b/icu4c/source/samples/case/ucase.c index 439c3c5b684..3cabe22d606 100644 --- a/icu4c/source/samples/case/ucase.c +++ b/icu4c/source/samples/case/ucase.c @@ -17,6 +17,9 @@ ******************************************************************************* */ +#include +#include + #include "unicode/uchar.h" #include "unicode/ustring.h" #include "unicode/utypes.h" diff --git a/icu4c/source/samples/citer/citer.cpp b/icu4c/source/samples/citer/citer.cpp index 958fe42587e..f8193707144 100644 --- a/icu4c/source/samples/citer/citer.cpp +++ b/icu4c/source/samples/citer/citer.cpp @@ -73,7 +73,7 @@ void Test::TestUChariter() { test2->getText(result2); if (result1 != result2) { u_fprintf(out, "iter.getText() != clone.getText()\n"); - } + } u_fprintf(out, "\n"); @@ -95,7 +95,7 @@ void Test::TestUChariter() { u_fprintf(out, "Iterator reached end prematurely"); } else if (c != testText[i]) { - u_fprintf(out, "Character mismatch at position %d\n" + i); + u_fprintf(out, "Character mismatch at position %d\n", i); } if (iter.current() != c) { u_fprintf(out, "current() isn't working right"); @@ -111,7 +111,7 @@ void Test::TestUChariter() { u_fprintf(out, "|"); printUChar(c); - } while (c != CharacterIterator::DONE); + } while (c != CharacterIterator::DONE); delete test2; u_fprintf(out, "\n"); @@ -146,7 +146,7 @@ void Test::TestStringiter() { int32_t i = iter.endIndex(); printUChar(c); - i--; // already printed out the last char + i--; // already printed out the last char if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) { u_fprintf(out, "startIndex() or endIndex() failed\n"); diff --git a/icu4c/source/samples/coll/coll.cpp b/icu4c/source/samples/coll/coll.cpp index d8d067a8520..909412f5b67 100644 --- a/icu4c/source/samples/coll/coll.cpp +++ b/icu4c/source/samples/coll/coll.cpp @@ -45,8 +45,8 @@ const char gHelpString[] = * These global variables are set according to the options specified * on the command line by the user. */ -char * opt_locale = "en_US"; -char * opt_rules = 0; +char const *opt_locale = "en_US"; +char *opt_rules = 0; UBool opt_help = false; UBool opt_norm = false; UBool opt_french = false; @@ -55,8 +55,8 @@ UBool opt_lower = false; UBool opt_upper = false; UBool opt_case = false; int opt_level = 0; -char * opt_source = "abc"; -char * opt_target = "abd"; +char const *opt_source = "abc"; +char const *opt_target = "abd"; UCollator * collator = 0; /** diff --git a/icu4c/source/samples/datecal/cal.cpp b/icu4c/source/samples/datecal/cal.cpp index ba581b88d80..f8c0ef500bc 100644 --- a/icu4c/source/samples/datecal/cal.cpp +++ b/icu4c/source/samples/datecal/cal.cpp @@ -17,6 +17,8 @@ #include "unicode/gregocal.h" #include +using namespace icu; + extern "C" void c_main(); void cpp_main() diff --git a/icu4c/source/samples/dtitvfmtsample/Makefile b/icu4c/source/samples/dtitvfmtsample/Makefile new file mode 100644 index 00000000000..404c9603c74 --- /dev/null +++ b/icu4c/source/samples/dtitvfmtsample/Makefile @@ -0,0 +1,27 @@ +# Copyright (C) 2016 and later: Unicode, Inc. and others. +# License & terms of use: http://www.unicode.org/copyright.html +# +# Copyright (c) 2000-2006 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=dtitvfmtsample + +# All object files (C or C++) +OBJECTS=dtitvfmtsample.o + +#### rules +# Load in standard makefile definitions +include ../defs.mk + +LDFLAGS += $(LDFLAGS_USTDIO) + +# the actual rules (this is a simple sample) +include ../rules.mk diff --git a/icu4c/source/samples/dtitvfmtsample/dtitvfmtsample.cpp b/icu4c/source/samples/dtitvfmtsample/dtitvfmtsample.cpp index c4f7b18c89e..1f160dcf367 100644 --- a/icu4c/source/samples/dtitvfmtsample/dtitvfmtsample.cpp +++ b/icu4c/source/samples/dtitvfmtsample/dtitvfmtsample.cpp @@ -15,130 +15,130 @@ using namespace std; using namespace icu; static void dtitvfmtPreDefined() { - - u_printf("===============================================================================\n"); - u_printf(" dtitvfmtPreDefined()\n"); + + u_printf("===============================================================================\n"); + u_printf(" dtitvfmtPreDefined()\n"); u_printf("\n"); u_printf(" Use DateIntervalFormat to get date interval format for pre-defined skeletons:\n"); u_printf(" yMMMd, MMMMd, jm per locale\n"); u_printf("===============================================================================\n"); - - //! [dtitvfmtPreDefined] - UFILE *out = u_finit(stdout, NULL, "UTF-8"); - UErrorCode status =U_ZERO_ERROR; - // create 3 Date Intervals - UnicodeString data[] = { - UnicodeString("2007-10-10 10:10:10"), - UnicodeString("2008-10-10 10:10:10"), - UnicodeString("2008-11-10 10:10:10"), - UnicodeString("2008-11-10 15:10:10") - }; - Calendar *cal = Calendar::createInstance(status); - cal->set(2007,10,10,10,10,10); - UDate date1 = cal->getTime(status); - cal->set(2008,10,10,10,10,10); - UDate date2 = cal->getTime(status); - cal->set(2008,11,10,10,10,10); - UDate date3 = cal->getTime(status); - cal->set(2008,11,10,15,10,10); - UDate date4 = cal->getTime(status); - DateInterval* dtitvsample[] = { - new DateInterval(date1,date2), - new DateInterval(date2,date3), - new DateInterval(date3,date4), + + //! [dtitvfmtPreDefined] + UFILE *out = u_finit(stdout, NULL, "UTF-8"); + UErrorCode status =U_ZERO_ERROR; + // create 3 Date Intervals + UnicodeString data[] = { + UnicodeString("2007-10-10 10:10:10"), + UnicodeString("2008-10-10 10:10:10"), + UnicodeString("2008-11-10 10:10:10"), + UnicodeString("2008-11-10 15:10:10") }; - UnicodeString skeletons[] = { + Calendar *cal = Calendar::createInstance(status); + cal->set(2007,10,10,10,10,10); + UDate date1 = cal->getTime(status); + cal->set(2008,10,10,10,10,10); + UDate date2 = cal->getTime(status); + cal->set(2008,11,10,10,10,10); + UDate date3 = cal->getTime(status); + cal->set(2008,11,10,15,10,10); + UDate date4 = cal->getTime(status); + DateInterval* dtitvsample[] = { + new DateInterval(date1,date2), + new DateInterval(date2,date3), + new DateInterval(date3,date4), + }; + UnicodeString skeletons[] = { UnicodeString("yMMMd"), UnicodeString("MMMMd"), UnicodeString("jm"), - 0, - }; + 0, + }; u_fprintf(out,"%-10s%-22s%-22s%-35s%-35s\n", "Skeleton","from","to","Date Interval in en_US","Date Interval in Ja"); - int i=0; - UnicodeString formatEn,formatJa; - FieldPosition pos=0; - for (int j=0;skeletons[j]!=NULL ;j++) { - u_fprintf(out,"%-10S%-22S%-22S",skeletons[j].getTerminatedBuffer(),data[i].getTerminatedBuffer(),data[i+1].getTerminatedBuffer()); + int i=0; + UnicodeString formatEn,formatJa; + FieldPosition pos=0; + for (int j=0; !skeletons[j].isEmpty() ;j++) { + u_fprintf(out,"%-10S%-22S%-22S",skeletons[j].getTerminatedBuffer(),data[i].getTerminatedBuffer(),data[i+1].getTerminatedBuffer()); //create a DateIntervalFormat instance for given skeleton, locale - DateIntervalFormat* dtitvfmtEn = DateIntervalFormat::createInstance(skeletons[j], Locale::getEnglish(),status); + DateIntervalFormat* dtitvfmtEn = DateIntervalFormat::createInstance(skeletons[j], Locale::getEnglish(),status); DateIntervalFormat* dtitvfmtJa = DateIntervalFormat::createInstance(skeletons[j], Locale::getJapanese(),status); - formatEn.remove(); - formatJa.remove(); - //get the DateIntervalFormat - dtitvfmtEn->format(dtitvsample[i],formatEn,pos,status); - dtitvfmtJa->format(dtitvsample[i],formatJa,pos,status); - u_fprintf(out,"%-35S%-35S\n", formatEn.getTerminatedBuffer(),formatJa.getTerminatedBuffer()); - delete dtitvfmtEn; - delete dtitvfmtJa; + formatEn.remove(); + formatJa.remove(); + //get the DateIntervalFormat + dtitvfmtEn->format(dtitvsample[i],formatEn,pos,status); + dtitvfmtJa->format(dtitvsample[i],formatJa,pos,status); + u_fprintf(out,"%-35S%-35S\n", formatEn.getTerminatedBuffer(),formatJa.getTerminatedBuffer()); + delete dtitvfmtEn; + delete dtitvfmtJa; i++; } - u_fclose(out); - //! [dtitvfmtPreDefined] + u_fclose(out); + //! [dtitvfmtPreDefined] } static void dtitvfmtCustomized() { - - u_printf("===============================================================================\n"); - u_printf("\n"); - u_printf(" dtitvfmtCustomized()\n"); - u_printf("\n"); + + u_printf("===============================================================================\n"); + u_printf("\n"); + u_printf(" dtitvfmtCustomized()\n"); + u_printf("\n"); u_printf(" Use DateIntervalFormat to create customized date interval format for yMMMd, Hm"); - u_printf("\n"); + u_printf("\n"); u_printf("================================================================================\n"); - //! [dtitvfmtCustomized] - UFILE *out = u_finit(stdout, NULL, "UTF-8"); - UErrorCode status =U_ZERO_ERROR; - UnicodeString data[] = { - UnicodeString("2007-9-10 10:10:10"), - UnicodeString("2007-10-10 10:10:10"), - UnicodeString("2007-10-10 22:10:10") - }; - // to create 2 Date Intervals + //! [dtitvfmtCustomized] + UFILE *out = u_finit(stdout, NULL, "UTF-8"); + UErrorCode status =U_ZERO_ERROR; + UnicodeString data[] = { + UnicodeString("2007-9-10 10:10:10"), + UnicodeString("2007-10-10 10:10:10"), + UnicodeString("2007-10-10 22:10:10") + }; + // to create 2 Date Intervals Calendar *cal1 = Calendar::createInstance(status); - cal1->set(2007,9,10,10,10,10); - Calendar *cal2 = Calendar::createInstance(status); - cal2->set(2007,10,10,10,10,10); - Calendar *cal3 = Calendar::createInstance(status); - cal3->set(2007,10,10,22,10,10); - DateInterval* dtitvsample[] = { - new DateInterval(cal1->getTime(status),cal2->getTime(status)), + cal1->set(2007,9,10,10,10,10); + Calendar *cal2 = Calendar::createInstance(status); + cal2->set(2007,10,10,10,10,10); + Calendar *cal3 = Calendar::createInstance(status); + cal3->set(2007,10,10,22,10,10); + DateInterval* dtitvsample[] = { + new DateInterval(cal1->getTime(status),cal2->getTime(status)), new DateInterval(cal2->getTime(status),cal3->getTime(status)) - }; - UnicodeString skeletons[] = { + }; + UnicodeString skeletons[] = { UnicodeString("yMMMd"), UnicodeString("Hm"), - 0, + 0, }; - u_printf("%-10s%-22s%-22s%-45s%-35s\n", "Skeleton", "from","to", "Date Interval in en_US","Date Interval in Ja"); - // Create an empty DateIntervalInfo object + u_printf("%-10s%-22s%-22s%-45s%-35s\n", "Skeleton", "from","to", "Date Interval in en_US","Date Interval in Ja"); + // Create an empty DateIntervalInfo object DateIntervalInfo dtitvinf = DateIntervalInfo(status); - // Set Date Time internal pattern for MONTH, HOUR_OF_DAY + // Set Date Time internal pattern for MONTH, HOUR_OF_DAY dtitvinf.setIntervalPattern("yMMMd", UCAL_MONTH, "y 'Diff' MMM d --- MMM d",status); dtitvinf.setIntervalPattern("Hm", UCAL_HOUR_OF_DAY, "yyyy MMM d HH:mm ~ HH:mm",status); - // Set fallback interval pattern + // Set fallback interval pattern dtitvinf.setFallbackIntervalPattern("{0} ~~~ {1}",status); - // Get the DateIntervalFormat with the custom pattern + // Get the DateIntervalFormat with the custom pattern UnicodeString formatEn,formatJa; - FieldPosition pos=0; - for (int i=0;i<2;i++){ - for (int j=0;skeletons[j]!=NULL;j++) { - u_fprintf(out,"%-10S%-22S%-22S", skeletons[i].getTerminatedBuffer(),data[j].getTerminatedBuffer(), data[j+1].getTerminatedBuffer()); + FieldPosition pos=0; + for (int i=0;i<2;i++){ + for (int j=0;!skeletons[j].isEmpty();j++) { + u_fprintf(out,"%-10S%-22S%-22S", skeletons[i].getTerminatedBuffer(),data[j].getTerminatedBuffer(), data[j+1].getTerminatedBuffer()); DateIntervalFormat* dtitvfmtEn = DateIntervalFormat::createInstance(skeletons[i],Locale::getEnglish(),dtitvinf,status); DateIntervalFormat* dtitvfmtJa = DateIntervalFormat::createInstance(skeletons[i],Locale::getJapanese(),dtitvinf,status); - formatEn.remove(); - formatJa.remove(); - dtitvfmtEn->format(dtitvsample[j],formatEn,pos,status); - dtitvfmtJa->format(dtitvsample[j],formatJa,pos,status); - u_fprintf(out,"%-45S%-35S\n", formatEn.getTerminatedBuffer(),formatJa.getTerminatedBuffer()); + formatEn.remove(); + formatJa.remove(); + dtitvfmtEn->format(dtitvsample[j],formatEn,pos,status); + dtitvfmtJa->format(dtitvsample[j],formatJa,pos,status); + u_fprintf(out,"%-45S%-35S\n", formatEn.getTerminatedBuffer(),formatJa.getTerminatedBuffer()); } } - u_fclose(out); - //! [dtitvfmtCustomized] + u_fclose(out); + //! [dtitvfmtCustomized] } int main (int argc, char* argv[]) { - dtitvfmtPreDefined(); - dtitvfmtCustomized(); - return 0; + dtitvfmtPreDefined(); + dtitvfmtCustomized(); + return 0; } diff --git a/icu4c/source/samples/dtptngsample/Makefile b/icu4c/source/samples/dtptngsample/Makefile new file mode 100644 index 00000000000..95393190014 --- /dev/null +++ b/icu4c/source/samples/dtptngsample/Makefile @@ -0,0 +1,27 @@ +# Copyright (C) 2016 and later: Unicode, Inc. and others. +# License & terms of use: http://www.unicode.org/copyright.html +# +# Copyright (c) 2000-2006 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=dtptngsample + +# All object files (C or C++) +OBJECTS=dtptngsample.o + +#### rules +# Load in standard makefile definitions +include ../defs.mk + +LDFLAGS += $(LDFLAGS_USTDIO) + +# the actual rules (this is a simple sample) +include ../rules.mk diff --git a/icu4c/source/samples/dtptngsample/dtptngsample.cpp b/icu4c/source/samples/dtptngsample/dtptngsample.cpp index 58d394d57df..1c39f9794f9 100644 --- a/icu4c/source/samples/dtptngsample/dtptngsample.cpp +++ b/icu4c/source/samples/dtptngsample/dtptngsample.cpp @@ -1,7 +1,7 @@ // © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /******************************************************************** - * COPYRIGHT: + * COPYRIGHT: * Copyright (c) 2008-2014, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ @@ -16,151 +16,151 @@ using namespace std; using namespace icu; static void getBestPatternExample() { - - u_printf("========================================================================\n"); - u_printf(" getBestPatternExample()\n"); + + u_printf("========================================================================\n"); + u_printf(" getBestPatternExample()\n"); u_printf("\n"); u_printf(" Use DateTimePatternGenerator to create customized date/time pattern:\n"); u_printf(" yQQQQ,yMMMM, MMMMd, hhmm, jjmm per locale\n"); u_printf("========================================================================\n"); - //! [getBestPatternExample] - UnicodeString skeletons [] = { - UnicodeString("yQQQQ"), // year + full name of quarter, i.e., 4th quarter 1999 + //! [getBestPatternExample] + UnicodeString skeletons [] = { + UnicodeString("yQQQQ"), // year + full name of quarter, i.e., 4th quarter 1999 UnicodeString("yMMMM"), // year + full name of month, i.e., October 1999 UnicodeString("MMMMd"), // full name of month + day of the month, i.e., October 25 UnicodeString("hhmm"), // 12-hour-cycle format, i.e., 1:32 PM UnicodeString("jjmm"), // preferred hour format for the given locale, i.e., 24-hour-cycle format for fr_FR - 0, - }; + 0, + }; - Locale locales[] = { - Locale ("en_US"), - Locale ("fr_FR"), - Locale ("zh_CN"), - }; - - const char* filename = "sample.txt"; - /* open a UTF-8 file for writing */ - UFILE* f = u_fopen(filename, "w", NULL,"UTF-8"); - UnicodeString dateReturned; - UErrorCode status =U_ZERO_ERROR; - Calendar *cal = Calendar::createInstance(status); - cal->set (1999,9,13,23,58,59); - UDate date = cal->getTime(status); - u_fprintf(f, "%-20S%-20S%-20S%-20S\n", UnicodeString("Skeleton").getTerminatedBuffer(),UnicodeString("en_US").getTerminatedBuffer(),UnicodeString("fr_FR").getTerminatedBuffer(),UnicodeString("zh_CN").getTerminatedBuffer()); - for (int i=0;skeletons[i]!=NULL;i++) { - u_fprintf(f, "%-20S",skeletons[i].getTerminatedBuffer()); - for (int j=0;jgetBestPattern(skeletons[i],status); - // Constructs a SimpleDateFormat with the best pattern generated above and the given locale - SimpleDateFormat *sdf = new SimpleDateFormat(pattern,locales[j],status); - dateReturned.remove(); - // Get the format of the given date - sdf->format(date,dateReturned,status); - /* write Unicode string to file */ - u_fprintf(f, "%-20S", dateReturned.getTerminatedBuffer()); - delete dtfg; - delete sdf; - } - u_fprintf(f,"\n"); - } - /* close the file resource */ - u_fclose(f); - delete cal; - //! [getBestPatternExample] + Locale locales[] = { + Locale ("en_US"), + Locale ("fr_FR"), + Locale ("zh_CN"), + }; + + const char* filename = "sample.txt"; + /* open a UTF-8 file for writing */ + UFILE* f = u_fopen(filename, "w", NULL,"UTF-8"); + UnicodeString dateReturned; + UErrorCode status =U_ZERO_ERROR; + Calendar *cal = Calendar::createInstance(status); + cal->set (1999,9,13,23,58,59); + UDate date = cal->getTime(status); + u_fprintf(f, "%-20S%-20S%-20S%-20S\n", UnicodeString("Skeleton").getTerminatedBuffer(),UnicodeString("en_US").getTerminatedBuffer(),UnicodeString("fr_FR").getTerminatedBuffer(),UnicodeString("zh_CN").getTerminatedBuffer()); + for (int i=0;!skeletons[i].isEmpty();i++) { + u_fprintf(f, "%-20S",skeletons[i].getTerminatedBuffer()); + for (int j=0;jgetBestPattern(skeletons[i],status); + // Constructs a SimpleDateFormat with the best pattern generated above and the given locale + SimpleDateFormat *sdf = new SimpleDateFormat(pattern,locales[j],status); + dateReturned.remove(); + // Get the format of the given date + sdf->format(date,dateReturned,status); + /* write Unicode string to file */ + u_fprintf(f, "%-20S", dateReturned.getTerminatedBuffer()); + delete dtfg; + delete sdf; + } + u_fprintf(f,"\n"); + } + /* close the file resource */ + u_fclose(f); + delete cal; + //! [getBestPatternExample] } static void addPatternExample() { - - u_printf("========================================================================\n"); + + u_printf("========================================================================\n"); u_printf(" addPatternExample()\n"); - u_printf("\n"); + u_printf("\n"); u_printf(" Use addPattern API to add new '. von' to existing pattern\n"); u_printf("========================================================================\n"); - //! [addPatternExample] - UErrorCode status =U_ZERO_ERROR; - UnicodeString conflictingPattern,dateReturned, pattern; - Locale locale=Locale::getFrance(); - Calendar *cal = Calendar::createInstance(status); - cal->set (1999,9,13,23,58,59); - UDate date = cal->getTime(status); + //! [addPatternExample] + UErrorCode status =U_ZERO_ERROR; + UnicodeString conflictingPattern,dateReturned, pattern; + Locale locale=Locale::getFrance(); + Calendar *cal = Calendar::createInstance(status); + cal->set (1999,9,13,23,58,59); + UDate date = cal->getTime(status); // Create an DateTimePatternGenerator instance for the given locale - DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locale,status); - SimpleDateFormat *sdf = new SimpleDateFormat(dtfg->getBestPattern("MMMMddHmm",status),locale,status); + DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locale,status); + SimpleDateFormat *sdf = new SimpleDateFormat(dtfg->getBestPattern("MMMMddHmm",status),locale,status); // Add '. von' to the existing pattern dtfg->addPattern("dd'. von' MMMM", true, conflictingPattern,status); // Apply the new pattern sdf->applyPattern(dtfg->getBestPattern("MMMMddHmm",status)); - dateReturned = sdf->format(date, dateReturned, status); - pattern =sdf->toPattern(pattern); - u_printf("%s\n", "New Pattern for FRENCH: "); - u_printf("%S\n", pattern.getTerminatedBuffer()); - u_printf("%s\n", "Date Time in new Pattern: "); - u_printf("%S\n", dateReturned.getTerminatedBuffer()); - delete dtfg; - delete sdf; - delete cal; + dateReturned = sdf->format(date, dateReturned, status); + pattern =sdf->toPattern(pattern); + u_printf("%s\n", "New Pattern for FRENCH: "); + u_printf("%S\n", pattern.getTerminatedBuffer()); + u_printf("%s\n", "Date Time in new Pattern: "); + u_printf("%S\n", dateReturned.getTerminatedBuffer()); + delete dtfg; + delete sdf; + delete cal; - //! [addPatternExample] + //! [addPatternExample] /* output of the sample code: ************************************************************************************************ New Pattern for FRENCH: dd. 'von' MMMM HH:mm Date Time in new Pattern: 13. von octobre 23:58 - + *************************************************************************************************/ - } + } static void replaceFieldTypesExample() { - // Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv' + // Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv' u_printf("========================================================================\n"); u_printf(" replaceFieldTypeExample()\n"); u_printf("\n"); u_printf(" Use replaceFieldTypes API to replace zone 'zzzz' with 'vvvv'\n"); u_printf("========================================================================\n"); - //! [replaceFieldTypesExample] - UFILE *out = u_finit(stdout, NULL, "UTF-8"); - UErrorCode status =U_ZERO_ERROR; - UnicodeString pattern,dateReturned; - Locale locale =Locale::getFrance(); - Calendar *cal = Calendar::createInstance(status); - cal->set (1999,9,13,23,58,59); - UDate date = cal->getTime(status); - TimeZone *zone = TimeZone::createTimeZone(UnicodeString("Europe/Paris")); - DateTimePatternGenerator *dtfg = DateTimePatternGenerator::createInstance(locale,status); - SimpleDateFormat *sdf = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale,status); - sdf->setTimeZone(*zone); - pattern = sdf->toPattern(pattern); - u_fprintf(out, "%S\n", UnicodeString("Pattern before replacement:").getTerminatedBuffer()); - u_fprintf(out, "%S\n", pattern.getTerminatedBuffer()); - dateReturned.remove(); - dateReturned = sdf->format(date, dateReturned, status); - u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer()); - u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer()); + //! [replaceFieldTypesExample] + UFILE *out = u_finit(stdout, NULL, "UTF-8"); + UErrorCode status =U_ZERO_ERROR; + UnicodeString pattern,dateReturned; + Locale locale =Locale::getFrance(); + Calendar *cal = Calendar::createInstance(status); + cal->set (1999,9,13,23,58,59); + UDate date = cal->getTime(status); + TimeZone *zone = TimeZone::createTimeZone(UnicodeString("Europe/Paris")); + DateTimePatternGenerator *dtfg = DateTimePatternGenerator::createInstance(locale,status); + SimpleDateFormat *sdf = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale,status); + sdf->setTimeZone(*zone); + pattern = sdf->toPattern(pattern); + u_fprintf(out, "%S\n", UnicodeString("Pattern before replacement:").getTerminatedBuffer()); + u_fprintf(out, "%S\n", pattern.getTerminatedBuffer()); + dateReturned.remove(); + dateReturned = sdf->format(date, dateReturned, status); + u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer()); + u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer()); // Replace zone "zzzz" in the pattern with "vvvv" - UnicodeString newPattern = dtfg->replaceFieldTypes(pattern, "vvvv", status); - // Apply the new pattern - sdf->applyPattern(newPattern); - dateReturned.remove(); - dateReturned = sdf->format(date, dateReturned, status); - u_fprintf(out, "%S\n", UnicodeString("Pattern after replacement:").getTerminatedBuffer()); - u_fprintf(out, "%S\n", newPattern.getTerminatedBuffer()); - u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer()); - u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer()); - delete sdf; - delete dtfg; - delete zone; - delete cal; - u_fclose(out); - //! [replaceFieldTypesExample] + UnicodeString newPattern = dtfg->replaceFieldTypes(pattern, "vvvv", status); + // Apply the new pattern + sdf->applyPattern(newPattern); + dateReturned.remove(); + dateReturned = sdf->format(date, dateReturned, status); + u_fprintf(out, "%S\n", UnicodeString("Pattern after replacement:").getTerminatedBuffer()); + u_fprintf(out, "%S\n", newPattern.getTerminatedBuffer()); + u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer()); + u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer()); + delete sdf; + delete dtfg; + delete zone; + delete cal; + u_fclose(out); + //! [replaceFieldTypesExample] } int main (int argc, char* argv[]) { - getBestPatternExample(); - addPatternExample(); - replaceFieldTypesExample(); - return 0; + getBestPatternExample(); + addPatternExample(); + replaceFieldTypesExample(); + return 0; } diff --git a/icu4c/source/samples/plurfmtsample/Makefile b/icu4c/source/samples/plurfmtsample/Makefile new file mode 100644 index 00000000000..ef7d7d4ace8 --- /dev/null +++ b/icu4c/source/samples/plurfmtsample/Makefile @@ -0,0 +1,27 @@ +# Copyright (C) 2016 and later: Unicode, Inc. and others. +# License & terms of use: http://www.unicode.org/copyright.html +# +# Copyright (c) 2000-2006 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=plurfmtsample + +# All object files (C or C++) +OBJECTS=plurfmtsample.o + +#### rules +# Load in standard makefile definitions +include ../defs.mk + +LDFLAGS += $(LDFLAGS_USTDIO) + +# the actual rules (this is a simple sample) +include ../rules.mk diff --git a/icu4c/source/samples/plurfmtsample/plurfmtsample.cpp b/icu4c/source/samples/plurfmtsample/plurfmtsample.cpp index f910c4f56e1..b03e63541d4 100644 --- a/icu4c/source/samples/plurfmtsample/plurfmtsample.cpp +++ b/icu4c/source/samples/plurfmtsample/plurfmtsample.cpp @@ -19,17 +19,17 @@ using namespace std; using namespace icu; static void PluralFormatExample() { - - u_printf("=============================================================================\n"); - u_printf(" PluralFormatExample()\n"); + + u_printf("=============================================================================\n"); + u_printf(" PluralFormatExample()\n"); u_printf("\n"); u_printf(" Use PluralFormat and Messageformat to get Plural Form for languages below:\n"); u_printf(" English, Slovenian\n"); u_printf("=============================================================================\n"); - - //! [PluralFormatExample] - UErrorCode status =U_ZERO_ERROR; - Locale locEn = Locale("en"); + + //! [PluralFormatExample] + UErrorCode status =U_ZERO_ERROR; + Locale locEn = Locale("en"); Locale locSl = Locale("sl"); UnicodeString patEn = UnicodeString("one{dog} other{dogs}"); // English 'dog' @@ -42,19 +42,19 @@ static void PluralFormatExample() { MessageFormat* msgfmtEn = new MessageFormat("{0,number} {1}", locEn,status); MessageFormat* msgfmtSl = new MessageFormat("{0,number} {1}", locSl,status); - int numbers[] = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102}; - u_printf("Output by using PluralFormat and MessageFormat API\n"); + int numbers[] = {0, 1, 2, 3, 4, 5, 10, 100, 101, 102}; + u_printf("Output by using PluralFormat and MessageFormat API\n"); u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian"); - + // Use MessageFormat.format () to format the objects and append to the given StringBuffer for (int i=0;iformat(argEn,2,msgEn,fpos,status); - msgfmtSl->format(argSl,2,msgSl,fpos,status); - u_printf("%-16d%-16S%-16S\n", numbers[i], msgEn.getTerminatedBuffer(),msgSl.getTerminatedBuffer()); + UnicodeString msgEn,msgSl; + FieldPosition fpos = 0; + Formattable argEn[]={Formattable(numbers[i]), Formattable(plfmtEn.format(numbers[i],status))}; + Formattable argSl[]={Formattable(numbers[i]), Formattable(plfmtSl.format(numbers[i],status))}; + msgfmtEn->format(argEn,2,msgEn,fpos,status); + msgfmtSl->format(argSl,2,msgSl,fpos,status); + u_printf("%-16d%-16S%-16S\n", numbers[i], msgEn.getTerminatedBuffer(),msgSl.getTerminatedBuffer()); } u_printf("\n"); @@ -62,27 +62,27 @@ static void PluralFormatExample() { // Equivalent code with message format pattern UnicodeString msgPatEn = "{0,plural, one{# dog} other{# dogs}}"; UnicodeString msgPatSl = "{0,plural, one{# pes} two{# psa} few{# psi} other{# psov}}"; - - MessageFormat* altMsgfmtEn = new MessageFormat(msgPatEn, locEn,status); + + MessageFormat* altMsgfmtEn = new MessageFormat(msgPatEn, locEn,status); MessageFormat* altMsgfmtSl = new MessageFormat(msgPatSl, locSl,status); u_printf("Same Output by using MessageFormat API only\n"); u_printf("%-16s%-16s%-16s\n","Number", "English","Slovenian"); for (int i=0;iformat(arg, 1, msgEn, fPos, status); + Formattable arg[] = {numbers[i]}; + FieldPosition fPos =0; + altMsgfmtEn->format(arg, 1, msgEn, fPos, status); altMsgfmtSl->format(arg, 1, msgSl, fPos,status); u_printf("%-16d%-16S%-16S\n", numbers[i], msgEn.getTerminatedBuffer(), msgSl.getTerminatedBuffer()); } - delete msgfmtEn; - delete msgfmtSl; - delete altMsgfmtEn; - delete altMsgfmtSl; - //! [PluralFormatExample] + delete msgfmtEn; + delete msgfmtSl; + delete altMsgfmtEn; + delete altMsgfmtSl; + //! [PluralFormatExample] - /* output of the sample code: + /* output of the sample code: ******************************************************************** Number English Slovenian 0 0 dogs 0 psov @@ -100,6 +100,6 @@ static void PluralFormatExample() { } int main (int argc, char* argv[]) { - PluralFormatExample(); - return 0; + PluralFormatExample(); + return 0; } diff --git a/icu4c/source/samples/readme.txt b/icu4c/source/samples/readme.txt index 0ef21771da8..1dfef216d96 100644 --- a/icu4c/source/samples/readme.txt +++ b/icu4c/source/samples/readme.txt @@ -4,36 +4,46 @@ ## Copyright (c) 2002-2010, International Business Machines Corporation ## and others. All Rights Reserved. -This directory contains sample code -Below is a short description of the contents of this directory. +This directory contains sample code using ICU4C routines. Below is a +short description of the contents of this directory. -break - demonstrates how to use BreakIterators in C and C++. +break - demonstrates how to use BreakIterators in C and C++. -cal - prints out a calendar. +cal - prints out a calendar. case - demonstrates how to do Unicode case conversion in C and C++. +coll - shows how collation compares strings + csdet - demonstrates using ICU's CharSet Detection API -date - prints out the current date, localized. +date - prints out the current date, localized. -datefmt - an exercise using the date formatting API +datecal - demonstrates how a calendar object provides information + +datefmt - an exercise using the date formatting API -layout - demonstrates the ICU LayoutEngine +dtitvfmtsample - shows how date interval format uses predefined skeletons -legacy - demonstrates using two versions of ICU in one application +dtptngsample - uses DateTimePatternGenerator to create customized date/time pattern + +layout - demonstrates the ICU LayoutEngine (obsolete) + +legacy - demonstrates using two versions of ICU in one application (obsolete) msgfmt - demonstrates the use of the Message Format numfmt - demonstrates the use of the number format +plurfmtsample - uses PluralFormat and Messageformat to get Plural Form + props - demonstrates the use of Unicode properties strsrch - demonstrates how to search for patterns in Unicode text using the usearch interface. translit - demonstrates the use of ICU transliteration -uciter8.c - demonstrates how to leniently read 8-bit Unicode text. +uciter8 - demonstrates how to leniently read 8-bit Unicode text. ucnv - demonstrates the use of ICU codepage conversion @@ -41,7 +51,7 @@ udata - demonstrates the use of ICU low level data routines (reader/writer in ufortune - demonstrates packaging and use of resources in an application -ugrep - demonstrates ICU Regular Expressions. +ugrep - demonstrates ICU Regular Expressions. uresb - demonstrates building and loading resource bundles @@ -52,12 +62,11 @@ ustring - demonstrates ICU string manipulation functions * Where can I find more sample code? - The "uconv" utility is a full-featured command line application. - It is normally built with ICU, and is located in icu/source/extra/uconv + It is normally built with ICU, and is located in icu/source/extra/uconv - - The "icuapps" CVS module contains other applications and libraries not - included with ICU. You can check it out from the CVS command line - by using for example, "cvs co icuapps" instead of "cvs co icu", - or through WebCVS at http://dev.icu-project.org/cgi-bin/viewcvs.cgi/icuapps/ + - The "icu-demos" contains other applications and libraries not + included with ICU. You can check it out from https://github.com/unicode-org/icu-demos + using github clone. See the README file for additional information. == * How do I build the samples? @@ -76,7 +85,7 @@ ustring - demonstrates ICU string manipulation functions - be sure 'icu-config' is accessible from the PATH - type 'make all-samples' from this directory (other targets: clean-samples, check-samples) - Note: 'make all-samples' won't work correctly in out of source builds. + Note: 'make all-samples' won't work correctly in out of source builds. - - legacy and layout are not included in these lists, - please see their individual readmes. + Note that legacy and layout are obsolete samples that may not compile or run without + adjustments to their makefiles. diff --git a/icu4c/source/samples/strsrch/strsrch.cpp b/icu4c/source/samples/strsrch/strsrch.cpp index c5fff0c40ba..c275fef66f9 100644 --- a/icu4c/source/samples/strsrch/strsrch.cpp +++ b/icu4c/source/samples/strsrch/strsrch.cpp @@ -47,7 +47,7 @@ const char gHelpString[] = * These global variables are set according to the options specified * on the command line by the user. */ -char * opt_locale = "en_US"; +char const *opt_locale = "en_US"; char * opt_rules = 0; UBool opt_help = false; UBool opt_norm = false; @@ -59,8 +59,8 @@ UBool opt_case = false; UBool opt_overlap = false; UBool opt_canonical = false; int opt_level = 0; -char * opt_source = "International Components for Unicode"; -char * opt_pattern = "Unicode"; +char const *opt_source = "International Components for Unicode"; +char const *opt_pattern = "Unicode"; UCollator * collator = 0; UStringSearch * search = 0; UChar rules[100];