This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fstream Vs. File Descriptor


Hi All

I had a Class which opened and locked the files. We were using fstream
object. Now I am shifting basic C calls, open/write, using file
descriptor. 

Following is the code for original class as well as new class. Issue :
New class is not working. And I am not able to find the error.


#############...............Original
Flock.cpp............................
#include <stdio.h>

#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <iostream>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <iostream>
#include <fstream>

#define FILE_READ_LOCK				(ios::in  |
IOS_NOCREATE)
#define FILE_READ_CREATE_LOCK		(ios::in)
#define FILE_WRITE_LOCK				(ios::out | ios::trunc |
IOS_NOCREATE)
#define FILE_READWRITE_LOCK			(ios::in  | ios::out   |
ios::trunc | IOS_NOCREATE)
#define FILE_WRITE_CREATE_LOCK	 	(ios::out | ios::trunc)
#define FILE_READWRITE_CREATE_LOCK	(ios::in  | ios::out   |
ios::trunc)
#define FILE_WRITE_EXCL_LOCK	 	(ios::out | ios::trunc |
IOS_NOREPLACE)
#define FILE_READWRITE_EXCL_LOCK	(ios::out | ios::trunc |
IOS_NOREPLACE)

class FLock
{
public:
	FLock( const char *fileName = 0, int op = 0);
	bool OpenAndLockFile( const char *fileName, int op);
	bool CloseAndUnLockFile( void );
	bool WriteLockFile( void );
	bool LockFile(  );
	bool UnLockFile( void );
	void CloseFile( void );
	char ReadCharacter( void );
	bool ReadLine( char * aline, int dwsize );
	bool WriteLine( char * aline, int dwsize );
	long ReadBlk( char * buf, long bufsize );
	long WriteBlk( char * buf, long bufsize );
	int GetFileDescriptor(void);
	inline fstream& GetIoStream() { return iostrm; }
private:
	fstream iostrm;
};

FLock::FLock( const char *fileName, int op )
{
	  OpenAndLockFile(fileName, op); // calls clearinfo
}

bool FLock::OpenAndLockFile( const char *fileName, int op)
{
        errno = 0;

        iostrm.open(fileName, op | ios::out); //lockf doesnt permit for
locks on files opened in read mode.

        if (!iostrm)
		{
 			cout<<"OpenAndLockFile - Cannot open file
"<<fileName<<endl;
			return false;
		}
        else
		{
            switch (op)
			{
                case FILE_READ_LOCK:                // in
                case FILE_READ_CREATE_LOCK:         // in
                case FILE_READWRITE_LOCK:           // in, out, trunc
                case FILE_WRITE_EXCL_LOCK:          // out, trunc,
noreplace
                case FILE_WRITE_LOCK:               // out, trunc,
nocreate
                case FILE_WRITE_CREATE_LOCK:        // out, trunc
                case FILE_READWRITE_CREATE_LOCK:    // in, out, trunc
                    if (LockFile() == false) {
                        fprintf(stderr, "lock failed, %s\n", strerror(
errno ) );
                        CloseFile();
                    }
                    else
                        return true;
                    break;

                default:
                    break;
			}
		}

	return false;
}

bool FLock::CloseAndUnLockFile( void )
{
	if (!UnLockFile())
		return false;

	CloseFile();

	return true;
}

bool FLock::LockFile( void )
{
	int fd = GetFileDescriptor();
	return !lockf(fd, F_LOCK, 0);
}

bool FLock::UnLockFile( void )
{
	int fd = GetFileDescriptor();

	return !lockf(fd, F_ULOCK, 0);
}

void FLock::CloseFile( void )
{
	filebuf *b = iostrm.rdbuf();
	if (b && b->is_open())
		iostrm.close();
}

char FLock::ReadCharacter( void )
{
	char c;
	if (!iostrm)
		return EOF;

	iostrm.get(c);
	return c;
}

bool FLock::ReadLine(char * aline, DWORD dwsize)
{
	if (!iostrm)
		return false;

	iostrm.getline(aline, dwsize, '\n');
	return (!iostrm == false);
}

bool FLock::WriteLine( char * aline, DWORD dwsize)
{
	if (!iostrm)
		return false;

	iostrm.write(aline, dwsize);
	return (!iostrm == false);
}

long FLock::ReadBlk( char * buf, long bufsize)
{
	iostrm.read(buf, bufsize);
	if (!iostrm.bad())
		return iostrm.gcount();
	else
		return 0;
}

long FLock::WriteBlk( char * buf, long bufsize)
{
	iostrm.write(buf, bufsize);
	if (!iostrm.bad())
		return bufsize;
	else
		return 0;
}

int FLock::GetFileDescriptor()
{
	filebuf *b = iostrm.rdbuf();
	return b ? b->fd() : -1;
}

#############...........................................................
..

AND the changed file is


#############...............Modified
Flock.cpp............................

#include <stdio.h>

#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <iostream>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <iostream>
#include <fstream>

