📄 file.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include "zped.h"
#include "myExternal.h"
/*add a colum node*/
Cnode * addCnode(Cnode * target)
{
Cnode * temp = 0;
if(!(temp = (Cnode *)malloc(sizeof(Cnode))))
return (Cnode *)0;
/*if the node add at the front of the row*/
if(current_x == 0) {
temp->next = current_R->rowHead;
if(current_R->rowHead != 0)
current_R->rowHead->pre = temp;
current_R->rowHead = temp;
temp->pre = 0;
} else if (current_x == current_R->num) { /*if the node add at the end of the row*/
target->next = temp;
temp->pre = target;
temp->next = 0;
} else { /*if the node add at the middle of the row*/
temp->next = target->next;
target->next->pre = temp;
temp->pre = target;
target->next = temp;
}
return temp;
}
/*delect a colum node*/
bool deleCnode(Cnode * target,Rnode * row)
{
/*if the node is at the front of the row*/
if(target->pre == 0) {
row->rowHead = target->next;
if(target->next != 0) {
target->next->pre = 0;
}
} else if(target->next == 0) { /*if the node is at the end of the row*/
target->pre ->next = 0;
} else { /*if the node is at the middle of the row*/
target->pre->next = target->next;
target->next->pre = target->pre;
}
free(target);
return 1;
}
/*delect a row node*/
bool deleRnode(Rnode * target)
{
if(target->pre == 0)
return 0;
/*if the node is at the end of the file*/
if(target->next == 0) {
target->pre->next = 0;
} else {
target->pre->next = target->next;
target->next->pre = target->pre;
}
free(target);
return 1;
}
/*add a row node*/
Rnode * addRnode()
{
Rnode * temp;
if(!(temp = (Rnode *)malloc(sizeof(Rnode))))
return (Rnode *)0;
/*if the node add at the end of the file*/
if(current_R->next == 0) {
temp->next = 0;
current_R->next = temp;
temp->pre = current_R;
temp->rowHead = 0;
temp->num = 0;
} else {
temp->next = current_R->next;
current_R->next->pre = temp;
temp->pre = current_R;
current_R->next = temp;
temp->rowHead = 0;
temp->num = 0;
}
return temp;
}
void add_one(int ch)
{
int i = 0;
Cnode * p = current_R->rowHead;
Cnode * temp = 0;
while(i < current_x - 1 && p) {
p = p->next;
i++;
}
/*if the char is an enter*/
if(ch == FKEY_ENTER || ch == '\n') {
if(current_x == 0) { /*if the enter is added at the front of the row*/
temp = addCnode((Cnode *)0);
temp->ch = '\n';
current_R = addRnode();
current_R->num = current_R->pre->num;
current_R->pre->num = 1;
current_R->rowHead = temp->next;
if(temp->next != 0) {
temp->next->pre = 0;
temp->next = 0;
}
mvwaddch(edit,3,3,current_R->pre->rowHead->ch);
} else if(current_x == current_R-> num) { /*if the enter is added at the end of the row*/
temp = addCnode(p);
temp->ch = '\n';
current_R->num++;
current_R = addRnode();
current_R->num = 0;
} else {
temp = addCnode(p);
temp->ch = '\n';
current_R = addRnode();
current_R->num = current_R->pre->num - current_x;
current_R->pre->num = current_x + 1;
current_R->rowHead = temp->next;
temp->next->pre = 0;
temp->next = 0;/*update the associable variables*/
}
/*update the associable variables*/
current_x = 0;
cursor_y++;
current_y++;
if(/*current_R->pre == editBot && totalLine >= LINES - 2*/cursor_y > LINES - 3) {
cursor_y--;
//editBot = editBot->next;
editTop = editTop->next;
}
totalLine++;
if(fileBot == current_R->pre)
fileBot = current_R;
} else { /*if the char is not an enter*/
if(current_x == 0) { /*if the char is added at the front of the row*/
temp = addCnode(0);
current_R->num++;
temp->pre = 0;
} else if(current_x == current_R->num) { /*if the char is added at the end of the row*/
temp = addCnode(p);
current_R->num++;
temp->next = 0;
} else {
temp = addCnode(p);
current_R->num++;
}
temp->ch = ch;
current_x++;
}
move_x = current_x;
}
bool dele_one()
{
long int i = 0;
Cnode * p;
Cnode * temp = 0;
/*if the char to be delected is an enter that means the cursor is at the front of the line*/
if(current_x == 0) {
if(current_R == fileTop) { /*if the cursor is at the first line of the file*/
return 0;
} else { /*if the cursor is not at the first line of the file*/
p = current_R->pre->rowHead;
while(p->ch != '\n')
p = p->next;
if(p->pre != 0) {
temp = p->pre;
}
deleCnode(p,current_R->pre);
if(temp == 0) {
current_R->pre->rowHead = current_R->rowHead;
//mvwaddch(edit,6,6,current_R->rowHead->ch);
} else {
temp->next = current_R->rowHead;
if(current_R->rowHead != 0)
current_R->rowHead->pre = temp;
}
/*update the associable variables*/
current_x = current_R->pre->num -1;
current_R->pre->num = current_R->pre->num + current_R->num - 1;
cursor_y--;
current_y--;
if(current_R == fileBot)
fileBot = current_R->pre;
if(editTop == current_R /*&& totalLine > LINES - 2*/) {
editTop = editTop->pre;
cursor_y++;
}
current_R = current_R->pre;
deleRnode(current_R->next);
totalLine--;
}
} else if(current_x == current_R->num) { /*if the char to be delected is at the end of the line*/
p = current_R->rowHead;
while(p->next != 0)
p = p->next;
if(p->pre == 0)
current_R->rowHead = 0;
else {
p->pre->next = 0;
current_R->rowHead->pre = 0;
}
current_x--;
current_R->num--;
deleCnode(p,current_R);
} else { /*if the char to be delected is at the middle of the line*/
p = current_R->rowHead;
while(i < current_x - 1) {
p = p->next;
i++;
}
if(p->pre == 0)
current_R->rowHead = 0;
else {
p->pre->next = 0;
current_R->rowHead->pre = 0;
}
current_x--;
current_R->num--;
deleCnode(p,current_R);
}
move_x = current_x;
}
bool readOneFile(char * pathName)
{
int input;
int i = 0;
FILE * fp;
if(!(fp = fopen(pathName,"r")))
return false;
while((input = getc(fp)) != EOF) {
if(input == '\t')
for(i = 0; i < 4; i++)
add_one(' ');
else
add_one(input);
}
current_x = 0;
current_y = 0;
cursor_y = 0;
move_x = 0;
current_R = fileTop;
editTop = fileTop;
fclose(fp);
return 1;
}
bool writeOneFile(char * pathName)
{
int output;
Rnode * Rtemp;
Cnode * Ctemp;
FILE * fp;
if(!(fp = fopen(pathName,"w")))
return false;
for(Rtemp = fileTop; Rtemp != 0; Rtemp = Rtemp->next) {
for(Ctemp = Rtemp->rowHead; Ctemp != 0; Ctemp = Ctemp->next) {
putc(Ctemp->ch,fp);
}
}
fclose(fp);
return 1;
}
void insert_add_one(int ch)
{
Cnode * Ctemp;
long count = 0;
long number;
if(current_R == fileBot)
number = current_R->num;
else
number = current_R->num-1;
if(ch == '\n' || ch == FKEY_ENTER || current_x >= number) {
add_one(ch);
} else {
Ctemp = current_R->rowHead;
while(count < current_x) {
Ctemp = Ctemp->next;
count++;
}
Ctemp->ch = ch;
do_right();
}
}
void insert_dele_one()
{
Cnode * Ctemp;
long count = 0;
if(current_x == 0) {
current_R->rowHead->ch = ' ';
dele_one();
} else {
Ctemp = current_R->rowHead;
while(count < current_x) {
Ctemp = Ctemp->next;
count++;
}
Ctemp->ch = ' ';
do_left();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -