Initial commits for collation and string search sample code

X-SVN-Rev: 10175
This commit is contained in:
Syn Wee Quek 2002-11-06 23:09:39 +00:00
parent 880efe6d02
commit c56e1ea2a9
10 changed files with 1093 additions and 0 deletions

View file

@ -0,0 +1,80 @@
## Makefile.in for ICU - samples/coll
## Copyright (c) 1999-2001, International Business Machines Corporation and
## others. All Rights Reserved.
## Source directory information
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = ../..
include $(top_builddir)/icudefs.mk
## Platform-specific setup
include @platform_make_fragment@
## Build directory information
subdir = samples/coll
## Extra files to remove for 'make clean'
CLEANFILES = *~ $(DEPS)
## Target information
TARGET = coll
ENABLE_STATIC = @ENABLE_STATIC@
LINK = $(LINK.cc)
CPPFLAGS += -I$(top_builddir)/common -I$(top_srcdir)/common -I$(top_srcdir)/i18n
LIBS = $(LIBICUI18N) $(LIBICUUC) @LIBS@ @LIB_M@
OBJECTS = coll.o
DEPS = $(OBJECTS:.o=.d)
## List of phony targets
.PHONY : all all-local install install-local clean clean-local \
distclean distclean-local dist dist-local check check-local
## Clear suffix list
.SUFFIXES :
## List of standard targets
all: all-local
install: install-local
clean: clean-local
distclean : distclean-local
dist: dist-local
check: all check-local
all-local: $(TARGET)
install-local: all-local
dist-local:
clean-local:
test -z "$(CLEANFILES)" || $(RMV) $(CLEANFILES)
$(RMV) $(OBJECTS) $(TARGET)
distclean-local: clean-local
$(RMV) Makefile
check-local:
-$(INVOKE) ./$(TARGET)
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
cd $(top_builddir) \
&& CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
$(TARGET) : $(OBJECTS)
$(LINK) -o $@ $^ $(LIBS)
ifeq (,$(MAKECMDGOALS))
-include $(DEPS)
else
ifneq ($(patsubst %clean,,$(MAKECMDGOALS)),)
-include $(DEPS)
endif
endif

View file

