summaryrefslogtreecommitdiffstats
path: root/template.c
diff options
context:
space:
mode:
Diffstat (limited to 'template.c')
-rw-r--r--template.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/template.c b/template.c
index 768608f1..d422f8ee 100644
--- a/template.c
+++ b/template.c
@@ -269,6 +269,65 @@ finalize_it:
RETiRet;
}
+/* This functions converts a template into an array of templateField structures.
+ * For further general details, see the very similar funtion
+ * tpltoString().
+ * The caller is repsonsible for destroying that array as well as all of its
+ * elements (but not the fieldName strings).
+ */
+rsRetVal tplToFields(struct template *pTpl, msg_t *pMsg,
+ struct templateField **pFields)
+{
+ DEFiRet;
+ struct templateEntry *pTpe;
+ struct templateField *fields;
+ size_t i, propLen;
+ unsigned short bMustBeFreed;
+ uchar *pVal;
+
+ 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)));
+
+ 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++;
+ }
+ }
+
+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;
+ }
+ *pFields = fields;
+
+ RETiRet;
+}
+
/* Helper to doEscape. This is called if doEscape
* runs out of memory allocating the escaped string.