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

📄 agrep.c

📁 Mehldau和Myer的Agrep3版本
💻 C
📖 第 1 页 / 共 5 页
字号:
	int MM, base;
	unsigned V[WORD];

	base = WORD - M;
	temp = Bit[base]; 
	Bit[base] = 0;
	for (i=0; i<WORD; i++) V[i] = 0;
	for (i=1; i<M; i++)
	{  
		j=0;
		while (table[i][j] > 0 && j < 10) {
			V[i] = V[i] | Bit[base + table[i][j++]];
		}
	}
	Bit[base]=temp;
	if(M <= SHORTREG)
	{
		k = exponen(M);
		pp = 2*k;
		for(i=k; i<pp ; i++)
		{   
			n = i;
			Next[i]= (k>>1);
			for(j=M; j>=1; j--)
			{
				if(n & Bit[WORD]) Next[i] = Next[i] | V[j];
				n = (n>>1);
			}
		}      
		return;
	}
	if(M > MAXREG) fprintf(stderr, "%s: regular expression too long\n", Progname);
	MM = M;
	if(M & 1) M=M+1;
	k = exponen(M/2);
	pp = 2*k;
	mid = MM/2;
	for(i=k; i<pp ; i++)
	{     
		n = i;
		Next[i]= (Bit[base]>>1);
		for(j=MM; j>mid ; j--)
		{
			if(n & Bit[WORD]) Next[i] = Next[i] | V[j-mid];
			n = (n>>1);
		}
		n=i-k;
		Next1[i-k] = 0;
		for(j = 0; j<mid; j++)
		{
			if(n & Bit[WORD]) Next1[i-k] = Next1[i-k] | V[MM-j];
			n = (n>>1);
		}
	}      
	return;
}

int exponen(m)
int m;
{ 
	int i, ex;
	ex= 1;
	for (i=0; i<m; i++) ex <<= 1;	/* was ex *= 2 */
	return(ex);
}

