2009-01-09 22:30:44 +00:00
|
|
|
/******************************************************************************
|
2011-06-27 21:58:09 +00:00
|
|
|
* Copyright (C) 2009-2011, International Business Machines
|
2009-01-09 22:30:44 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "flagparser.h"
|
|
|
|
#include "filestrm.h"
|
2010-10-04 23:47:46 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "cmemory.h"
|
2009-01-09 22:30:44 +00:00
|
|
|
|
2010-10-05 04:52:16 +00:00
|
|
|
#define DEFAULT_BUFFER_SIZE 512
|
|
|
|
|
|
|
|
static int32_t currentBufferSize = DEFAULT_BUFFER_SIZE;
|
2009-01-09 22:30:44 +00:00
|
|
|
|
2011-06-27 21:58:09 +00:00
|
|
|
static int32_t extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t flagSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status);
|
2009-01-09 22:30:44 +00:00
|
|
|
static int32_t getFlagOffset(const char *buffer, int32_t bufferSize);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Opens the given fileName and reads in the information storing the data in flagBuffer.
|
|
|
|
*/
|
2010-10-05 04:52:16 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2011-06-27 21:58:09 +00:00
|
|
|
parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status) {
|
2010-10-04 23:47:46 +00:00
|
|
|
char* buffer = uprv_malloc(sizeof(char) * currentBufferSize);
|
2011-06-27 21:58:09 +00:00
|
|
|
char* tmpFlagBuffer = uprv_malloc(sizeof(char) * flagBufferSize);
|
2010-10-04 23:47:46 +00:00
|
|
|
UBool allocateMoreSpace = FALSE;
|
2011-06-27 21:58:09 +00:00
|
|
|
int32_t index, i;
|
2010-10-05 04:52:16 +00:00
|
|
|
int32_t result = 0;
|
2009-01-09 22:30:44 +00:00
|
|
|
|
|
|
|
FileStream *f = T_FileStream_open(fileName, "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
*status = U_FILE_ACCESS_ERROR;
|
2010-10-05 04:52:16 +00:00
|
|
|
return -1;
|
2009-01-09 22:30:44 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 23:47:46 +00:00
|
|
|
if (buffer == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2010-10-05 04:52:16 +00:00
|
|
|
return -1;
|
2010-10-04 23:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (allocateMoreSpace) {
|
|
|
|
allocateMoreSpace = FALSE;
|
|
|
|
currentBufferSize *= 2;
|
|
|
|
uprv_free(buffer);
|
|
|
|
buffer = uprv_malloc(sizeof(char) * currentBufferSize);
|
|
|
|
if (buffer == NULL) {
|
2011-06-27 21:58:09 +00:00
|
|
|
uprv_free(tmpFlagBuffer);
|
2010-10-04 23:47:46 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2010-10-08 20:14:26 +00:00
|
|
|
return -1;
|
2010-10-04 23:47:46 +00:00
|
|
|
}
|
2009-01-09 22:30:44 +00:00
|
|
|
}
|
2011-06-27 21:58:09 +00:00
|
|
|
for (i = 0; i < numOfFlags;) {
|
2010-10-04 23:47:46 +00:00
|
|
|
if (T_FileStream_readLine(f, buffer, currentBufferSize) == NULL) {
|
2011-06-27 21:58:09 +00:00
|
|
|
/* End of file reached. */
|
2010-10-04 23:47:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-06-27 21:58:09 +00:00
|
|
|
if (buffer[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-09 22:30:44 +00:00
|
|
|
|
2010-10-04 23:47:46 +00:00
|
|
|
if (uprv_strlen(buffer) == (currentBufferSize - 1) && buffer[currentBufferSize-2] != '\n') {
|
|
|
|
/* Allocate more space for buffer if it didnot read the entrire line */
|
|
|
|
allocateMoreSpace = TRUE;
|
|
|
|
T_FileStream_rewind(f);
|
|
|
|
break;
|
|
|
|
} else {
|
2011-06-27 21:58:09 +00:00
|
|
|
index = extractFlag(buffer, currentBufferSize, tmpFlagBuffer, flagBufferSize, flagNames, numOfFlags, status);
|
2010-10-04 23:47:46 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2010-10-05 04:52:16 +00:00
|
|
|
if (*status == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
result = currentBufferSize;
|
|
|
|
} else {
|
|
|
|
result = -1;
|
|
|
|
}
|
2010-10-04 23:47:46 +00:00
|
|
|
break;
|
2011-06-27 21:58:09 +00:00
|
|
|
} else {
|
|
|
|
if (flagNames != NULL) {
|
|
|
|
if (index >= 0) {
|
|
|
|
uprv_strcpy(flagBuffer[index], tmpFlagBuffer);
|
|
|
|
} else {
|
|
|
|
/* No match found. Skip it. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uprv_strcpy(flagBuffer[i++], tmpFlagBuffer);
|
|
|
|
}
|
2010-10-04 23:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-24 17:29:26 +00:00
|
|
|
}
|
2010-10-04 23:47:46 +00:00
|
|
|
} while (allocateMoreSpace && U_SUCCESS(*status));
|
2009-01-09 22:30:44 +00:00
|
|
|
|
2011-06-27 21:58:09 +00:00
|
|
|
uprv_free(tmpFlagBuffer);
|
2010-10-05 04:52:16 +00:00
|
|
|
uprv_free(buffer);
|
|
|
|
|
2009-01-09 22:30:44 +00:00
|
|
|
T_FileStream_close(f);
|
2010-10-05 04:52:16 +00:00
|
|
|
|
|
|
|
if (U_SUCCESS(*status) && result == 0) {
|
|
|
|
currentBufferSize = DEFAULT_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2009-01-09 22:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the setting after the '=' and store it in flag excluding the newline character.
|
|
|
|
*/
|
2011-06-27 21:58:09 +00:00
|
|
|
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;
|
2009-01-09 22:30:44 +00:00
|
|
|
char *pBuffer;
|
|
|
|
int32_t offset;
|
|
|
|
UBool bufferWritten = FALSE;
|
|
|
|
|
|
|
|
if (buffer[0] != 0) {
|
|
|
|
/* Get the offset (i.e. position after the '=') */
|
|
|
|
offset = getFlagOffset(buffer, bufferSize);
|
|
|
|
pBuffer = buffer+offset;
|
|
|
|
for(i = 0;;i++) {
|
2009-09-24 17:29:26 +00:00
|
|
|
if (i >= flagSize) {
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
2011-06-27 21:58:09 +00:00
|
|
|
return -1;
|
2009-09-24 17:29:26 +00:00
|
|
|
}
|
2009-01-09 22:30:44 +00:00
|
|
|
if (pBuffer[i+1] == 0) {
|
|
|
|
/* Indicates a new line character. End here. */
|
|
|
|
flag[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
flag[i] = pBuffer[i];
|
|
|
|
if (i == 0) {
|
|
|
|
bufferWritten = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bufferWritten) {
|
|
|
|
flag[0] = 0;
|
|
|
|
}
|
2011-06-27 21:58:09 +00:00
|
|
|
|
|
|
|
if (flagNames != NULL) {
|
|
|
|
offset--; /* Move offset back 1 because of '='*/
|
|
|
|
for (i = 0; i < numOfFlags; i++) {
|
|
|
|
if (uprv_strncmp(buffer, flagNames[i], offset) == 0) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2009-01-09 22:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the position after the '=' character.
|
|
|
|
*/
|
|
|
|
static int32_t getFlagOffset(const char *buffer, int32_t bufferSize) {
|
|
|
|
int32_t offset = 0;
|
|
|
|
|
|
|
|
for (offset = 0; offset < bufferSize;offset++) {
|
|
|
|
if (buffer[offset] == '=') {
|
|
|
|
offset++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset == bufferSize || (offset - 1) == bufferSize) {
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|