📄 file.c
字号:
/* * Name: file_all * Purpose: To save all windows and exit the editor. * Date: August 21, 1997 * Author: Jason Hood * Passed: window: pointer to current window * Notes: if any window couldn't be saved, all windows will remain open. * * 020911: possibly save the workspace. */int file_all( TDE_WIN *window ){register TDE_WIN *wp;int rc = OK; if (g_status.output_redir) { error( WARNING, g_display.end_line, main22 ); return( ERROR ); } if (un_copy_line( window->ll, window, TRUE, TRUE ) == ERROR) return( ERROR ); for (wp=g_status.window_list; wp != NULL; wp=wp->next) { if (save_file( wp ) == ERROR) rc = ERROR; } if (rc == OK) { if (mode.auto_save_wksp) rc = save_workspace( window ); g_status.stop = TRUE; } return( rc );}/* * Name: save_all * Purpose: To save all files. * Date: July 15, 2004 * Author: Jason Hood * Passed: window: pointer to current window * Notes: scratch and read-only windows are not saved. */int save_all( TDE_WIN *window ){register TDE_WIN *wp;int rc = OK; if (un_copy_line( window->ll, window, TRUE, TRUE ) == ERROR) return( ERROR ); for (wp = g_status.window_list; wp != NULL; wp = wp->next) { if (!wp->file_info->scratch && !wp->file_info->read_only) if (save_file( wp ) == ERROR) rc = ERROR; } return( rc );}/* * Name: save_file * Purpose: To save the current file to disk. * Date: June 5, 1991 * Passed: window: pointer to current window * Notes: If anything goes wrong, then the modified flag is set. * If the file is saved successfully, then modified flag is * cleared. * * jmh 021023: allow read-only files to be saved. If the file is read-only, * prompt to overwrite; if marked read-only, prompt to save; * if both, only prompt to overwrite. * jmh 030318: added SaveUntouched function to preserve the timestamp. */int save_file( TDE_WIN *window ){register file_infos *file;char *fname;int rc = ERROR;line_list_ptr temp_ll;int prompt_line;fattr_t fattr;int restore_fattr = FALSE;int save = TRUE; file = window->file_info; if (file->modified == FALSE) return( OK ); if (un_copy_line( window->ll, window, TRUE, TRUE ) == ERROR) return( ERROR ); fname = file->file_name; prompt_line = window->bottom_line; /* * the file is marked read-only, but is not itself read-only, * so ask to save. * jmh 030730: likewise with files loaded as scratch. */ if (file->scratch == -1 || (file->read_only && file_exists( fname ) != READ_ONLY)) { combine_strings( line_out, main7a, fname, (file->read_only) ? utils7c : utils7d ); if (get_yn( line_out, prompt_line, R_PROMPT | R_ABORT ) != A_YES) save = FALSE; } /* * see if there was a file name - if not, then make the user * supply one. */ if (*fname == '\0') { if (save) rc = save_as_file( window ); } else { /* * save the file. prompt to overwrite if it is read-only. */ if (file_exists( fname ) == READ_ONLY) { get_fattr( fname, &fattr );#if !defined( __UNIX__ ) /* * saving a file sets the archive attribute, so make sure * it is set when the read-only attribute is restored. */ fattr |= ARCHIVE;#endif if (change_mode( fname, prompt_line ) == ERROR) save = FALSE; else restore_fattr = TRUE; } if (save) { rc = write_to_disk( window, fname ); if (restore_fattr) set_fattr( fname, fattr ); if (rc != ERROR) { file->modified = FALSE; file->new_file = FALSE; /* * (re)set the timestamp */ if (g_status.command == SaveUntouched) set_ftime( fname, &file->ftime ); else get_ftime( fname, &file->ftime ); /* * clear the dirty flags */ if (rc == OK) { temp_ll = file->line_list->next; for (; temp_ll->len != EOF; temp_ll=temp_ll->next) temp_ll->type &= ~DIRTY; file->dirty = GLOBAL; } } } } if (!save && (g_status.command == File || g_status.command == FileAll)) /* * should closing a read-only file abandon changes? */ if (get_yn( utils12, prompt_line, R_PROMPT | R_ABORT ) == A_YES) rc = OK; return( rc );}/* * Name: save_backup * Purpose: To save a backup copy of the current file to disk. * Date: June 5, 1991 * Passed: window: pointer to current window */int save_backup( TDE_WIN *window ){ /* * set up file name */ return( write_to_disk( window, window->file_info->backup_fname ) );}/* * Name: save_as_file * Purpose: To save the current file to disk, but under a new name. * Date: June 5, 1991 * Passed: window: pointer to current window * * jmh: April 29, 1998 - renames and saves the current file. * jmh 980803: default to current name. * jmh 020722: store full path. * jmh 030318: added SaveTo function to keep the current name (like the * original SaveAs). * jmh 040716: moved SaveTo to block_write (to allow appending). */int save_as_file( TDE_WIN *window ){int prompt_line;int rc;register TDE_WIN *win; /* put window pointer in a register */register file_infos *file;char answer[PATH_MAX+2];line_list_ptr temp_ll;int newtitle;char *title; win = window; if (un_copy_line( win->ll, win, TRUE, TRUE ) == ERROR) return( ERROR ); /* * read in name */ prompt_line = win->bottom_line; strcpy( answer, win->file_info->file_name ); /* * new file name: */ if ((rc = get_name( utils9, prompt_line, answer, &h_file )) > 0) { /* * make sure it is OK to overwrite any existing file */ rc = file_exists( answer ); if (rc == SUBDIRECTORY) rc = ERROR; else if (rc == OK || rc == READ_ONLY) { /* * overwrite existing file? */ if (get_yn( utils10, prompt_line, R_PROMPT | R_ABORT ) != A_YES || change_mode( answer, prompt_line ) == ERROR) rc = ERROR; } else /* * file name does not exist. take a chance on a valid file name. */ rc = OK; if (rc != ERROR) rc = write_to_disk( win, answer ); if (rc == OK) { file = win->file_info; newtitle = (win->title && strcmp( win->title, file->file_name ) == 0); get_full_path( answer, file->file_name ); make_backup_fname( file ); if (newtitle) { title = my_strdup( file->file_name ); if (title != NULL) { my_free( win->title ); win->title = title; } /* else stick with the old name or turn the title off? */ } show_window_fname( win ); file->modified = FALSE; file->new_file = FALSE; file->scratch = 0; get_ftime( answer, &file->ftime ); temp_ll = file->line_list->next; for (; temp_ll->len != EOF; temp_ll=temp_ll->next) temp_ll->type &= ~DIRTY; file->dirty = GLOBAL; } } return( rc );}/* * Name: make_backup_fname * Purpose: change the file name's extension to "bak" * Date: January 6, 1992 * Passed: file: information allowing access to the current file * * jmh 010604: if we're in UNIX, or djgpp with LFN, simply append ".bak" (eg: * file.h -> file.h.bak; file.c -> file.c.bak). * jmh 021021: determine the pointer to the file name itself. * jmh 031117: use it store the current directory of a scratch window, so * browsing can find the right files. */void make_backup_fname( file_infos *file ){char *p;int len;#if defined( __MSDOS__ )char temp[PATH_MAX+2];int i;#endif p = file->file_name; if (g_status.command == Execute) *p = '\0'; /* * if this is a new file or scratch then don't create a backup - can't * backup a nonexisting file. */ if (file->new_file || *p == '\0') { file->backed_up = TRUE; if (*p == '\0') get_current_directory( file->backup_fname ); /* * find the file name extension if it exists */ } else { assert( strlen( p ) < PATH_MAX );#if defined( __UNIX__ ) || defined( __WIN32__ ) join_strings( file->backup_fname, p, ".bak" );#else# if defined( __DJGPP__ ) if (_USE_LFN) join_strings( file->backup_fname, p, ".bak" ); else {# endif strcpy( temp, p ); len = strlen( temp ); for (i=len, p=temp + len; i>=0; i--) { /* * we found the '.' extension character. get out */ if (*p == '.') break; /* * we found the drive or directory character. no extension so * set the pointer to the end of file name string. */ else if (*p == '/' || *p == ':') { p = temp + len; break; /* * we're at the beginning of the string - no '.', drive, or directory * char was found. set the pointer to the end of file name string. */ } else if (i == 0) { p = temp + len; break; } --p; } strcpy( p, ".bak" ); strcpy( file->backup_fname, temp );# if defined( __DJGPP__ ) } /* else !_USE_LFN */# endif#endif /* __UNIX__ || __WIN32__ */ } p = file->file_name; if (*p == '\0') file->fname = p; else { len = strlen( p ) - 2; /* start from the second-last character */ p += len; for (; len >= 0 && *p != '/'#if !defined( __UNIX__ ) && *p != ':'#endif ; --p, --len) ; file->fname = p + 1; }}/* * Name: write_to_disk * Purpose: To write file from memory to disk * Date: June 5, 1991 * Passed: window: pointer to current window * fname: file name to save on disk */int write_to_disk( TDE_WIN *window, char *fname ){register file_infos *file;int rc;int prompt_line;DISPLAY_BUFF; file = window->file_info; prompt_line = window->bottom_line; SAVE_LINE( prompt_line ); /* * saving */ combine_strings( line_out, utils6, fname, "'" ); set_prompt( line_out, prompt_line ); if ((rc = hw_save( fname, file, 1L, file->length, NOTMARKED )) == ERROR) { if (!CEH_ERROR) { if (file_exists( fname ) == READ_ONLY) /* * file is read only */ combine_strings( line_out, main7a, fname, utils7b ); else /* * cannot write to */ combine_strings( line_out, utils8, fname, "'" ); error( WARNING, prompt_line, line_out );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -