NITFPRIV(NITF_BOOL) defaultInit(nitf_TRE* tre, const char* id, nitf_Error * error) { nitf_TREDescription* descr; /* create a new private data struct */ tre->priv = nitf_TREPrivateData_construct(error); if (!tre->priv) return NITF_FAILURE; descr = (nitf_TREDescription *) NITF_MALLOC(2 * sizeof(nitf_TREDescription)); if (!descr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NITF_FAILURE; } descr[0].data_type = NITF_BINARY; descr[0].data_count = NITF_TRE_GOBBLE; descr[0].label = _NITF_DEFAULT_TRE_LABEL; descr[0].tag = NITF_TRE_RAW; descr[1].data_type = NITF_END; descr[1].data_count = 0; descr[1].label = NULL; descr[1].tag = NULL; ((nitf_TREPrivateData*)tre->priv)->description = descr; return NITF_SUCCESS; }
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; } }
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_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; }
NITFPRIV(int) defaultRead(nitf_IOInterface *io, nitf_Uint32 length, nitf_TRE * tre, struct _nitf_Record* record, nitf_Error * error) { nitf_Field *field = NULL; nitf_TREDescription *descr = NULL; char *data = NULL; NITF_BOOL success; if (!tre) { /* set error ??? */ goto CATCH_ERROR; } /* malloc the space for the raw data */ data = (char *) NITF_MALLOC(length + 1); if (!data) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } memset(data, 0, length + 1); descr = (nitf_TREDescription *) NITF_MALLOC(2 * sizeof(nitf_TREDescription)); if (!descr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } descr[0].data_type = NITF_BINARY; descr[0].data_count = length; descr[0].label = _NITF_DEFAULT_TRE_LABEL; descr[0].tag = NITF_TRE_RAW; descr[1].data_type = NITF_END; descr[1].data_count = 0; descr[1].label = NULL; descr[1].tag = NULL; tre->priv = nitf_TREPrivateData_construct(error); if (!tre->priv) goto CATCH_ERROR; ((nitf_TREPrivateData*)tre->priv)->length = length; ((nitf_TREPrivateData*)tre->priv)->description = descr; /* Read the data extension into the tre */ success = nitf_TREUtils_readField(io, data, (int) length, error); if (!success) goto CATCH_ERROR; field = nitf_Field_construct(length, NITF_BINARY, error); if (field == NULL) { goto CATCH_ERROR; } /* TODO -- likely inefficient, since we end up copying the raw data here */ if (!nitf_Field_setRawData (field, (NITF_DATA *) data, length, error)) { goto CATCH_ERROR; } nitf_HashTable_insert(((nitf_TREPrivateData*)tre->priv)->hash, NITF_TRE_RAW, field, error); NITF_FREE(data); #ifdef NITF_PRINT_TRES printf ("------------------------------------------------------------\n"); printf("[%s] length %d (unknown TRE default handler)\n", tre->tag, length); printf ("------------------------------------------------------------\n"); printf("\n"); #endif return NITF_SUCCESS; /* Handle any errors */ CATCH_ERROR: if (descr) NITF_FREE(descr); if (tre && tre->priv) nitf_TREPrivateData_destruct((nitf_TREPrivateData**)&tre->priv); return NITF_FAILURE; }
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; }