/******************************************************************************
- * Copyright (C) 2009-2011, International Business Machines
+ * Copyright (C) 2009-2015, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
*/
*/
U_CAPI int32_t U_EXPORT2
parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status) {
- char* buffer = uprv_malloc(sizeof(char) * currentBufferSize);
- char* tmpFlagBuffer = uprv_malloc(sizeof(char) * flagBufferSize);
+ char* buffer = NULL;
+ char* tmpFlagBuffer = NULL;
UBool allocateMoreSpace = FALSE;
- int32_t index, 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 {
uprv_free(buffer);
buffer = uprv_malloc(sizeof(char) * currentBufferSize);
if (buffer == NULL) {
- uprv_free(tmpFlagBuffer);
*status = U_MEMORY_ALLOCATION_ERROR;
- return -1;
+ goto parseFlagsFile_cleanup;
}
}
for (i = 0; i < numOfFlags;) {
T_FileStream_rewind(f);
break;
} else {
- index = extractFlag(buffer, currentBufferSize, tmpFlagBuffer, flagBufferSize, flagNames, numOfFlags, status);
+ idx = extractFlag(buffer, currentBufferSize, tmpFlagBuffer, flagBufferSize, flagNames, numOfFlags, status);
if (U_FAILURE(*status)) {
if (*status == U_BUFFER_OVERFLOW_ERROR) {
result = currentBufferSize;
break;
} else {
if (flagNames != NULL) {
- if (index >= 0) {
- uprv_strcpy(flagBuffer[index], tmpFlagBuffer);
+ if (idx >= 0) {
+ uprv_strcpy(flagBuffer[idx], tmpFlagBuffer);
} else {
/* No match found. Skip it. */
continue;
}
} 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;
* Extract the setting after the '=' and store it in flag excluding the newline character.
*/
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, index = -1;
+ int32_t i, idx = -1;
char *pBuffer;
int32_t offset=0;
UBool bufferWritten = FALSE;
offset--; /* Move offset back 1 because of '='*/
for (i = 0; i < numOfFlags; i++) {
if (uprv_strncmp(buffer, flagNames[i], offset) == 0) {
- index = i;
+ idx = i;
break;
}
}
}
- return index;
+ return idx;
}
/*