Example #1
0
File: rex.c Project: DeadZen/qse
static qse_rex_node_t* comp_atom (comp_t* com)
{
	qse_rex_node_t* atom;

	if (!IS_ESC(com))
	{
		switch (com->c.value)
		{
			case QSE_T('('):
				atom = comp_group (com);
				if (atom == QSE_NULL) return QSE_NULL;
				break;
			
			case QSE_T('.'):
				atom = newnode (com, QSE_REX_NODE_ANY);
				if (atom == QSE_NULL) return QSE_NULL;
				if (getc_esc(com) <= -1) return QSE_NULL;
				break;

			case QSE_T('^'):
				atom = newnode (com, QSE_REX_NODE_BOL);
				if (atom == QSE_NULL) return QSE_NULL;
				if (getc_esc(com) <= -1) return QSE_NULL;
				break;
	
			case QSE_T('$'):
				atom = newnode (com, QSE_REX_NODE_EOL);
				if (atom == QSE_NULL) return QSE_NULL;
				if (getc_esc(com) <= -1) return QSE_NULL;
				break;

			case QSE_T('['):
				atom = newnode (com, QSE_REX_NODE_CSET);
				if (atom == QSE_NULL) return QSE_NULL;
				if (getc_esc(com) <= -1) return QSE_NULL;
				if (charset(com, atom) <= -1) return QSE_NULL;
				break;

			default:
				if (com->rex->option & QSE_REX_STRICT)
				{
					/* check if a special charcter is at the 
					 * position that requires a normal character. */
					switch (com->c.value)
					{
						case QSE_T('{'):
							/* { is a normal charcter when bound is disabled */
							if (com->rex->option & QSE_REX_NOBOUND) break;

						case QSE_T(')'):
						case QSE_T('?'):
						case QSE_T('*'):
						case QSE_T('+'):
							/* it's at the wrong postion */
							com->rex->errnum = QSE_REX_ESPCAWP;
							return QSE_NULL;
					}
				}

				goto normal_char;
		}
	}
	else
	{
	normal_char:
		/* normal character */
		atom = newcharnode (com, com->c.value);
		if (atom == QSE_NULL) return QSE_NULL;
		if (getc_esc(com) <= -1) return QSE_NULL;
	}

	atom->occ.min = 1;
	atom->occ.max = 1;

	if (!IS_ESC(com))
	{
		/* handle the occurrence specifier, if any */
		if (comp_occ (com, atom) == QSE_NULL) return QSE_NULL;
	}

	return atom;
}
Example #2
0
int main (int argc, char **argv)
{
/* Declare main function variables */

struct charnode* head;
FILE* thefile;
char c; /* active character*/
struct wordnode* pointer; /*where you are in the word (long words require more than one node)*/ 
struct wordnode* word; /*current word*/
struct charnode* pos; /*position in tree*/
struct varnode* test; /* for testing new words against existing variation*/
struct varnode* lag; /*lags test by one position to allow insertion at end of linked list*/
int i; /* counter variable for iterators*/
	


	if (argc != 2) 
	{
		fprintf (stderr, "The wrong number of arguments were entered. Please try again.\n");
		return 0;
	}
	
	

	
	if (strcmp(argv[1], "-h") == 0)
	{
		fprintf (stdout, "\n\nWordstat has the following usage interface: wordstat <argument>;\n\nwhere <argument> is either the name of the file that wordstat should process,\n\nor -h, which means that wordstat should print this help menu.\n\nWhen invoked with a valid file name, wordstat should find and output \n\nall the unique words in the file in lexicographical order,\n\nalong with the total number of times each word appears (case-insensitive) \n\nand a count of different case-sensitive versions of the word.\n\n");
		return 0;
	}





	thefile = fopen(argv[1],"r");
	if (thefile == NULL)
	{
		fprintf (stderr, "File could not be opened. Please check the file name and try again.\n");
		fprintf (stderr, "%s\n", argv[1]);
		return 0;
	}


	
	head=newcharnode();
	c=fgetc(thefile);

	/* read in file*/
	while (c != EOF)
	{
		/*get the word*/
		if (isalpha(c)!=0)
		{
			word = newwordnode();
			pointer=word;
			i=0;
			while(isalnum(c)!=0)
			{
				if(i==39)
				{
					pointer->next = newwordnode();
				}
				if(i==40)
				{
					pointer=pointer->next;	
					i=0;
				}
									
					pointer->word[i]=c;
					i++;
				
				
				c=fgetc(thefile);
			}
			


			/*put the word in the tree*/
			c=word->word[0];
			pointer=word;
			pos=head;
			i=0;
			while (c!='\0')
			{
				if (pos->next==NULL)
				{
					pos->next=newnextarray();
				}
				if (isalpha(c)!=0)
				{
					if(pos->next->next[tolower(c)-'a']==NULL)
					{
						pos->next->next[tolower(c)-'a']=newcharnode();
					}
					pos = pos->next->next[tolower(c)-'a'];
				}
				if (isdigit(c)!=0)
				{
					if(pos->next->next[tolower(c)-'0'+26]==NULL)
					{
						pos->next->next[tolower(c)-'0'+26]=newcharnode();
					}
					pos = pos->next->next[tolower(c)-'0'+26];
				}
				i++;
				if(i==40)
				{
					i=0;
					pointer=pointer->next;	
				}
				c=pointer->word[i];
			}
			pos->occurcount++;
			
			/* scan variations */
			/* write this method*/
			if(pos->variations==NULL)
			{
				pos->variations=newvarnode();
				pos->variations->thisvar=word;
				pos->varcount++;

			}else
			{
				test=pos->variations;
				i=0; /*using i for a boolean here*/
				while(test != NULL)
				{
					lag=test;
					pointer=test->thisvar;
					if(compare(pointer,word)==1)
					{
						i=1;
						freewordnode(word);
						break;
					}
					test=test->next;
				}
				if(i==0)
				{
					lag->next=newvarnode();
					lag->next->thisvar=word;
					pos->varcount++;
				}
			}
		}
		c=fgetc(thefile);
	}
	/*end file read in*/
	i=fclose(thefile);
	if (i!=0)
	{
		fprintf(stderr, "the file failed to close");
	}


	/*print and free*/
	print(head);
	freecharnode(head);

return 0;
}