home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / RiscOS / APP / DEVS / FORTH / BEETLE / BEETLE.ZIP / Beetle / saveobj.c < prev    next >
C/C++ Source or Header  |  1997-04-22  |  1KB  |  56 lines

  1. /* SAVEOBJ.C
  2.  
  3.     Vrsn  Date   Comment
  4.     ----|-------|---------------------------------------------------------------
  5.     0.00 24feb95
  6.     0.01 26feb95 Added code to ensure that if length is ridiculously large the
  7.                  correct error is definitely returned (odd things might have
  8.                  happened before).
  9.  
  10.     Reuben Thomas
  11.  
  12.  
  13.     The interface call save_object(file, address, length) : integer.
  14.  
  15. */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <setjmp.h>
  21. #include "beetle.h"     /* main header */
  22.  
  23.  
  24. jmp_buf env;
  25.  
  26. void put(int c, FILE *fp)
  27. {
  28.     int t = putc(c, fp);
  29.     if (t == EOF) longjmp(env, -3);
  30. }
  31.  
  32. int save_object(FILE *file, CELL *address, UCELL length)
  33. {
  34.     char magic[] = "BEETLE\0";
  35.     int i, err = 0;
  36.  
  37.     if (length > (UCELL)0x3fffffff) { err = -1; goto error; }
  38.  
  39.     if ((err = setjmp(env)) == 0) {
  40.         if (address - (CELL *)M0 > MEMORY ||
  41.             ((address - (CELL *)M0) + length) * CELL_W > MEMORY) {
  42.             err = -1;
  43.             goto error;
  44.         }
  45.  
  46.         for (i = 0; i < 7; i++) put(magic[i], file);
  47.         put(ENDISM, file);
  48.         for (i = 0; i < CELL_W; i++) put(((BYTE *)&length)[i], file);
  49.         for (i = 0; i < length * CELL_W; i++) put(((BYTE *)address)[i], file);
  50.  
  51.         return 0;
  52.     } else {
  53. error:  return err;
  54.     }
  55. }
  56.