1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/******************************************************************************
* Copyright (C) 2009-2015, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
*/
#include "flagparser.h"
#include "filestrm.h"
#include "cstring.h"
#include "cmemory.h"
#define DEFAULT_BUFFER_SIZE 512
static int32_t currentBufferSize = DEFAULT_BUFFER_SIZE;
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, const char ** flagNames, int32_t numOfFlags, UErrorCode *status) {
char* buffer = NULL;
char* tmpFlagBuffer = NULL;
UBool allocateMoreSpace = FALSE;
int32_t idx, i;
int32_t result = 0;
FileStream *f = T_FileStream_open(fileName, "r");
if (f == NULL) {
*status = U_FILE_ACCESS_ERROR;
goto parseFlagsFile_cleanup;
}
buffer = uprv_malloc(sizeof(char) * currentBufferSize);
tmpFlagBuffer = uprv_malloc(sizeof(char) * flagBufferSize);
if (buffer == NULL || tmpFlagBuffer == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
goto parseFlagsFile_cleanup;
}
do {
if (allocateMoreSpace) {
allocateMoreSpace = FALSE;
currentBufferSize *= 2;
uprv_free(buffer);
buffer = uprv_malloc(sizeof(char) * currentBufferSize);
if (buffer == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
goto parseFlagsFile_cleanup;
}
}
for (i = 0; i < numOfFlags;) {
if (T_FileStream_readLine(f, buffer, currentBufferSize) == NULL) {
/* End of file reached. */
break;
}
if (buffer[0] == '#') {
continue;
}
if ((int32_t)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 {
idx = extractFlag(buffer, currentBufferSize, tmpFlagBuffer, flagBufferSize, flagNames, numOfFlags, status);
if (U_FAILURE(*status)) {
if (*status == U_BUFFER_OVERFLOW_ERROR) {
result = currentBufferSize;
} else {
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;
}
return result;
}
/*
* 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, idx = -1;
char *pBuffer;
int32_t offset=0;
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++) {
if (i >= flagSize) {
*status = U_BUFFER_OVERFLOW_ERROR;
return -1;
}
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;
}
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;
}
/*
* 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;
}
|