📄 file.c
字号:
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" 遍历缓存区1来找到文件尾。
* "t1"保存缓存区1中的字符数
*/
e = g_status.line_buff;
while (t1 && rc == OK) {
/*
* 当t1非空,并且len比最大值小,让e一直遍历直到碰到<LF>.
*/
for (; t1 && len < READ_LENGTH && *e != '\n'; len++, e++, t1--);
/*
* 当t1非空,e遍历到碰到<LF>或者超过最大值
*/
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 );
/*
* 为我们刚刚读入的行分配空间
*/
l = (text_ptr)my_malloc( len * sizeof(char), &rc );
temp_ll =
(line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
if (rc != ERROR) {
/*
* 如果一切正常,把io缓存中的内容拷贝到内存中去
* "residue" 跟踪io缓存中的行首
*/
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;
/*
* 重新设置io缓存的指针和计数器
*/
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;
/*
* 我们已经读入了所有的以'\n'结尾的行,但是'\n'之后可能还有
* 一些其他的字符,比如^Z。
*/
if (feof( stream )) {
if (len > 1) {
--len;
if (t1 == 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) {
/*
* 如果一切正常,把io缓存中的内容拷贝到内存中去
* "residue" 跟踪io缓存中的行首
*/
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 );
if (t != NULL)
my_free( t );
if (ll != NULL)
my_free( ll );
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( WINDOW *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_copied = g_status.copied;
old_line_buff_len = g_status.line_buff_len;
old_line_buff = calloc( MAX_LINE_LENGTH, sizeof(char) );
old_tabout_buff_len = g_status.tabout_buff_len;
old_tabout_buff = calloc( MAX_LINE_LENGTH, sizeof(char) );
if (old_line_buff != NULL && old_tabout_buff != NULL) {
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;
else
rc = ERROR;
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 );
}
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -