- 5
- 0
- 约8.16千字
- 约 9页
- 2017-09-02 发布于浙江
- 举报
简单的行编辑器C语言
简单的行编辑器
【要求】
(1) 设置一个简单的行编辑器,每行以回车结束
(2) 数据以文件形式存储
(3) 编辑器具有查找、替换、修改数据的功能
201-7-9。请把所有的注释信息提取出来就可以写程序设计报告。
typedef struct LINE {
char text[szLINE]; /*文本内容*/
struct LINE * next; /*链表指针*/
}L;
/*简写无类型整数*/
typedef unsigned int U; /*定义12个行编辑命令的标准格式*/
typedef void (*FUNC)(L **, char*); /*定义12个标准行编辑命令的关键字*/
char keywords[CMDS][8]={
quit, help, load, save,
view, count, append, insert,
erase, edit, lookup, replace
}; /*end keywords*/
/*清空链表操作*/
void clear(L ** lines)
{ L *a =0, *b=0;
if(!lines) return ;
a = *lines;
while(a) {
b=a-next ;
free(a);
a=b;
} /*end while*/
*lines=0;
} /*end clear*/
/*在链表中根据行号index调出指定的行*/
L *locate(L *lines, U index)
{ L *t=lines; U i = 0;
if(!t) return 0;
if(index == 0) return t;
for(i=0; iindex;i++) {
t=t-next;
if(!t) return 0;
}/*next*/
return t;
}/*end locate*/
/*浏览命令,如果f存在则以带行号格式保存文件(如果f==stdout则打印到屏幕上),浏览范围为from到to(行号)。view(lines, 0, 0, 0)表示统计已加载到内存的文本行数量*/
int view(L * lines, FILE * f, U from, U to)
{L *t=lines; U index=0;
while(t) {
index ++;
if(f index = from index = to) fprintf(f, %d: %s, index, t-text);
t=t-next;
}/*end while*/
return index;
}/*end view*/
/*在当前文档中根据关键字进行搜索,并将搜索结果打印出来*/
void lookup(L * lines, char * string)
{L *t=0; U index = 0;
if(!string) return ;
t=lines;
while(t) {
index ++;
if(strstr(t-text , string)) printf(%d: %s, index, t-text );
t=t-next;
}/*end while*/
}/*end lookup*/
/*在一行文本中执行替换命令,把所有关键字替换为新关键字*/
void rpc(char * string, char * key, char * replacement)
{ char fine[szLINE], *x=0, *y=0, *z=0;
int la=0, lb=0, r=0;
if(!string || !key || !replacement) return ;
memset(fine, 0, szLINE);
x=string; y=fine;
/*首先记录新旧关键字长度*/
la=strlen(key);
lb=strlen(replacement);
do { /*用指针逐个比较*/
r = memcmp(x, key, la);
if(r){/*如果关键字不匹配则复制字符串*/
*y=*x;
x++; y++;
}else{/*如果关键字匹配则替换字符串*/
memcpy(y, replacement, lb);
x += la; y += lb;
}/*end if*/
}while(*x);
/*将替换完成的结果返回*/
memcpy(string, fine, szLINE);
}/*end rpc*/
/*全文替换*/
void replace(L * lines, char * string, char * replacement)
{L *t=0;U index=0;
if(!string || !lin
原创力文档

文档评论(0)