⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mystring.cpp

📁 这是一个linux下的Shell.有命令历史和命令提示
💻 CPP
字号:
#include "mystring.h"void free__(char** buf){	if(!buf)		return;	char** temp = buf;	while(*(buf++))		delete *buf;	delete temp;}void free__(char** buf,int num){	for(int i = 0; i < num; i++)		delete buf[i];	delete buf;}bool isprint_(char a){	if( a == '\t' || (a >= 32 && a <= 126 ))		return true;	else		return false;}/*	process the filepath.	replace the the "."and ".."	"."   is current    directory.	".."  is higher-up  directory.	the highed-up directory of root directory  is root directory.	/../ is just as /*/void dirproc(char* s){	if(!s)		return;	int j;	for(int i = 0; s[i]!=0; i++)	{		if(s[i] == '.' && s[i+1] == '/')		{			strcpy(s+i,s+i+2);			i -= 2;		}		if(s[i] == '.' && s[i+1] == '.' && s[i+2]=='/')		{			if(i == 1 && s[0] == '/')			{				strcpy(s,s+3);				i = 0;			}			else			{				for(j = i-2; j >= 0 && s[j] != '/'; j--){};				strcpy(s+j+1,s+i+3);				i = j-1;			}		}	}	}char* find_dirlast(char* s){	if(!s)		return 0;	if(strcmp(s, "/") == 0)		return s;	else	{		int len = strlen(s);		if(s[len - 1] == '/')			len--;		int j;		for( j = len-1; j >= 0 &&  s[j] != '/'; j--);		if(j == -1)			return s;					else			return &s[j + 1];	}}inline void cursormove(char a){	write(1,"\e[",2);	a &= 0x7f;	write(1,&a,1);}inline void cursormove(char a,int num){	a &= 0x7f;	for(int i = 0; i < num; i++)	{		write(1,"\e[",2);		write(1,&a,1);	}}int find(char* a,char b){	if(a == 0)		return -2;	char* temp = a;	while(*a != b && *a != 0)		a++;	if(*a == b)		return (int)(a - temp);	else		return -1;}void setnoecho(void){	struct termios* a = new struct termios[sizeof(struct termios)];	ioctl(0,TCGETS,a);	a->c_lflag &= ~ECHO;	ioctl(0,TCSETS,a);	delete a;}void setnonor(void){	struct termios* a = new struct termios[sizeof(struct termios)];	ioctl(0,TCGETS,a);	a->c_lflag &= ~ICANON;	ioctl(0,TCSETS,a);	delete a;}/*	get the key what the user press	include printable char,ender,space,delete,direction key.	use gdb or other debug program ,you can know the what be maked if you press a control key*/char getch_(void){	char a;	read(0,&a,1);	if(a == 27)			//if the key is a control key,the first char be read is 27('\e') and the following one char 		{				// is '['.		read(0,(char*)&a,1);			read(0,(char*)&a,1);			//the key is a direction key,the third char be read A up, B down, C right, D down		if(a == 'A' || a== 'B' || a == 'C' || a == 'D')			a |= 0x80;		else if(a == '3')								{			read(0,(char*)&a,1);			a = DELETE;		}		else		{			read(0,(char*)&a,1);			a = 27;		}	}	return a;}//consider buf as  not initchar scanf_(char*& buf){	buf = 0;	int a;	return scanf_(buf,a,false);}/*	the function is the most important in mystring.h	scanf_  get the user's input.	the buf is a reference of point of character point.	the size is a int reference.	if  buf== 0 I will malloc a new memory for it.	else I will realloc it to store the new input data.	I use reference but not use point.	point may be no valid,but the reference is always valid	if the showbufflag is true,and the buf is not zero.	I will show buf before get input.*/char scanf_(char*& buf,int& sze,bool showbufflag){	const int BLOCK_SIZE = 32;	int size;	 //  current input num (not include '\0')	int block;	 //  the number of buf block. block*BLOCK_SIZE is always bigger than size	int cur_pos;	 //  the position of cursor.	int i;	char b;	if(buf == 0)	{		size = 0;		block = 1;		cur_pos = 0;		buf = (char*)malloc(BLOCK_SIZE);	}	else	{		size = 0;		while(buf[size++] != 0){}		if(showbufflag)			for(i = 0; i < size - 1; i++)								write(1,&buf[i],1);				size--;		cur_pos = size;		block = size/BLOCK_SIZE + 1;		buf = (char*)realloc(buf,BLOCK_SIZE*block);	}	struct termios* save = new struct termios[sizeof(struct termios)];	ioctl(0,TCGETS,save);		setnoecho();//no echo	setnonor();//not normal	while(1)	{			while ( 27 == (b = getch_()));					if(isprint_((int)b) && b != '\t')		{			if(BLOCK_SIZE * block < (size + 1))			{				block++;				buf = (char *)realloc(buf,BLOCK_SIZE*block);			}			//move the data after(include cur_pos) cur_pos back one term			strncpy_(buf + cur_pos + 1, buf + cur_pos, size - cur_pos);			buf[cur_pos] = b;			cur_pos++;			size++;			for(i = cur_pos - 1; i < size; i++)				write(1,&buf[i],1);			//the system call write will move the cursor,so must move cursor to the position before call write			cursormove(KEYLEFT,size - cur_pos);				}		else if(b == KEYLEFT || b == KEYRIGHT)		{			if(b == KEYLEFT)			{				if(cur_pos > 0)				{					cursormove(KEYLEFT);						cur_pos--;				}			}			else			{				if(cur_pos >= size)				{					if(BLOCK_SIZE * block < (size + 1))					{						block++;						buf = (char *)realloc(buf,BLOCK_SIZE*block);					}					buf[cur_pos] = ' ';		//the cursor is bigged than the size,just consider keyright as space 					size++;				}				cursormove(KEYRIGHT);				cur_pos++;			}		}		else if(b == DELETE)		{			if(cur_pos < size)			{				strncpy_(buf + cur_pos, buf + cur_pos + 1, size - cur_pos - 1);				if(size > 0)					size--;							for(i = cur_pos; i < size; i++)					write(1,&buf[i],1);				write(1," ",1);				//must show a space to cover last printable char							cursormove(KEYLEFT,size - cur_pos + 1);			}					}		else if(b == BACKSPACE)		{			if((cur_pos > 0) && (cur_pos <= size))			{				strncpy_(buf + cur_pos - 1, buf + cur_pos, size - cur_pos);				if(size > 0)					size--;				if(cur_pos > 0)					cur_pos--;								cursormove(KEYLEFT);				int cur_reg = cur_pos;				for(i = cur_pos; i < size; i++)					write(1,&((buf)[i]),1);								write(1," ",1);				cursormove(KEYLEFT,size - cur_pos + 1);				cur_pos = cur_reg;					}					} 		else if(b == '\n' || b == KEYDOWN || b == KEYUP || b == '\t')	//the over char.		{			for(i = size -1; i >= 0;i--)				if(buf[i] == ' ')					size--;				else					break;			buf = (char*)realloc(buf,size + 1);		//make the size of buf be valid			buf[size] = 0;				sze = size;			if(size > cur_pos)				cursormove(KEYRIGHT,size - cur_pos);			if(size < cur_pos)				cursormove(KEYLEFT, cur_pos - size);							ioctl(1,TCSETS,save);				//recover the io mode from "save"			delete save;			return b;					//return the over char		}	}}int connect(char** block, int num, char*& buf){	if(!block)	{		buf = 0;		return 0;	}	int size = 0;	for(int i = 0; i < num; i++)		size += strlen(block[i]);		buf = new char[size + num + num -1];	buf[size + num + num -2] = 0;	buf[0] = 0;	for(int i = 0; i < (num-1); i++)	{		strcat(buf, block[i]);		strcat(buf, " ");	} 	strcat(buf, block[num-1]);	return size;}void replace(char*& o, int begin, int end,  char* x){	if(!o || begin < 0 || end >= (int)strlen(o))		return;		if(!x)	{		strcpy(o+begin, o+end+1);		o = (char*)realloc(o, strlen(o)+1);		return;	}	else	{		int len_x = strlen(x);		int len_o = strlen(o);		if((end - begin + 1) < len_x)		{			o = (char*)realloc(o,len_o + 1 + (len_x - (end - begin + 1)));			o[len_o + len_x - (end - begin + 1)] = 0;			strncpy_(o+begin + len_x, o + end + 1,len_o - end - 1);			strncpy_(o+begin,x, len_x);					}		else		{			strncpy_(o+begin+len_x, o + end + 1, len_o - end);			strncpy_(o+begin,x, len_x);			o = (char*)realloc(o, strlen(o) + 1);		}	}}void tranable(char* s)							//make the translate char valid or no valid{	if(!s)		return;	int i = 1;	while(s[i] != 0)	{		if(s[i] == TRAN_ABLE && s[i - 1] == TRAN_ABLE)			s[i - 1] = s[i] = TRAN_NOABLE;		i++;	}				}/*	the blockchar is very important for grammar analyze.	the function will divide the string what s point.	the result will save to buf and num	the buf point to buffer of string.and num is the number of block	I use reference but not point.	beacuse reference is always valid,buf point may be no valid.	if string between two ' ' will not be divided.	consider the ' is a special char,except there is a translate char before it.*///note: before call the function,the caller must be make process the tranlate char in sint  blockchar(char* s,char**& buf,int&  num){			if(!s)	{		buf = 0;		num = 0;		return 0;	}		int len = strlen(s);	for(len--;(len>=0) && ( s[len] == ' ' || s[len] == '\t');len--){};//dismiss the space in the string's tail	if(len == -1)	{		buf = 0;		num = 0;		return 0;	}	len++;	int sta = 0;	while(s[sta] == ' '|| s[sta] == '\t')				sta++;		num = 0;	//spaceAbleFlag is true	//then is space is valid	//space is not valid	bool spaceAbleFlag = ((s[sta]=='\'') ? false:true);	int i = (spaceAbleFlag ? -1:0);	do	{		i++;		if((s[i] == ' ' || s[i] == '\0') && spaceAbleFlag )		{			num++;			for(; s[i]==' '||s[i]=='\t';i++){};		}		if(s[i] =='\'') 		{			if(s[i-1] != TRAN_ABLE)				spaceAbleFlag = !spaceAbleFlag;		}	}while(s[i] != 0);	buf = new char*[num];	spaceAbleFlag = ((s[sta]=='\'') ? false:true);	i = (spaceAbleFlag ? -1:0);	num = 0;	do	{		i++;		if((s[i] == ' ' || s[i] == '\0') && spaceAbleFlag )		{			num++;			buf[num-1] = new char[i - sta + 1];			memcpy(buf[num-1],s+sta,i-sta);			buf[num-1][i-sta] = 0;			for(int j = 0; j < (i-sta);j++)				buf[num - 1][j] = (buf[num - 1][j] == TRAN_NOABLE) ? TRAN_ABLE : buf[num - 1][j]; 			for(; s[i]==' '||s[i]=='\t';i++){};			sta = i;		}		if(s[i] =='\'') 		{			if(s[i-1] != TRAN_ABLE)			{				strcpy(s+i,s+i+1);				i--;				spaceAbleFlag = !spaceAbleFlag;			}		}	}while(s[i] != 0);	int len_s = strlen(s);	for(int j = 0; j < len_s; j++)		s[j] = (s[j] == TRAN_NOABLE) ? TRAN_ABLE : s[j];	return num;}void show_arr_char(char** buf, int num){	for(int i = 0; i < num;i++)		if(buf[i])			puts(buf[i]);}void show_arr_char(char** buf){	if(buf)	{		while(*buf)			puts(*(buf++));	}}bool ismatchin(char* s){	if(!s)		return true;	while(*s != 0)	{		int i = 0;		while(i < 6 && "*?![-]"[i++] != *s);		if(i != 6)			return true;		s++;	}	return false;}char* strncpy_(char* o, char* s, int size){	if(!o || !s || size <= 0)		return o;	if((int)strlen(s) + 1 < size)		return o;	if((unsigned long)o >(unsigned long)s)		for(int i = size - 1; i >= 0; i--)			o[i] = s[i];	else		for(int i = 0; i < size; i++)			o[i] = s[i];	return o;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -