diff options
Diffstat (limited to 'template.c')
-rw-r--r-- | template.c | 44 |
1 files changed, 16 insertions, 28 deletions
@@ -280,50 +280,38 @@ rsRetVal tplToFields(struct template *pTpl, msg_t *pMsg, { DEFiRet; struct templateEntry *pTpe; - struct templateField *fields; - size_t i, propLen; - unsigned short bMustBeFreed; - uchar *pVal; + struct fieldBuildingState state; + size_t i; assert(pTpl != NULL); assert(pMsg != NULL); assert(pFields != NULL); - /* loop through the template. We obtain one value, create a - * private copy (if necessary), add it to the string array - * and then on to the next until we have processed everything. - */ - - /* The zeroization implicitly terminates the output array. */ - CHKmalloc(fields = calloc(pTpl->tpenElements + 1, sizeof(*fields))); + state.nextField = 0; + state.allocatedFields = pTpl->tpenElements + 1; + CHKmalloc(state.fields = malloc(state.allocatedFields + * sizeof(*state.fields))); - i = 0; for(pTpe = pTpl->pEntryRoot; pTpe != NULL; pTpe = pTpe->pNext) { if(pTpe->eEntryType == CONSTANT) { /* Completely ingore this - we don't know a field name to use anyway. */ } else if(pTpe->eEntryType == FIELD) { - fields[i].fieldName = pTpe->data.field.fieldName; - pVal = MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, - pTpe->data.field.propName, &propLen, - &bMustBeFreed); - if(bMustBeFreed) { /* if it must be freed, it is our own private copy... */ - fields[i].value = pVal; /* ... so we can use it! */ - } else { - CHKmalloc(fields[i].value = (uchar*)strdup((char*) pVal)); - } - i++; + CHKiRet(MsgAppendFields(pMsg, pTpe, &state)); } } + CHKiRet(FBSensureSpace(&state)); + /* Terminate the array */ + state.fields[state.nextField].fieldName = NULL; finalize_it: - if(iRet != RS_RET_OK && fields != NULL) { - for(i = 0; fields[i].fieldName != NULL; i++) - free(fields[i].value); - free(fields); - fields = NULL; + if(iRet != RS_RET_OK && state.fields != NULL) { + for(i = 0; i < state.nextField; i++) + free(state.fields[i].value); + free(state.fields); + state.fields = NULL; } - *pFields = fields; + *pFields = state.fields; RETiRet; } |