#include "cfile.h"

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

cTBFile::cTBFile(void) {
	m_Filename = NULL;
}

cTBFile::~cTBFile() {
}

bool cTBFile::Open(const char *Filename, int Flags, mode_t Mode) {
	char *newfilename;
	int   newfiled;

	if (IsOpen())
		Close();

	if ((newfiled = ::open(Filename, Flags, Mode)) == -1)
		return false;

	if (!Open(newfiled))
		return false;

	if (::fcntl(FileNo(), F_SETFL, O_NONBLOCK) == -1)
		return false;

	newfilename = new char[strlen(Filename) + 1];
	strcpy(newfilename, Filename);

	m_Filename = newfilename;
	return true;
}

bool cTBFile::Close(void) {
	if (m_Filename) {
		delete[] m_Filename;
		m_Filename = NULL;
	}
	return cTBFileNo::Close();
}

bool cTBFile::Unlink(void) const {
	if (!m_Filename)
		return false;

	return cTBFile::Unlink(m_Filename);
}

bool cTBFile::Unlink(const char *Filename) {
	return (::unlink(Filename) != -1);
}

