📄 file.c
字号:
save_screen_line( 0, prompt_line, line_buff );
set_prompt( buff, prompt_line );
getkey( );
restore_screen_line( 0, prompt_line, line_buff );
if (fp->line_list != NULL)
my_free( fp->line_list );
if (fp->undo_top != NULL)
my_free( fp->undo_top );
rc = ERROR;
} else {
if (*file_mode == BINARY) {
mode.trailing = FALSE;
crlf = BINARY;
if (bin_len < 0 || bin_len > READ_LENGTH)
bin_len = DEFAULT_BIN_LENGTH;
for (; rc == OK;) {
t1 = fread( g_status.line_buff, sizeof(char), bin_len, stream );
if (ferror( stream ) || ceh.flag == ERROR) {
combine_strings( buff, "error reading file '", name, "'" );
error( WARNING, prompt_line, buff );
rc = ERROR;
} else if (t1) {
assert( t1 < MAX_LINE_LENGTH );
l = (text_ptr)my_malloc( t1 * sizeof(char), &rc );
temp_ll = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (rc != ERROR) {
/*
* if everything is everything, copy from io buff to
* dynamic mem.
*/
if (t1 > 0)
_fmemcpy( l, g_status.line_buff, t1 );
++line_count;
temp_ll->line = l;
temp_ll->dirty = FALSE;
temp_ll->len = t1;
insert_node( fp, ll, temp_ll );
ll = temp_ll;
} else
rc = show_file_2big( name, prompt_line, temp_ll, l );
} else
break;
}
} else {
crlf = LF;
for (; rc == OK;) {
t1 = fread( g_status.line_buff, sizeof(char), READ_LENGTH, stream );
if (ferror( stream ) || ceh.flag == ERROR) {
combine_strings( buff, "error reading file '", name, "'" );
error( WARNING, prompt_line, buff );
rc = ERROR;
} else {
/*
* "e" walks down io buffer 1 looking for end of lines. "t1"
* keeps count of number of characters in io buffer 1.
*/
e = g_status.line_buff;
while (t1 && rc == OK) {
/*
* while "t1" is not zero and "len" is less than max line length,
* let "e" walk down the buffer until it find <LF>.
*/
for (; t1 && len < READ_LENGTH && *e != '\n'; len++, e++, t1--);
/*
* if "t1" is not zero, we either found a <LF> or the line
* length max'd out.
*/
if (t1 || len >= READ_LENGTH) {
if (len > 1 && *e == '\n') {
if (len - res == 1) {
if (*(residue + res - 1) == '\r') {
--len;
--res;
crlf = CRLF;
}
} else {
if (*(e - 1) == '\r') {
--len;
crlf = CRLF;
}
}
}
if (len > 0)
--len;
assert( len >= 0 );
assert( len < MAX_LINE_LENGTH );
/*
* allocate space for relocatable array of line pointers and
* allocate space for the line we just read.
*/
l = (text_ptr)my_malloc( len * sizeof(char), &rc );
temp_ll =
(line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (rc != ERROR) {
/*
* if everything is everything, copy from io buff to
* dynamic mem. "residue" keeps up with the beginning
* of line in io buffer.
*/
if (res > 0) {
assert( res >= 0 );
assert( len - res >= 0 );
if (res > 0)
_fmemcpy( l, residue, res );
if (len - res > 0)
_fmemcpy( l + res, g_status.line_buff, len - res );
res = 0;
} else
if (len > 0)
_fmemcpy( l, residue, len );
++line_count;
temp_ll->line = l;
temp_ll->dirty = FALSE;
temp_ll->len = len;
insert_node( fp, ll, temp_ll );
ll = temp_ll;
/*
* reset io buffer pointers and counters.
*/
len = 1;
if (t1 == 0)
residue = g_status.tabout_buff;
else {
t1--;
residue = t1 == 0 ? g_status.tabout_buff : ++e;
}
} else
rc = show_file_2big( name, prompt_line, temp_ll, l );
} else if (len < READ_LENGTH ) {
if (!feof( stream ))
res = len - 1;
} else {
error( WARNING, prompt_line, "FRANK: error reading file!" );
rc = ERROR;
}
}
}
if (rc != OK)
break;
/*
* 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 (feof( stream )) {
if (len > 1) {
--len;
if (t1 == 0)
--e;
assert( len >= 0 );
assert( len < MAX_LINE_LENGTH );
/*
* allocate space for relocatable array of line pointers and
* allocate space for the line we just read.
*/
l = (text_ptr)my_malloc( len * sizeof(char), &rc );
temp_ll =
(line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (rc != ERROR) {
/*
* if everything is everything, copy from io buff to
* dynamic mem. "residue" keeps up with the beginning
* of line in io buffer.
*/
if (res > 0) {
assert( res >= 0 );
assert( res < MAX_LINE_LENGTH);
assert( len - res >= 0 );
assert( len - res < MAX_LINE_LENGTH);
if (res > 0 )
_fmemcpy( l, residue, res );
if (len - res > 0)
_fmemcpy( l + res, g_status.line_buff, len - res );
} else
if (len > 0)
_fmemcpy( l, residue, len );
++line_count;
temp_ll->line = l;
temp_ll->dirty = FALSE;
temp_ll->len = len;
insert_node( fp, ll, temp_ll );
} else
rc = show_file_2big( name, prompt_line, temp_ll, l );
}
break;
}
t2 = fread( g_status.tabout_buff, sizeof(char), READ_LENGTH, stream );
if (ferror( stream ) || ceh.flag == ERROR) {
combine_strings( buff, "error reading file '", name, "'" );
error( WARNING, prompt_line, buff );
rc = ERROR;
} else if (rc == OK) {
e = g_status.tabout_buff;
while (t2 && rc == OK) {
for (; t2 && len < READ_LENGTH && *e != '\n'; len++, e++, t2--);
if (t2 || len >= READ_LENGTH) {
if (len > 1 && *e == '\n') {
if (len - res == 1) {
if (*(residue + res - 1) == '\r') {
--len;
--res;
crlf = CRLF;
}
} else {
if (*(e - 1) == '\r') {
--len;
crlf = CRLF;
}
}
}
if (len > 0)
--len;
assert( len >= 0 );
assert( len < MAX_LINE_LENGTH );
l = (text_ptr)my_malloc( len * sizeof(char), &rc );
temp_ll =
(line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (rc != ERROR) {
if (res > 0) {
assert( res >= 0 );
assert( res < MAX_LINE_LENGTH);
assert( len - res >= 0 );
assert( len - res < MAX_LINE_LENGTH);
if (res > 0)
_fmemcpy( l, residue, res );
if (len - res > 0)
_fmemcpy( l+res, g_status.tabout_buff, len - res );
res = 0;
} else
if (len > 0)
_fmemcpy( l, residue, len );
++line_count;
temp_ll->line = l;
temp_ll->dirty = FALSE;
temp_ll->len = len;
insert_node( fp, ll, temp_ll );
ll = temp_ll;
len = 1;
if (t2 == 0)
residue = g_status.line_buff;
else {
t2--;
residue = t2 == 0 ? g_status.line_buff : ++e;
}
} else
rc = show_file_2big( name, prompt_line, temp_ll, l );
} else if (len < READ_LENGTH) {
if (!feof( stream ))
res = len - 1;
} else {
error( WARNING, prompt_line, "FRANK: error reading file!" );
rc = ERROR;
}
}
}
if (rc != ERROR && feof( stream )) {
if (len > 1) {
--len;
if (t2 == 0)
--e;
assert( len >= 0 );
assert( len < MAX_LINE_LENGTH );
l = (text_ptr)my_malloc( len * sizeof(char), &rc );
temp_ll =
(line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (rc != ERROR) {
if (res > 0) {
assert( res >= 0 );
assert( res < MAX_LINE_LENGTH);
assert( len - res >= 0 );
assert( len - res < MAX_LINE_LENGTH);
if (res > 0)
_fmemcpy( l, residue, res );
if (len - res > 0)
_fmemcpy( l+res, g_status.tabout_buff, len - res );
} else
if (len > 0)
_fmemcpy( l, residue, len );
++line_count;
temp_ll->line = l;
temp_ll->dirty = FALSE;
temp_ll->len = len;
insert_node( fp, ll, temp_ll );
} else
rc = show_file_2big( name, prompt_line, temp_ll, l );
}
break;
}
}
*file_mode = crlf;
}
/*
* close the file
*/
fp->length = line_count;
}
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 code behind current node.
*/
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;
if (current->prev != NULL)
current->prev->next = new;
new->prev = current->prev;
current->prev = new;
if (new->prev == NULL)
fp->line_list = new;
}
}
/*
* 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
* ll: double linked list pointer
* t: text line pointer
* 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, line_list_ptr ll, text_ptr t )
{
char buff[MAX_COLS+2];
combine_strings( buff, main10a, name, main10b );
error( WARNING, prompt_line, buff );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -