/*****************************************************************
 *								  *
 *		COMPUTER GURU X : codelibrary/cgx_file.h	  *
 *		========================================	  *
 *								  *
 *	For More Files Visit :					  *
 *	http://compgurux.tripod.com/codelibrary/index.htm 	  *
 *								  *
 ******************************************************************
 *	Copyright :						  *
 *	Author    : Vivek Mohan					  *
 *	Email     : mailvivek27@sify.com			  *
 *      HomePage  : http://compgurux.tripod.com/		  *
 *	Message -						  *
 *	You are free to use this code , or distribute , as long   *
 *	this message is retained in the copies.			  * 
 *								  *
 *	For Details on how to use this library , please refer	  *
 *	the file LIB_REFERENCE.html packaged along with this	  *
 *								  *
 ******************************************************************/

 # include 
 # include 
 # include 
 # include 

 const int fLen 	= 80;

 class	CGX_File {

	private:
	FILE*	fPtr		;	// file pointer
	char    fName[fLen]	;	// file name
	int	isOpen		;	// flag

	public:

	CGX_File();
	CGX_File(const char *fileName);

	int exists();		// returns 1 if exists else 0

	int  createBinary();	// returns 1 if successful else 0
	int  createText  ();	// returns 1 if successful else 0
	int  openBinary	 ();	// returns 1 if successful else 0
	int  openText	 ();	// returns 1 if successful else 0
	int  appendBinary();  	// returns 1 if successful else 0
	int  appendText	 ();	// returns 1 if successful else 0
	int  isAtEof     ();	// returns 1 if EOF and 0 if not
	int  close       ();	// returns ..
	long int size    ();	// returns the length of file

	long int lengthOfLine(char)	; // returns length of next line
	char	read  ()		; // reads a char
	char*	readLn()		; // reads a line returns it
	char* 	readLn(const int length); // reads a line returns it
	char*	readS (char)		; // reads a string of a given length
	char*	readS (const int length , char)	;// reads a string of a given length
	int	write (const char)  ;
	int	write (const char*) ;
	int	write (const int)   ;
	int	write (const double);

	char	charAt   (const long index);
	char	setCharAt(const long index , char c);

	// returns the position of the file pointer
	long getPosition()	;
	long setPosition(long)	;
	void gotoBegining()	;
	void gotoEndOfFile()	;

	// Operators
	CGX_File& operator <<(const char  c);
	CGX_File& operator <<(const char *s);
	CGX_File& operator <<(const int     i);
	CGX_File& operator <<(const double  f);
	CGX_File& operator >>(char &c);
	CGX_File& operator >>(char *s);
	CGX_File& operator ++();
	CGX_File& operator --();
	CGX_File& operator [](const long);
	char	  operator ()(const long);
	char      operator = (char c);	
 };

 // Implementing the << operator for displaying the
 // the charcter at the current position

 ostream& operator<<(ostream& out,CGX_File& CGX_FileObj){
	out<>(char &c){
	c = read();
	return *this;
 }

 CGX_File& CGX_File::operator >>(char *s){
	s = readLn();
	return *this;
 }

 CGX_File& CGX_File::operator ++(){
	if(!feof(fPtr))
		fseek(fPtr,ftell(fPtr)+1,SEEK_SET);
	return *this;
 }

 CGX_File& CGX_File::operator --(){
	if(ftell(fPtr)!=0)
		fseek(fPtr,ftell(fPtr)-1,SEEK_SET);
	return *this;
 }

 char CGX_File::operator = (char c){
	fputc(c,fPtr);
	return c;
 }

 CGX_File&  CGX_File::operator[](const long index){
 	fseek(fPtr,index,SEEK_SET);
	return *this;
 }

 char CGX_File::operator()(const long index){
	return charAt(index);
 }

 char CGX_File::charAt(const long index){
 	fseek(fPtr,index,SEEK_SET);
	return fgetc(fPtr);
 }

 char CGX_File::setCharAt(const long index,char c){
 	fseek(fPtr,index,SEEK_SET);
	fputc(c,fPtr);
	return c;
 }