📄 file.c
字号:
len += t1; break; } else { t2 = READ_LENGTH - len; e = s + t2 - 1; t1 -= t2; } } else { t2 = (size_t)(e - s); t1 -= t2 + 1; if (t2) { if (e[-1] == '\r') { --t2; crlf = CRLF; } } else if (len) { if (g_status.line_buff[len-1] == '\r') { --len; crlf = CRLF; } } if (len) { if (len + t2 > READ_LENGTH) { t2 = READ_LENGTH - len; t1 += (size_t)(e - (s + t2 - 1)); e = s + t2 - 1; } } } /* * allocate space for relocatable array of line pointers * and allocate space for the line we just read. */ if (len) { temp_ll = new_line_text(g_status.line_buff, len + t2, 0, &rc); if (rc != ERROR) { my_memcpy( temp_ll->line + len, s, t2 ); len = 0; } } else temp_ll = new_line_text( (text_ptr)s, t2, 0, &rc ); if (rc != ERROR) { insert_node( fp, ll, temp_ll ); ll = temp_ll; } else { rc = show_file_2big( name, prompt_line ); break; } s = e + 1; } while (t1 != 0); } /* * we may have read all lines that end in '\n', but there may * be some residue after the last '\n'. ^Z is a good example. */ if (len && rc == OK) { temp_ll = new_line_text( g_status.line_buff, len, 0, &rc ); if (rc != ERROR) insert_node( fp, ll, temp_ll ); else rc = show_file_2big( name, prompt_line ); } *file_mode = crlf; } } /* * Restore input back to the keyboard for shelling. */ if (stream == stdin) {#if defined( __WIN32__ ) DWORD mode; SetStdHandle( STD_INPUT_HANDLE, conin ); GetConsoleMode( conin, &mode ); SetConsoleMode( conin, ENABLE_EXTENDED_FLAGS | ENABLE_MOUSE_INPUT | (mode & ENABLE_QUICK_EDIT) );#else freopen( STDFILE, "r", stdin );#endif g_status.input_redir = FALSE; } else if (stream != NULL) fclose( stream ); return( rc );}/* * Name: insert_node * Purpose: To insert a node into a double linked list * Date: December 1, 1992 * Passed: fp: pointer to file structure that owns the double linked list * current: pointer to current node in double linked list * new: pointer to new node to insert into double linked list * Notes: if the current list pointer is the last node in the list, insert * new node behind current node. * * jmh 031116: increase the file's length. */void insert_node( file_infos *fp, line_list_ptr current, line_list_ptr new ){ /* * standard double linked list insert */ if (current->next != NULL) { current->next->prev = new; new->next = current->next; current->next = new; new->prev = current; /* * if the current node is the NULL node, insert the new node behind current */ } else { new->next = current; current->prev->next = new; new->prev = current->prev; current->prev = new; } ++fp->length;}/* * Name: show_file_2big * Purpose: tell user we ran out of room loading file * Date: December 1, 1992 * Passed: name: name of disk file * line: line to display messages * Returns: WARNING * Notes: one or both of the malloc requests overflowed the heap. free the * dynamic if allocated. */int show_file_2big( char *name, int prompt_line ){ combine_strings( line_out, main7a, name, main10b ); error( WARNING, prompt_line, line_out ); return( WARNING );}/* * Name: backup_file * Purpose: To make a back-up copy of current file. * Date: June 5, 1991 * Passed: window: current window pointer */int backup_file( TDE_WIN *window ){char *old_line_buff;char *old_tabout_buff;int old_line_buff_len;int old_tabout_buff_len;int old_copied;int rc;file_infos *file; rc = OK; file = window->file_info; if (file->backed_up == FALSE && file->modified == TRUE) { old_line_buff = malloc( MAX_LINE_LENGTH ); old_tabout_buff = malloc( MAX_LINE_LENGTH ); if (old_line_buff != NULL && old_tabout_buff != NULL) { old_line_buff_len = g_status.line_buff_len; old_tabout_buff_len = g_status.tabout_buff_len; old_copied = g_status.copied; memcpy( old_line_buff, g_status.line_buff, MAX_LINE_LENGTH ); memcpy( old_tabout_buff, g_status.tabout_buff, MAX_LINE_LENGTH ); if ((rc = save_backup( window )) != ERROR) { file->backed_up = TRUE; set_ftime( file->backup_fname, &file->ftime ); } memcpy( g_status.line_buff, old_line_buff, MAX_LINE_LENGTH ); memcpy( g_status.tabout_buff, old_tabout_buff, MAX_LINE_LENGTH ); g_status.line_buff_len = old_line_buff_len; g_status.tabout_buff_len = old_tabout_buff_len; g_status.copied = old_copied; } else { error( WARNING, window->bottom_line, main4 ); rc = ERROR; } if (old_line_buff != NULL) free( old_line_buff ); if (old_tabout_buff != NULL) free( old_tabout_buff ); } return( rc );}/* * Name: edit_file * Purpose: To allocate space for a new file structure and set up some * of the relevant fields. * Date: June 5, 1991 * Modified: November 13, 1993, Frank Davis per Byrial Jensen * Passed: name: name of the file to edit * file_mode: BINARY or TEXT * bin_length: if opened in BINARY mode, length of binary lines * Returns: OK if file structure could be created * ERROR if out of memory * * Change: file->next_letter is initialized to 0 instead 'a' * * jmh: August 27, 1997 - if name is empty (ie. "") then read from stdin. * This allows TDE to be used in a pipe. * * jmh 980503: Use of get_full_path to store the filename. * jmh 991111: Honour EndOfLineStyle setting for new files. * jmh 010521: removed code to free undo and line lists (done in load_file()). * jmh 020724: record the binary length. * jmh 021020: read-only files are marked as read-only. * jmh 031202: free the memory from an unsuccessful grep. * jmh 050705: if file doesn't exist, grep should error, not create new. * jmh 050918: likewise if path doesn't exist (same as command line). */int edit_file( char *name, int file_mode, int bin_length ){int rc; /* return code */int existing;int line;int rcol;register file_infos *file; /* file structure for file belonging to new window */file_infos *fp;long found_line;long result_length;line_list_ptr ll;line_list_ptr temp_ll;int grep; grep = (g_status.command == DefineGrep || g_status.command == RepeatGrep); line = g_display.end_line; rc = OK; /* * allocate a file structure for the new file */ file = (file_infos *)calloc( 1, sizeof(file_infos) ); if (file == NULL) rc = ERROR; else { ll = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc ); if (rc == OK) { ll->line = NULL; ll->next = ll->prev = NULL; ll->type = 0; ll->len = EOF; file->undo_top = file->undo_bot = ll; ll = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc ); temp_ll = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc ); if (rc == OK) { ll->type = temp_ll->type = 0; ll->len = temp_ll->len = EOF; ll->line = temp_ll->line = NULL; ll->prev = temp_ll->next = NULL; ll->next = temp_ll; temp_ll->prev = ll; file->line_list = ll; file->line_list_end = temp_ll; } else { my_free( ll ); my_free( file->undo_top ); } } } if (rc == ERROR) { error( WARNING, line, main4 ); if (file != NULL) free( file ); return( ERROR ); } existing = TRUE; if (*name == '\0' && (g_status.command != ScratchWindow && !(g_status.search & SEARCH_RESULTS))) rc = load_file( name, file, &file_mode, bin_length, NULL ); else if (file_exists( name ) != ERROR) { if (!grep) rc = load_file( name, file, &file_mode, bin_length, NULL ); else { if (g_status.sas_defined) { rc = load_file( name, file, &file_mode, bin_length, NULL ); if (rc != ERROR) { found_line = 1L; rcol = 0; ll = file->line_list->next; result_length = -1; if (g_status.search & SEARCH_RESULTS) { get_full_path( name, file->file_name ); search_file = file; if (CB_G_LoadAll) result_length = (results_file) ? results_file->length : 0; } ll = (g_status.sas_search_type == BOYER_MOORE) ? search_forward( ll, &found_line, &rcol ) : regx_search_forward( ll, &found_line, &rcol ); if (ll == NULL) { if (result_length == -1 || results_file == NULL || result_length == results_file->length) { rc = ERROR; my_free_group( TRUE ); ll = file->line_list; while (ll != NULL) { temp_ll = ll->next; my_free( ll->line ); my_free( ll ); ll = temp_ll; } my_free_group( FALSE ); file->line_list = file->line_list_end = NULL; } } else { g_status.sas_rline = found_line; g_status.sas_rcol = rcol; g_status.sas_ll = ll; } } } else rc = ERROR; } } else { if (CEH_ERROR || (*name && grep)) rc = ERROR; else { existing = FALSE; if (file_mode == TEXT) file_mode = (mode.crlf != NATIVE) ? mode.crlf :#if defined( __UNIX__ ) LF;#else CRLF;#endif } } if (rc != ERROR) { /* * add file into list */ file->prev = NULL; file->next = NULL; if (g_status.file_list == NULL) g_status.file_list = file; else { if (g_status.current_file == NULL || (CB_G_LoadAll && (g_status.search & SEARCH_RESULTS) && results_file && results_file->prev == g_status.current_file)) { g_status.current_file = results_file; g_status.current_window = results_window; } fp = g_status.current_file; file->prev = fp; if (fp->next) fp->next->prev = file; file->next = fp->next; fp->next = file; } /* * set up all the info we need to know about a file. */ assert( file_mode == CRLF || file_mode == LF || file_mode == BINARY ); assert( strlen( name ) < PATH_MAX ); if (*name != '\0') { get_full_path( name, file->file_name ); get_fattr( name, &file->file_attrib ); get_ftime( name, &file->ftime ); file->scratch = g_option.scratch; } else if (g_status.command == ScratchWindow) { file->scratch = ++g_status.scratch_count; if (file->scratch == 1) strcpy( file->file_name + 1, scratch_file ); else sprintf( file->file_name+1, "%s%d", scratch_file, file->scratch-1 ); } else { file->scratch = -1; if (g_status.search & SEARCH_RESULTS) { file->file_name[1] = RESULTS; strcpy( file->file_name + 2, (char *)((g_status.search & SEARCH_REGX) ? regx.pattern : bm.pattern) ); } else strcpy( file->file_name + 1, pipe_file ); } file->block_type = NOTMARKED; file->block_br = file->block_er = 0L; file->block_bc = file->block_ec = 0; file->ref_count = 0; file->modified = FALSE; file->backed_up = FALSE; file->new_file = !existing; file->next_letter = 0; file->file_no = ++g_status.file_count; file->crlf = file_mode; file->read_only = (file_exists( name ) == READ_ONLY); file->binary_len = bin_length; file->ltab_size = mode.ltab_size; if (g_option.tab_size == -1) { file->ptab_size = mode.ptab_size; file->inflate_tabs = (file_mode == BINARY) ? 0 : mode.inflate_tabs; } else if (g_option.tab_size == 0) { file->ptab_size = mode.ptab_size; file->inflate_tabs = 0; } else { file->ptab_size = g_option.tab_size; file->inflate_tabs = (file_mode == BINARY) ? 0 :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -