home *** CD-ROM | disk | FTP | other *** search
- /****************************** LOAD FUNCTIONS ******************************/
- /*--- Load checks: determines if the file type is valid, and if so deletes
- the old file, then claims memory as necessary. ---*/
- static BOOL hp11_load_check(int filetype, hp11_data *d, int size)
- {
- /* Check that the application is not loading from itself */
- if (d->whandle == hp11_save_window)
- {
- werr(0, "You cannot save from hp11 into itself");
- return FALSE;
- }
-
- /* Check validity of file type */
- if (filetype != hp11_drawtype)
- {
- werr(0, "You can only load Draw files");
- return FALSE;
- }
-
- /* Remove any old file */
- hp11_dispose(d);
-
- /* Allocate new memory */
- if (flex_alloc((flex_ptr) &d->diagram.data, size))
- {
- return TRUE;
- }
- else
- {
- werr(0, "Memory could not be allocated for the new diagram");
- return FALSE;
- }
- }
-
- /*--- Handle loading errors. ---*/
- static void hp11_load_error(hp11_data *d)
- {
- /* Dispose of the diagram, and clear the name */
- hp11_dispose(d);
- strcpy(d->file, hp11_null_name);
- }
-
- /*--- Complete the loading process. ---*/
- static void hp11_load_finish(hp11_data *d, int size)
- {
- draw_error error;
-
- /* Verify the file */
- if (draw_verify_diag(&d->diagram, &error))
- {
- /* Record length */
- d->diagram.length = size;
- d->loaded = TRUE;
-
- /* Redraw the diagram */
- hp11_force_redraw(d);
- }
- else
- {
- /* Say there was an error. Strictly, we should say what it was as laid down
- in drawfdiag.h */
- werr(0, "Error in verifying Draw file");
- hp11_load_error(d);
- }
- }
-
-
- /*--- Read the size of a file. Return 0 if not a file. ---*/
- static 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, "File not found"); return 0;
- case 2: werr(0, "You cannot load a directory"); return 0;
- }
-
- if ((file.loadaddr & 0xFFF00000) == 0xFFF00000)
- return file.start;
- else
- {
- werr(0, "File cannot be loaded");
- return 0;
- }
- }
-
- /*--- Do the work of reading the file. Return TRUE if successful. ---*/
- static BOOL hp11_load(hp11_data *d)
- {
- os_filestr file;
-
- file.action = 255; /* Load named file into memory */
- file.name = d->file;
- file.loadaddr = (int)d->diagram.data; /* Where to load file */
- file.execaddr = 0; /* Load at address flag */
-
- return (wimpt_complain(os_file(&file)) == 0);
- }
-
- /*--- Load from a file. ---*/
- static void hp11_load_file(hp11_data *d)
- {
- char *filename;
- int size;
-
- /* Fetch the type and name of the file */
- int filetype = xferrecv_checkinsert(&filename);
-
- /* Check the name and make a permanent copy */
- if (strlen(filename) > hp11_max_name)
- {
- werr(0, "File name is too long");
- }
- else
- {
- strcpy(d->file, filename);
-
- /* Get size of file (and check it is a file) */
- size = hp11_filesize(d->file);
-
- /* Check file type, get rid of old memory and allocate new */
- if (size > 0 && hp11_load_check(filetype, d, size))
- {
- visdelay_begin();
-
- if (hp11_load(d))
- hp11_load_finish(d, size);
- else
- hp11_load_error(d);
-
- visdelay_end();
- }
- }
-
- /* Indicate load is completed */
- xferrecv_insertfileok();
- }
-
- /*--- Pointer to the diagram being loaded via ram transfer. ---*/
- static draw_diag *hp11_load_block;
-
- /*--- Buffer processor used during RAM load. ---*/
- static BOOL hp11_ram_loader(char **buffer, int *size)
- {
- /* Record the number of bytes loaded so far */
- hp11_load_block->length += *size;
-
- /* Attempt to extend the diagram */
- if (flex_extend((flex_ptr) &hp11_load_block->data,
- hp11_load_block->length + hp11_buffer_extension) == 0)
- return FALSE; /* Flex failed */
-
- /* Advance the buffer pointer by size of full block and set size to the
- amount of memory just allocated. */
- *buffer += *size;
- *size = hp11_buffer_extension;
- return TRUE;
- }
-
- /*--- Load via RAM transfer. ---*/
- static void hp11_load_ram(hp11_data *d)
- {
- 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)
- {
-
-
- /Check file type, get rid of old memory amd allocate new */
- if (hp11_load_check(filetype, d, estsize))
- {
- int final_size;
-
- /* Clear the file name */
- strcpy(d->file, hp11_null_name);
-
- visdelay_begin();
-
- /* Initiate the load */
- hp11_load_block = &d->diagram;
- if (final_size = xferrecv_doimport(d->diagram.data, estsize,
- hp11_ram_loader), final_size >= 0)
- hp11_load_finish(d, d->diagram.length + final_size);
- else
- hp11_load_error(d);
-
- visdelay_end();
- }
- }
- else /* Filetype of -1 indicates we should try to load via a file */
- {
- hp11_load_file(d);
- }
- }