📄 file.c
字号:
* Name: edit_file
* Purpose: To allocate space for a new file structure and set up some
* of the relevant fields.
* Date: June 5, 1991
* 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
*/
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;
line_list_ptr ll;
line_list_ptr temp_ll;
line = g_display.nlines;
rc = OK;
/*
* allocate a file structure for the new file
*/
file = (file_infos *)calloc( 1, sizeof(file_infos) );
if (file == NULL) {
error( WARNING, line, main4 );
rc = ERROR;
}
existing = FALSE;
if (rc == OK && hw_fattrib( name ) == OK) {
existing = TRUE;
/*
* g_status.temp_end is set last character read in file
*/
if (g_status.command != DefineGrep &&
g_status.command != DefineRegXGrep &&
g_status.command != RepeatGrep)
rc = load_file( name, file, &file_mode, bin_length );
else {
if (g_status.sas_defined) {
rc = load_file( name, file, &file_mode, bin_length );
if (rc != ERROR) {
found_line = 1L;
rcol = 0;
if (g_status.sas_search_type == BOYER_MOORE)
ll = search_forward( file->line_list, &found_line,
(size_t *)&rcol );
else
ll = regx_search_forward( file->line_list, &found_line,
&rcol );
if (ll == NULL)
rc = ERROR;
else {
g_status.sas_rline = found_line;
g_status.sas_rcol = rcol;
g_status.sas_ll = ll;
}
}
} else
rc = ERROR;
}
} else {
if (ceh.flag == ERROR)
rc = ERROR;
else {
existing = FALSE;
file->length = 0l;
file->undo_top = file->undo_bot = NULL;
file->line_list_end = file->line_list = NULL;
file->undo_count = 0;
ll = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (ll != NULL) {
ll->line = NULL;
ll->next = ll->prev = NULL;
ll->dirty = FALSE;
ll->len = EOF;
file->undo_top = file->undo_bot = ll;
} else
rc = ERROR;
ll = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (ll != NULL) {
ll->line = NULL;
ll->next = ll->prev = NULL;
ll->dirty = FALSE;
ll->len = EOF;
file->line_list_end = file->line_list = ll;
} else
rc = ERROR;
if (rc == ERROR) {
if (file->undo_top != NULL)
my_free( file->undo_top );
if (file->line_list != NULL)
my_free( file->line_list );
} else
if (file_mode == TEXT)
file_mode = CRLF;
}
}
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 {
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 ) < MAX_COLS );
strcpy( file->file_name, name );
get_fattr( name, (int *)&file->file_attrib );
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 = 'a';
file->file_no = ++g_status.file_count;
file->crlf = file_mode;
g_status.current_file = file;
make_backup_fname( file );
} else if (file != NULL) {
/*
* free the line pointers and linked list of line pointers.
*/
ll = file->undo_top;
while (ll != NULL) {
temp_ll = ll->next;
if (ll->line != NULL)
my_free( ll->line );
my_free( ll );
ll = temp_ll;
}
ll = file->line_list;
while (ll != NULL) {
temp_ll = ll->next;
if (ll->line != NULL)
my_free( ll->line );
my_free( ll );
ll = temp_ll;
}
#if defined( __MSC__ )
_fheapmin( );
#endif
free( file );
}
return( rc );
}
/*
* Name: edit_another_file
* Purpose: Bring in another file to editor.
* Date: June 5, 1991
* Passed: window: pointer to current window
* Notes: New window replaces old window. Old window becomes invisible.
*/
int edit_another_file( WINDOW *window )
{
char fname[MAX_COLS]; /* new name for file */
char spdrive[_MAX_DRIVE]; /* splitpath drive buff */
char spdir[_MAX_DIR]; /* splitpath dir buff */
char spname[_MAX_FNAME]; /* splitpath fname buff */
char spext[_MAX_EXT]; /* splitpath ext buff */
register WINDOW *win; /* put window pointer in a register */
int rc;
int file_mode;
int bin_length;
win = window;
entab_linebuff( );
if (un_copy_line( win->ll, win, TRUE ) == ERROR)
return( ERROR );
/*
* read in name, no default
*/
fname[0] = '\0';
/*
* file name to edit
*/
if ((rc = get_name( ed15, win->bottom_line, fname,
g_display.message_color )) == OK && *fname != '\0') {
file_mode = TEXT;
bin_length = 0;
assert( strlen( fname ) <= MAX_COLS );
_splitpath( fname, spdrive, spdir, spname, spext );
if (stricmp( spext, ".exe" ) == 0 || stricmp( spext, ".com" ) == 0) {
file_mode = BINARY;
bin_length = g_status.file_chunk;
}
rc = attempt_edit_display( fname, LOCAL, file_mode, bin_length );
if (rc == OK)
show_avail_mem( );
}
return( rc );
}
/*
* Name: edit_next_file
* Purpose: edit next file on command line.
* Date: January 6, 1992
* Passed: window: pointer to current window
* Notes: New window replaces old window. Old window becomes invisible.
*/
int edit_next_file( WINDOW *window )
{
char name[MAX_COLS]; /* new name for file */
char spdrive[_MAX_DRIVE]; /* splitpath drive buff */
char spdir[_MAX_DIR]; /* splitpath dir buff */
char spname[_MAX_FNAME]; /* splitpath fname buff */
char spext[_MAX_EXT]; /* splitpath ext buff */
int file_mode;
int bin_length;
int i;
int update_type;
register int rc = ERROR;
register WINDOW *win; /* put window pointer in a register */
win = window;
update_type = win == NULL ? GLOBAL : LOCAL;
if (g_status.arg < g_status.argc) {
if (win != NULL) {
entab_linebuff( );
if (un_copy_line( win->ll, win, TRUE ) == ERROR)
return( ERROR );
}
/*
* while we haven't found a valid file, search thru the command
* line path.
* we may have an invalid file name when we finish matching all
* files according to a pattern. then, we need to go to the next
* command line argument if it exists.
*/
while (rc == ERROR && g_status.arg < g_status.argc) {
/*
* if we haven't starting searching for a file, check to see if
* the file is a valid file name. if no file is found, then let's
* see if we can find according to a search pattern.
*/
if (g_status.found_first == FALSE) {
assert( strlen( g_status.argv[g_status.arg] ) < MAX_COLS );
strcpy( name, g_status.argv[g_status.arg] );
rc = get_fattr( name, &i );
/*
* a new or blank file generates a return code of 2.
* a pattern with wild cards generates a return code of 3.
*/
if (rc == OK || rc == 2) {
++g_status.arg;
rc = OK;
/*
* if we get this far, we got an invalid path name.
* let's try to find a matching file name using pattern.
*/
} else if (rc != ERROR) {
rc = my_findfirst( &g_status.dta, name, NORMAL | READ_ONLY |
HIDDEN | SYSTEM | ARCHIVE );
/*
* if we found a file using wildcard characters,
* set the g_status.found_first flag to true so we can
* find the next matching file. we need to save the
* pathname stem so we know which directory we are working in.
*/
if (rc == OK) {
g_status.found_first = TRUE;
i = strlen( name ) - 1;
while (i >= 0) {
if (name[i] == ':' || name[i] == '\\')
break;
--i;
}
name[++i] = '\0';
assert( strlen( name ) < MAX_COLS );
strcpy( g_status.path, name );
strcpy( name, g_status.path );
strcat( name, g_status.dta.name );
} else {
++g_status.arg;
if (win != NULL)
/*
* invalid path or file name
*/
error( WARNING, win->bottom_line, win8 );
}
} else if (rc == ERROR)
++g_status.arg;
} else {
/*
* we already found one file with wild card characters,
* find the next matching file.
*/
rc = my_findnext( &g_status.dta );
if (rc == OK) {
assert( strlen( g_status.path ) + strlen( g_status.dta.name )
< MAX_COLS );
strcpy( name, g_status.path );
strcat( name, g_status.dta.name );
} else {
g_status.found_first = FALSE;
++g_status.arg;
}
}
/*
* if everything is everything so far, set up the file
* and window structures and bring the file into the editor.
*/
if (rc == OK) {
file_mode = g_status.file_mode;
bin_length = g_status.file_chunk;
assert( strlen( name ) <= MAX_COLS );
_splitpath( name, spdrive, spdir, spname, spext );
if (stricmp( spext, ".exe" ) == 0 || stricmp( spext, ".com" ) == 0)
file_mode = BINARY;
rc = attempt_edit_display( name, update_type, file_mode, bin_length );
if (rc == OK)
show_avail_mem( );
}
/*
* either there are no more matching files or we had an
* invalid file name, set rc to ERROR and let's look at the
* next file name or pattern on the command line.
*/
else
rc = ERROR;
}
}
if (rc == ERROR && g_status.arg >= g_status.argc && win != NULL)
/*
* no more files to load
*/
error( WARNING, win->bottom_line, win9 );
return( rc );
}
/*
* Name: search_and_seize
* Purpose: search files for a pattern
* Date: October 31, 1992
* Passed: window: pointer to current window
* Notes: New window replaces old window. Old window becomes invisible.
*/
int search_and_seize( WINDOW *window )
{
char name[MAX_COLS]; /* new name for file */
char searching[MAX_COLS]; /* buffer for displaying file name */
char line_buff[(MAX_COLS+1)*2]; /* buffer for char and attribute */
char spdrive[_MAX_DRIVE]; /* splitpath drive buff */
char spdir[_MAX_DIR]; /* splitpath dir buff */
char spname[_MAX_FNAME]; /* splitpath fname buff */
char spext[_MAX_EXT]; /* splitpath ext buff */
int file_mode;
int bin_length;
int i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -