X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/729e4ab9bc6618bc3d8a898e575df7f4019e29ca..ef6cf650f4a75c3f97de06b51fa104f2069b9ea2:/icuSources/tools/toolutil/flagparser.c diff --git a/icuSources/tools/toolutil/flagparser.c b/icuSources/tools/toolutil/flagparser.c index 41ec9c9c..fac18e2f 100644 --- a/icuSources/tools/toolutil/flagparser.c +++ b/icuSources/tools/toolutil/flagparser.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (C) 2009-2010, International Business Machines + * Copyright (C) 2009-2015, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* */ @@ -13,28 +13,32 @@ 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 extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status); 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 int32_t U_EXPORT2 -parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, int32_t numOfFlags, UErrorCode *status) { - char* buffer = uprv_malloc(sizeof(char) * currentBufferSize); +parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status) { + char* buffer = NULL; + char* tmpFlagBuffer = NULL; UBool allocateMoreSpace = FALSE; - int32_t i; + int32_t idx, i; int32_t result = 0; FileStream *f = T_FileStream_open(fileName, "r"); if (f == NULL) { *status = U_FILE_ACCESS_ERROR; - return -1; + goto parseFlagsFile_cleanup; } + + buffer = uprv_malloc(sizeof(char) * currentBufferSize); + tmpFlagBuffer = uprv_malloc(sizeof(char) * flagBufferSize); - if (buffer == NULL) { + if (buffer == NULL || tmpFlagBuffer == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; - return -1; + goto parseFlagsFile_cleanup; } do { @@ -45,14 +49,17 @@ parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, buffer = uprv_malloc(sizeof(char) * currentBufferSize); if (buffer == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; - return -1; + goto parseFlagsFile_cleanup; } } - for (i = 0; i < numOfFlags; i++) { + for (i = 0; i < numOfFlags;) { if (T_FileStream_readLine(f, buffer, currentBufferSize) == NULL) { - *status = U_FILE_ACCESS_ERROR; + /* End of file reached. */ break; } + if (buffer[0] == '#') { + continue; + } if (uprv_strlen(buffer) == (currentBufferSize - 1) && buffer[currentBufferSize-2] != '\n') { /* Allocate more space for buffer if it didnot read the entrire line */ @@ -60,7 +67,7 @@ parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, T_FileStream_rewind(f); break; } else { - extractFlag(buffer, currentBufferSize, flagBuffer[i], flagBufferSize, status); + idx = extractFlag(buffer, currentBufferSize, tmpFlagBuffer, flagBufferSize, flagNames, numOfFlags, status); if (U_FAILURE(*status)) { if (*status == U_BUFFER_OVERFLOW_ERROR) { result = currentBufferSize; @@ -68,14 +75,31 @@ parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, result = -1; } break; + } else { + if (flagNames != NULL) { + if (idx >= 0) { + uprv_strcpy(flagBuffer[idx], tmpFlagBuffer); + } else { + /* No match found. Skip it. */ + continue; + } + } else { + uprv_strcpy(flagBuffer[i++], tmpFlagBuffer); + } } } } } while (allocateMoreSpace && U_SUCCESS(*status)); +parseFlagsFile_cleanup: + uprv_free(tmpFlagBuffer); uprv_free(buffer); T_FileStream_close(f); + + if (U_FAILURE(*status) && *status != U_BUFFER_OVERFLOW_ERROR) { + return -1; + } if (U_SUCCESS(*status) && result == 0) { currentBufferSize = DEFAULT_BUFFER_SIZE; @@ -88,10 +112,10 @@ parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, /* * Extract the setting after the '=' and store it in flag excluding the newline character. */ -static void extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, UErrorCode *status) { - int32_t i; +static int32_t extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, const char **flagNames, int32_t numOfFlags, UErrorCode *status) { + int32_t i, idx = -1; char *pBuffer; - int32_t offset; + int32_t offset=0; UBool bufferWritten = FALSE; if (buffer[0] != 0) { @@ -101,7 +125,7 @@ static void extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t fl for(i = 0;;i++) { if (i >= flagSize) { *status = U_BUFFER_OVERFLOW_ERROR; - return; + return -1; } if (pBuffer[i+1] == 0) { /* Indicates a new line character. End here. */ @@ -119,6 +143,18 @@ static void extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t fl if (!bufferWritten) { flag[0] = 0; } + + if (flagNames != NULL && offset>0) { + offset--; /* Move offset back 1 because of '='*/ + for (i = 0; i < numOfFlags; i++) { + if (uprv_strncmp(buffer, flagNames[i], offset) == 0) { + idx = i; + break; + } + } + } + + return idx; } /*