@ -0,0 +1,265 @@
/********************************************************************
* COPYRIGHT:
* Copyright (C) 2002 IBM, Inc. All Rights Reserved.
*
********************************************************************/
/**
* This program demos string collation
*/
const char gHelpString[] =
"usage: coll [options*] -source source_string -target target_string\n"
"-help Display this message.\n"
"-locale name ICU locale to use. Default is en_US\n"
"-rules rule Collation rules file (overrides locale)\n"
"-french French accent ordering\n"
"-norm Normalizing mode on\n"
"-shifted Shifted mode\n"
"-lower Lower case first\n"
"-upper Upper case first\n"
"-case Enable separate case level\n"
"-level n Sort level, 1 to 5, for Primary, Secndary, Tertiary, Quaternary, Identical\n"
"-source string Source string for comparison\n"
"-target string Target string for comparison\n"
"Example coll -rules \\u0026b\\u003ca -source a -target b\n"
"The format \\uXXXX is supported for the rules and comparison strings\n"
;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unicode/utypes.h>
#include <unicode/ucol.h>
#include <unicode/ustring.h>
/**
* Command line option variables
* 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;
UBool opt_help = FALSE;
UBool opt_norm = FALSE;
UBool opt_french = FALSE;
UBool opt_shifted = FALSE;
UBool opt_lower = FALSE;
UBool opt_upper = FALSE;
UBool opt_case = FALSE;
int opt_level = 0;
char * opt_source = 0;
char * opt_target = 0;
UCollator * collator = 0;
/**
* Definitions for the command line options
*/
struct OptSpec {
const char *name;
enum {FLAG, NUM, STRING} type;
void *pVar;
};
OptSpec opts[] = {
{"-locale", OptSpec::STRING, &opt_locale},
{"-rules", OptSpec::STRING, &opt_rules},
{"-source", OptSpec::STRING, &opt_source},
{"-target", OptSpec::STRING, &opt_target},
{"-norm", OptSpec::FLAG, &opt_norm},
{"-french", OptSpec::FLAG, &opt_french},
{"-shifted", OptSpec::FLAG, &opt_shifted},
{"-lower", OptSpec::FLAG, &opt_lower},
{"-upper", OptSpec::FLAG, &opt_upper},
{"-case", OptSpec::FLAG, &opt_case},
{"-level", OptSpec::NUM, &opt_level},
{"-help", OptSpec::FLAG, &opt_help},
{"-?", OptSpec::FLAG, &opt_help},
{0, OptSpec::FLAG, 0}
};
/**
* processOptions() Function to read the command line options.
*/
UBool processOptions(int argc, const char **argv, OptSpec opts[])
{
for (int argNum = 1; argNum < argc; argNum ++) {
const char *pArgName = argv[argNum];
for (OptSpec *pOpt = opts; pOpt->name != 0; pOpt ++) {
if (strcmp(pOpt->name, pArgName) == 0) {
switch (pOpt->type) {
case OptSpec::FLAG:
*(UBool *)(pOpt->pVar) = TRUE;
break;
case OptSpec::STRING:
argNum ++;
if (argNum >= argc) {
fprintf(stderr, "value expected for \"%s\" option.\n",
pOpt->name);
return FALSE;
}
*(const char **)(pOpt->pVar) = argv[argNum];
break;
case OptSpec::NUM:
argNum ++;
if (argNum >= argc) {
fprintf(stderr, "value expected for \"%s\" option.\n",
pOpt->name);
return FALSE;
}
char *endp;
int i = strtol(argv[argNum], &endp, 0);
if (endp == argv[argNum]) {
fprintf(stderr,
"integer value expected for \"%s\" option.\n",
pOpt->name);
return FALSE;
}
*(int *)(pOpt->pVar) = i;
}
break;
}
}
if (pOpt->name == 0)
{
fprintf(stderr, "Unrecognized option \"%s\"\n", pArgName);
return FALSE;
}
}
return TRUE;
}
/**
* ICU string comparison
*/
int strcmp()
{
UChar source[100];
UChar target[100];
u_unescape(opt_source, source, 100);
u_unescape(opt_target, target, 100);
UCollationResult result = ucol_strcoll(collator, source, -1, target, -1);
if (result == UCOL_LESS) {
return -1;
}
else if (result == UCOL_GREATER) {
return 1;
}
return 0;
}
/**
* Creates a collator
*/
UBool processCollator()
{
// Set up an ICU collator
UErrorCode status = U_ZERO_ERROR;
UChar rules[100];
if (opt_rules != 0) {
u_unescape(opt_rules, rules, 100);
collator = ucol_openRules(rules, -1, UCOL_OFF, UCOL_TERTIARY,
NULL, &status);
}
else {
collator = ucol_open(opt_locale, &status);
}
if (U_FAILURE(status)) {
fprintf(stderr, "Collator creation failed.: %d\n", status);
return FALSE;
}
if (status == U_USING_DEFAULT_WARNING) {
fprintf(stderr, "Warning, U_USING_DEFAULT_WARNING for %s\n",
opt_locale);
}
if (status == U_USING_FALLBACK_WARNING) {
fprintf(stderr, "Warning, U_USING_FALLBACK_ERROR for %s\n",
opt_locale);
}
if (opt_norm) {
ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
}
if (opt_french) {
ucol_setAttribute(collator, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
}
if (opt_lower) {
ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_LOWER_FIRST,
&status);
}
if (opt_upper) {
ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_UPPER_FIRST,
&status);
}
if (opt_case) {
ucol_setAttribute(collator, UCOL_CASE_LEVEL, UCOL_ON, &status);
}
if (opt_shifted) {
ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED,
&status);
}
if (opt_level != 0) {
switch (opt_level) {
case 1:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
break;
case 2:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_SECONDARY,
&status);
break;
case 3:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_TERTIARY, &status);
break;
case 4:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_QUATERNARY,
&status);
break;
case 5:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_IDENTICAL,
&status);
break;
default:
fprintf(stderr, "-level param must be between 1 and 5\n");
return FALSE;
}
}
if (U_FAILURE(status)) {
fprintf(stderr, "Collator attribute setting failed.: %d\n", status);
return FALSE;
}
return TRUE;
}
/**
* Main -- process command line, read in and pre-process the test file,
* call other functions to do the actual tests.
*/
int main(int argc, const char** argv)
{
if (processOptions(argc, argv, opts) != TRUE || opt_help) {
printf(gHelpString);
return -1;
}
if (processCollator() != TRUE) {
fprintf(stderr, "Error creating collator for comparison\n");
return -1;
}
fprintf(stdout, "Comparing source=%s and target=%s\n", opt_source,
opt_target);
int result = strcmp();
if (result == 0) {
fprintf(stdout, "source is equals to target\n");
}
else if (result < 0) {
fprintf(stdout, "source is less than target\n");
}
else {
fprintf(stdout, "source is greater than target\n");
}
ucol_close(collator);
return 0;
}

View file

