NITFAPI(NITF_BOOL) nitf_TREUtils_basicInit(nitf_TRE * tre, const char* id, nitf_Error * error) { nitf_TREDescriptionSet* set = NULL; nitf_TREDescriptionInfo* descInfo = NULL; nitf_TREPrivateData *priv = NULL; NITF_BOOL found = 0; assert(tre); set = (nitf_TREDescriptionSet*)tre->handler->data; /* create a new private data struct */ priv = nitf_TREPrivateData_construct(error); if (!priv) return NITF_FAILURE; /* search for the description with the given ID - if one was provided */ if (id) { descInfo = set->descriptions; while(descInfo && descInfo->name && !found) { if (strcmp(descInfo->name, id) == 0) { /* we have a match! */ found = 1; } else { descInfo++; } } /* if we couldn't find it, we get out of here... */ if (!found) { nitf_Error_initf(error, NITF_CTXT, NITF_ERR_INVALID_OBJECT, "No matching id '%s' found!", id); return NITF_FAILURE; } } else { /* otherwise, if no ID was given, we'll just use the first description. * in most cases, this will be the only description. */ descInfo = set->descriptions; } /* set the name */ if (!nitf_TREPrivateData_setDescriptionName( priv, descInfo->name, error)) { nitf_TREPrivateData_destruct(&priv); tre->priv = NULL; return NITF_FAILURE; } /* assign it to the TRE */ tre->priv = priv; /* try to fill the TRE */ if (nitf_TREUtils_fillData(tre, descInfo->description, error)) return NITF_SUCCESS; else { nitf_TRE_destruct(&tre); return NITF_FAILURE; } }
nitf_TREUtils_setDescription(nitf_TRE* tre, nitf_Uint32 length, nitf_Error* error) { nitf_TREDescriptionSet *descriptions = NULL; nitf_TREDescriptionInfo *infoPtr = NULL; int numDescriptions = 0; if (!tre) { nitf_Error_init(error, "setDescription -> invalid tre object", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return NITF_FAILURE; } descriptions = (nitf_TREDescriptionSet*)tre->handler->data; if (!descriptions) { nitf_Error_init(error, "TRE Description Set is NULL", NITF_CTXT, NITF_ERR_INVALID_OBJECT); return NITF_FAILURE; } tre->priv = NULL; infoPtr = descriptions->descriptions; while (infoPtr && (infoPtr->description != NULL)) { if (numDescriptions == descriptions->defaultIndex) { nitf_TREPrivateData *priv = nitf_TREPrivateData_construct(error); if (!priv) { return NITF_FAILURE; } priv->length = length; priv->description = infoPtr->description; if (!nitf_TREPrivateData_setDescriptionName( priv, infoPtr->name, error ) ) { nitf_TREPrivateData_destruct(&priv); return NITF_FAILURE; } tre->priv = priv; break; } numDescriptions++; infoPtr++; } if (!tre->priv) { nitf_Error_init(error, "TRE Description is NULL", NITF_CTXT, NITF_ERR_INVALID_OBJECT); return NITF_FAILURE; } return NITF_SUCCESS; }
NITFAPI(NITF_BOOL) nitf_TREUtils_basicRead(nitf_IOInterface* io, nitf_Uint32 length, nitf_TRE* tre, struct _nitf_Record* record, nitf_Error* error) { int ok; char *data = NULL; nitf_TREDescriptionSet *descriptions = NULL; nitf_TREDescriptionInfo *infoPtr = NULL; if (!tre) return NITF_FAILURE; /* set the description/length */ /*nitf_TREUtils_setDescription(tre, length, error);*/ /*if (!tre->descrip) goto CATCH_ERROR;*/ data = (char*)NITF_MALLOC( length ); if (!data) { nitf_Error_init(error, NITF_STRERROR( NITF_ERRNO ),NITF_CTXT, NITF_ERR_MEMORY ); return NITF_FAILURE; } memset(data, 0, length); if (!nitf_TREUtils_readField(io, data, length, error)) { NITF_FREE(data); return NITF_FAILURE; } descriptions = (nitf_TREDescriptionSet*)tre->handler->data; if (!descriptions) { nitf_Error_init(error, "TRE Description Set is NULL", NITF_CTXT, NITF_ERR_INVALID_OBJECT); NITF_FREE(data); return NITF_FAILURE; } tre->priv = NULL; infoPtr = descriptions->descriptions; tre->priv = nitf_TREPrivateData_construct(error); ((nitf_TREPrivateData*)tre->priv)->length = length; ok = NITF_FAILURE; while (infoPtr && (infoPtr->description != NULL)) { if (!tre->priv) { break; } ((nitf_TREPrivateData*)tre->priv)->description = infoPtr->description; #ifdef NITF_DEBUG printf("Trying TRE with description: %s\n\n", infoPtr->name); #endif ok = nitf_TREUtils_parse(tre, data, error); if (ok) { nitf_TREPrivateData *priv = (nitf_TREPrivateData*)tre->priv; /* copy the name */ if (!nitf_TREPrivateData_setDescriptionName( priv, infoPtr->name, error)) { /* something bad happened... so we need to cleanup */ NITF_FREE(data); nitf_TREPrivateData_destruct(&priv); tre->priv = NULL; return NITF_FAILURE; } /*nitf_HashTable_print( ((nitf_TREPrivateData*)tre->priv)->hash );*/ break; } infoPtr++; } if (data) NITF_FREE(data); return ok; }
nitf_TREPrivateData_clone(nitf_TREPrivateData *source, nitf_Error * error) { nitf_TREPrivateData *priv = NULL; /* temporary nitf_List pointer */ nitf_List *lPtr; /* temporary nitf_Field pointer */ nitf_Field *field; /* temporary nitf_Pair pointer */ nitf_Pair *pair; /* iterator to front of list */ nitf_ListIterator iter; /* iterator to back of list */ nitf_ListIterator end; /* used for iterating */ int i; if (source) { priv = nitf_TREPrivateData_construct(error); if (!priv) goto CATCH_ERROR; if (!nitf_TREPrivateData_setDescriptionName( priv, source->descriptionName, error)) { goto CATCH_ERROR; } /* Copy the entire contents of the hash */ for (i = 0; i < source->hash->nbuckets; i++) { /* Foreach chain in the hash table... */ lPtr = source->hash->buckets[i]; iter = nitf_List_begin(lPtr); end = nitf_List_end(lPtr); /* And while they are different... */ while (nitf_ListIterator_notEqualTo(&iter, &end)) { /* Retrieve the field at the iterator... */ pair = (nitf_Pair *) nitf_ListIterator_get(&iter); /* Cast it back to a field... */ field = (nitf_Field *) pair->data; /* clone the field */ field = nitf_Field_clone(field, error); /* If that failed, we need to destruct */ if (!field) goto CATCH_ERROR; /* Yes, now we can insert the new field! */ if (!nitf_HashTable_insert(priv->hash, pair->key, field, error)) { goto CATCH_ERROR; } nitf_ListIterator_increment(&iter); } } } else { nitf_Error_initf(error, NITF_CTXT, NITF_ERR_INVALID_OBJECT, "Trying to clone NULL pointer"); } return priv; CATCH_ERROR: if (priv) nitf_TREPrivateData_destruct(&priv); return NULL; }