1/****************************************************************************** 2 * Copyright (C) 2009, International Business Machines 3 * Corporation and others. All Rights Reserved. 4 ******************************************************************************* 5 */ 6 7#include "flagparser.h" 8#include "filestrm.h" 9 10#define LARGE_BUFFER_MAX_SIZE 2048 11 12static void extractFlag(char* buffer, int32_t bufferSize, char* flag); 13static int32_t getFlagOffset(const char *buffer, int32_t bufferSize); 14 15/* 16 * Opens the given fileName and reads in the information storing the data in flagBuffer. 17 */ 18U_CAPI void U_EXPORT2 19parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize, int32_t numOfFlags, UErrorCode *status) { 20 char buffer[LARGE_BUFFER_MAX_SIZE]; 21 int32_t i; 22 23 FileStream *f = T_FileStream_open(fileName, "r"); 24 if (f == NULL) { 25 *status = U_FILE_ACCESS_ERROR; 26 } 27 28 for (i = 0; i < numOfFlags; i++) { 29 if (T_FileStream_readLine(f, buffer, LARGE_BUFFER_MAX_SIZE) == NULL) { 30 *status = U_FILE_ACCESS_ERROR; 31 break; 32 } 33 34 extractFlag(buffer, LARGE_BUFFER_MAX_SIZE, flagBuffer[i]); 35 } 36 37 T_FileStream_close(f); 38} 39 40 41/* 42 * Extract the setting after the '=' and store it in flag excluding the newline character. 43 */ 44static void extractFlag(char* buffer, int32_t bufferSize, char* flag) { 45 int32_t i; 46 char *pBuffer; 47 int32_t offset; 48 UBool bufferWritten = FALSE; 49 50 if (buffer[0] != 0) { 51 /* Get the offset (i.e. position after the '=') */ 52 offset = getFlagOffset(buffer, bufferSize); 53 pBuffer = buffer+offset; 54 for(i = 0;;i++) { 55 if (pBuffer[i+1] == 0) { 56 /* Indicates a new line character. End here. */ 57 flag[i] = 0; 58 break; 59 } 60 61 flag[i] = pBuffer[i]; 62 if (i == 0) { 63 bufferWritten = TRUE; 64 } 65 } 66 } 67 68 if (!bufferWritten) { 69 flag[0] = 0; 70 } 71} 72 73/* 74 * Get the position after the '=' character. 75 */ 76static int32_t getFlagOffset(const char *buffer, int32_t bufferSize) { 77 int32_t offset = 0; 78 79 for (offset = 0; offset < bufferSize;offset++) { 80 if (buffer[offset] == '=') { 81 offset++; 82 break; 83 } 84 } 85 86 if (offset == bufferSize || (offset - 1) == bufferSize) { 87 offset = 0; 88 } 89 90 return offset; 91} 92