home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / h / hp11 / Amiga_Code / c / loadfrag < prev    next >
Encoding:
Text File  |  1992-05-07  |  4.9 KB  |  204 lines

  1. /****************************** LOAD FUNCTIONS ******************************/
  2. /*--- Load checks: determines if the file type is valid, and if so deletes
  3.       the old file, then claims memory as necessary. ---*/
  4. static BOOL hp11_load_check(int filetype, hp11_data *d, int size)
  5. {
  6.   /* Check that the application is not loading from itself */
  7.   if (d->whandle == hp11_save_window)
  8.   {
  9.     werr(0, "You cannot save from hp11 into itself");
  10.     return FALSE;
  11.   }
  12.  
  13.   /* Check validity of file type */
  14.   if (filetype != hp11_drawtype)
  15.   {
  16.     werr(0, "You can only load Draw files");
  17.     return FALSE;
  18.   }
  19.  
  20.   /* Remove any old file */
  21.   hp11_dispose(d);
  22.  
  23.   /* Allocate new memory */
  24.   if (flex_alloc((flex_ptr) &d->diagram.data, size))
  25.   {
  26.     return TRUE;
  27.   }
  28.   else
  29.   {
  30.     werr(0, "Memory could not be allocated for the new diagram");
  31.     return FALSE;
  32.   }
  33. }
  34.  
  35. /*--- Handle loading errors. ---*/
  36. static void hp11_load_error(hp11_data *d)
  37. {
  38.   /* Dispose of the diagram, and clear the name */
  39.   hp11_dispose(d);
  40.   strcpy(d->file, hp11_null_name);
  41. }
  42.  
  43. /*--- Complete the loading process. ---*/
  44. static void hp11_load_finish(hp11_data *d, int size)
  45. {
  46.   draw_error error;
  47.  
  48.   /* Verify the file */
  49.   if (draw_verify_diag(&d->diagram, &error))
  50.   {
  51.     /* Record length */
  52.     d->diagram.length = size;
  53.     d->loaded         = TRUE;
  54.  
  55.     /* Redraw the diagram */
  56.     hp11_force_redraw(d);
  57.   }
  58.   else
  59.   {
  60.     /* Say there was an error. Strictly, we should say what it was as laid down
  61.        in drawfdiag.h */
  62.     werr(0, "Error in verifying Draw file");
  63.     hp11_load_error(d);
  64.   }
  65. }
  66.  
  67.  
  68. /*--- Read the size of a file. Return 0 if not a file. ---*/
  69. static int hp11_filesize(char *filename)
  70. {
  71.   os_filestr file;
  72.  
  73.   /* Read the size of the file */
  74.   file.action = 5;    /* Get catalogue info */
  75.   file.name   = filename;
  76.   if (wimpt_complain(os_file(&file)) != 0) return 0;
  77.  
  78.   switch (file.action)
  79.   {
  80.     case 0: werr(0, "File not found");              return 0;
  81.     case 2: werr(0, "You cannot load a directory"); return 0;
  82.   }
  83.  
  84.   if ((file.loadaddr & 0xFFF00000) == 0xFFF00000)
  85.     return file.start;
  86.   else
  87.   {
  88.     werr(0, "File cannot be loaded");
  89.     return 0;
  90.   }
  91. }
  92.  
  93. /*--- Do the work of reading the file. Return TRUE if successful. ---*/
  94. static BOOL hp11_load(hp11_data *d)
  95. {
  96.   os_filestr file;
  97.  
  98.   file.action   = 255;                   /* Load named file into memory */
  99.   file.name     = d->file;
  100.   file.loadaddr = (int)d->diagram.data;  /* Where to load file */
  101.   file.execaddr = 0;                     /* Load at address flag */
  102.  
  103.   return (wimpt_complain(os_file(&file)) == 0);
  104. }
  105.  
  106. /*--- Load from a file. ---*/
  107. static void hp11_load_file(hp11_data *d)
  108. {
  109.   char      *filename;
  110.   int       size;
  111.  
  112.   /* Fetch the type and name of the file */
  113.   int filetype = xferrecv_checkinsert(&filename);
  114.  
  115.   /* Check the name and make a permanent copy */
  116.   if (strlen(filename) > hp11_max_name)
  117.   {
  118.     werr(0, "File name is too long");
  119.   }
  120.   else
  121.   {
  122.     strcpy(d->file, filename);
  123.  
  124.     /* Get size of file (and check it is a file) */
  125.     size = hp11_filesize(d->file);
  126.  
  127.     /* Check file type, get rid of old memory and allocate new */
  128.     if (size > 0 && hp11_load_check(filetype, d, size))
  129.     {
  130.       visdelay_begin();
  131.  
  132.       if (hp11_load(d))
  133.         hp11_load_finish(d, size);
  134.       else
  135.         hp11_load_error(d);
  136.  
  137.       visdelay_end();
  138.     }
  139.   }
  140.  
  141.   /* Indicate load is completed */
  142.   xferrecv_insertfileok();
  143. }
  144.  
  145. /*--- Pointer to the diagram being loaded via ram transfer. ---*/
  146. static draw_diag *hp11_load_block;
  147.  
  148. /*--- Buffer processor used during RAM load. ---*/
  149. static BOOL hp11_ram_loader(char **buffer, int *size)
  150. {
  151.   /* Record the number of bytes loaded so far */
  152.   hp11_load_block->length += *size;
  153.  
  154.   /* Attempt to extend the diagram */
  155.   if (flex_extend((flex_ptr) &hp11_load_block->data,
  156.                   hp11_load_block->length + hp11_buffer_extension) == 0)
  157.     return FALSE;  /* Flex failed */
  158.  
  159.   /* Advance the buffer pointer by size of full block and set size to the
  160.      amount of memory just allocated. */
  161.   *buffer += *size;
  162.   *size    = hp11_buffer_extension;
  163.   return TRUE;
  164. }
  165.  
  166. /*--- Load via RAM transfer. ---*/
  167. static void hp11_load_ram(hp11_data *d)
  168. {
  169.   int estsize;   /* Estimate size of file */
  170.  
  171.   /* Get the type of the file being loaded, and an estimate of its size */
  172.   int filetype = xferrecv_checkimport(&estsize);
  173.  
  174.   if (filetype != -1)
  175.   {
  176.  
  177.  
  178. /Check file type, get rid of old memory amd allocate new */
  179.     if (hp11_load_check(filetype, d, estsize))
  180.     {
  181.       int final_size;
  182.  
  183.       /* Clear the file name */
  184.       strcpy(d->file, hp11_null_name);
  185.  
  186.       visdelay_begin();
  187.  
  188.       /* Initiate the load */
  189.       hp11_load_block = &d->diagram;
  190.       if (final_size = xferrecv_doimport(d->diagram.data, estsize,
  191.                                          hp11_ram_loader), final_size >= 0)
  192.         hp11_load_finish(d, d->diagram.length + final_size);
  193.       else
  194.         hp11_load_error(d);
  195.  
  196.       visdelay_end();
  197.     }
  198.   }
  199.   else /* Filetype of -1 indicates we should try to load via a file */
  200.   {
  201.     hp11_load_file(d);
  202.   }
  203. }
  204. O