Word Reader

A.Very First Edition
This is the very early edition of my own library(I am too ambitious to want to write my own lib.) And I make 
a little amendment.
1¡£ Basic idea: A dynamic array which will automatically expand size like that in Delphi. And a word reader 
which will read all word from a file. It is also use the dynamic list to store words.;
2¡£ Program design: 
3¡£ Major function£º
     A. int count();
	  int wordCount(); 
	  int items(int index);
	  char* items(int index);
	These  methods exists in both list and wordReader, they are in similar functions for you to determine how
	many items are stored and how to access them by index.
	B. int filereader::readword()
	This is the kernel function for reading words from file. I also use some trivial function like trim(),
	filter() etc. which I forgot the purpose by myself.
¡¡
4¡£ Further improvement£º
	A. These functions are not so good which I re-write when design my dictionary program.
	B. However, some piece of code is still not bad as long as it runs well.
	
		
#include <iostream>
#include <iomanip>
#include <math.h>
¡¡
using namespace std;
enum FilterMode
{
	WORDONLY,
	WITHPUNCTUATION
};
¡¡
class mylist
{
private:
	int *flist;
	int LENGTH;
	int SIZE;
	int counter;
protected:
	void uninitialize();
	void initialize();
	bool checksize();
	void expand();
public:
	mylist();
	~mylist();
	void add(int ptr);
	void display();
	int count();
	int items(int index);
};

class filereader
{
private:
	FILE *stream;
	mylist list;
//	char delimiter;
	int BUFFERSIZE;
	int WORDLENGTH;
//	char *buffer;
	bool wordonly;
	char trim();
	void expandBuffer(char *buf);
	bool filter(char ch);
protected:
	void initialize(bool wordonly);
	void uninitialize();
public:
	filereader(bool wordonly= true);
	~filereader();
	filereader(char *filename);
	void openfile(char *filename);
//	void setdelimiter(char delimiter);
//	int readword(char delimiter); 
	int readword();
	void display();
	const int wordCount(); 
	const char* items(const int index);
};
¡¡
int main()
{
	filereader reader;
	reader.openfile("c:\\myfile.txt");
	cout<<reader.readword();
	reader.display();
	cout<<"\nnow use another way to show word\n";
	for (int i=0; i< reader.wordCount(); i++)
	{
		cout<<"word no. "<<i<<" is :  "<<reader.items(i)<<endl;
	}
	return 0;
}
bool filereader::filter(char ch)
{
	if (wordonly)
		return (isalnum(ch));
	else
		return ((ch != 10)&&(ch != 32));
}
void filereader::uninitialize()
{
	for (int i =0; i < list.count(); i++)
	{
		free((void *)(list.items(i)));
	}
	fclose(stream);
}
filereader::filereader(bool wordonly)
{
	initialize(wordonly);
}
void filereader::display()
{
	for (int i=0; i< list.count(); i++)
	{
		cout<<(char*)(list.items(i))<<endl;
	}
}
const int filereader::wordCount()
{
	return list.count();
}
const char* filereader::items(const int index)
{
	return (char*)list.items(index);
}
filereader::~filereader()
{
	uninitialize();
	
}
void filereader::initialize(bool wordonly)
{
//	delimiter = ';';
	this->wordonly = wordonly;
	WORDLENGTH = 5;
//	BUFFERSIZE = 5;
//	buffer = (char *) malloc(sizeof(char) *BUFFERSIZE);
}
/*
int filereader::readword()
{
	this->readword(this->delimiter);
}
*/
char filereader::trim()
{
	char ch;
	ch = fgetc(stream);
	while (!feof(stream)&&(!isalnum(ch)))
	{
		ch = fgetc(stream);
	}
	return ch;
}
filereader::filereader(char *filename)
{
	filereader();
	openfile(filename);
}
/*
void filereader::setdelimiter(char delimiter)
{
	this->delimiter = delimiter;
}
*/
void filereader::openfile(char *filename)
{
	if ((stream = fopen(filename, "r"))==NULL)
		cout<<"unable open file "<<filename<<endl;
}
int filereader::readword()
{
	char ch, *buf, *temp;
	int counter, wordLength, wordCounter=0; 	
	while (!feof(stream))
	{
		wordLength = WORDLENGTH;
		if ((buf = (char *)malloc(sizeof(char) * wordLength))== NULL)
			cout<<"unable alloc memory!";
		counter = 0;
		ch = fgetc(stream);
		temp = buf;
		while (filter(ch)&&!feof(stream))
		{			
			*temp = ch;			
			counter++;
			if (counter >= wordLength)
			{
				wordLength += WORDLENGTH;
				buf = (char *) realloc(buf, wordLength * sizeof(char));
				temp = buf + counter - 1;
			}
			temp++;
			ch = fgetc(stream);
		}
		if (counter!=0)
		{
			*temp = '\0';
			list.add((int)(buf));
			wordCounter++;
		}
	}
	return wordCounter;
}
			
void filereader::expandBuffer(char *buf)
{
	
}
int mylist::count()
{
	return counter;
}
int mylist::items(int index)
{
	return flist[index];
}
void mylist::display()
{
	for (int i = 0; i < counter; i ++)
	{
		cout<<"Number "<<i<<" item is:"<<flist[i]<<endl;
	}
}
void mylist::uninitialize()
{
//	for (int i = 0; i < counter; i++)
	{
		//free(flist[i]);
	}
	free(flist);
}
mylist::~mylist()
{
	uninitialize();
}
void mylist::add(int ptr)
{
//	int *temp;	
	if (!checksize())
		expand();
//	temp = flist;
//	temp+=counter;
//	*temp = ptr;
	flist[counter] = ptr;
	counter++;
}
void mylist::initialize()
{
	LENGTH = 10;
	SIZE = LENGTH;
	if ((flist =(int*)(malloc(sizeof(int) * SIZE)))==NULL)
		cout<<"Unable malloc memory for size of "<<SIZE<<endl;  //exception need to be handled here!!
	counter = 0;
}
bool mylist::checksize()
{
	return (counter < SIZE);
}
void mylist::expand()
{
	SIZE += LENGTH;
	if ((flist = (int*)(realloc(flist, sizeof(int) * SIZE)))== NULL)
		cout<<"Unable realloc memory for mylist of size "<<SIZE<<endl;
}
mylist::mylist()
{
	initialize();
}
¡¡
¡¡
	

                                                         back.gif (341 bytes)       up.gif (335 bytes)         next.gif (337 bytes)

Hosted by www.Geocities.ws

1