@@ -103,12 +103,12 @@ hashFn(void *entry, void *userData) {
103
103
char * key = pair -> key ;
104
104
UDATA length = strlen (key );
105
105
UDATA i ;
106
-
106
+
107
107
/* Super-lame xor hash */
108
108
for (i = 0 ; i < length ; i ++ ) {
109
109
hash ^= key [i ];
110
110
}
111
-
111
+
112
112
return hash ;
113
113
}
114
114
@@ -117,11 +117,11 @@ static UDATA
117
117
equalsFn (void * leftEntry , void * rightEntry , void * userData ) {
118
118
KeyValuePair * lhs = leftEntry ;
119
119
KeyValuePair * rhs = rightEntry ;
120
-
120
+
121
121
if (strcmp (lhs -> key , rhs -> key ) == 0 ) {
122
122
return TRUE;
123
123
}
124
-
124
+
125
125
return FALSE;
126
126
}
127
127
@@ -144,30 +144,29 @@ trimTrailing(char* input)
144
144
input [index ] = 0 ;
145
145
index -- ;
146
146
break ;
147
-
147
+
148
148
default :
149
149
return ;
150
150
}
151
- }
151
+ }
152
152
}
153
153
154
154
155
- j9props_file_t
155
+ j9props_file_t
156
156
props_file_open (J9PortLibrary * portLibrary , const char * file , char * errorBuf , UDATA errorBufLength )
157
157
{
158
158
PORT_ACCESS_FROM_PORT (portLibrary );
159
- j9props_file_t result = NULL ;
160
159
IDATA fileHandle = -1 ;
161
160
UDATA lineNumber = 0 ;
162
161
char line [1024 ] = "" ;
163
- /* points to some place in line */
164
- char * linePtr = line ;
162
+ UDATA lineLength = 0 ;
163
+ char * linePtr = line ;
165
164
166
- /* Allocate the structure */
167
- result = (j9props_file_t ) j9mem_allocate_memory (sizeof (J9PropsFile ), OMRMEM_CATEGORY_VM );
165
+ j9props_file_t result = (j9props_file_t )j9mem_allocate_memory (sizeof (* result ), OMRMEM_CATEGORY_VM );
168
166
if (NULL == result ) {
169
167
return NULL ;
170
168
}
169
+ memset (result , 0 , sizeof (* result ));
171
170
result -> portLibrary = portLibrary ;
172
171
173
172
/* Allocate the hash table */
@@ -177,84 +176,94 @@ props_file_open(J9PortLibrary* portLibrary, const char* file, char* errorBuf, UD
177
176
}
178
177
179
178
fileHandle = j9file_open (file , EsOpenRead , 0 );
180
- if (fileHandle == -1 ) {
179
+ if (-1 == fileHandle ) {
181
180
goto fail ;
182
181
}
183
-
184
- /* Read a line at a time */
182
+
183
+ /* Read a line at a time. */
185
184
lineNumber = 0 ;
186
- /* previously had a j9file_read_text here, but functionality to do the charset conversion (for zos) is not there yet
187
- * so we use this hack for now*/
188
- while (NULL != propsfile_read_text (portLibrary , fileHandle , linePtr , sizeof (line ) - (line - linePtr ) )) {
189
- char * equalsSign ;
190
-
191
- if (strlen (line ) > 1 && line [strlen (line )- 1 ] == '\n' && line [strlen (line )- 2 ] == '\\' ){ /* line continuation */
185
+ /* Previously had a j9file_read_text here, but functionality to do the charset conversion (for zos) is not there yet
186
+ * so we use this hack for now.
187
+ */
188
+ while (NULL != propsfile_read_text (portLibrary , fileHandle , linePtr , sizeof (line ) - (linePtr - line ))) {
189
+ UDATA allocLength = 0 ;
190
+ char * equalsSign = NULL ;
191
+ lineLength += strlen (linePtr );
192
+
193
+ if ((lineLength >= 2 ) && ('\n' == line [lineLength - 1 ]) && ('\\' == line [lineLength - 2 ])) { /* line continuation */
192
194
/* update linePtr to new position and overwrite the '\\' */
193
- linePtr = linePtr + strlen (linePtr ) - 2 ;
195
+ lineLength -= 2 ;
196
+ linePtr = line + lineLength ;
194
197
continue ;
195
198
}
196
199
197
200
linePtr = line ;
198
- lineNumber ++ ;
199
- equalsSign = strstr (line ,"=" );
200
- if (equalsSign && line [0 ] != '#' ) { /*lines starting with # are comments*/
201
- UDATA lineLength ;
202
- char * copiedLine ;
203
- KeyValuePair pair ;
204
-
205
- /* Ensure that we have something on the LHS */
201
+ allocLength = lineLength + 1 ; /* + 1 for NULL */
202
+ lineLength = 0 ;
203
+ lineNumber += 1 ;
204
+
205
+ /* Lines starting with # are comments. */
206
+ if ('#' == line [0 ]) {
207
+ continue ;
208
+ }
209
+
210
+ equalsSign = strstr (line , "=" );
211
+ if (NULL != equalsSign ) {
212
+ char * copiedLine = NULL ;
213
+ KeyValuePair pair = { NULL , NULL };
214
+
215
+ /* Ensure that we have something on the LHS. */
206
216
if (equalsSign == line ) {
207
217
goto fail ;
208
218
}
209
219
210
220
/* Copy the line */
211
- lineLength = strlen (line );
212
- copiedLine = j9mem_allocate_memory (lineLength + 1 /* for NULL */ , OMRMEM_CATEGORY_VM );
221
+ copiedLine = j9mem_allocate_memory (allocLength , OMRMEM_CATEGORY_VM );
213
222
if (NULL == copiedLine ) {
214
223
goto fail ;
215
224
}
216
- memcpy (copiedLine , line , lineLength + 1 );
217
-
218
- /* Slam a NULL where the equals sign was */
225
+ memcpy (copiedLine , line , allocLength );
226
+
227
+ /* Slam a NULL where the equals sign was. */
219
228
pair .key = copiedLine ;
220
- pair .value = & copiedLine [equalsSign - line + 1 ];
229
+ pair .value = & copiedLine [equalsSign - line + 1 ];
221
230
pair .value [-1 ] = 0 ;
222
-
223
- /* Deal with any trailing whitespace on the value */
231
+
232
+ /* Deal with any trailing whitespace on the value. */
224
233
trimTrailing (pair .key );
225
234
trimTrailing (pair .value );
226
-
227
- /* Insert the halves into the hash table */
235
+
236
+ /* Insert the halves into the hash table. */
228
237
if (NULL == hashTableAdd (result -> properties , & pair )) {
229
238
j9mem_free_memory (pair .key );
230
239
goto fail ;
231
240
}
232
241
}
233
242
}
234
243
235
- /* Done with the file */
244
+ /* Done with the file. */
236
245
j9file_close (fileHandle );
237
246
return result ;
238
247
239
248
fail :
240
- if (fileHandle != -1 ) {
249
+ if (-1 != fileHandle ) {
241
250
j9file_close (fileHandle );
242
251
}
243
- /* Call close to free memory */
252
+ /* Call close to free memory. */
244
253
props_file_close (result );
245
254
return NULL ;
246
255
}
247
256
248
257
249
- void
258
+ void
250
259
props_file_close (j9props_file_t file )
251
260
{
252
261
PORT_ACCESS_FROM_PORT (file -> portLibrary );
253
-
262
+
254
263
if (file -> properties != NULL ) {
255
264
J9HashTableState walkState ;
256
265
KeyValuePair * pair ;
257
-
266
+
258
267
/* Loop through and free any keys */
259
268
pair = hashTableStartDo (file -> properties , & walkState );
260
269
while (pair != NULL ) {
@@ -265,33 +274,33 @@ props_file_close(j9props_file_t file)
265
274
/* The free the entries */
266
275
hashTableFree (file -> properties );
267
276
}
268
-
277
+
269
278
/* Free the heap allocated structure */
270
279
j9mem_free_memory (file );
271
280
}
272
281
273
282
274
- const char *
283
+ const char *
275
284
props_file_get (j9props_file_t file , const char * key )
276
285
{
277
286
KeyValuePair pair , * result ;
278
287
pair .key = (char * )key ;
279
-
288
+
280
289
result = hashTableFind (file -> properties , & pair );
281
290
if (result == NULL ) {
282
291
return NULL ;
283
292
}
284
-
293
+
285
294
return result -> value ;
286
295
}
287
296
288
297
289
- void
298
+ void
290
299
props_file_do (j9props_file_t file , j9props_file_iterator iterator , void * userData )
291
300
{
292
301
J9HashTableState walkState ;
293
302
KeyValuePair * pair ;
294
-
303
+
295
304
pair = hashTableStartDo (file -> properties , & walkState );
296
305
while (pair != NULL ) {
297
306
BOOLEAN keepGoing = (* iterator )(file , pair -> key , pair -> value , userData );
0 commit comments