def encode_value_length(value_length, writer): """Enocde the length of a value as a var_width length. This will be the smallest of a byte, short or int which can hold the length. Arguments: value_length: the length in buytes of the value object writer: the writer object to write the length to """ assert value_length >= 0 and value_length <= utils.MAX_INT if value_length <= utils.MAX_BYTE: writer.write(codecs.enc_byte(value_length)) elif value_length <= utils.MAX_SHORT: writer.write(codecs.enc_short(value_length)) else: writer.write(codecs.enc_int(value_length))
def encode(self, writer, taxonomy=None): """Encode a Field. This encodes Field Prefix, Type, Ordinal, Name, Data """ variable_width = 0 if self.type_.is_variable_sized: if self.is_type(types.FUDGEMSG_TYPE_ID): # sub-message, need to be taxonomy aware value_length = self.type_.calc_size(self.value, taxonomy) else: value_length = self.type_.calc_size(self.value) variable_width = bytes_for_value_length(value_length) ordinal = self.ordinal name = self.name if taxonomy and self.name: tax_ord = taxonomy.get_ordinal(self.name) if tax_ord: ordinal = tax_ord name = None prefix_byte = prefix.encode_prefix(not self.type_.is_variable_sized, \ variable_width, ordinal is not None, name is not None) # And write it out writer.write(chr(prefix_byte)) writer.write(codecs.enc_byte(self.type_.type_id)) if ordinal is not None: writer.write(codecs.enc_short(ordinal)) if name: assert len(name) <= utils.MAX_BYTE writer.write(codecs.enc_name(name)) if self.type_.is_variable_sized: encode_value_length(value_length, writer) if self.is_type(types.FUDGEMSG_TYPE_ID): self.value.encode(writer, taxonomy) else: writer.write(self.type_.encoder(self.value))