2002-07-09 06:16:35 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: uenumtst.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:2
|
|
|
|
*
|
|
|
|
* created on: 2002jul08
|
|
|
|
* created by: Vladimir Weinstein
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cintltst.h"
|
|
|
|
#include "uenumimp.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
|
|
|
|
static const char* test1[] = {
|
2002-07-16 22:29:23 +00:00
|
|
|
"first",
|
2002-07-09 06:16:35 +00:00
|
|
|
"second",
|
|
|
|
"third",
|
|
|
|
"fourth"
|
|
|
|
};
|
|
|
|
|
2002-07-09 18:19:16 +00:00
|
|
|
struct chArrayContext {
|
2002-07-16 22:29:23 +00:00
|
|
|
int32_t currIndex;
|
|
|
|
int32_t maxIndex;
|
|
|
|
char *currChar;
|
|
|
|
UChar *currUChar;
|
|
|
|
char **array;
|
2002-07-09 18:19:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct chArrayContext chArrayContext;
|
|
|
|
|
|
|
|
#define cont ((chArrayContext *)en->context)
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static void U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayClose(UEnumeration *en) {
|
2002-07-16 22:29:23 +00:00
|
|
|
if(cont->currUChar != NULL) {
|
|
|
|
uprv_free(cont->currUChar);
|
|
|
|
}
|
|
|
|
uprv_free(en);
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static int32_t U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayCount(UEnumeration *en, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
return cont->maxIndex;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static const UChar* U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayUNext(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
if(cont->currIndex >= cont->maxIndex) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cont->currUChar == NULL) {
|
|
|
|
cont->currUChar = (UChar *)uprv_malloc(1024*sizeof(UChar));
|
|
|
|
}
|
|
|
|
|
|
|
|
cont->currChar = (cont->array)[cont->currIndex];
|
|
|
|
*resultLength = uprv_strlen(cont->currChar);
|
|
|
|
u_charsToUChars(cont->currChar, cont->currUChar, *resultLength);
|
|
|
|
cont->currIndex++;
|
|
|
|
return cont->currUChar;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static const char* U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayNext(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
if(cont->currIndex >= cont->maxIndex) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cont->currChar = (cont->array)[cont->currIndex];
|
|
|
|
*resultLength = uprv_strlen(cont->currChar);
|
|
|
|
cont->currIndex++;
|
|
|
|
return cont->currChar;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static void U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayReset(UEnumeration *en, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
cont->currIndex = 0;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-09 18:19:16 +00:00
|
|
|
chArrayContext myCont = {
|
2002-07-16 22:29:23 +00:00
|
|
|
0, 0,
|
2002-07-09 06:16:35 +00:00
|
|
|
NULL, NULL,
|
2002-07-09 18:19:16 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
UEnumeration chEnum = {
|
|
|
|
&myCont,
|
2002-07-09 06:16:35 +00:00
|
|
|
chArrayClose,
|
|
|
|
chArrayCount,
|
|
|
|
chArrayUNext,
|
|
|
|
chArrayNext,
|
|
|
|
chArrayReset
|
|
|
|
};
|
|
|
|
|
|
|
|
static UEnumeration *getchArrayEnum(const char** source, int32_t size) {
|
2002-07-16 22:29:23 +00:00
|
|
|
UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration));
|
|
|
|
memcpy(en, &chEnum, sizeof(UEnumeration));
|
|
|
|
cont->array = (char **)source;
|
|
|
|
cont->maxIndex = size;
|
|
|
|
return en;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void EnumerationTest(void) {
|
2002-07-16 22:29:23 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t len = 0;
|
|
|
|
UEnumeration *en = getchArrayEnum(test1, sizeof(test1)/sizeof(test1[0]));
|
|
|
|
const char *string = NULL;
|
|
|
|
const UChar *uString = NULL;
|
|
|
|
while ((string = uenum_next(en, &len, &status))) {
|
|
|
|
log_verbose("read %s, length %i\n", string, len);
|
|
|
|
}
|
|
|
|
uenum_reset(en, &status);
|
|
|
|
while ((uString = uenum_unext(en, &len, &status))) {
|
|
|
|
log_verbose("read uchar of len %i\n", len);
|
|
|
|
}
|
|
|
|
|
|
|
|
uenum_close(en);
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addEnumerationTest(TestNode** root)
|
|
|
|
{
|
|
|
|
addTest(root, &EnumerationTest, "tsutil/uenumtst/EnumerationTest");
|
2002-07-16 22:29:23 +00:00
|
|
|
}
|