From 0478d87d537bb7e23b750f3fd26cad46db91f316 Mon Sep 17 00:00:00 2001 From: skarg Date: Tue, 21 Feb 2012 23:58:35 +0000 Subject: [PATCH] Added Append feature required by BACnet spec when File Offset is -1. Thank you Robert Bouwens! --- bacnet-stack/demo/object/bacfile.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/bacnet-stack/demo/object/bacfile.c b/bacnet-stack/demo/object/bacfile.c index 84695568..e0732973 100644 --- a/bacnet-stack/demo/object/bacfile.c +++ b/bacnet-stack/demo/object/bacfile.c @@ -460,16 +460,25 @@ bool bacfile_write_stream_data( pFilename = bacfile_name(data->object_instance); if (pFilename) { found = true; - /* open the file as a clean slate when starting at 0 */ - if (data->type.stream.fileStartPosition == 0) + if (data->type.stream.fileStartPosition == 0) { + /* open the file as a clean slate when starting at 0 */ pFile = fopen(pFilename, "wb"); - else + } else if (data->type.stream.fileStartPosition == -1) { + /* If 'File Start Position' parameter has the special + value -1, then the write operation shall be treated + as an append to the current end of file. */ + pFile = fopen(pFilename, "ab+"); + } else { + /* open for update */ pFile = fopen(pFilename, "rb+"); + } if (pFile) { - (void) fseek(pFile, data->type.stream.fileStartPosition, SEEK_SET); + if (data->type.stream.fileStartPosition != -1) { + (void) fseek(pFile, data->type.stream.fileStartPosition, SEEK_SET); + } if (fwrite(octetstring_value(&data->fileData), octetstring_length(&data->fileData), 1, pFile) != 1) { - + /* do something if it fails? */ } fclose(pFile); }