OLD | NEW |
1 /****************************************************************************** | 1 /****************************************************************************** |
2 * Copyright (C) 2009-2012, International Business Machines | 2 * Copyright (C) 2009-2015, International Business Machines |
3 * Corporation and others. All Rights Reserved. | 3 * Corporation and others. All Rights Reserved. |
4 ******************************************************************************* | 4 ******************************************************************************* |
5 */ | 5 */ |
6 | 6 |
7 #include "flagparser.h" | 7 #include "flagparser.h" |
8 #include "filestrm.h" | 8 #include "filestrm.h" |
9 #include "cstring.h" | 9 #include "cstring.h" |
10 #include "cmemory.h" | 10 #include "cmemory.h" |
11 | 11 |
12 #define DEFAULT_BUFFER_SIZE 512 | 12 #define DEFAULT_BUFFER_SIZE 512 |
13 | 13 |
14 static int32_t currentBufferSize = DEFAULT_BUFFER_SIZE; | 14 static int32_t currentBufferSize = DEFAULT_BUFFER_SIZE; |
15 | 15 |
16 static int32_t extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t
flagSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status); | 16 static int32_t extractFlag(char* buffer, int32_t bufferSize, char* flag, int32_t
flagSize, const char ** flagNames, int32_t numOfFlags, UErrorCode *status); |
17 static int32_t getFlagOffset(const char *buffer, int32_t bufferSize); | 17 static int32_t getFlagOffset(const char *buffer, int32_t bufferSize); |
18 | 18 |
19 /* | 19 /* |
20 * Opens the given fileName and reads in the information storing the data in fla
gBuffer. | 20 * Opens the given fileName and reads in the information storing the data in fla
gBuffer. |
21 */ | 21 */ |
22 U_CAPI int32_t U_EXPORT2 | 22 U_CAPI int32_t U_EXPORT2 |
23 parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize,
const char ** flagNames, int32_t numOfFlags, UErrorCode *status) { | 23 parseFlagsFile(const char *fileName, char **flagBuffer, int32_t flagBufferSize,
const char ** flagNames, int32_t numOfFlags, UErrorCode *status) { |
24 char* buffer = uprv_malloc(sizeof(char) * currentBufferSize); | 24 char* buffer = NULL; |
25 char* tmpFlagBuffer = uprv_malloc(sizeof(char) * flagBufferSize); | 25 char* tmpFlagBuffer = NULL; |
26 UBool allocateMoreSpace = FALSE; | 26 UBool allocateMoreSpace = FALSE; |
27 int32_t idx, i; | 27 int32_t idx, i; |
28 int32_t result = 0; | 28 int32_t result = 0; |
29 | 29 |
30 FileStream *f = T_FileStream_open(fileName, "r"); | 30 FileStream *f = T_FileStream_open(fileName, "r"); |
31 if (f == NULL) { | 31 if (f == NULL) { |
32 *status = U_FILE_ACCESS_ERROR; | 32 *status = U_FILE_ACCESS_ERROR; |
33 return -1; | 33 goto parseFlagsFile_cleanup; |
34 } | 34 } |
| 35 |
| 36 buffer = uprv_malloc(sizeof(char) * currentBufferSize); |
| 37 tmpFlagBuffer = uprv_malloc(sizeof(char) * flagBufferSize); |
35 | 38 |
36 if (buffer == NULL) { | 39 if (buffer == NULL || tmpFlagBuffer == NULL) { |
37 *status = U_MEMORY_ALLOCATION_ERROR; | 40 *status = U_MEMORY_ALLOCATION_ERROR; |
38 return -1; | 41 goto parseFlagsFile_cleanup; |
39 } | 42 } |
40 | 43 |
41 do { | 44 do { |
42 if (allocateMoreSpace) { | 45 if (allocateMoreSpace) { |
43 allocateMoreSpace = FALSE; | 46 allocateMoreSpace = FALSE; |
44 currentBufferSize *= 2; | 47 currentBufferSize *= 2; |
45 uprv_free(buffer); | 48 uprv_free(buffer); |
46 buffer = uprv_malloc(sizeof(char) * currentBufferSize); | 49 buffer = uprv_malloc(sizeof(char) * currentBufferSize); |
47 if (buffer == NULL) { | 50 if (buffer == NULL) { |
48 uprv_free(tmpFlagBuffer); | |
49 *status = U_MEMORY_ALLOCATION_ERROR; | 51 *status = U_MEMORY_ALLOCATION_ERROR; |
50 return -1; | 52 goto parseFlagsFile_cleanup; |
51 } | 53 } |
52 } | 54 } |
53 for (i = 0; i < numOfFlags;) { | 55 for (i = 0; i < numOfFlags;) { |
54 if (T_FileStream_readLine(f, buffer, currentBufferSize) == NULL) { | 56 if (T_FileStream_readLine(f, buffer, currentBufferSize) == NULL) { |
55 /* End of file reached. */ | 57 /* End of file reached. */ |
56 break; | 58 break; |
57 } | 59 } |
58 if (buffer[0] == '#') { | 60 if (buffer[0] == '#') { |
59 continue; | 61 continue; |
60 } | 62 } |
(...skipping 21 matching lines...) Expand all Loading... |
82 continue; | 84 continue; |
83 } | 85 } |
84 } else { | 86 } else { |
85 uprv_strcpy(flagBuffer[i++], tmpFlagBuffer); | 87 uprv_strcpy(flagBuffer[i++], tmpFlagBuffer); |
86 } | 88 } |
87 } | 89 } |
88 } | 90 } |
89 } | 91 } |
90 } while (allocateMoreSpace && U_SUCCESS(*status)); | 92 } while (allocateMoreSpace && U_SUCCESS(*status)); |
91 | 93 |
| 94 parseFlagsFile_cleanup: |
92 uprv_free(tmpFlagBuffer); | 95 uprv_free(tmpFlagBuffer); |
93 uprv_free(buffer); | 96 uprv_free(buffer); |
94 | 97 |
95 T_FileStream_close(f); | 98 T_FileStream_close(f); |
| 99 |
| 100 if (U_FAILURE(*status)) { |
| 101 return -1; |
| 102 } |
96 | 103 |
97 if (U_SUCCESS(*status) && result == 0) { | 104 if (U_SUCCESS(*status) && result == 0) { |
98 currentBufferSize = DEFAULT_BUFFER_SIZE; | 105 currentBufferSize = DEFAULT_BUFFER_SIZE; |
99 } | 106 } |
100 | 107 |
101 return result; | 108 return result; |
102 } | 109 } |
103 | 110 |
104 | 111 |
105 /* | 112 /* |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 break; | 169 break; |
163 } | 170 } |
164 } | 171 } |
165 | 172 |
166 if (offset == bufferSize || (offset - 1) == bufferSize) { | 173 if (offset == bufferSize || (offset - 1) == bufferSize) { |
167 offset = 0; | 174 offset = 0; |
168 } | 175 } |
169 | 176 |
170 return offset; | 177 return offset; |
171 } | 178 } |
OLD | NEW |