1 /******************************************************************************
2 * Copyright (C) 2009-2010, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 *******************************************************************************
7 #include "flagparser.h"
12 #define DEFAULT_BUFFER_SIZE 512
14 static int32_t currentBufferSize
= DEFAULT_BUFFER_SIZE
;
16 static void extractFlag(char* buffer
, int32_t bufferSize
, char* flag
, int32_t flagSize
, UErrorCode
*status
);
17 static int32_t getFlagOffset(const char *buffer
, int32_t bufferSize
);
20 * Opens the given fileName and reads in the information storing the data in flagBuffer.
22 U_CAPI
int32_t U_EXPORT2
23 parseFlagsFile(const char *fileName
, char **flagBuffer
, int32_t flagBufferSize
, int32_t numOfFlags
, UErrorCode
*status
) {
24 char* buffer
= uprv_malloc(sizeof(char) * currentBufferSize
);
25 UBool allocateMoreSpace
= FALSE
;
29 FileStream
*f
= T_FileStream_open(fileName
, "r");
31 *status
= U_FILE_ACCESS_ERROR
;
36 *status
= U_MEMORY_ALLOCATION_ERROR
;
41 if (allocateMoreSpace
) {
42 allocateMoreSpace
= FALSE
;
43 currentBufferSize
*= 2;
45 buffer
= uprv_malloc(sizeof(char) * currentBufferSize
);
47 *status
= U_MEMORY_ALLOCATION_ERROR
;
51 for (i
= 0; i
< numOfFlags
; i
++) {
52 if (T_FileStream_readLine(f
, buffer
, currentBufferSize
) == NULL
) {
53 *status
= U_FILE_ACCESS_ERROR
;
57 if (uprv_strlen(buffer
) == (currentBufferSize
- 1) && buffer
[currentBufferSize
-2] != '\n') {
58 /* Allocate more space for buffer if it didnot read the entrire line */
59 allocateMoreSpace
= TRUE
;
60 T_FileStream_rewind(f
);
63 extractFlag(buffer
, currentBufferSize
, flagBuffer
[i
], flagBufferSize
, status
);
64 if (U_FAILURE(*status
)) {
65 if (*status
== U_BUFFER_OVERFLOW_ERROR
) {
66 result
= currentBufferSize
;
74 } while (allocateMoreSpace
&& U_SUCCESS(*status
));
78 T_FileStream_close(f
);
80 if (U_SUCCESS(*status
) && result
== 0) {
81 currentBufferSize
= DEFAULT_BUFFER_SIZE
;
89 * Extract the setting after the '=' and store it in flag excluding the newline character.
91 static void extractFlag(char* buffer
, int32_t bufferSize
, char* flag
, int32_t flagSize
, UErrorCode
*status
) {
95 UBool bufferWritten
= FALSE
;
98 /* Get the offset (i.e. position after the '=') */
99 offset
= getFlagOffset(buffer
, bufferSize
);
100 pBuffer
= buffer
+offset
;
103 *status
= U_BUFFER_OVERFLOW_ERROR
;
106 if (pBuffer
[i
+1] == 0) {
107 /* Indicates a new line character. End here. */
112 flag
[i
] = pBuffer
[i
];
114 bufferWritten
= TRUE
;
119 if (!bufferWritten
) {
125 * Get the position after the '=' character.
127 static int32_t getFlagOffset(const char *buffer
, int32_t bufferSize
) {
130 for (offset
= 0; offset
< bufferSize
;offset
++) {
131 if (buffer
[offset
] == '=') {
137 if (offset
== bufferSize
|| (offset
- 1) == bufferSize
) {