Skip to content

AddTreToRecord15C

anna-dodd edited this page Jun 3, 2015 · 1 revision
In order to add a new TRE to the record, we must instantiate a TRE. This is very simple if the TRE handler exists in the plugin path. For example, to create an ACFTB TRE, I can just instantiate the default TRE constructor:

  /* construct a tre */
    nitf_TRE *tre = nitf_TRE_construct("ACFTA", NULL, 154, &error);
    if (!tre)
    {
        nitf_Error_print(&error, stdout, "Exiting...");
        exit(EXIT_FAILURE);
    }


I can also declare using a specific description, retrieved from function

nitf_TREDescriptionSet* set = nitf_TRE_getDescriptionSet("ACFTA", error);


I can get the one I want and set the second argument in the constructor to that value.

However, I can also make a TRE from scratch, using my own description. It works for anything

Note: This particular XML example is not encouraged when developing with version 2.0 of the library. In 2.0, you will have a plugin that reads and writes the XML data for you. The methodology is also slightly different for TRE invocation in 2.0.

Let's say I want to create a TRE containing some XML data. In 1.5, we cannot just invoke our plugin to do this for us (in 2.0, we can). So we read in some XML data into a character array, and we envelope it in our raw description. Here is an example where I am creating a TRE I call XMLTRE, which is simply one big chunk of XML data:

    io = nitf_IOHandle_create(argv[2], NITF_ACCESS_READONLY,
                              NITF_OPEN_EXISTING, &error);
    if (NITF_INVALID_HANDLE(io))
    {
        nitf_Error_print(&error, stdout, "Failed to open XML file");
        exit(EXIT_FAILURE);
    }

    off_t xmlSize = nitf_IOHandle_getSize(io, &error);
   
    printf("Size is %ld\n", xmlSize);
    if (xmlSize <= 0)
    exit(EXIT_FAILURE);

    xmlData = (char*)malloc(xmlSize + 1);
    xmlData[xmlSize] = 0;
    if (!nitf_IOHandle_read(io, xmlData, xmlSize, &error))
    {
        nitf_Error_print(&error, stdout, "Failed to read XML file");
        exit(EXIT_FAILURE);
    }

Now I can pass that data to a function like this one:

nitf_TRE* createXMLTRE(const char* data, const int length)
{
    nitf_TRE *tre;
    static nitf_TREDescription description[2];

    nitf_Error error;

    description[0].data_type = NITF_BCS_A;
    description[0].data_count = length;
    description[0].label = "XML Data";
    description[0].tag = "XML";
    description[1].data_type = NITF_END;
    description[1].data_count = 0;
    description[1].label = NULL;
    description[1].tag = NULL;
    tre = nitf_TRE_construct("XMLTRE", description, length, &error);
    if (!tre)
    {
        nitf_Error_print(&error, stdout, "Exiting...");
    return NULL;
    }

    nitf_TRE_setValue(tre, "XML", data, length, &error);
    return tre;
}


Once I have a TRE, its easy to append it to the extensions segment:

nitf_TRE* xmlTre = createXMLTRE(xmlData, xmlSize);
if (!nitf_Extensions_appendTRE(record->header->extendedSection, xmlTre, &error))
{
    nitf_Error_print(&error, stdout, "Append failed");
    exit(EXIT_FAILURE);
}
Clone this wiki locally