行编辑器c语言,简单的行编辑器(C语言).doc
簡單的行編輯器(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; 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 * replaceme
總結
以上是生活随笔為你收集整理的行编辑器c语言,简单的行编辑器(C语言).doc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PCB设计中的正片和负片设计原理
- 下一篇: Total Recorder去除噪音