Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
17
Добавлен:
01.05.2014
Размер:
2.14 Кб
Скачать
	#ifndef FILEBASE_H					/* filebase.h header */
	#define FILEBASE_H

	#include	<iostream.h>
	#include 	<fstream.h>
	#include	<sys/types.h>
	#include	<sys/stat.h>
	#include	<unistd.h>
	#include	<utime.h>
	#include 	<fcntl.h>
	#include 	<string.h>

	typedef enum 	{	REG_FILE='r', 	DIR_FILE='d', 	CHAR_FILE='c', 
				BLK_FILE='b', 	PIPE_FILE='p', 	SYM_FILE='s',
				UNKNOWN_FILE='?'	} 
		FILE_TYPE_ENUM;

	/* A base class to encapsulate POSIX and UNIX file objects' properties */
	class filebase	:	public fstream 
	{
		protected:
			char*	filename;	
			friend ostream& operator<<(ostream& os, filebase& fobj)
			{
			        /* display file attributes in UNIX ls -l command output format */
				return os;
			};
		public:
			filebase()				{	;					};
			filebase(const char* fn, int flags, int prot=filebuf::openprot) 
                                      : fstream(fn,flags,prot)
			{
				filename = new char[strlen(fn)+1];
				strcpy(filename,fn);
			};
			virtual ~filebase()			{ 	delete filename; 			};
			int  fileno()				{ 	return rdbuf()->fd();			};
			virtual int create( const char* fn, mode_t mode)			
								{	return ::creat(fn, mode);		};
			int chmod(mode_t mode)			{	return ::chmod (filename, mode);	};
			int chown( uid_t uid, gid_t gid)	{	return ::chown(filename, uid, gid);	};
			int utime(const struct utimbuf *timbuf_Ptr)			
								{	return ::utime(filename,timbuf_Ptr);	};
			int link( const char* new_link)		{	return ::link(filename,new_link);	};
			virtual int remove( )			{	return ::unlink(filename);		};
			// Query a filebase object's file type
			FILE_TYPE_ENUM file_type() 
			{
				struct stat	statv;
				if (stat(filename,&statv)==0)
					switch (statv.st_mode & S_IFMT)	
					{
						case 	S_IFREG:		return REG_FILE;		// regular file
						case 	S_IFDIR:		return DIR_FILE;		// directory file
						case 	S_IFCHR:		return CHAR_FILE;		// char device file
						case 	S_IFIFO:		return PIPE_FILE;		// block device file
						case 	S_IFLNK:		return SYM_FILE;		// symbolic link file
					}
				return UNKNOWN_FILE;
			};
	};

	#endif 		/* filebase.h */
Соседние файлы в папке ch7