int re1(Text, M, D)
int Text, M, D;
{
	register unsigned i, c, r0, r1, r2, r3, CMask, Newline, Init0, r_NO_ERR; 
	register unsigned end;
	register unsigned hh, LL=0, k;  /* Lower part */
	int  FIRST_TIME=ON, num_read , j=0, base;
	unsigned A[MaxRerror+1], B[MaxRerror+1];
	unsigned Next[MaxNext], Next1[MaxNext];
	CHAR *buffer;
	int FIRST_LOOP = 1;

	r_NO_ERR = NO_ERR_MASK;
	if(M > 30) {
		fprintf(stderr, "%s: regular expression too long\n", Progname);
		if (!EXITONERROR){
			errno = AGREP_ERROR;
			return -1;
		}
		else exit(2);
	}
	base = WORD - M;
	hh = M/2;
	for(i=WORD, j=0; j < hh ; i--, j++) LL = LL | Bit[i];
	if(FIRST_IN_RE) compute_next(M, Next, Next1); 
	/*SUN: try: change to memory allocation */
	FIRST_IN_RE = 0;
	Newline = '\n';
	Init[0] = Bit[base];
	if(HEAD) Init[0] = Init[0] | Bit[base+1];
	for(i=1; i<= D; i++) Init[i] = Init[i-1] | Next[Init[i-1]>>hh] | Next1[Init[i-1]&LL];
	Init1 = Init[0] | 1; 
	Init0 = Init[0];
	r2 = r3 = Init[0];
	for(k=0; k<= D; k++) { 
		A[k] = B[k] = Init[k]; 
	}

	if ( D == 0 )
	{
#if	AGREP_POINTER
	    if (Text != -1)
	    {
#endif	/*AGREP_POINTER*/
		alloc_buf(Text, &buffer, BlockSize+Maxline+1);
		while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
		{
			i=Maxline; 
			end = num_read + Maxline;
			if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end] = '\n';
			if(FIRST_LOOP) {         /* if first time in the loop add a newline */
				buffer[i-1] = '\n';  /* in front the  text.  */
				i--;
				CurrentByteOffset --;
				FIRST_LOOP = 0;
			}

			/* RE1_PROCESS_WHEN_DZERO: the while-loop below */
			while ( i < end )
			{
				c = buffer[i++];
				CurrentByteOffset ++;
				CMask = Mask[c];
				if(c != Newline)
				{
					if(CMask != 0) {
						r1 = Init1 & r3;
						r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
					}
					else  {
						r2 = r3 & Init1;
					}
				}
				else {
					j++;
					if (DELIMITER) CurrentByteOffset -= D_length;
					else CurrentByteOffset -= 1;
					r1 = Init1 & r3;            /* match against endofline */
					r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
					if(TAIL) r2 = (Next[r2>>hh] | Next1[r2&LL]) | r2;                                        /* epsilon move */
					if(( r2 & 1 ) ^ INVERSE) {
						if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
							num_of_matched++;

							if (agrep_finalfp != NULL) 
								fprintf(agrep_finalfp, "%s\n", CurrentFileName);
							else {
								int outindex;
								for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
										(CurrentFileName[outindex] != '\0'); outindex++) {
									agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
								}
								if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
									OUTPUT_OVERFLOW;
									free_buf(Text, buffer);
									return -1;
								}
								else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
								agrep_outpointer += outindex;
							}

							free_buf(Text, buffer);
							NEW_FILE = OFF;
							return 0;
						}
						if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
						if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
						    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
							free_buf(Text, buffer);
							return 0;	/* done */
						}
					}
					r3 = Init0;
					r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | Init0;
					/* match begin of line */
					if (DELIMITER) CurrentByteOffset += 1*D_length;
					else CurrentByteOffset += 1*1;
				}
				c = buffer[i++];
				CurrentByteOffset ++;
				CMask = Mask[c];
				if(c != Newline)
				{
					if(CMask != 0) {
						r1 = Init1 & r2;
						r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
					}
					else   r3 = r2 & Init1;
				} /* if(NOT Newline) */
				else {
					j++;
					if (DELIMITER) CurrentByteOffset -= D_length;
					else CurrentByteOffset -= 1;
					r1 = Init1 & r2;            /* match against endofline */
					r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
					if(TAIL) r3 = ( Next[r3>>hh] | Next1[r3&LL] ) | r3;
					/* epsilon move */
					if(( r3 & 1 ) ^ INVERSE) {
						if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
							num_of_matched++;

							if (agrep_finalfp != NULL)
								fprintf(agrep_finalfp, "%s\n", CurrentFileName);
							else {
								int outindex;
								for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
										(CurrentFileName[outindex] != '\0'); outindex++) {
									agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
								}
								if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
									OUTPUT_OVERFLOW;
									free_buf(Text, buffer);
									return -1;
								}
								else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
								agrep_outpointer += outindex;
							}

							free_buf(Text, buffer);
							NEW_FILE = OFF;
							return 0;
						}
						if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
						if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
						    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
							free_buf(Text, buffer);
							return 0;	/* done */
						}
					}
					r2 = Init0;
					r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | Init0;
					/* match begin of line */
					if (DELIMITER) CurrentByteOffset += 1*D_length;
					else CurrentByteOffset += 1*1;
				}
			} /* while i < end ... */

			strncpy(buffer, buffer+num_read, Maxline);
		} /* end while fill_buf()... */

		free_buf(Text, buffer);
		return 0;
