Minor update: code compacting
This commit is contained in:
73
FileCore.cpp
73
FileCore.cpp
@@ -39,8 +39,7 @@ CFileCore::~CFileCore()
|
||||
TFileHandle * NextFile = NULL;
|
||||
|
||||
// Destroy file specs
|
||||
while (FirstFile)
|
||||
{
|
||||
while (FirstFile) {
|
||||
// Close file if open
|
||||
CloseFile( FirstFile );
|
||||
|
||||
@@ -63,19 +62,16 @@ TFileHandle * CFileCore::AddFile( const char * Name, const char * Path, bool Ap
|
||||
TFileHandle ** FileHandle = NULL;
|
||||
|
||||
// Validate
|
||||
if (!Name || !*Name || !Path || !*Path) {
|
||||
if (!Name || !*Name || !Path || !*Path)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Find File or End of list
|
||||
FileHandle = &FirstFile;
|
||||
while (*FileHandle && strcmp( Name, (*FileHandle)->Name )) {
|
||||
while (*FileHandle && strcmp( Name, (*FileHandle)->Name ))
|
||||
FileHandle = &((*FileHandle)->Next);
|
||||
}
|
||||
|
||||
// Check if found
|
||||
if (!*FileHandle)
|
||||
{
|
||||
if (!*FileHandle) {
|
||||
// Create new
|
||||
*FileHandle = (TFileHandle*)calloc( 1, sizeof(TFileHandle) );
|
||||
|
||||
@@ -85,9 +81,8 @@ TFileHandle * CFileCore::AddFile( const char * Name, const char * Path, bool Ap
|
||||
}
|
||||
|
||||
// Create Channel if necessary
|
||||
if (CreateChannel) {
|
||||
if (CreateChannel)
|
||||
AddChannel( Name, CH_ready );
|
||||
}
|
||||
|
||||
// Set Parameters
|
||||
(*FileHandle)->Append = Append;
|
||||
@@ -101,9 +96,8 @@ TFileHandle * CFileCore::AddFile( const char * Name, const char * Path, bool Ap
|
||||
bool CFileCore::SetFilePersistence( TFileHandle * FileHandle, bool Persistent, int PersistTimeout )
|
||||
{
|
||||
// Validate
|
||||
if (!FileHandle) {
|
||||
if (!FileHandle)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set parameters
|
||||
FileHandle->Persistent = Persistent;
|
||||
@@ -126,19 +120,16 @@ bool CFileCore::SetFilePersistence( TFileHandle * FileHandle, bool Persistent, i
|
||||
bool CFileCore::OpenFile( TFileHandle * FileHandle )
|
||||
{
|
||||
// Validate
|
||||
if (!FileHandle) {
|
||||
if (!FileHandle)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Open file if not already open
|
||||
if (!isOpen( FileHandle ))
|
||||
{
|
||||
if (!isOpen( FileHandle )) {
|
||||
// GEt file handle
|
||||
(FileHandle)->FD = open( FileHandle->Path, O_WRONLY | O_CREAT | ((FileHandle->Append)? O_APPEND : O_TRUNC), 0664 );
|
||||
|
||||
// Report event
|
||||
if (isOpen(FileHandle))
|
||||
{
|
||||
if (isOpen(FileHandle)) {
|
||||
// Set timer
|
||||
SetStartTime( &(FileHandle->PersistTime) );
|
||||
|
||||
@@ -149,8 +140,7 @@ bool CFileCore::OpenFile( TFileHandle * FileHandle )
|
||||
}
|
||||
|
||||
// Check if failed
|
||||
if (!isOpen(FileHandle))
|
||||
{
|
||||
if (!isOpen(FileHandle)) {
|
||||
// Report result
|
||||
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: File '%s' - Could not open (%d) %s",
|
||||
ProcessName, Name, FileHandle->Name, errno, strerror(errno) );
|
||||
@@ -164,13 +154,11 @@ bool CFileCore::OpenFile( TFileHandle * FileHandle )
|
||||
bool CFileCore::CloseFile( TFileHandle * FileHandle )
|
||||
{
|
||||
// Validate
|
||||
if (!FileHandle) {
|
||||
if (!FileHandle)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close file if not already open
|
||||
if (isOpen(FileHandle))
|
||||
{
|
||||
if (isOpen(FileHandle)) {
|
||||
// Close file
|
||||
close( FileHandle->FD );
|
||||
FileHandle->FD = NO_FD;
|
||||
@@ -179,7 +167,8 @@ bool CFileCore::CloseFile( TFileHandle * FileHandle )
|
||||
if (!isOpen(FileHandle)) {
|
||||
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: File '%s' - Closed",
|
||||
ProcessName, Name, FileHandle->Name );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: File '%s' - Could not close",
|
||||
ProcessName, Name, FileHandle->Name );
|
||||
}
|
||||
@@ -196,13 +185,11 @@ int CFileCore::ReadFromFD( int FD, char * Data, int MaxLen )
|
||||
bool Error = false;
|
||||
|
||||
// Check if buffer created
|
||||
if ((FD == -1) || !Data) {
|
||||
if ((FD == -1) || !Data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read Data into buffer
|
||||
while (DataRemain)
|
||||
{
|
||||
while (DataRemain) {
|
||||
// Read from file descriptor
|
||||
BytesRead = read( FD, &Data[TotalRead], DataRemain );
|
||||
if (BytesRead <= 0) {
|
||||
@@ -215,11 +202,9 @@ int CFileCore::ReadFromFD( int FD, char * Data, int MaxLen )
|
||||
TotalRead += BytesRead;
|
||||
DataRemain -= BytesRead;
|
||||
|
||||
if (DataRemain) {
|
||||
if (DataRemain)
|
||||
usleep( 500 );
|
||||
}
|
||||
}
|
||||
|
||||
return (Error)? -TotalRead : TotalRead; // Report negative total on error
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
@@ -232,13 +217,11 @@ int CFileCore::WriteToFD( int FD, const char * Data, int Len )
|
||||
bool Error = false;
|
||||
|
||||
// Check if buffer created
|
||||
if ((FD == -1) || !DataRemain) {
|
||||
if ((FD == -1) || !DataRemain)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read Data into buffer
|
||||
while (DataRemain)
|
||||
{
|
||||
while (DataRemain) {
|
||||
// Read from file descriptor
|
||||
BytesWritten = write( FD, &Data[TotalWritten], DataRemain );
|
||||
if ((BytesWritten <= 0) && (errno != EAGAIN)) {
|
||||
@@ -251,9 +234,8 @@ int CFileCore::WriteToFD( int FD, const char * Data, int Len )
|
||||
TotalWritten += BytesWritten;
|
||||
DataRemain -= BytesWritten;
|
||||
|
||||
if (DataRemain) {
|
||||
if (DataRemain)
|
||||
usleep( 500 );
|
||||
}
|
||||
}
|
||||
return (Error)? -TotalWritten : TotalWritten; // Report negative total on error
|
||||
}
|
||||
@@ -266,16 +248,13 @@ int CFileCore::Input( const char * ChannelName, const char * SourceRef, const ch
|
||||
int BytesWritten = 0;
|
||||
|
||||
// Validate
|
||||
if (!ChannelName || !Data) {
|
||||
if (!ChannelName || !Data)
|
||||
return 0;
|
||||
}
|
||||
else if (Len == -1) {
|
||||
else if (Len == -1)
|
||||
Len = strlen( Data );
|
||||
};
|
||||
|
||||
// Get Channel
|
||||
if (!(FileHandle = GetFile( ChannelName )))
|
||||
{
|
||||
if (!(FileHandle = GetFile( ChannelName ))) {
|
||||
// Log event
|
||||
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s' - Input rejected, Channel not found",
|
||||
ProcessName, Name, ChannelName );
|
||||
@@ -287,9 +266,8 @@ int CFileCore::Input( const char * ChannelName, const char * SourceRef, const ch
|
||||
ProcessName, Name, ChannelName );
|
||||
|
||||
// Open file
|
||||
if (!OpenFile( FileHandle )) {
|
||||
if (!OpenFile( FileHandle ))
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Handle Incoming data
|
||||
BytesWritten = WriteToFD( FileHandle->FD, Data, Len );
|
||||
@@ -306,8 +284,7 @@ bool CFileCore::Process()
|
||||
|
||||
// Close Persistent files not used
|
||||
FileHandle = FirstFile;
|
||||
while (FileHandle)
|
||||
{
|
||||
while (FileHandle) {
|
||||
if (isOpen(FileHandle) &&
|
||||
(!FileHandle->Persistent ||
|
||||
(FileHandle->PersistTimeout && Timeout( FileHandle->PersistTime, FileHandle->PersistTimeout )))) {
|
||||
|
||||
Reference in New Issue
Block a user