@ -0,0 +1,101 @@
# Microsoft Developer Studio Project File - Name="coll" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=coll - 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 "coll.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 "coll.mak" CFG="coll - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "coll - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "coll - 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)" == "coll - 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 Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MT /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 icuind.lib icuucd.lib /nologo /subsystem:console /machine:I386 /libpath:"..\..\..\lib"
!ELSEIF "$(CFG)" == "coll - 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 /MTd /W3 /Gm /GX /ZI /Od /I "..\..\..\include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FR /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 icuind.lib icuucd.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"..\..\..\lib"
!ENDIF
# Begin Target
# Name "coll - Win32 Release"
# Name "coll - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\coll.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

View file

@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "coll"=.\coll.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

View file

@ -0,0 +1,54 @@
coll: a sample program which compares 2 strings with a user-defined collator.
This sample demonstrates
Creating a user-defined collator
Comparing 2 string using the collator created
Files:
coll.c Main source file
coll.dsw Windows MSVC workspace. Double-click this to get started.
coll.dsp Windows MSVC project file
To Build coll on Windows
1. Install and build ICU
2. In MSVC, open the workspace file icu\samples\coll\coll.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 coll directory, e.g.
cd c:\icu\source\samples\coll\debug
4. Run it
coll [options*] -source source_string -target target_string
To Build on Unixes
1. Build ICU. coll is built automatically by default unless samples are turned off.
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
To Run on Unixes
cd <icu directory>/source/samples/coll
gmake check
-or-
export LD_LIBRARY_PATH=<icu install directory>/lib:.:$LD_LIBRARY_PATH
cal
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.

View file

@ -0,0 +1,80 @@
## Makefile.in for ICU - samples/strsrch
## Copyright (c) 1999-2001, International Business Machines Corporation and
## others. All Rights Reserved.
## Source directory information
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = ../..
include $(top_builddir)/icudefs.mk
## Platform-specific setup
include @platform_make_fragment@
## Build directory information
subdir = samples/strsrch
## Extra files to remove for 'make clean'
CLEANFILES = *~ $(DEPS)
## Target information
TARGET = strsrch
ENABLE_STATIC = @ENABLE_STATIC@
LINK = $(LINK.cc)
CPPFLAGS += -I$(top_builddir)/common -I$(top_srcdir)/common -I$(top_srcdir)/i18n
LIBS = $(LIBICUI18N) $(LIBICUUC) @LIBS@ @LIB_M@
OBJECTS = strsrch.o
DEPS = $(OBJECTS:.o=.d)
## List of phony targets
.PHONY : all all-local install install-local clean clean-local \
distclean distclean-local dist dist-local check check-local
## Clear suffix list
.SUFFIXES :
## List of standard targets
all: all-local
install: install-local
clean: clean-local
distclean : distclean-local
dist: dist-local
check: all check-local
all-local: $(TARGET)
install-local: all-local
dist-local:
clean-local:
test -z "$(CLEANFILES)" || $(RMV) $(CLEANFILES)
$(RMV) $(OBJECTS) $(TARGET)
distclean-local: clean-local
$(RMV) Makefile
check-local:
-$(INVOKE) ./$(TARGET)
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
cd $(top_builddir) \
&& CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
$(TARGET) : $(OBJECTS)
$(LINK) -o $@ $^ $(LIBS)
ifeq (,$(MAKECMDGOALS))
-include $(DEPS)
else
ifneq ($(patsubst %clean,,$(MAKECMDGOALS)),)
-include $(DEPS)
endif
endif

View file

@ -0,0 +1,54 @@
strsrch: a sample program which finds the occurrences of a pattern string in a source string, using user-defined collation rules.
This sample demonstrates
Creating a user-defined string search mechanism.
Finding all occurrences of a pattern string in a given source string.
Files:
strsrch.c Main source file
strsrch.dsw Windows MSVC workspace. Double-click this to get started.
strsrch.dsp Windows MSVC project file
To Build strsrch on Windows
1. Install and build ICU
2. In MSVC, open the workspace file icu\samples\strsrch\strsrch.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 strsrch directory, e.g.
cd c:\icu\source\samples\strsrch\debug
4. Run it
strsrch [options*] -source source_string -pattern pattern_string
To Build on Unixes
1. Build ICU. strsrch is built automatically by default unless samples are turned off.
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
To Run on Unixes
cd <icu directory>/source/samples/strsrch
gmake check
-or-
export LD_LIBRARY_PATH=<icu install directory>/lib:.:$LD_LIBRARY_PATH
cal
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.

View file