#if	AGREP_POINTER
	    }
	    else {	/* within the memory buffer: assume it starts with a newline at position 0, the actual pattern follows that, and it ends with a '\n' */
		num_read = agrep_inlen;
		buffer = (CHAR *)agrep_inbuffer;
		end = num_read;
		/* buffer[end-1] = '\n';*/ /* at end of the text. */
		/* buffer[0] = '\n';*/  /* in front of the  text. */
		i = 0;

			/* An exact copy of the above RE1_PROCESS_WHEN_DZERO: the while-loop below */
			while ( i < end )
			{
				c = buffer[i++];
				CurrentByteOffset ++;
				CMask = Mask[c];
				if(c != Newline)
				{
					if(CMask != 0) {
						r1 = Init1 & r3;
						r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
					}
					else  {
						r2 = r3 & Init1;
					}
				}
				else {
					j++;
					if (DELIMITER) CurrentByteOffset -= D_length;
					else CurrentByteOffset -= 1;
					r1 = Init1 & r3;            /* match against endofline */
					r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
					if(TAIL) r2 = (Next[r2>>hh] | Next1[r2&LL]) | r2;                                        /* epsilon move */
					if(( r2 & 1 ) ^ INVERSE) {
						if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
							num_of_matched++;

							if (agrep_finalfp != NULL) 
								fprintf(agrep_finalfp, "%s\n", CurrentFileName);
							else {
								int outindex;
								for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
										(CurrentFileName[outindex] != '\0'); outindex++) {
									agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
								}
								if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
									OUTPUT_OVERFLOW;
									free_buf(Text, buffer);
									return -1;
								}
								else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
								agrep_outpointer += outindex;
							}

							free_buf(Text, buffer);
							NEW_FILE = OFF;
							return 0;
						}
						if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
						if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
						    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
							free_buf(Text, buffer);
							return 0;	/* done */
						}
					}
					r3 = Init0;
					r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | Init0;
					/* match begin of line */
					if (DELIMITER) CurrentByteOffset += 1*D_length;
					else CurrentByteOffset += 1*1;
				}
				c = buffer[i++];
				CurrentByteOffset ++;
				CMask = Mask[c];
				if(c != Newline)
				{
					if(CMask != 0) {
						r1 = Init1 & r2;
						r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
					}
					else   r3 = r2 & Init1;
				} /* if(NOT Newline) */
				else {
					j++;
					if (DELIMITER) CurrentByteOffset -= D_length;
					else CurrentByteOffset -= 1;
					r1 = Init1 & r2;            /* match against endofline */
					r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
					if(TAIL) r3 = ( Next[r3>>hh] | Next1[r3&LL] ) | r3;
					/* epsilon move */
					if(( r3 & 1 ) ^ INVERSE) {
						if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
							num_of_matched++;

							if (agrep_finalfp != NULL)
								fprintf(agrep_finalfp, "%s\n", CurrentFileName);
							else {
								int outindex;
								for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
										(CurrentFileName[outindex] != '\0'); outindex++) {
									agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
								}
								if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
									OUTPUT_OVERFLOW;
									free_buf(Text, buffer);
									return -1;
								}
								else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
								agrep_outpointer += outindex;
							}

							free_buf(Text, buffer);
							NEW_FILE = OFF;
							return 0;
						}
						if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
						if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
						    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
							free_buf(Text, buffer);
							return 0;	/* done */
						}
					}
					r2 = Init0;
					r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | Init0;
					/* match begin of line */
					if (DELIMITER) CurrentByteOffset += 1*D_length;
					else CurrentByteOffset += 1*1;
				}
			} /* while i < end ... */

		return 0;
	    }
#endif	/*AGREP_POINTER*/
	} /*  end if (D == 0) */

#if	AGREP_POINTER
	if (Text != -1)
	{
#endif	/*AGREP_POINTER*/
		while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
		{
			i=Maxline; 
			end = Maxline + num_read;
			if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end] = '\n';
			if(FIRST_TIME) {         /* if first time in the loop add a newline */
				buffer[i-1] = '\n';  /* in front the  text.  */
				i--;
				CurrentByteOffset --;
				FIRST_TIME = 0;
			}

⌨️ 快捷键说明

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