Skip to content

AddTreToRecrod20C

anna-dodd edited this page Jun 3, 2015 · 1 revision
This API is subject to change

Adding a TRE to the record (2.0)


TRE construction in version 2.0 of the library is tied to the plug-in that handles the TRE. When the constructor is called for a TRE, the user provides the string that identifies what type of TRE to create and also, a description that can clarify what version of a specific type of TRE, and how long it must be (these values can be defaulted).

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("ACFTB", NULL, &error);
    if (!tre)
    {
        nitf_Error_print(&error, stdout, "Exiting...");
        exit(EXIT_FAILURE);
    }


Now that I created the TRE, I can access its fields using the accessor API.

Look up a field value using find()

        /* nitf_ListIterator currentInst, lastInst */
    nitf_List* found = nitf_TRE_find(tre, "AC_", &error); /* This will find AC_MSN_ID, AC_TAIL_NO, AC_TO */
    if (!found) return;
   
    currentInst = nitf_List_begin(found);
    lastInst = nitf_List_end(found);
   
    while (nitf_ListIterator_notEqualTo(&currentInst, &lastInst))
    {
        nitf_Pair* pair = nitf_ListIterator_get(&currentInst);
        nitf_Field* field = (nitf_Field*)pair->data;
        printf("Found: %s [%.*s]\n", pair->key, field->length, field->raw);
        nitf_ListIterator_increment(&currentInst);

    }
    nitf_ListIterator_increment(&current);


Look up a field value using getField()

If I know exactly what Im looking for, I can call getField():

nitf_Field* field = nitf_TRE_getField(tre, "AC_MSN_ID");
if (!field)
{
   printf("No such field exists!\n");
}


Setting a field value using setField()

If I want to set the value of an existing TRE, that's easy enough as well:

if (!nitf_TRE_setField(tre, "CONTRIBUTOR", "DP", strlen("DP"), error))
{
   /* Handle error */
}


Note:
In most TRE specifications, when you are looping, you have a variable that specifies the number of times to loop. You cannot just set a field in the TRE, if it is inside of a loop (e.g. nitf_TRE_setField(tre, "CONTRIBUTOR[7]", "DP", strlen("DP"), error) ). First, you must adjust the size of the loop variable to contain this new index (which causes a new field to be added for each internal value that does not already exist).

Adding your new TRE to the record

To add a TRE to the record, in the file header, for instance:

if (!nitf_Extensions_appendTRE(record->fileHeader->userDefinedSection, tre, error))
{
   /* Handle error */
}


Note
If you try and make your own extension on the spot that is not in the plugin registry, you will not be able to use the TRE to write! For almost all purposes TRE descriptions must be defined within a plug-in!

Clone this wiki locally