home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************
- *
- * DISKFILE.C
- *
- **************************************************************/
-
- #include <acs.h> /* because of NULL, Ax_malloc() */
- #include <tos.h> /* because of Fopen(), ... */
- #include <aes.h> /* because of form_alert() */
- #include <ext.h> /* because of findfirst(), struct ffblk */
- #include <string.h> /* because of strcat(), ... */
- #include <acs_plus.pif> /* for use of self-built library */
- #include <diskfile.pif>
- #include "diskfile.ah"
-
-
- int load_file( char path[], long *size, void *(*RAM_file) )
- {
- struct ffblk fileRec ; /* structure for file informations */
- int handle ;
-
- /*** init output ***/
- *size = 0 ;
- *RAM_file = NULL ;
-
- /*** get file size ***/
- if ( findfirst(path, &fileRec, 0 /* all attributes allowed */) < 0 )
- return(path_not_found) ;
-
- /*** allocate RAM ***/
- *size = fileRec.ff_fsize ;
- *RAM_file = My_alloc(*size) ;
- if (!*RAM_file) { alert_str(FILE_TOO_LARGE, "") ; return(file_too_large) ; }
-
- /*** open file, load into RAM, and close file ***/
- switch ( handle = Fopen(path, FO_READ) )
- {
- case -33: alert_str(PATH_NOT_FOUND, "") ; return(path_not_found) ;
- case -35: alert_str(NO_MORE_HANDLE, "") ; return(no_more_handle) ;
- case -36: alert_str(ACCESS_DENIED, "") ; return(access_denied) ;
- default :
- /* good case: load file into RAM */
- Fread(handle, *size, *RAM_file) ;
- if ( Fclose(handle) == -37 ) { alert_str(FILE_NOT_CLOSED, path) ; return(file_not_closed) ; }
- else return(file_loaded) ;
- }
- }
-
-
- void unload_file(void *RAM_file)
- { My_free(RAM_file) ; }
-
-
- unsigned int get_file_name(char whole_path[], char path[],
- char file[], char title[])
- {
- int button ;
- int success ;
-
- file[0] = 0 ;
- if (_GemParBlk.global[0] >= 0x0140)
- /*** TOS version 1.04 or higher has the fsel_exinput function ***/
- success = fsel_exinput(path, file, &button, title) ;
- else
- {
- /*** TOS < 1.04 does not have the fsel_exinput() function, ***/
- /*** simulate the title by an alert box. ***/
- if ( title && title[0] )
- {
- /*** title is not empty ***/
- alert_str(GET_FILE_NAME, title) ;
- success = fsel_input(path, file, &button) ;
- }
- }
- if (!button || !file[0])
- /*** CANCEL selected or filename empty ***/
- return(no_file_selected) ;
- if (!success)
- /*** error during file select occurred ***/
- return(file_select_error) ;
- /*** take away the file mask (e.g. *.*) ***/
- strcpy(whole_path, path) ;
- *(strrchr(whole_path, '\\') + 1) = 0 ;
- strcat(whole_path, file) ;
- return(valid_file_selected) ;
- }
-
-
- int Fcopy(char *output_name, char *input_name)
- {
- long long_handle, length ;
- int handle ;
- void *buffer ;
-
- /*** read file content into buffer ***/
- long_handle = Fopen(input_name, FO_READ) ;
- if (long_handle < 0) return -1 ;
- handle = (int)long_handle ;
- length = filelength(handle) ;
- buffer = My_alloc(length) ;
- if (!buffer) { Fclose(handle) ; return -1 ; }
- Fread(handle, length, buffer ) ;
- Fclose(handle) ;
-
- /*** write buffer content into file ***/
- long_handle = Fopen(output_name, FO_WRITE) ;
- if (long_handle < 0)
- {
- /*** if not yet existing, create it ***/
- long_handle = Fcreate(output_name, 0 /* normal file */) ;
- if (long_handle < 0) { My_free(buffer) ; return -1 ; }
- }
- handle = (int)long_handle ;
- Fwrite(handle, length, buffer) ;
- Fclose(handle) ;
- My_free(buffer) ;
- return 0 ;
- }