Logo Search packages:      
Sourcecode: c2n version File versions  Download package

unsigned encode_oric ( FILE *  in,
pulse_error_t  err,
pulse_w_t  wr,
unsigned  begin,
unsigned  intra 
)

Oric pulse stream encoder

Parameters:
in the data stream
err the error reporter
wr the pulse stream writer
begin number of initial synchronization pulses
intra number of intra-block synchronization pulses
Returns:
number of blocks written; 0 on failure

current block

program block header

program data buffer

program length in bytes

received data length

null-terminated program name

length of the name

Definition at line 110 of file oric_e.c.

References block, enc_wr, encBlock(), encodeByte(), encodeSync(), LongBlock, Pause, ShortBlock, and Unexpected.

Referenced by main().

{
  /** current block */
  unsigned block;
  enc_wr = wr;

  for (block = 0;; block++) {
    /** program block header */
    static char header[9];
    int c;
    /* read and skip synchronization bytes */
    while ((c = fgetc (in)) == 0x16);
    if (c == EOF)
      break;
    else if (c != 0x24) {
      if (!err || !(*err) (Unexpected, block, c))
      break;
      continue;
    }
    if (1 != fread (header, sizeof header, 1, in))
      break;
    else {
      /** program data buffer */
      char* buf;
      /** program length in bytes */
      unsigned length;
      /** received data length */
      unsigned readlength;
      /** null-terminated program name */
      static char name[16];
      /** length of the name */
      unsigned namelength;

      length = ((unsigned) (unsigned char) header[5]) |
      ((unsigned) (unsigned char) header[4]) << 8;
      length -= ((unsigned) (unsigned char) header[7]) |
      ((unsigned) (unsigned char) header[6]) << 8;
      length &= 0xffff;
      length++;

      /* read the file name */
      for (namelength = 0; namelength < sizeof name; )
      if ((c = fgetc (in)) == EOF || !(name[namelength++] = c))
        break;

      if (c == EOF)
      break;
      else if (c) {
      if (err)
        (*err) (LongBlock, block, sizeof header + sizeof name);
      break;
      }
      
      if (!(buf = malloc (length))) {
      fputs ("out of memory\n", stderr);
      break;
      }

      block++;
      readlength = fread (buf, 1, length, in);

      if (readlength != length) {
      if (!err || (*err) (ShortBlock, block, length - readlength)) {
        free (buf);
        return block;
      }
      }

      if (block)
      (*enc_wr) (Pause);

      /* write the synchronization sequence followed by the header */
      for (c = begin; c--; )
      encodeByte (0x16);
      encodeByte (0x24);
      encBlock (header, sizeof header);
      encBlock (name, namelength);
      encodeSync (intra);
      encBlock (buf, readlength);
      free (buf);
      encodeSync (4);
    }
  }

  (*enc_wr) (Pause);
  return block;
}


Generated by  Doxygen 1.6.0   Back to index