WriteDesData15C - mdaus/nitro GitHub Wiki
Writing DES data is slightly different between 1.5 and 2.0. There is an example in the repository (test_create_des.c) that is a good example for how to add TREs and it will be fairly different depending on which version of NITRO you are using. You can also read about
Writing the DES is easy. The DESSHF will already live in the record (which is where you should be manipulating it). Now you just need to write the DESDATA. Lets assume for this example that we already have DE data in a raw buffer, and that we have the length. Writing the data looks like this. Lets also assume that the DES has a set of user defined headers (DESSHF) that we also want to write:
static char DESDATA[] = "123456789ABCDEF0" /* Length is 16 */
...
des = nitf_Record_newDataExtensionSegment(record, &error);
if (!des)
{
/* Error */
}
/* Copy our security record from the FHDR */
security = nitf_FileSecurity_clone(record->header->securityGroup, &error);
nitf_FileSecurity_destruct(&(des->subheader->securityGroup));
des->subheader->securityGroup = security;
des->subheader->subheaderFields =
nitf_TRE_construct("TEST DES", "TEST DES",
NITF_TRE_DEFAULT_LENGTH, &error);
if (!des->subheader->subheaderFields)
{
/* Error */
}
/* Lets pretend these fields are important */
nitf_TRE_setField(des->subheader->subheaderFields,
"TEST_DES_COUNT_PLUS_ONE", "17", 2, &error); /* Total length of LDSH + 1 */
nitf_Field_setString(des->subheader->NITF_DE, "DE", &error);
nitf_Field_setString(des->subheader->NITF_DESTAG, "TEST DES", &error);
nitf_Field_setString(des->subheader->NITF_DESVER, "01", &error);
nitf_Field_setString(des->subheader->NITF_DESCLAS, "U", &error);
...
/* By now, we have a nitf_Writer, so we will use it to create our DEWriter and attach our DE data (DESDATA) */
desWriter = nitf_Writer_newDEWriter(writer, 0, &error);
if (desWriter == NULL)
{
/* Error */
}
desData = nitf_SegmentMemorySource_construct(DESDATA, 16, /* Data and length */
0, 0, &error);
if (desData == NULL)
{
/* Error */
}
if (!nitf_SegmentWriter_attachSource(desWriter, desData, &error))
{
/* Error */
}
if (!nitf_Writer_write(writer, &error))
{
/* Error */
}
...
des = nitf_Record_newDataExtensionSegment(record, &error);
if (!des)
{
/* Error */
}
/* Copy our security record from the FHDR */
security = nitf_FileSecurity_clone(record->header->securityGroup, &error);
nitf_FileSecurity_destruct(&(des->subheader->securityGroup));
des->subheader->securityGroup = security;
des->subheader->subheaderFields =
nitf_TRE_construct("TEST DES", "TEST DES",
NITF_TRE_DEFAULT_LENGTH, &error);
if (!des->subheader->subheaderFields)
{
/* Error */
}
/* Lets pretend these fields are important */
nitf_TRE_setField(des->subheader->subheaderFields,
"TEST_DES_COUNT_PLUS_ONE", "17", 2, &error); /* Total length of LDSH + 1 */
nitf_Field_setString(des->subheader->NITF_DE, "DE", &error);
nitf_Field_setString(des->subheader->NITF_DESTAG, "TEST DES", &error);
nitf_Field_setString(des->subheader->NITF_DESVER, "01", &error);
nitf_Field_setString(des->subheader->NITF_DESCLAS, "U", &error);
...
/* By now, we have a nitf_Writer, so we will use it to create our DEWriter and attach our DE data (DESDATA) */
desWriter = nitf_Writer_newDEWriter(writer, 0, &error);
if (desWriter == NULL)
{
/* Error */
}
desData = nitf_SegmentMemorySource_construct(DESDATA, 16, /* Data and length */
0, 0, &error);
if (desData == NULL)
{
/* Error */
}
if (!nitf_SegmentWriter_attachSource(desWriter, desData, &error))
{
/* Error */
}
if (!nitf_Writer_write(writer, &error))
{
/* Error */
}
Thats pretty much all there is to it! You can use any SegmentSource to attach, or you can define your own.