From 7dfdfdb4ce4625cc38cc11a5823e8371c79bfe82 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Fri, 22 Jul 2022 11:56:19 +0200 Subject: [PATCH] Minor update: code compacting --- CharBufferCore.cpp | 3 +- DeviceCore.cpp | 181 +++++++++++++++---------------------------- EventBufferCore.cpp | 14 +--- FileCore.cpp | 73 ++++++----------- FunctionCore.cpp | 50 ++++-------- ItemBufferCore.cpp | 12 +-- LiteProtocolCore.cpp | 24 ++---- SelectCore.cpp | 11 ++- SelectableBare.cpp | 6 +- WatchdogCore.cpp | 3 +- 10 files changed, 129 insertions(+), 248 deletions(-) diff --git a/CharBufferCore.cpp b/CharBufferCore.cpp index 4cbd2a6..2dcd93c 100644 --- a/CharBufferCore.cpp +++ b/CharBufferCore.cpp @@ -284,8 +284,7 @@ bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundP // Search for needle in haystack FoundPos = StartPos; - while (FoundPos < BufLen) - { + while (FoundPos < BufLen) { // Check if char match if (*CheckChar == *MatchChar) { if (MatchChar == MatchLimit) { diff --git a/DeviceCore.cpp b/DeviceCore.cpp index 17b635f..1d2d2e1 100644 --- a/DeviceCore.cpp +++ b/DeviceCore.cpp @@ -141,8 +141,7 @@ bool CDeviceCore::InitDevices( CDataMember * FunctionConfig ) // Load Device Types DeviceConfig = Config->GetChFirstChild( "DeviceTypes", true ); - while (DeviceConfig) - { + while (DeviceConfig) { DeviceType = (char*)DeviceConfig->GetName(); if (!DeviceConfig->isObject()) { if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Invalid device type config for '%s'", @@ -177,8 +176,7 @@ bool CDeviceCore::InitDevices( CDataMember * FunctionConfig ) // Load Actual Devices DeviceConfig = Config->GetChFirstChild( "Devices", true ); - while (DeviceConfig) - { + while (DeviceConfig) { DeviceName = (char*)DeviceConfig->GetName(); if (!DeviceConfig->isObject()) { if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Invalid device config for '%s'", @@ -193,9 +191,8 @@ bool CDeviceCore::InitDevices( CDataMember * FunctionConfig ) DataPath = (char*)DeviceConfig->GetChStr( "DataPath", NULL ); Device = AddDevice( DeviceName, DeviceType, DeviceID, DeviceAddr, DataPath ); - if (Device) { + if (Device) InitDeviceParams( Device, DeviceConfig, Device->DataNode ); - } DeviceConfig = DeviceConfig->GetNextPeer(); } return true; @@ -222,7 +219,6 @@ bool CDeviceCore::InitDeviceParams( TDevice * Device, CDataMember * DeviceConfi InitDeviceParam( Device, ParamConfig, NULL, ((isType)? NULL : Device->DataPath), ParentNode, NULL ); ParamConfig = ParamConfig->GetNextPeer(); } - return true; } //--------------------------------------------------------------------------- @@ -233,8 +229,7 @@ bool CDeviceCore::CopyTemplateParam( TDevice * Device, TDeviceParam * Template, CDataMember * DataNode = NULL; char * DataPath = NULL; - if ((Param = AddDeviceParam( Device, Template->Name, Template->DataType, Template->Len ))) - { + if ((Param = AddDeviceParam( Device, Template->Name, Template->DataType, Template->Len ))) { SetParamAccess( Param, Template->Read, Template->Write ); if (Template->DataPath) { @@ -332,7 +327,8 @@ bool CDeviceCore::InitDeviceParam( TDevice * Device, CDataMember * ParamConfig, if (ParentName && *ParentName) { FullName = (char*)malloc( strlen(ParentName) + strlen(NodeName) + 2 ); sprintf( FullName, "%s_%s", ParentName, NodeName ); - } else { + } + else { FullName = strdup( NodeName ); } @@ -341,17 +337,17 @@ bool CDeviceCore::InitDeviceParam( TDevice * Device, CDataMember * ParamConfig, if (ParentPath && *ParentPath) { DataPath = (char*)malloc( strlen(ParentPath) + strlen(NodeName) + 2 ); sprintf( DataPath, "%s/%s", ParentPath, NodeName ); - } else { + } + else { DataPath = strdup( NodeName ); } if ((Children = ParamConfig->GetChild( "Children", false ))) { // Data Branch - if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false ))) { + if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false ))) ParamGroup = AddParamGroup( Device, ParamGroupName ); - } else { + else ParamGroup = ParentParamGroup; - } if (Children->isString()) { // Load from file @@ -414,11 +410,11 @@ bool CDeviceCore::InitDeviceParam( TDevice * Device, CDataMember * ParamConfig, } } - if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false ))) { + if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false ))) ParamGroup = AddParamGroup( Device, ParamGroupName ); - } else { + else ParamGroup = ParentParamGroup; - } + if (ParamGroup) AddParamItem( ParamGroup, Param ); @@ -492,10 +488,8 @@ void CDeviceCore::ValidReplyReceived() bool CDeviceCore::CheckReplyTimeout() { // Check for Reply timeout - if (!WaitingForReply || !Timeout( PollWait, ReplyTimeout )) { - // No timeout + if (!WaitingForReply || !Timeout( PollWait, ReplyTimeout )) return false; - } // Update flags WaitingForReply = false; @@ -503,8 +497,7 @@ bool CDeviceCore::CheckReplyTimeout() PollRetry++; // Handle No Reply / Retry - if (PollRetry > MaxRetries) - { + if (PollRetry > MaxRetries) { if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s' - Device '%s' %s - Max retries", ProcessName, Name, DeviceChannel->Name, ActiveDevice->Name, ((InvalidReply)? "invalid reply" : "timeout"), PollRetry ); @@ -587,9 +580,8 @@ TDevice * CDeviceCore::AddDevice( const char * DeviceName, const char * DeviceTy (*Device)->Name = strdup( DeviceName ); (*Device)->ID = DeviceID; - if (DeviceAddress) { + if (DeviceAddress) (*Device)->Address = strdup( DeviceAddress ); - } if (DeviceType && *DeviceType) { (*Device)->Type = strdup( DeviceType ); @@ -638,12 +630,10 @@ bool CDeviceCore::DestroyDevice( TDevice ** Device ) free( (*Device)->DataPath ); // Destroy parameters & groups - while ((*Device)->FirstParam) { + while ((*Device)->FirstParam) DestroyDeviceParam( &((*Device)->FirstParam) ); - } - while ((*Device)->FirstParamGroup) { + while ((*Device)->FirstParamGroup) DestroyParamGroup( &((*Device)->FirstParamGroup) ); - } // Destroy Device delete *Device; @@ -684,8 +674,7 @@ TDeviceParam * CDeviceCore::AddDeviceParam( TDevice * Device, const char * Param (*Param)->Device = Device; // Init values - switch (DataType) - { + switch (DataType) { case dtBool : // Create Value pointer (*Param)->Value = (bool*)malloc( sizeof(bool) ); @@ -861,9 +850,8 @@ bool CDeviceCore::DestroyParamGroup( TDeviceParamGroup ** ParamGroup ) free( (*ParamGroup)->Name ); // Destroy items - while ((*ParamGroup)->FirstParam) { + while ((*ParamGroup)->FirstParam) DestroyParamItem( &((*ParamGroup)->FirstParam) ); - } // Destroy ParamGroup delete *ParamGroup; @@ -986,11 +974,9 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val if (!Param) return false; - switch (Param->DataType) - { + switch (Param->DataType) { case dtBool : - if (Init || (*((bool*)Param->Value) != Value)) - { + if (Init || (*((bool*)Param->Value) != Value)) { // Set new value & mark change *((bool*)Param->Value) = Value; @@ -1008,8 +994,7 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val break; case dtUnsigned16 : - if (Init || (*((u_int16_t*)Param->Value) != Value)) - { + if (Init || (*((u_int16_t*)Param->Value) != Value)) { // Set new value & mark change *((u_int16_t*)Param->Value) = Value; @@ -1028,8 +1013,7 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val case dtUnsigned32_HL : case dtUnsigned32_LH : - if (Init || (*((u_int32_t*)Param->Value) != Value)) - { + if (Init || (*((u_int32_t*)Param->Value) != Value)) { // Set new value & mark change *((u_int32_t*)Param->Value) = Value; @@ -1049,8 +1033,7 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val case dtFloat32_L : case dtFloat32_B : // Special case, where float value is stored in Integer memory (e.g. modbus register) - if (Init || (*((u_int32_t*)Param->Value) != Value)) - { + if (Init || (*((u_int32_t*)Param->Value) != Value)) { // Set new value & mark change *((u_int32_t*)Param->Value) = Value; @@ -1088,11 +1071,9 @@ bool CDeviceCore::UpdateSignedValue( TDeviceParam * Param, const int32_t Value, if (!Param) return false; - switch (Param->DataType) - { + switch (Param->DataType) { case dtBool : - if (Init || (*((bool*)Param->Value) != Value)) - { + if (Init || (*((bool*)Param->Value) != Value)) { // Set new value & mark change *((bool*)Param->Value) = Value; @@ -1110,8 +1091,7 @@ bool CDeviceCore::UpdateSignedValue( TDeviceParam * Param, const int32_t Value, break; case dtSigned16 : - if (Init || (*((int16_t*)Param->Value) != Value)) - { + if (Init || (*((int16_t*)Param->Value) != Value)) { // Set new value & mark change *((int16_t*)Param->Value) = Value; @@ -1130,8 +1110,7 @@ bool CDeviceCore::UpdateSignedValue( TDeviceParam * Param, const int32_t Value, case dtSigned32_HL : case dtSigned32_LH : - if (Init || (*((int32_t*)Param->Value) != Value)) - { + if (Init || (*((int32_t*)Param->Value) != Value)) { // Set new value & mark change *((int32_t*)Param->Value) = Value; @@ -1169,12 +1148,10 @@ bool CDeviceCore::UpdateFloatValue( TDeviceParam * Param, const float Value, boo if (!Param) return false; - switch (Param->DataType) - { + switch (Param->DataType) { case dtFloat32_L : case dtFloat32_B : - if (Init || (*((float*)Param->Value) != Value)) - { + if (Init || (*((float*)Param->Value) != Value)) { // Set new value & mark change *((float*)Param->Value) = Value; @@ -1212,12 +1189,10 @@ bool CDeviceCore::UpdateStringValue( TDeviceParam * Param, const char * Value, c if (!Param || (Param->DataType != dtString)) return false; - switch (Param->DataType) - { + switch (Param->DataType) { case dtString : // Update register - if (Init || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len)) - { + if (Init || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len)) { if (Len >= Param->Len) { // Copy full register length (ignore additional bytes) memcpy( Param->Value, Value, Param->Len ); @@ -1262,11 +1237,9 @@ bool CDeviceCore::SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value, if (!Param) return false; - switch (Param->DataType) - { + switch (Param->DataType) { case dtBool : - if (Force || (*((bool*)Param->SetValue) != Value)) - { + if (Force || (*((bool*)Param->SetValue) != Value)) { // Set new value *((bool*)Param->SetValue) = Value; @@ -1276,8 +1249,7 @@ bool CDeviceCore::SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value, break; case dtUnsigned16 : - if (Force || (*((u_int16_t*)Param->SetValue) != Value)) - { + if (Force || (*((u_int16_t*)Param->SetValue) != Value)) { // Set new value *((u_int16_t*)Param->SetValue) = Value; @@ -1288,8 +1260,7 @@ bool CDeviceCore::SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value, case dtUnsigned32_HL : case dtUnsigned32_LH : - if (Force || (*((u_int32_t*)Param->SetValue) != Value)) - { + if (Force || (*((u_int32_t*)Param->SetValue) != Value)) { // Set new value *((u_int32_t*)Param->SetValue) = Value; @@ -1313,11 +1284,9 @@ bool CDeviceCore::SetSignedValue( TDeviceParam * Param, const int32_t Value, boo if (!Param) return false; - switch (Param->DataType) - { + switch (Param->DataType) { case dtBool : - if (Force || (*((bool*)Param->SetValue) != Value)) - { + if (Force || (*((bool*)Param->SetValue) != Value)) { // Set new value *((bool*)Param->SetValue) = Value; @@ -1327,8 +1296,7 @@ bool CDeviceCore::SetSignedValue( TDeviceParam * Param, const int32_t Value, boo break; case dtSigned16 : - if (Force || (*((int16_t*)Param->SetValue) != Value)) - { + if (Force || (*((int16_t*)Param->SetValue) != Value)) { // Set new value *((int16_t*)Param->SetValue) = Value; @@ -1339,8 +1307,7 @@ bool CDeviceCore::SetSignedValue( TDeviceParam * Param, const int32_t Value, boo case dtSigned32_HL : case dtSigned32_LH : - if (Force || (*((int32_t*)Param->SetValue) != Value)) - { + if (Force || (*((int32_t*)Param->SetValue) != Value)) { // Set new value *((int32_t*)Param->SetValue) = Value; @@ -1364,12 +1331,10 @@ bool CDeviceCore::SetFloatValue( TDeviceParam * Param, const float Value, bool F if (!Param) return false; - switch (Param->DataType) - { + switch (Param->DataType) { case dtFloat32_L : case dtFloat32_B : - if (Force || (*((float*)Param->SetValue) != Value)) - { + if (Force || (*((float*)Param->SetValue) != Value)) { // Set new value *((float*)Param->SetValue) = Value; @@ -1394,8 +1359,7 @@ bool CDeviceCore::SetStringValue( TDeviceParam * Param, const char * Value, cons return false; // Update register - if (Force || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len)) - { + if (Force || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len)) { if (Len >= Param->SetLen) { // Copy full register length (ignore additional bytes) memcpy( Param->SetValue, Value, Param->SetLen ); @@ -1429,13 +1393,11 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int return false; // Check if string - if (Param->DataType == dtString) - { + if (Param->DataType == dtString) { // Simply set string SetStringValue( Param, Value, Len, Force ); } - else - { + else { // Ensure string is zero terminated if (Value[Len] != 0) { TempStr = strndup( Value, Len ); @@ -1444,8 +1406,7 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int } // Convert to correct type - switch (Param->DataType) - { + switch (Param->DataType) { case dtBool : case dtUnsigned16 : case dtUnsigned32_HL : @@ -1472,9 +1433,8 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int } // Clear memory - if (UseTempStr) { + if (UseTempStr) free( TempStr ); - } } return true; } @@ -1489,8 +1449,7 @@ bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len ) } // Check if return value longer than actual value - switch (Param->DataType) - { + switch (Param->DataType) { case dtBool : sprintf( Value, "%u", (*((bool*)Param->Value)) ); break; @@ -1519,14 +1478,12 @@ bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len ) break; case dtString : - if (Len < Param->Len) - { + if (Len < Param->Len) { // Only copy requested no of chars memcpy( Value, Param->Value, Len ); Value[ Len ] = 0; } - else - { + else { memcpy( Value, Param->Value, Param->Len ); Value[ Param->Len ] = 0; } @@ -1548,15 +1505,14 @@ bool CDeviceCore::CompareParamString( const char * ParamValue, const int ParamLe // Compare Value against current value if (Len >= ParamLen) { // Compare full register length (ignore additional bytes) - if (memcmp( Value, ParamValue, ParamLen)) { + if (memcmp( Value, ParamValue, ParamLen)) Match = false; - } } else { // Compare length of new value only - if (memcmp( ParamValue, Value, Len)) { + if (memcmp( ParamValue, Value, Len)) Match = false; - } + // Remaining bytes must be null else { for (Pos = (char*)ParamValue; Pos < (char*)ParamValue + ParamLen; Pos++) { @@ -1580,12 +1536,10 @@ bool CDeviceCore::TimedParamEvents() // Loop through all devices Device = FirstDevice; - while (Device) - { + while (Device) { // Loop through all Read parameters - while ((Param = GetNextReadParam( Device, Param ))) { + while ((Param = GetNextReadParam( Device, Param ))) EventOutput( Param, false ); - } Device = Device->Next; } return true; @@ -1641,8 +1595,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef, // Get command command GetCmdParam( Data, Value, &NextParam ); - if (!strcasecmp( "get", Value )) - { + if (!strcasecmp( "get", Value )) { // Validate parameters if (!GetCmdParam( NextParam, Value, &NextParam)) { strcpy( OutputStr, "- Missing device name" ); @@ -1686,8 +1639,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef, Output( ChannelName, NULL, true, OutputStr, strlen(OutputStr) ); } } - else if (!strcasecmp( "set", Value )) - { + else if (!strcasecmp( "set", Value )) { // Validate parameters if (!GetCmdParam( NextParam, Value, &NextParam)) { strcpy( OutputStr, "- Missing device name" ); @@ -1732,8 +1684,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef, Output( ChannelName, NULL, true, OutputStr, strlen(OutputStr) ); } } - else if (!strcasecmp( "status", Value )) - { + else if (!strcasecmp( "status", Value )) { // Validate parameters if (!GetCmdParam( NextParam, Value, &NextParam)) { strcpy( OutputStr, "- Missing device name" ); @@ -1760,8 +1711,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef, Output( ChannelName, NULL, true, OutputStr, strlen(OutputStr) ); } } - else - { + else { // Unrecognised command strcpy( OutputStr, "- Unknown command"); Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s', %s%s", @@ -1786,10 +1736,7 @@ bool CDeviceCore::EventOutput( TDeviceParam * Param, bool Force ) return false; // Check Timer or force - if (Force || - (Param->EventInterval && Timeout( Param->EventTimeout, Param->EventInterval )) ) - { - // Create message + if (Force || (Param->EventInterval && Timeout( Param->EventTimeout, Param->EventInterval ))) { // Post event if (true) { // JSON output @@ -1851,8 +1798,7 @@ bool CDeviceCore::EventOutput( TDeviceParam * Param, bool Force ) JSONparse->WriteToString( NULL, EventMsg, EventLen, 0 ); } else { - switch (Param->DataType) - { + switch (Param->DataType) { case dtBool : sprintf( EventMsg, "%s: %u\n", Param->DataPath, *((bool*)Param->Value) ); break; @@ -1886,9 +1832,8 @@ bool CDeviceCore::EventOutput( TDeviceParam * Param, bool Force ) Output( Param->EventChannel, NULL, true, EventMsg, strlen(EventMsg) ); // Reset timer - if (Param->EventInterval) { + if (Param->EventInterval) SetStartTime( &(Param->EventTimeout) ); - } } return true; } diff --git a/EventBufferCore.cpp b/EventBufferCore.cpp index 3c317dc..7272f75 100644 --- a/EventBufferCore.cpp +++ b/EventBufferCore.cpp @@ -66,9 +66,7 @@ TEventEntry * CreateEvent( const char * EventType, const char * Parent, const // Check for minimum parameters if ((!EventType || !EventType[0]) || (!DeviceName || !DeviceName[0]) || (!DateTimeStr || !DateTimeStr[0]) || (!SourceName || !SourceName[0])) - { return NULL; - } // Create Blank Event Entry Event = CreateEvent(); @@ -171,16 +169,14 @@ CEventBuffer::~CEventBuffer() TEventType * NextEventType; // Free Event List - while (FirstItem) - { + while (FirstItem) { NextItem = FirstItem->NextItem; DestroyItem( &FirstItem ); FirstItem = NextItem; } // Free filters - while (FirstEventType) - { + while (FirstEventType) { NextEventType = FirstEventType->Next; free( FirstEventType->Name ); FirstEventType = NextEventType; @@ -201,8 +197,7 @@ bool CEventBuffer::SetEventFilter( int n, ... ) // Process Event Types va_start( EventList, n ); - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { // Create entry *EventType = (TEventType*)malloc( sizeof(TEventType) ); @@ -263,8 +258,7 @@ bool CEventBuffer::AddEvent( TEventEntry * Event, unsigned int EventNo ) TEventEntry * EventCopy = NULL; // Validate Event - if (PassEventFilter(Event)) - { + if (PassEventFilter(Event)) { // Save copy of event entry to Buffer EventCopy = CopyEvent( Event ); EventCopy->EventNo = (!EventNo)? ++EventCount : EventNo; diff --git a/FileCore.cpp b/FileCore.cpp index 1c0aa95..adb5166 100644 --- a/FileCore.cpp +++ b/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 )))) { diff --git a/FunctionCore.cpp b/FunctionCore.cpp index d60ab65..1b0f08b 100644 --- a/FunctionCore.cpp +++ b/FunctionCore.cpp @@ -45,8 +45,7 @@ CFunctionCore::~CFunctionCore() TChannel * NextChannel = NULL; // Destroy Channels - while (FirstChannel) - { + while (FirstChannel) { // Destroy Linked Channels while (FirstChannel->FirstLink) UnlinkChannel( FirstChannel->Name, FirstChannel->FirstLink->Function->Name, FirstChannel->FirstLink->Channel->Name ); @@ -122,13 +121,10 @@ bool CFunctionCore::InitChannelLinks( CDataMember * LinkConfig ) // Process each Channel ChannelMember = LinkConfig->GetFirstChild(); - while (ChannelMember) - { - if ((Channel = GetChannel( ChannelMember->GetName() ))) - { + while (ChannelMember) { + if ((Channel = GetChannel( ChannelMember->GetName() ))) { FunctionMember = ChannelMember->GetElement( 0 ); - while (FunctionMember) - { + while (FunctionMember) { // Get Parameters LinkChannel( Channel->Name, FunctionMember->GetChStr( "Function", NULL, true ), @@ -167,15 +163,13 @@ TChannel * CFunctionCore::AddChannel( const char * ChannelName, const EChannelSt TChannel ** Channel = NULL; // Validate - if (!ChannelName) { + if (!ChannelName) return NULL; - } // Check if exists Channel = &FirstChannel; - while (*Channel && strcmp( ChannelName, (*Channel)->Name )) { + while (*Channel && strcmp( ChannelName, (*Channel)->Name )) Channel = &((*Channel)->Next); - } // Create if not exist if (!*Channel) { @@ -224,7 +218,6 @@ bool CFunctionCore::SetChannelInState( TChannel * Channel, const EChannelState S LinkChannel = LinkChannel->Next; } - return true; } //--------------------------------------------------------------------------- @@ -258,18 +251,14 @@ EChannelState CFunctionCore::GetChannelOutState( TChannel * Channel, const char TChannelLink * LinkChannel = NULL; // Validate - if (!Channel) { + if (!Channel) return CH_off; - } - else if (!TargetRef || !*TargetRef) { + else if (!TargetRef || !*TargetRef) return Channel->OutState; - } - else if ((LinkChannel = GetLinkChannel( Channel, TargetRef ))) { + else if ((LinkChannel = GetLinkChannel( Channel, TargetRef ))) return LinkChannel->Channel->InState; - } - else { + else return CH_off; - } } //--------------------------------------------------------------------------- @@ -284,9 +273,8 @@ bool CFunctionCore::LinkChannel( const char * ChannelName, const char * LinkFunc // Check if Channels & Function exist if (!(Channel = GetChannel( ChannelName )) || !(LinkFunction = Application->GetFunction( LinkFunctionName )) || - !(LinkChannel = LinkFunction->GetChannel( LinkChannelName )) ) { + !(LinkChannel = LinkFunction->GetChannel( LinkChannelName )) ) return false; - } // Check if linked Channel exists LinkedChannel = &(Channel->FirstLink); @@ -344,9 +332,8 @@ bool CFunctionCore::UnlinkChannel( const char * ChannelName, const char * LinkFu // Check if Channels & Function exist if (!(Channel = GetChannel( ChannelName )) || !(LinkFunction = Application->GetFunction( LinkFunctionName )) || - !(LinkChannel = LinkFunction->GetChannel( LinkChannelName )) ) { + !(LinkChannel = LinkFunction->GetChannel( LinkChannelName )) ) return false; - } // Check if linked Channel exists LinkedChannel = &(Channel->FirstLink); @@ -388,9 +375,8 @@ int CFunctionCore::Input( const char * ChannelName, const char * SourceRef, cons TChannel * Channel = NULL; // Validate - if (!ChannelName || !*ChannelName || !Data) { + if (!ChannelName || !*ChannelName || !Data) return 0; - } // Get Channel if (!(Channel = GetChannel( ChannelName ))) { @@ -406,9 +392,9 @@ int CFunctionCore::Input( const char * ChannelName, const char * SourceRef, cons } // Return processed bytes - if (Len == -1) { + if (Len == -1) Len = strlen( Data ); - } + if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Channel '%s'->'%s' - IN:", ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); return Len; @@ -421,9 +407,8 @@ int CFunctionCore::Output( const char * ChannelName, const char * TargetRef, con TChannel * Channel = NULL; // Validate - if (!ChannelName || !*ChannelName) { + if (!ChannelName || !*ChannelName) return 0; - } // Get Channel if (!(Channel = GetChannel( ChannelName ))) { @@ -445,9 +430,8 @@ int CFunctionCore::Output( const TChannel * Channel, const char * TargetRef, con int OutLen = 0; // Validate - if (!Channel || !Data) { + if (!Channel || !Data) return 0; - } // Log event if (Log) Log->Output( LogLevel, dlHigh, ((!OutputFormat)? LogOutput : OutputFormat), Data, Len, "%s/%s: Channel '%s'->'%s' - OUT:", diff --git a/ItemBufferCore.cpp b/ItemBufferCore.cpp index b0854e5..6361390 100644 --- a/ItemBufferCore.cpp +++ b/ItemBufferCore.cpp @@ -45,8 +45,7 @@ TBufferItem * CItemBuffer::CreateItem( void * Entry, int Size ) // Create Entry BufferItem->Size = Size; - if (CopyEntries) - { + if (CopyEntries) { // Create copy if ((BufferItem->Entry = (void *)malloc( sizeof(char)*Size ))) { // Copy Value @@ -58,8 +57,7 @@ TBufferItem * CItemBuffer::CreateItem( void * Entry, int Size ) return NULL; } } - else - { + else { // Use pointer as is BufferItem->Entry = Entry; } @@ -123,9 +121,8 @@ void * CItemBuffer::Pop( int * Size ) void * Entry; // Return entry - if ((Entry = Peek( Size ))) { + if ((Entry = Peek( Size ))) Delete(); - } return Entry; } //--------------------------------------------------------------------------- @@ -179,8 +176,7 @@ void CItemBuffer::DeleteAll() { TBufferItem * NextItem; - while (FirstItem) - { + while (FirstItem) { NextItem = FirstItem->NextItem; DestroyItem( &FirstItem ); FirstItem = NextItem; diff --git a/LiteProtocolCore.cpp b/LiteProtocolCore.cpp index b644163..cdfac3e 100644 --- a/LiteProtocolCore.cpp +++ b/LiteProtocolCore.cpp @@ -279,17 +279,14 @@ bool CLiteProtocol::AppendCommandStr( char * pString, int &pLength ) Start = 0; // Look for other markers - while (Pos < pLength) - { + while (Pos < pLength) { // Check if start character - if (pString[Pos] == StartChar) - { + if (pString[Pos] == StartChar) { // Mark start point Start = Pos; } // Check if end character with valid start - else if ((pString[Pos] == EndChar) && (Start < pLength)) - { + else if ((pString[Pos] == EndChar) && (Start < pLength)) { // Mark end and continue End = Pos+1; break; @@ -300,8 +297,7 @@ bool CLiteProtocol::AppendCommandStr( char * pString, int &pLength ) }; // Check if anything available - if (Start < pLength) - { + if (Start < pLength) { // Calculate length newLen = End - Start; @@ -347,8 +343,7 @@ bool CLiteProtocol::AppendParam( const char * pString, const int pLength ) int StringLen; // Check if valid - if (!pLength || (!pString && (pLength == -1))) - { + if (!pLength || (!pString && (pLength == -1))) { // Increase string length if required StringLen = 0; IncCommandStr( Length + 1 ); @@ -357,8 +352,7 @@ bool CLiteProtocol::AppendParam( const char * pString, const int pLength ) CommandStr[CurrPos+StringLen] = Separator; Length += 1; // Allow for separator character } - else - { + else { // Increase string length if required StringLen = (pLength < 0)? strlen(pString) : pLength; IncCommandStr( Length + StringLen+1 ); @@ -501,8 +495,7 @@ char const * CLiteProtocol::GetParam( char ** pString, int * pLength ) char * Mark = NULL; // Check if valid parameter no - if (!Verified) - { + if (!Verified) { if (pString) *pString = NULL; if (pLength) pLength = 0; return NULL; @@ -529,8 +522,7 @@ bool CLiteProtocol::CreateCommand( char const * From, char const * To, char cons return false; // Insert Parameters in correct order - if (!AppendParam( From ) || !AppendParam( To ) || !AppendParam( Command )) - { + if (!AppendParam( From ) || !AppendParam( To ) || !AppendParam( Command )) { ClearCommandStr(); return false; }; diff --git a/SelectCore.cpp b/SelectCore.cpp index de56690..0d337dd 100644 --- a/SelectCore.cpp +++ b/SelectCore.cpp @@ -89,9 +89,9 @@ void CSelect::Add( int FD, bool Read, bool Write, THandle * Handle, CSelectableB // Check if SelectHandle already exists SelectHandle = &FirstHandle; - while (*SelectHandle && ((*SelectHandle)->FD != FD)) { + while (*SelectHandle && ((*SelectHandle)->FD != FD)) SelectHandle = &((*SelectHandle)->Next); - } + if (!*SelectHandle) { // Create if not exist *SelectHandle = new TSelectHandle; @@ -151,9 +151,9 @@ void CSelect::Remove( int FD, bool Read, bool Write ) // Check if SelectHandle already exists SelectHandle = &FirstHandle; - while (*SelectHandle && ((*SelectHandle)->FD != FD)) { + while (*SelectHandle && ((*SelectHandle)->FD != FD)) SelectHandle = &((*SelectHandle)->Next); - } + // Check if found if (!*SelectHandle) return; @@ -210,9 +210,8 @@ bool CSelect::Test() // Update Maximum Test FD if (Handle->FD == MaxFD-1) { for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) { - if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) { + if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) break; - } } MaxFD = TestFD+1; } diff --git a/SelectableBare.cpp b/SelectableBare.cpp index 0d34eee..e5f4baa 100644 --- a/SelectableBare.cpp +++ b/SelectableBare.cpp @@ -235,7 +235,6 @@ bool CSelectableBare::SetInBuffer( THandle * Handle, int InBufSize, int InTimeou memcpy( Handle->InMarker, InMarker, InMarkerLen ); Handle->InMarker[InMarkerLen] = 0; } - return true; } //--------------------------------------------------------------------------- @@ -391,6 +390,7 @@ bool CSelectableBare::Close( THandle * Handle, bool QuickReopen ) // Device Interface bool CSelectableBare::Read( THandle * Handle ) { + int BytesRead = 0; int BytesWaiting = -1; @@ -450,7 +450,6 @@ bool CSelectableBare::Read( THandle * Handle ) // Reset timer SetStartTime( &(Handle->InStart) ); - return (bool)BytesRead; } //--------------------------------------------------------------------------- @@ -535,7 +534,6 @@ bool CSelectableBare::Write( THandle * Handle ) } return true; } - return false; } //--------------------------------------------------------------------------- @@ -652,7 +650,6 @@ int CSelectableBare::Input( const char * ChannelName, const char * SourceRef, co ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); return 0; } - return BytesWritten; } //--------------------------------------------------------------------------- @@ -733,7 +730,6 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len if (BytesWritten != 0) SetStartTime( &(Handle->LastAction) ); } - return BytesWritten; } //--------------------------------------------------------------------------- diff --git a/WatchdogCore.cpp b/WatchdogCore.cpp index c040958..5ffad05 100644 --- a/WatchdogCore.cpp +++ b/WatchdogCore.cpp @@ -79,8 +79,7 @@ bool CWatchdogCore::Process() CSelectableCore::Process(); // Watchdog ping - if (Timeout( PingTimer, PingInterval )) - { + if (Timeout( PingTimer, PingInterval )) { // Send command OutputHandle( Ping, Protocol->GetCommandStr(), Protocol->GetCommandLen() );