-
Notifications
You must be signed in to change notification settings - Fork 38
GetField20C
anna-dodd edited this page Jun 3, 2015
·
1 revision
This API is subject to change
Getting a field as a string:
Getting a field as an int:
Getting a field as a real:
Finding a field in a TRE
You can see a full example of finding a TRE in test_find_field.c in the test cases
The API in 2.0 prefers that you find your values in the TRE, rather than pulling them out of the model directly (you can still use the hash to get fields by name, as in the 1.5 snippet, though).
Finding is especially useful when using XML, since you should
be able to use XPath expressions as arguments to this function. If you are using legacy TREs, you may be able to get away with direct hash access
Getting a field as a string:
/* Get the FHDR (NITF|NSIF)*/
char fhdr[NITF_FHDR_SZ + 1]; /* Fields are not null-terminated, so add a byte to our array */
/* Retrieve the data as a string with a null-byte on the end */
if (!nitf_Field_get(fhdr, fhdr_str, NITF_CONV_STRING, NITF_FHDR_SZ + 1, &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
char fhdr[NITF_FHDR_SZ + 1]; /* Fields are not null-terminated, so add a byte to our array */
/* Retrieve the data as a string with a null-byte on the end */
if (!nitf_Field_get(fhdr, fhdr_str, NITF_CONV_STRING, NITF_FHDR_SZ + 1, &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
Getting a field as an int:
nitf_Int32 int32;
/* Get the header length out as an integer */
if (!nitf_Field_get(hl, &int32, NITF_CONV_INT, 4, &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
/* Get the header length out as an integer */
if (!nitf_Field_get(hl, &int32, NITF_CONV_INT, 4, &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
Getting a field as a real:
/* Get this field as a double */
if (!nitf_Field_get(realField, &doubleData, NITF_CONV_REAL, sizeof(double), &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
/* Now get the same data as a float */
if (!nitf_Field_get(realField, &floatData, NITF_CONV_REAL, sizeof(float), &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
if (!nitf_Field_get(realField, &doubleData, NITF_CONV_REAL, sizeof(double), &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
/* Now get the same data as a float */
if (!nitf_Field_get(realField, &floatData, NITF_CONV_REAL, sizeof(float), &error))
{
nitf_Error_print(&error, stdout, "Error occured!");
exit(EXIT_FAILURE);
}
Finding a field in a TRE
You can see a full example of finding a TRE in test_find_field.c in the test cases
The API in 2.0 prefers that you find your values in the TRE, rather than pulling them out of the model directly (you can still use the hash to get fields by name, as in the 1.5 snippet, though).
Finding is especially useful when using XML, since you should
be able to use XPath expressions as arguments to this function. If you are using legacy TREs, you may be able to get away with direct hash access
nitf_Pair* pair;
nitf_Field* field;
int value;
nitf_List* found = nitf_TRE_find(tre, "NUMPTS[0]", &error);
if (!found)
{
/*... Handle not found case... */
}
if (nitf_List_size(found) != 1)
{
/*... Handle unexpected size case... */
}
inst = nitf_List_begin(found);
pair = nitf_ListIterator_get(&inst);
field = (nitf_Field*)pair->data;
/* Get the header length out as an integer */
if (!nitf_Field_get(field, &value, NITF_CONV_INT, sizeof(int), &error))
{
/* Handle retrieval error */
}
nitf_Field* field;
int value;
nitf_List* found = nitf_TRE_find(tre, "NUMPTS[0]", &error);
if (!found)
{
/*... Handle not found case... */
}
if (nitf_List_size(found) != 1)
{
/*... Handle unexpected size case... */
}
inst = nitf_List_begin(found);
pair = nitf_ListIterator_get(&inst);
field = (nitf_Field*)pair->data;
/* Get the header length out as an integer */
if (!nitf_Field_get(field, &value, NITF_CONV_INT, sizeof(int), &error))
{
/* Handle retrieval error */
}