📄 file.c
字号:
#include "file.h"//#ifdef _POSIX_MAPPED_FILES// success=((data=(char*)mmap(0,len,PROT_READ,MAP_PRIVATE,fd,0))!=MAP_FAILED)?ZC_OK:ZC_FF;//#else// data = (char*) malloc (len);// success = read (fd, data, len) == len ? ZC_OK : ZC_FF;//#endif//#ifdef _POSIX_MAPPED_FILES// if (data && data != MAP_FAILED)// munmap (data, len);//#else // if (data)// free (data);//#endifint codefile::readchrdev(char *f){ int d=-1; int l=0; char k; if((d=open(f,O_RDONLY))==-1) { ErrMsg(OKONLYDLG,"Read virtual device file error!"); return -1; } len=0; while((read(d,&k,1))>0) len++; close(d); data = (char*) malloc (len); while((read(fd,&k,1))>0) data[l++]=k; //Generally speaking, the follow setment should work more efficiently. //But the actual length of 'read' does not equal to the length of virtual device file. //I will repair it as soon as possible when I get the reason. //success = read (fd, data, len) == len ? ZC_OK : ZC_FF; success = (l==len)?(ZC_OK):(ZC_FF); return len;}int codefile::setmode(char *f){ struct stat statbuf; len=-1; if (stat (f, &statbuf) == -1) return -1; if(S_ISREG(statbuf.st_mode)) { len=statbuf.st_size; mode=ZC_REG; } else if(S_ISDIR(statbuf.st_mode)) mode=ZC_DIR; else if(S_ISBLK(statbuf.st_mode)) mode=ZC_BLK; else if(S_ISCHR(statbuf.st_mode)) mode=ZC_CHR; else if(S_ISFIFO(statbuf.st_mode)) mode=ZC_FIFO; else if(S_ISSOCK(statbuf.st_mode)) mode=ZC_SOCK; else if(S_ISLNK(statbuf.st_mode)) mode=ZC_LNK; return 0;}int codefile::getmode(){ return mode;}codefile::codefile (char *f){ data = NULL; success = ZC_NA; fd = -1; if ( setmode(f) == -1) return; if ( (mode==ZC_REG && len==-1) || ((fd=open(f,O_RDONLY))==-1) ) return; if(mode==ZC_REG) { data = (char*) malloc (len); success = read (fd, data, len) == len ? ZC_OK : ZC_FF; } else if(mode==ZC_CHR) { readchrdev(f); } curpos=0;}codefile::~codefile (){ if (data) free (data); if (fd != -1) close (fd);}codefile::codefile(char *f,int flags){ data = NULL; success = ZC_NA; fd = -1; if (setmode(f) == -1) return; if ( (mode==ZC_REG && len==-1) || ((fd=open(f,flags))==-1) ) return; if(mode==ZC_REG) { data = (char*) malloc (len); success = read (fd, data, len) == len ? ZC_OK : ZC_FF; } else if(mode==ZC_CHR) { readchrdev(f); } curpos=0;}codefile::codefile(char *f,int flags,mode_t mode)//create a regular file{ data = NULL; success = ZC_NA; fd = -1; if ( (fd=open(f,flags,mode))==-1 ) return; mode=ZC_REG; success=ZC_OK; curpos=0;}int codefile::putline(chtype *target){ int i=0; char linebuf[CODE_QUANTUM+1]; char ch=target[i]; while(ch!='\0' && i<CODE_QUANTUM-1) { linebuf[i]=ch; ch=target[++i]; } linebuf[i]='\n'; linebuf[++i]='\0'; i=write(fd,linebuf,i); return i;} int codefile::getline(chtype *target){ return getcodline(target);}void codefile::readhuge(char *buf){ strncpy(buf,data,len); return;}int codefile::getcodline(chtype *target){ int i=0; int j=0; while(curpos<len && i<CODE_QUANTUM && (data[curpos]!='\n')) { if(isprint(data[curpos])==0&&isblank(data[curpos])==0) { curpos++; continue; } if (data[curpos]!='\t') { target[i]=data[curpos]; i++; } else { for(j=0;j<TABSIZE;j++) target[i+j]=' '; i=i+TABSIZE; } curpos++; } target[i]='\0'; if (curpos<len && data[curpos]=='\n') { curpos++; if(i<=0) i=1; } return i;}int codefile::iseof(){ return (curpos<len)?(FALSE):(TRUE);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -