home *** CD-ROM | disk | FTP | other *** search
- /* Title: -> c.LoadSave
- *
- */
-
- /*
- #include "os.h"
- #include "win.h"
- #include "event.h"
- #include "res.h"
- #include "saveas.h"
- #include "template.h"
- */
- #include "menu.h"
- #include "dbox.h"
- #include "werr.h"
- #include "wimp.h"
- #include "wimpt.h"
- #include "heap.h"
- #include "visdelay.h"
- #include "xferrecv.h"
- #include "xfersend.h"
-
- #include <stdlib.h>
- #include <string.h>
-
- #include "types.h"
- #include "hp11.h"
- #include "arc_hp11.h"
- #include "translate.h"
- #include "loadsave.h"
- #include "io.h"
-
- /******************************** GLOBAL DATA *******************************/
-
- /* File names to use when none are loaded */
- char *hp11_null_prog_name = "HP11_Prog";
- char *hp11_null_stat_name = "HP11_Stat";
- char *hp11_null_disp_name = "HP11_Disp";
-
- struct Regs hp11buffer;
- hp11_data hp11_dat = {
- -1,
- "HP11_Prog",
- "HP11_Stat",
- "HP11_Disp"
- };
- int hp11_load_block_length;
- char *hp11_load_block;
-
- /****************************** SAVE FUNCTIONS ******************************/
- /*---------------------------------------------------------------------------*/
- /* Status */
- /* ------- */
- /*--- Save status, including program, to a file, also handles printing ---*/
- BOOL hp11_stat_saver(char *filename, void *handle)
- {
- hp11_data *d = (hp11_data *)handle;
- os_filestr file;
- BOOL ok;
-
- /* Check file name */
- if (strlen(filename) > HP11_MAX_NAME)
- {
- werr(0, "This file name is too long");
- return FALSE;
- }
-
- visdelay_begin();
-
- /* Save the entire file */
- file.action = 0x0a; /* Save block and date stamp file */
- file.name = filename;
- file.loadaddr = HP11_STATUSTYPE; /* File type */
- file.start = (int)&hp11r;
- file.end = (int)&hp11r + sizeof(struct Regs);
- ok = (wimpt_complain(os_file(&file)) == 0);
-
- if (ok)
- {
- /* Copy the file name */
- strcpy(d->stat, filename);
- }
-
- visdelay_end();
- return ok;
- }
-
- /*--- Send data via ram ---*/
- BOOL hp11_stat_sender(void *handle, int *maxbuf)
- {
- int to_send; /* Data still to send in bytes */
- char *buffer; /* pointer to next bit of data to send */
-
- visdelay_begin();
- to_send = sizeof(struct Regs);
- buffer = (char*)&hp11r;
-
- /* Send data a block at a time */
- while (to_send > 0)
- {
- /* Find the size of the block to send */
- int length = (to_send > *maxbuf) ? *maxbuf : to_send;
-
- /* The data save itself */
- if (!xfersend_sendbuf(buffer, length)) return FALSE;
- else
- {
- /* Advance to next block */
- buffer += length;
- to_send -= length;
- }
- }
-
- visdelay_end();
- return TRUE;
- }
- /*---------------------------------------------------------------------------*/
- /* Program */
- /* ------- */
-
- /*--- Save data to a file, also handles printing ---*/
- BOOL hp11_prog_saver(char *filename, void *handle)
- {
- hp11_data *d = (hp11_data *)handle;
- os_filestr file;
- BOOL ok;
- int file_length;
- char *old_buffer;
-
- /* Check file name */
- if (strlen(filename) > HP11_MAX_NAME)
- {
- werr(0, "This file name is too long; don't forget it includes the full path name");
- return FALSE;
- }
-
- visdelay_begin();
- old_buffer = code_buffer =
- (char*)heap_alloc((unsigned int)(lastIns+1)*MAXCODE+TITLELEN);
- strcat(strcat(strcpy(code_buffer, "Listing of -> "), filename), "\n\n");
- file_length = strlen(code_buffer);
- code_buffer += file_length;
- file_length += hp11_encode();
-
- /* Save the entire file */
- file.action = 0x0a; /* Save block and date stamp file */
- file.name = filename;
- file.loadaddr = HP11_PROGTYPE; /* File type */
- file.start = (int)old_buffer;
- file.end = (int)old_buffer + file_length;
- ok = (wimpt_complain(os_file(&file)) == 0);
-
- if (ok)
- {
- /* Copy the file name */
- strcpy(d->prog, filename);
- }
-
- visdelay_end();
- heap_free((void *)old_buffer);
- return ok;
- }
-
- /*--- Send data via ram ---*/
- BOOL hp11_prog_sender(void *handle, int *maxbuf)
- {
- hp11_data *d = (hp11_data *)handle;
- int to_send; /* Data still to send in bytes*/
- char *old_buffer;
-
- visdelay_begin();
- old_buffer = code_buffer =
- (char*)heap_alloc((unsigned int)(lastIns+1)*MAXCODE+TITLELEN);
- strcat(strcat(strcpy(code_buffer, "Listing of -> "), d->prog), "\n[NB - file name may be out-of-date]\n\n");
- to_send = strlen(code_buffer);
- code_buffer += to_send;
- to_send += hp11_encode();
- code_buffer = old_buffer;
-
- /* Send data a block at a time */
- while (to_send > 0)
- {
- /* Find the size of the block to send */
- int length = (to_send > *maxbuf) ? *maxbuf : to_send;
-
- /* The data save itself */
- if (!xfersend_sendbuf(code_buffer, length)) return FALSE;
- else
- {
- /* Advance to next block */
- code_buffer += length;
- to_send -= length;
- }
- }
-
- visdelay_end();
- heap_free((void *)old_buffer);
- return TRUE;
- }
-
- /*---------------------------------------------------------------------------*/
- /* Display */
- /* ------- */
- /*--- Save display to a file, also handles printing ---*/
- BOOL hp11_disp_saver(char *filename, void *handle)
- {
- hp11_data *d = (hp11_data *)handle;
- os_filestr file;
- BOOL ok;
- char *buffer;
- int buf_length;
-
- /* Check file name */
- if (strlen(filename) > HP11_MAX_NAME)
- {
- werr(0, "The file name (including path) is too long");
- return FALSE;
- }
-
- visdelay_begin();
- buffer = NbStr(X);
- buf_length = strlen(buffer);
-
- /* Save the entire file */
- file.action = 0x0a; /* Save block and date stamp file */
- file.name = d->disp;
- file.loadaddr = HP11_DISPLAYTYPE; /* File type */
- file.start = (int)buffer;
- file.end = (int)buffer + buf_length;
- ok = (wimpt_complain(os_file(&file)) == 0);
-
- if (ok)
- {
- /* Copy the file name */
- strcpy(d->disp, filename);
- }
-
- visdelay_end();
- return ok;
- }
-
- /*--- Send data via ram ---*/
- BOOL hp11_disp_sender(void *handle, int *maxbuf)
- {
- int to_send; /* Data still to send in bytes */
- char *buffer; /* pointer to next bit of data to send */
-
- visdelay_begin();
- buffer = NbStr(X);
- to_send = strlen(buffer);
-
- /* Send data a block at a time */
- while (to_send > 0)
- {
- /* Find the size of the block to send */
- int length = (to_send > *maxbuf) ? *maxbuf : to_send;
-
- /* The data save itself */
- if (!xfersend_sendbuf(buffer, length)) return FALSE;
- else
- {
- /* Advance to next block */
- buffer += length;
- to_send -= length;
- }
- }
- visdelay_end();
- return TRUE;
- }
-
- /****************************** LOAD FUNCTIONS ******************************/
- /*---------------------------------------------------------------------------*/
- /* from file */
- /* --------- */
- void hp11_load_file(void *handle)
- {
- hp11_data *d = (hp11_data *)handle;
- char *filename;
- int size;
-
- int filetype = xferrecv_checkinsert(&filename); /* Fetch the type and */
- /* name of the file */
- if (strlen(filename) > HP11_MAX_NAME) /* Check name and make copy */
- {
- werr(0, "File name is too long -- how did that happen?");
- }
- else
- {
- strcpy(d->stat, filename);
- size = hp11_filesize(d->stat); /* Get file size; check it is a file*/
- if (size > 0 && hp11_load_check(filetype, d)) /* Check file type */
- {
- visdelay_begin();
- hp11_load(d);
- visdelay_end();
- Disp();
- }
- }
- xferrecv_insertfileok(); /* Indicate load is completed */
- }
-
- /*--- Read the size of a file. Return 0 if not a file. ---*/
- int hp11_filesize(char *filename)
- {
- os_filestr file;
-
- /* Read the size of the file */
- file.action = 5; /* Get catalogue info */
- file.name = filename;
- if (wimpt_complain(os_file(&file)) != 0) return 0;
- switch (file.action)
- {
- case 0: werr(0, "I can't find that file"); return 0;
- case 2: werr(0, "Hey! that's a directory; I can't load that"); return 0;
- }
- if ((file.loadaddr & 0xFFF00000) == 0xFFF00000)
- return file.start;
- else
- {
- werr(0, "Sorry, there's something wrong with this file");
- return 0;
- }
- }
-
- /*--- Load checks: determines if the file type is valid ---*/
- BOOL hp11_load_check(int filetype, hp11_data *d)
- {
- /* Check that the application is not loading from itself */
- if (d->hp11_w == hp11_save_window)
- {
- werr(0, "You can't save from HP11 into itself !");
- return FALSE;
- }
-
- /* Check validity of file type */
- if (filetype != HP11_STATUSTYPE)
- {
- werr(0, "I can't load this; it's the wrong type of file");
- return FALSE;
- }
- return TRUE;
- }
-
- /*--- Do the work of reading the file. Return TRUE if successful. ---*/
- BOOL hp11_load(hp11_data *d)
- {
- os_filestr file;
-
- file.action = 255; /* Load named file into memory */
- file.name = d->stat;
- file.loadaddr = (int)&hp11r; /* Where to load file */
- file.execaddr = 0; /* Load at address flag */
-
- return (wimpt_complain(os_file(&file)) == 0);
- }
-
- /*---------------------------------------------------------------------------*/
- /* from ram */
- /* --------- */
- void hp11_load_ram(void *handle)
- {
- hp11_data *d = (hp11_data *)handle;
- int estsize; /* Estimate size of file */
-
- /* Get the type of the file being loaded, and an estimate of its size */
- int filetype = xferrecv_checkimport(&estsize);
- if (filetype != -1)
- {
- if (hp11_load_check(filetype, d)) /* Check file type */
- {
- int final_size;
-
- strcpy(d->stat, hp11_null_stat_name); /* Clear the file name */
- visdelay_begin();
- hp11_load_block = (char*)&hp11buffer; /* Initiate the load */
- hp11_load_block_length = 0;
- if (final_size = xferrecv_doimport(hp11_load_block, estsize,
- hp11_ram_loader), final_size == sizeof(struct Regs))
- hp11r = hp11buffer;
- }
- }
- else /* Filetype of -1 indicates we should try to load via a file */
- hp11_load_file(handle);
- visdelay_end();
- Disp();
- }
- /*--- Buffer processor used during RAM load. ---*/
- BOOL hp11_ram_loader(char **buffer, int *size)
- {
- /* Record the number of bytes loaded so far */
- hp11_load_block_length += *size;
- if(hp11_load_block_length > sizeof(struct Regs))
- return FALSE;
- else
- {
- *buffer += *size;
- *size = hp11_load_block_length - sizeof(struct Regs);
- }
- return TRUE;
- }
-
-
- void hp11_bar_load(wimp_eventstr *e, void *handle)
- {
- switch (e->e)
- {
- case wimp_ESEND:
- case wimp_ESENDWANTACK:
- {
- switch (e->data.msg.hdr.action)
- {
- case wimp_MDATASAVE: /* import data */
- hp11_load_ram(handle);
- break;
- case wimp_MDATALOAD: /* insert data */
- case wimp_MDATAOPEN:
- hp11_load_file(handle);
- break;
- default: break; /* ignore other messages */
- }
- break;
- }
- default: break;
- }
- if (!hp11_window_open)
- {
- dbox_showstatic(hp11_dbox_handle);
- dbox_raw_eventhandler(hp11_dbox_handle, hp11_dbox_raw_handler, &hp11_dat);
- dbox_eventhandler(hp11_dbox_handle, hp11_dbox_handler, &hp11_dat);
- hp11_window_open = TRUE;
- wimpt_complain(wimp_get_icon_info(hp11_win_handle,HP11_DISPLAY,&hp11_disp)); if (hp11_show_stack)
- {
- dbox_showstatic(hp11_stack_dbox_handle);
- }
- }
- Disp();
- }
-