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

  1. /* Title: ->  c.LoadSave
  2.  *
  3.  */
  4.  
  5. /*
  6. #include "os.h"
  7. #include "win.h"
  8. #include "event.h"
  9. #include "res.h"
  10. #include "saveas.h"
  11. #include "template.h"
  12. */
  13. #include "menu.h"
  14. #include "dbox.h"
  15. #include "werr.h"
  16. #include "wimp.h"
  17. #include "wimpt.h"
  18. #include "heap.h"
  19. #include "visdelay.h"
  20. #include "xferrecv.h"
  21. #include "xfersend.h"
  22.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #include "types.h"
  27. #include "hp11.h"
  28. #include "arc_hp11.h"
  29. #include "translate.h"
  30. #include "loadsave.h"
  31. #include "io.h"
  32.  
  33. /******************************** GLOBAL DATA *******************************/
  34.  
  35. /* File names to use when none are loaded */
  36. char *hp11_null_prog_name = "HP11_Prog";
  37. char *hp11_null_stat_name = "HP11_Stat";
  38. char *hp11_null_disp_name = "HP11_Disp";
  39.  
  40. struct Regs hp11buffer;
  41. hp11_data hp11_dat = {
  42. -1,
  43. "HP11_Prog",
  44. "HP11_Stat",
  45. "HP11_Disp"
  46. };
  47. int hp11_load_block_length;
  48. char *hp11_load_block;
  49.  
  50. /****************************** SAVE FUNCTIONS ******************************/
  51. /*---------------------------------------------------------------------------*/
  52. /* Status */
  53. /* ------- */
  54. /*--- Save status, including program, to a file, also handles printing ---*/
  55. BOOL hp11_stat_saver(char *filename, void *handle)
  56. {
  57.   hp11_data *d = (hp11_data *)handle;
  58.   os_filestr  file;
  59.   BOOL        ok;
  60.  
  61.   /* Check file name */
  62.   if (strlen(filename) > HP11_MAX_NAME)
  63.   {
  64.     werr(0, "This file name is too long");
  65.     return FALSE;
  66.   }
  67.  
  68.   visdelay_begin();
  69.  
  70.   /* Save the entire file */
  71.   file.action   = 0x0a;                    /* Save block and date stamp file */
  72.   file.name     = filename;
  73.   file.loadaddr = HP11_STATUSTYPE;           /* File type */
  74.   file.start    = (int)&hp11r;
  75.   file.end      = (int)&hp11r + sizeof(struct Regs);
  76.   ok = (wimpt_complain(os_file(&file)) == 0);
  77.  
  78.   if (ok)
  79.   {
  80.     /* Copy the file name */
  81.     strcpy(d->stat, filename);
  82.   }
  83.  
  84.   visdelay_end();
  85.   return ok;
  86. }
  87.  
  88. /*--- Send data via ram ---*/
  89. BOOL hp11_stat_sender(void *handle, int *maxbuf)
  90. {
  91.   int   to_send;                              /* Data still to send in bytes */
  92.   char *buffer;                       /* pointer to next bit of data to send */
  93.  
  94.   visdelay_begin();
  95.   to_send = sizeof(struct Regs);
  96.   buffer = (char*)&hp11r;
  97.  
  98.   /* Send data a block at a time */
  99.   while (to_send > 0)
  100.   {
  101.     /* Find the size of the block to send */
  102.     int length = (to_send > *maxbuf) ? *maxbuf : to_send;
  103.  
  104.     /* The data save itself */
  105.     if (!xfersend_sendbuf(buffer, length)) return FALSE;
  106.     else
  107.     {
  108.       /* Advance to next block */
  109.       buffer         += length;
  110.       to_send        -= length;
  111.     }
  112.   }    
  113.  
  114.   visdelay_end();
  115.   return TRUE;
  116. }
  117. /*---------------------------------------------------------------------------*/
  118. /* Program */
  119. /* ------- */
  120.  
  121. /*--- Save data to a file, also handles printing ---*/
  122. BOOL hp11_prog_saver(char *filename, void *handle)
  123. {
  124.   hp11_data *d = (hp11_data *)handle;
  125.   os_filestr  file;
  126.   BOOL        ok;
  127.   int         file_length;
  128.   char       *old_buffer;
  129.  
  130.   /* Check file name */
  131.   if (strlen(filename) > HP11_MAX_NAME)
  132.   {
  133.     werr(0, "This file name is too long; don't forget it includes the full path name");
  134.     return FALSE;
  135.   }
  136.  
  137.   visdelay_begin();
  138.   old_buffer = code_buffer = 
  139.                  (char*)heap_alloc((unsigned int)(lastIns+1)*MAXCODE+TITLELEN);
  140.   strcat(strcat(strcpy(code_buffer, "Listing of -> "), filename), "\n\n");
  141.   file_length = strlen(code_buffer);
  142.   code_buffer += file_length;
  143.   file_length += hp11_encode();
  144.  
  145.   /* Save the entire file */
  146.   file.action   = 0x0a;                    /* Save block and date stamp file */
  147.   file.name     = filename;
  148.   file.loadaddr = HP11_PROGTYPE;           /* File type */
  149.   file.start    = (int)old_buffer;
  150.   file.end      = (int)old_buffer + file_length;
  151.   ok = (wimpt_complain(os_file(&file)) == 0);
  152.  
  153.   if (ok)
  154.   {
  155.     /* Copy the file name */
  156.     strcpy(d->prog, filename);
  157.   }
  158.  
  159.   visdelay_end();
  160.   heap_free((void *)old_buffer);
  161.   return ok;
  162. }
  163.  
  164. /*--- Send data via ram ---*/
  165. BOOL hp11_prog_sender(void *handle, int *maxbuf)
  166. {
  167.   hp11_data *d = (hp11_data *)handle;
  168.   int   to_send;                         /* Data still to send in bytes*/
  169.   char *old_buffer;
  170.  
  171.   visdelay_begin();
  172.   old_buffer = code_buffer = 
  173.                  (char*)heap_alloc((unsigned int)(lastIns+1)*MAXCODE+TITLELEN);
  174.   strcat(strcat(strcpy(code_buffer, "Listing of -> "), d->prog), "\n[NB - file name may be out-of-date]\n\n");
  175.   to_send = strlen(code_buffer);
  176.   code_buffer += to_send;
  177.   to_send += hp11_encode();
  178.   code_buffer = old_buffer;
  179.  
  180.   /* Send data a block at a time */
  181.   while (to_send > 0)
  182.   {
  183.     /* Find the size of the block to send */
  184.     int length = (to_send > *maxbuf) ? *maxbuf : to_send;
  185.  
  186.     /* The data save itself */
  187.     if (!xfersend_sendbuf(code_buffer, length)) return FALSE;
  188.     else
  189.     {
  190.       /* Advance to next block */
  191.       code_buffer    += length;
  192.       to_send        -= length;
  193.     }
  194.   }    
  195.  
  196.   visdelay_end();
  197.   heap_free((void *)old_buffer);
  198.   return TRUE;
  199. }
  200.  
  201. /*---------------------------------------------------------------------------*/
  202. /* Display */
  203. /* ------- */
  204. /*--- Save display to a file, also handles printing ---*/
  205. BOOL hp11_disp_saver(char *filename, void *handle)
  206. {
  207.   hp11_data *d = (hp11_data *)handle;
  208.   os_filestr  file;
  209.   BOOL        ok;
  210.   char       *buffer;
  211.   int         buf_length;
  212.  
  213.   /* Check file name */
  214.   if (strlen(filename) > HP11_MAX_NAME)
  215.   {
  216.     werr(0, "The file name (including path) is too long");
  217.     return FALSE;
  218.   }
  219.  
  220.   visdelay_begin();
  221.   buffer = NbStr(X);
  222.   buf_length = strlen(buffer);
  223.  
  224.   /* Save the entire file */
  225.   file.action   = 0x0a;                    /* Save block and date stamp file */
  226.   file.name     = d->disp;
  227.   file.loadaddr = HP11_DISPLAYTYPE;           /* File type */
  228.   file.start    = (int)buffer;
  229.   file.end      = (int)buffer + buf_length;
  230.   ok = (wimpt_complain(os_file(&file)) == 0);
  231.  
  232.   if (ok)
  233.   {
  234.     /* Copy the file name */
  235.     strcpy(d->disp, filename);
  236.   }
  237.  
  238.   visdelay_end();
  239.   return ok;
  240. }
  241.  
  242. /*--- Send data via ram ---*/
  243. BOOL hp11_disp_sender(void *handle, int *maxbuf)
  244. {
  245.   int   to_send;                              /* Data still to send in bytes */
  246.   char *buffer;                       /* pointer to next bit of data to send */
  247.  
  248.   visdelay_begin();
  249.   buffer = NbStr(X);
  250.   to_send = strlen(buffer);
  251.  
  252.   /* Send data a block at a time */
  253.   while (to_send > 0)
  254.   {
  255.     /* Find the size of the block to send */
  256.     int length = (to_send > *maxbuf) ? *maxbuf : to_send;
  257.  
  258.     /* The data save itself */
  259.     if (!xfersend_sendbuf(buffer, length)) return FALSE;
  260.     else
  261.     {
  262.       /* Advance to next block */
  263.       buffer         += length;
  264.       to_send        -= length;
  265.     }
  266.   }    
  267.   visdelay_end();
  268.   return TRUE;
  269. }
  270.  
  271. /****************************** LOAD FUNCTIONS ******************************/
  272. /*---------------------------------------------------------------------------*/
  273. /* from file */
  274. /* --------- */
  275. void hp11_load_file(void *handle)
  276.   {
  277.   hp11_data *d = (hp11_data *)handle;
  278.   char      *filename;
  279.   int       size;
  280.  
  281.   int filetype = xferrecv_checkinsert(&filename);      /* Fetch the type and */
  282.                                                          /* name of the file */
  283.   if (strlen(filename) > HP11_MAX_NAME)          /* Check name and make copy */
  284.     {
  285.     werr(0, "File name is too long -- how did that happen?");
  286.     }
  287.   else
  288.     {
  289.     strcpy(d->stat, filename);
  290.     size = hp11_filesize(d->stat);       /* Get file size; check it is a file*/
  291.     if (size > 0 && hp11_load_check(filetype, d))         /* Check file type */
  292.       {
  293.       visdelay_begin();
  294.       hp11_load(d);
  295.       visdelay_end();
  296.       Disp();
  297.       }
  298.     }
  299.   xferrecv_insertfileok();                     /* Indicate load is completed */
  300.   }
  301.  
  302. /*--- Read the size of a file. Return 0 if not a file. ---*/
  303. int hp11_filesize(char *filename)
  304.   {
  305.   os_filestr file;
  306.  
  307.   /* Read the size of the file */
  308.   file.action = 5;                                  /* Get catalogue info */
  309.   file.name   = filename;
  310.   if (wimpt_complain(os_file(&file)) != 0) return 0;
  311.   switch (file.action)
  312.     {
  313.     case 0: werr(0, "I can't find that file");                     return 0;
  314.     case 2: werr(0, "Hey! that's a directory; I can't load that"); return 0;
  315.     }
  316.   if ((file.loadaddr & 0xFFF00000) == 0xFFF00000)
  317.     return file.start;
  318.   else
  319.     {
  320.     werr(0, "Sorry, there's something wrong with this file");
  321.     return 0;
  322.     }
  323.   }
  324.  
  325. /*--- Load checks: determines if the file type is valid ---*/
  326. BOOL hp11_load_check(int filetype, hp11_data *d)
  327. {
  328.   /* Check that the application is not loading from itself */
  329.     if (d->hp11_w == hp11_save_window)
  330.     {
  331.     werr(0, "You can't save from HP11 into itself !");
  332.     return FALSE;
  333.     }
  334.  
  335.   /* Check validity of file type */
  336.   if (filetype != HP11_STATUSTYPE)
  337.     {
  338.     werr(0, "I can't load this; it's the wrong type of file");
  339.     return FALSE;
  340.     }
  341.   return TRUE;
  342. }
  343.  
  344. /*--- Do the work of reading the file. Return TRUE if successful. ---*/
  345. BOOL hp11_load(hp11_data *d)
  346.   {
  347.   os_filestr file;
  348.  
  349.   file.action   = 255;                   /* Load named file into memory */
  350.   file.name     = d->stat;
  351.   file.loadaddr = (int)&hp11r;           /* Where to load file */
  352.   file.execaddr = 0;                     /* Load at address flag */
  353.  
  354.   return (wimpt_complain(os_file(&file)) == 0);
  355.   }
  356.  
  357. /*---------------------------------------------------------------------------*/
  358. /* from ram */
  359. /* --------- */
  360. void hp11_load_ram(void *handle)
  361.   {
  362.   hp11_data *d = (hp11_data *)handle;
  363.   int estsize;                                      /* Estimate size of file */
  364.  
  365.   /* Get the type of the file being loaded, and an estimate of its size */
  366.   int filetype = xferrecv_checkimport(&estsize);
  367.   if (filetype != -1)
  368.     {
  369.     if (hp11_load_check(filetype, d))      /* Check file type */
  370.       {
  371.       int final_size;
  372.  
  373.       strcpy(d->stat, hp11_null_stat_name);     /* Clear the file name */
  374.       visdelay_begin();
  375.       hp11_load_block = (char*)&hp11buffer;             /* Initiate the load */
  376.       hp11_load_block_length = 0;
  377.       if (final_size = xferrecv_doimport(hp11_load_block, estsize,
  378.                    hp11_ram_loader), final_size == sizeof(struct Regs))
  379.         hp11r = hp11buffer;
  380.       }
  381.     }
  382.   else          /* Filetype of -1 indicates we should try to load via a file */
  383.     hp11_load_file(handle);
  384.   visdelay_end();
  385.   Disp();
  386. }
  387. /*--- Buffer processor used during RAM load. ---*/
  388. BOOL hp11_ram_loader(char **buffer, int *size)
  389. {
  390.   /* Record the number of bytes loaded so far */
  391.   hp11_load_block_length += *size;
  392.   if(hp11_load_block_length > sizeof(struct Regs))
  393.     return FALSE;
  394.   else
  395.     {
  396.     *buffer += *size;
  397.     *size    = hp11_load_block_length - sizeof(struct Regs);
  398.     }
  399.   return TRUE;
  400. }
  401.  
  402.  
  403. void hp11_bar_load(wimp_eventstr *e, void *handle)
  404.   {
  405.   switch (e->e) 
  406.     {
  407.     case wimp_ESEND:
  408.     case wimp_ESENDWANTACK:
  409.       {
  410.       switch (e->data.msg.hdr.action)
  411.         {
  412.         case wimp_MDATASAVE:   /* import data */
  413.           hp11_load_ram(handle);
  414.           break;
  415.         case wimp_MDATALOAD:   /* insert data */
  416.         case wimp_MDATAOPEN:
  417.           hp11_load_file(handle);
  418.           break;
  419.         default: break;        /* ignore other messages */
  420.         }
  421.       break;
  422.       }
  423.     default: break;
  424.     }  
  425.   if (!hp11_window_open)
  426.     {
  427.     dbox_showstatic(hp11_dbox_handle);
  428.     dbox_raw_eventhandler(hp11_dbox_handle, hp11_dbox_raw_handler, &hp11_dat); 
  429.     dbox_eventhandler(hp11_dbox_handle, hp11_dbox_handler, &hp11_dat);
  430.     hp11_window_open = TRUE;
  431.     wimpt_complain(wimp_get_icon_info(hp11_win_handle,HP11_DISPLAY,&hp11_disp));    if (hp11_show_stack)
  432.       {
  433.       dbox_showstatic(hp11_stack_dbox_handle);
  434.       }
  435.     }
  436.   Disp();
  437.   }
  438.