#define FILE_READ_LOCK              O_RDONLY
#define FILE_READ_CREATE_LOCK       O_RDONLY | O_CREAT
#define FILE_WRITE_LOCK             O_WRONLY | O_TRUNC
#define FILE_READWRITE_LOCK         O_RDWR | O_TRUNC
#define FILE_WRITE_CREATE_LOCK      O_WRONLY | O_CREAT | O_TRUNC
#define FILE_READWRITE_CREATE_LOCK  O_RDWR | O_CREAT | O_TRUNC
#define FILE_WRITE_EXCL_LOCK        O_WRONLY | O_TRUNC | O_CREAT |
O_EXCL
#define FILE_READWRITE_EXCL_LOCK    O_WRONLY | |O_TRUNC | O_CREAT |
O_EXCL

class FLock
{
public:
	FLock( const char *fileName = 0, int op = 0);
	bool OpenAndLockFile( const char *fileName, int op);
	bool CloseAndUnLockFile( void );
	bool WriteLockFile( void );
	bool LockFile(  );
	bool UnLockFile( void );
	void CloseFile( void );
	char ReadCharacter( void );
	bool ReadLine( char * aline, int dwsize );
	bool WriteLine( char * aline, int dwsize );
	long ReadBlk( char * buf, long bufsize );
	long WriteBlk( char * buf, long bufsize );
	int GetFileDescriptor(void);
	ssize_t readline(int, void *, size_t);
private:
	int m_fhFLock;
};

FLock::FLock( const char *fileName, int op )
{
	  OpenAndLockFile(fileName, op); // calls clearinfo
}

bool FLock::OpenAndLockFile( const char *fileName, int op)
{
        errno = 0;

        m_fhFLock=open(fileName, op|O_WRONLY, S_IRUSR | S_IWUSR |
S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);//lockf doesnt permit for locks
on files opened in read mode.

       if (m_fhFLock==-1)
		{
 			cout<<"OpenAndLockFile - Cannot open file
"<<fileName<<endl;
			return false;
		}
        else
		{
            switch (op)
			{
                case FILE_READ_LOCK:                // in
                case FILE_READ_CREATE_LOCK:         // in
                case FILE_READWRITE_LOCK:           // in, out, trunc
                case FILE_WRITE_EXCL_LOCK:          // out, trunc,
noreplace
                case FILE_WRITE_LOCK:               // out, trunc,
nocreate
                case FILE_WRITE_CREATE_LOCK:        // out, trunc
                case FILE_READWRITE_CREATE_LOCK:    // in, out, trunc
                    if (LockFile() == false) {
                        fprintf(stderr, "lock failed, %s\n", strerror(
errno ) );
                        CloseFile();
                    }
                    else
                        return true;
                    break;

                default:
                    break;
			}
		}

	return false;
}

bool FLock::CloseAndUnLockFile( void )
{
	if (!UnLockFile())
		return false;

	CloseFile();

	return true;
}

bool FLock::LockFile( void )
{
	int fd = GetFileDescriptor();
	return !lockf(fd, F_LOCK, 0);
}

bool FLock::UnLockFile( void )
{
	int fd = GetFileDescriptor();

	return !lockf(fd, F_ULOCK, 0);
}

void FLock::CloseFile( void )
{
	if (m_fhFLock !=-1)
		close(m_fhFLock);

}

char FLock::ReadCharacter( void )
{
	char c;
	if (m_fhFLock==-1)
		return EOF;

	read(m_fhFLock,&c,1);
	return c;
}

bool FLock::ReadLine(char * aline, DWORD dwsize)
{
	if (m_fhFLock==-1)
		return false;

	ssize_t status=readline(m_fhFLock,aline,dwsize);

	if (status == -1)
		return false;
	else
		return true;
}

bool FLock::WriteLine( char * aline, DWORD dwsize)
{
	if (m_fhFLock==-1)
		return false;

	ssize_t status=write(m_fhFLock,aline,dwsize);

	if (status == -1)
		return false;
	else
		return true;
}

long FLock::ReadBlk( char * buf, long bufsize)
{
	ssize_t status=read(m_fhFLock, buf, bufsize);
	if (status != -1)
		return status;
	else
		return 0;
}

long FLock::WriteBlk( char * buf, long bufsize)
{
	ssize_t status=write(m_fhFLock,buf,bufsize);
	if (status!=-1)
		return bufsize;
	else
		return 0;
}

int FLock::GetFileDescriptor()
{

    if (m_fhFLock < 0)
        return -1;
    else
        return m_fhFLock;
}

ssize_t FileLock::readline(int fd, void *vptr, size_t maxlen)
{
    int     n, rc;
    char    c, *ptr;

    ptr = (char *)vptr;
    for (n = 1; n < maxlen; n++) {
		again:
		if ( (rc = read(fd, &c, 1)) == 1) {
			*ptr++ = c;
			if (c == '\n')
				break;	/* newline is stored, like
fgets() */
		} else if (rc == 0) {
			if (n == 1)
				return -1;	/* EOF, no data read */
			else
				break;		/* EOF, some data was
read */
		} else
		{
			if (errno == EINTR)
				goto again;
			return(-1);		/* error, errno set by
read() */
		}
	}

	*ptr = '\0';	/* null terminate like fgets() */
	return(n);

}


#############...........................................................
.



Can somebody please tell me -- WHERE AM I WORNG.. :((((

-Ajay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]