ICU-8015 Improve performance of pkgdata tool file parser.

X-SVN-Rev: 28763
This commit is contained in:
Michael Ow 2010-10-05 04:52:16 +00:00
parent 2ea7489697
commit df4608dd59
3 changed files with 25 additions and 10 deletions

View file

@ -752,6 +752,7 @@ static int32_t initializePkgDataFlags(UPKGOptions *o) {
UErrorCode status = U_ZERO_ERROR;
int32_t result = 0;
int32_t currentBufferSize = SMALL_BUFFER_MAX_SIZE;
int32_t tmpResult = 0;
/* Initialize pkgdataFlags */
pkgDataFlags = (char**)uprv_malloc(sizeof(char*) * PKGDATA_FLAGS_SIZE);
@ -785,12 +786,12 @@ static int32_t initializePkgDataFlags(UPKGOptions *o) {
fprintf(stdout, "# Reading options file %s\n", o->options);
}
status = U_ZERO_ERROR;
parseFlagsFile(o->options, pkgDataFlags, currentBufferSize, (int32_t)PKGDATA_FLAGS_SIZE, &status);
tmpResult = parseFlagsFile(o->options, pkgDataFlags, currentBufferSize, (int32_t)PKGDATA_FLAGS_SIZE, &status);
if (status == U_BUFFER_OVERFLOW_ERROR) {
for (int32_t i = 0; i < PKGDATA_FLAGS_SIZE; i++) {
uprv_free(pkgDataFlags[i]);
}
currentBufferSize *= 2;
currentBufferSize = tmpResult;
} else if (U_FAILURE(status)) {
fprintf(stderr,"Unable to open or read \"%s\" option file. status = %s\n", o->options, u_errorName(status));
return -1;

View file

@ -9,7 +9,9 @@
#include "cstring.h"
#include "cmemory.h"
#define BUFFER_DEFAULT_SIZE 512
#define DEFAULT_BUFFER_SIZE 512
static int32_t currentBufferSize = DEFAULT_BUFFER_SIZE;
static void extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, UErrorCode *status);
static int32_t getFlagOffset(const char *buffer, int32_t bufferSize);
@ -17,22 +19,22 @@ static int32_t getFlagOffset(const char *buffer, int32_t bufferSize);
/*
* Opens the given fileName and reads in the information storing the data in flagBuffer.
*/
U_CAPI void U_EXPORT2
U_CAPI int32_t U_EXPORT2
parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, int32_t numOfFlags, UErrorCode *status) {
int32_t currentBufferSize = BUFFER_DEFAULT_SIZE;
char* buffer = uprv_malloc(sizeof(char) * currentBufferSize);
UBool allocateMoreSpace = FALSE;
int32_t i;
int32_t result = 0;
FileStream *f = T_FileStream_open(fileName, "r");
if (f == NULL) {
*status = U_FILE_ACCESS_ERROR;
return;
return -1;
}
if (buffer == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return;
return -1;
}
do {
@ -58,16 +60,28 @@ parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize,
T_FileStream_rewind(f);
break;
} else {
extractFlag(buffer, currentBufferSize, flagBuffer[i], flagBufferSize, status);
if (U_FAILURE(*status)) {
if (*status == U_BUFFER_OVERFLOW_ERROR) {
result = currentBufferSize;
} else {
result = -1;
}
break;
}
}
}
} while (allocateMoreSpace && U_SUCCESS(*status));
uprv_free(buffer);
T_FileStream_close(f);
if (U_SUCCESS(*status) && result == 0) {
currentBufferSize = DEFAULT_BUFFER_SIZE;
}
return result;
}

View file

@ -1,7 +1,7 @@
/*
*******************************************************************************
*
* Copyright (C) 2009, International Business Machines
* Copyright (C) 2009-2010, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
@ -24,7 +24,7 @@
#include "unicode/utypes.h"
U_CAPI void U_EXPORT2
U_CAPI int32_t U_EXPORT2
parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, int32_t numOfFlags, UErrorCode *status);
#endif