/*--------------------------------------------------------------- Routine : overwrite_file_cb Purpose : This routine is called if the user presses the OK button on the dialog querying the overwriting of an existing file. It attempts to use the registered action procedure, and if it returns with an error, posts an error dialog. Otherwise it calls the end thread procedure to free up any allocated memory that is no longer needed. ---------------------------------------------------------------*/ static void overwrite_file_cb(fstruct *client_data, int called_from) { char *s; if ( (*client_data->action_procedure)( client_data->data_type, client_data->filename ) == FILE_ERROR ) { s = filename_from_pathname( client_data->filename ); client_data->file_error_string = (char *)strgrow( client_data->file_error_string, s ); if(called_from == FD_FTA) { /* routine called from FTA code */ FTAFramePostError(client_data->file_error_string, FTA_SAVE_TITLE); } else if(called_from == FD_PED) { /* routine called from FTA code */ PEDFramePostError(client_data->file_error_string, FTA_SAVE_TITLE); } strfree( s ); }; end_thread_cb(client_data); } /* overwrite_file_cb */
/*--------------------------------------------------------------- Routine : file_open_error Purpose : This routine is called if a file open error is detected. ---------------------------------------------------------------*/ static void file_open_error(fstruct *client_data, int called_from ) { char *error_message; error_message = strsave( client_data->file_error_string ); error_message = strgrow( error_message, filename_from_pathname( client_data->filename ) ); client_data->file_dialog_title = strgrow( client_data->file_dialog_title, "Open " ); client_data->file_dialog_title = strgrow( client_data->file_dialog_title, client_data->title ); printf("FileDialogs: file_open_error\n"); if(called_from == FD_FTA) { /* routine called from FTA code */ FTAFramePostError(error_message, client_data->file_dialog_title); } else if(called_from == FD_PED) { /* routine called from PED code */ PEDFramePostError(error_message, client_data->file_dialog_title); } } /* file_open_error */
/*--------------------------------------------------------------- Routine : GetStringField Purpose : Reads the next string field from the file. If no field exists, or EOF is encountered, returns NULL, else returns a pointer to a the string. Leaves the file pointer at the beginning of the next field (i.e., skips all delimiters). ---------------------------------------------------------------*/ char * GetStringField( FILE *FromFile ) { char *InputString; char InputChar; char c; char str[ 2 ]; /* int i = 0; */ /* Get characters, maintaining whitespace, up to the next delimiter */ /* records ending with a delimiter actually end ';'<LF> */ /* so should beware of this */ printf("Record_Io.c: In GetStringField\n"); /* Get first character of stream... */ InputChar = GetChar( FromFile, FALSE ); if ( InputChar == EOF ) { printf("Record_Io.c: First char is EOF\n"); return NULL; } c = InputChar; /* If first character is delimiter, field must be empty... */ if ( c == DELIMITER ) { printf("Record_Io.c: First char is DELIMITER\n"); return NULL; } /* skip leading <LF>... */ while ( c == '\n' ) { InputChar = GetChar( FromFile, FALSE ); if ( InputChar == EOF ) { printf("Record_Io.c: Skipping leading <LFs> First, but char is EOF\n"); return NULL; } c = InputChar; } /* Read string field... */ InputString = strsave( NULL ); str[ 1 ] = '\0'; while ( c != DELIMITER ) { str[ 0 ] = c; InputString = strgrow( InputString, str ); InputChar = GetChar( FromFile, FALSE ); if ( InputChar == EOF ) { InputString = strgrow( InputString, '\0' ); if(InputString == NULL) { printf("Record_Io.c: Returned string = NULL\n"); } else { printf("Record_Io.c: Returned string = %s\n", InputString); } return InputString; } c = InputChar; } InputString = strgrow( InputString, '\0' ); if(InputString == NULL) { printf("Record_Io.c: Returned string = NULL\n"); } else { printf("Record_Io.c: Returned string = %s\n", InputString); } return InputString; } /* GetStringField */
char* strappend(char* str, char* append) { return strcat( strgrow(str,strlen(append)), append); }
/*--------------------------------------------------------------- Routine : save_to_file Purpose : Routine called to save data to a file. This routine enforces the functionality required for file selection through dialogs. Sets up client data to be passed between routines in the save file thread. if filename given then if file exists then query if file is to be overwritten (overwrite if so) end if else if given but doesn't exist then save the data to the file end if end if ---------------------------------------------------------------*/ void save_to_file( char *filename, char *appln_title, char *title, char *filter, void *data_type, int (*save_data_procedure)(void *, char *), BOOL force_overwrite, int called_from ) { fstruct *client_data; char *s; if ( !fNewMemory( (void *)&client_data, sizeof( fstruct ) ) ) { exit( 1 ); } /* Set up the client data for use in callback */ client_data->action_procedure = save_data_procedure; client_data->title = title; client_data->filter = filter; client_data->data_type = data_type; client_data->file_error_string = strsave(SAVE_ERROR); client_data->file_dialog_title = strsave(appln_title); client_data->mode = W_OK; if ( !(filename == NULL) ) { /* Set up the filename in client data for use in callback */ client_data->filename = filename; /* if file exists and not forced overwrite then */ if ( file_exists( filename ) && !( force_overwrite ) ) { /* File exists, query overwrite */ s = filename_from_pathname( filename ); client_data->file_dialog_title = (char *)strgrow( client_data->file_dialog_title, s ); if(called_from == FD_FTA) { /* routine called from FTA code */ if(FTAFrameAskQuestion(FILE_EXISTS_QUESTION, client_data->file_dialog_title)) { overwrite_file_cb((fstruct *)client_data, FD_FTA); } } else if(called_from == FD_PED) { /* routine called from FTA code */ if(PEDFrameAskQuestion(FILE_EXISTS_QUESTION, client_data->file_dialog_title)) { overwrite_file_cb((fstruct *)client_data, FD_PED); } } /* Don't free client_data as the callback still needs it */ strfree( s ); } else { /* file given, does not exist, or is forced overwrite */ if ( (*save_data_procedure)(data_type, filename) == FILE_ERROR ) { s = filename_from_pathname( client_data->filename ); client_data->file_error_string = (char *)strgrow( client_data->file_error_string, s ); if(called_from == FD_FTA) { /* routine called from FTA code */ FTAFramePostError(client_data->file_error_string, client_data->file_dialog_title); } else if(called_from == FD_PED) { /* routine called from FTA code */ PEDFramePostError(client_data->file_error_string, client_data->file_dialog_title); } strfree( s ); end_thread_cb(client_data); } } /* end if file exists */ } else { /* NULL filename */ /* Get selection of filename and save using that name */ select_filename_and_act_cb(client_data); } } /* save_to_file */