@ -0,0 +1,300 @@
/********************************************************************
* COPYRIGHT:
* Copyright (C) 2002 IBM, Inc. All Rights Reserved.
*
********************************************************************/
/**
* This program demos string collation
*/
const char gHelpString[] =
"usage: strsrch [options*] -source source_string -pattern pattern_string\n"
"-help Display this message.\n"
"-locale name ICU locale to use. Default is en_US\n"
"-rules rule Collation rules file (overrides locale)\n"
"-french French accent ordering\n"
"-norm Normalizing mode on\n"
"-shifted Shifted mode\n"
"-lower Lower case first\n"
"-upper Upper case first\n"
"-case Enable separate case level\n"
"-level n Sort level, 1 to 5, for Primary, Secndary, Tertiary, Quaternary, Identical\n"
"-source string Source string\n"
"-pattern string Pattern string to look for in source\n"
"-overlap Enable searching to be done on overlapping patterns\n"
"-canonical Enable searching to be done matching canonical equivalent patterns"
"Example strsrch -rules \\u0026b\\u003ca -source a\\u0020b\\u0020bc -pattern b\n"
"The format \\uXXXX is supported for the rules and comparison strings\n"
;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unicode/utypes.h>
#include <unicode/ucol.h>
#include <unicode/usearch.h>
#include <unicode/ustring.h>
/**
* Command line option variables
* 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;
UBool opt_help = FALSE;
UBool opt_norm = FALSE;
UBool opt_french = FALSE;
UBool opt_shifted = FALSE;
UBool opt_lower = FALSE;
UBool opt_upper = FALSE;
UBool opt_case = FALSE;
UBool opt_overlap = FALSE;
UBool opt_canonical = FALSE;
int opt_level = 0;
char * opt_source = 0;
char * opt_pattern = 0;
UCollator * collator = 0;
UStringSearch * search = 0;
UChar rules[100];
UChar source[100];
UChar pattern[100];
/**
* Definitions for the command line options
*/
struct OptSpec {
const char *name;
enum {FLAG, NUM, STRING} type;
void *pVar;
};
OptSpec opts[] = {
{"-locale", OptSpec::STRING, &opt_locale},
{"-rules", OptSpec::STRING, &opt_rules},
{"-source", OptSpec::STRING, &opt_source},
{"-pattern", OptSpec::STRING, &opt_pattern},
{"-norm", OptSpec::FLAG, &opt_norm},
{"-french", OptSpec::FLAG, &opt_french},
{"-shifted", OptSpec::FLAG, &opt_shifted},
{"-lower", OptSpec::FLAG, &opt_lower},
{"-upper", OptSpec::FLAG, &opt_upper},
{"-case", OptSpec::FLAG, &opt_case},
{"-level", OptSpec::NUM, &opt_level},
{"-overlap", OptSpec::FLAG, &opt_overlap},
{"-canonical", OptSpec::FLAG, &opt_canonical},
{"-help", OptSpec::FLAG, &opt_help},
{"-?", OptSpec::FLAG, &opt_help},
{0, OptSpec::FLAG, 0}
};
/**
* processOptions() Function to read the command line options.
*/
UBool processOptions(int argc, const char **argv, OptSpec opts[])
{
for (int argNum = 1; argNum < argc; argNum ++) {
const char *pArgName = argv[argNum];
for (OptSpec *pOpt = opts; pOpt->name != 0; pOpt ++) {
if (strcmp(pOpt->name, pArgName) == 0) {
switch (pOpt->type) {
case OptSpec::FLAG:
*(UBool *)(pOpt->pVar) = TRUE;
break;
case OptSpec::STRING:
argNum ++;
if (argNum >= argc) {
fprintf(stderr, "value expected for \"%s\" option.\n",
pOpt->name);
return FALSE;
}
*(const char **)(pOpt->pVar) = argv[argNum];
break;
case OptSpec::NUM:
argNum ++;
if (argNum >= argc) {
fprintf(stderr, "value expected for \"%s\" option.\n",
pOpt->name);
return FALSE;
}
char *endp;
int i = strtol(argv[argNum], &endp, 0);
if (endp == argv[argNum]) {
fprintf(stderr,
"integer value expected for \"%s\" option.\n",
pOpt->name);
return FALSE;
}
*(int *)(pOpt->pVar) = i;
}
break;
}
}
if (pOpt->name == 0)
{
fprintf(stderr, "Unrecognized option \"%s\"\n", pArgName);
return FALSE;
}
}
return TRUE;
}
/**
* Creates a collator
*/
UBool processCollator()
{
// Set up an ICU collator
UErrorCode status = U_ZERO_ERROR;
if (opt_rules != 0) {
u_unescape(opt_rules, rules, 100);
collator = ucol_openRules(rules, -1, UCOL_OFF, UCOL_TERTIARY,
NULL, &status);
}
else {
collator = ucol_open(opt_locale, &status);
}
if (U_FAILURE(status)) {
fprintf(stderr, "Collator creation failed.: %d\n", status);
return FALSE;
}
if (status == U_USING_DEFAULT_WARNING) {
fprintf(stderr, "Warning, U_USING_DEFAULT_WARNING for %s\n",
opt_locale);
}
if (status == U_USING_FALLBACK_WARNING) {
fprintf(stderr, "Warning, U_USING_FALLBACK_ERROR for %s\n",
opt_locale);
}
if (opt_norm) {
ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
}
if (opt_french) {
ucol_setAttribute(collator, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
}
if (opt_lower) {
ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_LOWER_FIRST,
&status);
}
if (opt_upper) {
ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_UPPER_FIRST,
&status);
}
if (opt_case) {
ucol_setAttribute(collator, UCOL_CASE_LEVEL, UCOL_ON, &status);
}
if (opt_shifted) {
ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED,
&status);
}
if (opt_level != 0) {
switch (opt_level) {
case 1:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
break;
case 2:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_SECONDARY,
&status);
break;
case 3:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_TERTIARY, &status);
break;
case 4:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_QUATERNARY,
&status);
break;
case 5:
ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_IDENTICAL,
&status);
break;
default:
fprintf(stderr, "-level param must be between 1 and 5\n");
return FALSE;
}
}
if (U_FAILURE(status)) {
fprintf(stderr, "Collator attribute setting failed.: %d\n", status);
return FALSE;
}
return TRUE;
}
/**
* Creates a string search
*/
UBool processStringSearch()
{
u_unescape(opt_source, source, 100);
u_unescape(opt_pattern, pattern, 100);
UErrorCode status = U_ZERO_ERROR;
search = usearch_openFromCollator(pattern, -1, source, -1, collator, NULL,
&status);
if (U_FAILURE(status)) {
return FALSE;
}
if (opt_overlap == TRUE) {
usearch_setAttribute(search, USEARCH_OVERLAP, USEARCH_ON, &status);
}
if (opt_canonical == TRUE) {
usearch_setAttribute(search, USEARCH_CANONICAL_MATCH, USEARCH_ON,
&status);
}
if (U_FAILURE(status)) {
fprintf(stderr, "Error setting search attributes\n");
return FALSE;
}
return TRUE;
}
UBool findPattern()
{
UErrorCode status = U_ZERO_ERROR;
int32_t offset = usearch_next(search, &status);
if (offset == USEARCH_DONE) {
fprintf(stdout, "Pattern not found in source\n");
}
while (offset != USEARCH_DONE) {
fprintf(stdout, "Pattern found at offset %d size %d\n", offset,
usearch_getMatchedLength(search));
offset = usearch_next(search, &status);
}
if (U_FAILURE(status)) {
fprintf(stderr, "Error in searching for pattern %d\n", status);
return FALSE;
}
fprintf(stdout, "End of search\n");
return TRUE;
}
/**
* Main -- process command line, read in and pre-process the test file,
* call other functions to do the actual tests.
*/
int main(int argc, const char** argv)
{
if (processOptions(argc, argv, opts) != TRUE || opt_help) {
printf(gHelpString);
return -1;
}
if (processCollator() != TRUE) {
fprintf(stderr, "Error creating collator\n");
return -1;
}
if (processStringSearch() != TRUE) {
fprintf(stderr, "Error creating string search\n");
return -1;
}
fprintf(stdout, "Finding pattern %s in source %s\n", opt_pattern,
opt_source);
findPattern();
ucol_close(collator);
usearch_close(search);
return 0;
}

View file

@ -0,0 +1,101 @@
# Microsoft Developer Studio Project File - Name="strsrch" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=strsrch - 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 "strsrch.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 "strsrch.mak" CFG="strsrch - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "strsrch - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "strsrch - 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)" == "strsrch - 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 Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MT /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 icuind.lib icuucd.lib /nologo /subsystem:console /machine:I386 /libpath:"..\..\..\lib"
!ELSEIF "$(CFG)" == "strsrch - 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 /MTd /W3 /Gm /GX /ZI /Od /I "..\..\..\include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FR /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 icuind.lib icuucd.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"..\..\..\lib"
!ENDIF
# Begin Target
# Name "strsrch - Win32 Release"
# Name "strsrch - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\strsrch.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

View file

@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "strsrch"=.\strsrch.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################