/** * Resizes the field to the given length. * Use this method with caution as it can resize a field to be larger than * it should be, according to specs. */ void resize(size_t length) { nitf_Field *field = getNativeOrThrow(); NITF_BOOL resizable = field->resizable; field->resizable = 1; if (!nitf_Field_resizeField(field, length, &error)) throw nitf::NITFException(&error); field->resizable = resizable; }
NITFAPI(NITF_BOOL) nitf_Field_setRawData(nitf_Field * field, NITF_DATA * data, size_t dataLength, nitf_Error * error) { /* check for NULL data */ if (field == NULL || data == NULL) { nitf_Error_init(error, "NULL data", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return NITF_FAILURE; } /* if it's resizable and a different length, we resize */ if (field->resizable && dataLength != field->length) { if (!nitf_Field_resizeField(field, dataLength, error)) return NITF_FAILURE; } /* Should we also offer adoption? */ if (dataLength > field->length) { nitf_Error_init(error, "Invalid data length", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return NITF_FAILURE; } /* If it is the exact length, just memcpy */ if (field->length == dataLength) { memcpy(field->raw, data, field->length); return NITF_SUCCESS; } /* If it is not the exact length, and it is BCS-A, fill right */ else if (field->type == NITF_BCS_A) return copyAndFillSpaces(field, (const char *) data, dataLength, error); else if (field->type == NITF_BCS_N) return copyAndFillZeros(field, (const char *) data, dataLength, error); /* Otherwise, we are failures -- it was binary and the length didnt match! if (dataLength != field->length && field->type == NITF_BINARY) */ nitf_Error_init(error, "Invalid data length", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return NITF_FAILURE; }
NITFAPI(NITF_BOOL) nitf_Field_setString(nitf_Field * field, const char *str, nitf_Error * error) { nitf_Uint32 strLen; /* Length of input string */ /* Check the field type */ if (field->type == NITF_BINARY) { nitf_Error_init(error, "Type for string set for field can not be binary", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return (NITF_FAILURE); } /* Transfer and pad result (check for correct characters) */ strLen = strlen(str); /* if it's resizable and a different length, we resize */ if (field->resizable && strLen != field->length) { if (!nitf_Field_resizeField(field, strLen, error)) return NITF_FAILURE; } if (strLen > field->length) { nitf_Error_init(error, "Value for field is too long", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return (NITF_FAILURE); } if (field->type == NITF_BCS_A) { if (!isBCSA(str, strLen, error)) return (NITF_FAILURE); copyAndFillSpaces(field, str, strLen, error); } else { if (!isBCSN(str, strLen, error)) return (NITF_FAILURE); copyAndFillZeros(field, str, strLen, error); } return (NITF_SUCCESS); }
NITFAPI(NITF_BOOL) nitf_Field_setInt64(nitf_Field * field, nitf_Int64 number, nitf_Error * error) { char numberBuffer[20]; /* Holds converted number */ nitf_Uint32 numberLen; /* Length of converted number string */ /* Check the field type */ if (field->type == NITF_BINARY) { nitf_Error_init(error, "Integer set for binary field ", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return (NITF_FAILURE); } /* Convert thte number to a string */ NITF_SNPRINTF(numberBuffer, 20, "%lld", number); numberLen = strlen(numberBuffer); /* if it's resizable and a different length, we resize */ if (field->resizable && numberLen != field->length) { if (!nitf_Field_resizeField(field, numberLen, error)) return NITF_FAILURE; } if (numberLen > field->length) { nitf_Error_init(error, "Value for BCS_N field is too long", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); return (NITF_FAILURE); } /* Transfer and pad result */ if (field->type == NITF_BCS_N) copyAndFillZeros(field, numberBuffer, numberLen, error); else copyAndFillSpaces(field, numberBuffer, numberLen, error); return (NITF_SUCCESS); }
NITFAPI(nitf_Field *) nitf_Field_construct(size_t length, nitf_FieldType type, nitf_Error * error) { nitf_Field *field = NULL; if (length == 0) { nitf_Error_initf(error, NITF_CTXT, NITF_ERR_INVALID_PARAMETER, "Cannot create field of size 0"); goto CATCH_ERROR; } field = (nitf_Field *) NITF_MALLOC(sizeof(nitf_Field)); if (!field) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } field->type = type; field->raw = NULL; field->length = 0; /* this gets set by resizeField */ field->resizable = 1; /* set to 1 so we can use the resize code */ if (!nitf_Field_resizeField(field, length, error)) goto CATCH_ERROR; field->resizable = 0; /* set to 0 - the default value */ return field; CATCH_ERROR: if (field) nitf_Field_destruct(&field); return NULL; }
NITFAPI(NITF_BOOL) nitf_Field_setReal(nitf_Field * field, const char *type, NITF_BOOL plus, double value, nitf_Error *error) { nitf_Uint32 precision; /* Format precision */ nitf_Uint32 bufferLen; /* Length of buffer */ char *buffer; /* Holds intermediate and final results */ char fmt[64]; /* Format used */ /* Check type */ if ( (strcmp(type, "f") != 0) && (strcmp(type, "e") != 0) && (strcmp(type, "E") != 0)) { nitf_Error_initf(error, NITF_CTXT, NITF_ERR_INVALID_PARAMETER, "Invalid conversion type %s", type); return(NITF_FAILURE); } /* Allocate buffer used to build value */ /* The 64 covers the puncuation and exponent and is overkill */ bufferLen = field->length * 2 + 64; buffer = NITF_MALLOC(bufferLen + 1); if (buffer == NULL) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return(NITF_FAILURE); } /* Try a precision that will be too large and then adjust it based on the length of what you get. This results in a left justified string with the maximum number of decmal places. What you are actually figuring is the number of digits in the whole part of the number. */ precision = field->length; /* Must be too big */ if (plus) NITF_SNPRINTF(fmt, 64, "%%+-1.%dl%s", precision, type); else NITF_SNPRINTF(fmt, 64, "%%-1.%dl%s", precision, type); NITF_SNPRINTF(buffer, bufferLen + 1, fmt, value); bufferLen = strlen(buffer); /* if it's resizable and a different length, we resize */ if (field->resizable && bufferLen != field->length) { if (!nitf_Field_resizeField(field, bufferLen, error)) return NITF_FAILURE; } if (bufferLen > field->length) { if (precision > bufferLen - field->length) precision -= bufferLen - field->length; else precision = 0; if (plus) NITF_SNPRINTF(fmt, 64, "%%+-1.%dl%s", precision, type); else NITF_SNPRINTF(fmt, 64, "%%-1.%dl%s", precision, type); NITF_SNPRINTF(buffer, bufferLen + 1, fmt, value); } if (!nitf_Field_setRawData(field, buffer, field->length, error)) { NITF_FREE(buffer); return(NITF_FAILURE); } NITF_FREE(buffer); return(NITF_SUCCESS); }