Logo Search packages:      
Sourcecode: dballe version File versions

dba_err crex_encoder_encode ( bufrex_raw  in,
dba_rawmsg  out 
)

Encode a CREX message

Parameters:
in The bufrex_raw with the data to encode
out The dba_rawmsg that will hold the encoded data
Returns:
The error indicator for the function.
See also:
dba_err

Definition at line 132 of file crex_encoder.c.

References bufrex_raw_get_datadesc(), crex_encoder_encode(), dba_error_consistency, DBA_RUN_OR_GOTO, DBA_RUN_OR_RETURN, DBA_VAR_F, DBA_VAR_X, and DBA_VAR_Y.

Referenced by bufrex_raw_encode(), and crex_encoder_encode().

{
      dba_err err = DBA_OK;
      bufrex_opcode cur;
      encoder e = NULL;
      bufrex_opcode ops = NULL;

      DBA_RUN_OR_RETURN(encoder_create(&e));
      e->in = in;
      e->out = out;

      /* Initialise the encoder with the list of variables to encode */
      e->nextvar = e->in->vars;
      e->vars_left = e->in->vars_count;

      /* Encode section 0 */
      DBA_RUN_OR_GOTO(fail, encoder_raw_append(e, "CREX++\r\r\n", 9));

      /* Encode section 1 */
      e->sec1_start = e->out->len;

      DBA_RUN_OR_GOTO(fail, encoder_raw_appendf(e, "T%02d%02d%02d A%03d%03d",
                        e->in->opt.crex.master_table,
                        e->in->edition,
                        e->in->opt.crex.table,
                        e->in->type,
                        e->in->subtype));

      DBA_RUN_OR_GOTO(fail, bufrex_raw_get_datadesc(e->in, &ops));
      if (ops == NULL)
      {
            /* Generate data description section from the variable list, if it was missing */
            int i;
            TRACE("Regenerating datadesc\n");
            for (i = 0; i < e->vars_left; i++)
                  DBA_RUN_OR_GOTO(fail, bufrex_raw_append_datadesc(e->in, dba_var_code(e->nextvar[i])));

            /* Reread the descriptors */
            DBA_RUN_OR_GOTO(fail, bufrex_raw_get_datadesc(e->in, &ops));
      } else {
            TRACE("Reusing datadesc\n");
      }
      
      /* Encode the data descriptor section */
      for (cur = ops; cur != NULL; cur = cur->next)
      {
            char prefix;
            switch (DBA_VAR_F(cur->val))
            {
                  case 0: prefix = 'B'; break;
                  case 1: prefix = 'R'; break;
                  case 2: prefix = 'C'; break;
                  case 3: prefix = 'D'; break;
                  default: prefix = '?'; break;
            }

            /* Don't put delayed replication counts in the data section */
            if (DBA_VAR_F(cur->val) == 0 && DBA_VAR_X(cur->val) == 31 && DBA_VAR_Y(cur->val) < 3)
                  continue;

            DBA_RUN_OR_GOTO(fail, encoder_raw_appendf(e, " %c%02d%03d",
                              prefix, DBA_VAR_X(cur->val), DBA_VAR_Y(cur->val)));
      }
      if (e->has_check_digit)
      {
            DBA_RUN_OR_GOTO(fail, encoder_raw_append(e, " E", 2));
            e->expected_check_digit = 1;
      }
      DBA_RUN_OR_GOTO(fail, encoder_raw_append(e, "++\r\r\n", 5));

      /* Encode section 2 */
      e->sec2_start = e->out->len;
      while (e->vars_left > 0)
      {
            TRACE("Start encoding a subsection\n");
            DBA_RUN_OR_GOTO(fail, bufrex_opcode_prepend(&(e->ops), ops));

            DBA_RUN_OR_GOTO(fail, encode_data_section(e));

            if (e->vars_left > 0)
                  DBA_RUN_OR_GOTO(fail, encoder_raw_append(e, "+\r\r\n", 4));
            else
                  DBA_RUN_OR_GOTO(fail, encoder_raw_append(e, "++\r\r\n", 5));

            if (e->ops != NULL)
            {
                  err = dba_error_consistency("not all operators have been encoded");
                  goto fail;
            }
      }

      /* Encode section 3 */
      e->sec3_start = e->out->len;
      /* Nothing to do, as we have no custom section */

      /* Encode section 4 */
      e->sec4_start = e->out->len;
      DBA_RUN_OR_RETURN(encoder_raw_append(e, "7777\r\r\n", 7));

      e->out->encoding = CREX;

      return dba_error_ok();

fail:
      if (e != NULL)
            encoder_delete(e);
      return err;
}


Generated by  Doxygen 1.6.0   Back to index