From 7459763eb624dd06604766728226a6b2097a6155 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Sun, 2 Jun 2019 16:12:42 +0200 Subject: [PATCH] Important Update: - Remove unused: FunctionCore-ChannelBuffer - DataTreeCore: - Bug fix: Set Len=0 on element Clear() - SelectableBare/Core: - Move move BuildArgs() to SelectableBare - Make OutputHandle() to virtual method - UtilCore: - Bug fix: Handle NoCrLF correctly in BytesToSafeStr() --- DataTreeCore.cpp | 8 +- FunctionCore-ChannelBuffer.h | 222 ----------------------------------- SelectableBare.cpp | 79 ------------- SelectableCore.cpp | 78 +++++++++++- SelectableCore.h | 11 +- UtilCore.cpp | 5 +- 6 files changed, 91 insertions(+), 312 deletions(-) delete mode 100644 FunctionCore-ChannelBuffer.h diff --git a/DataTreeCore.cpp b/DataTreeCore.cpp index 8f4c7e5..6f6b05f 100644 --- a/DataTreeCore.cpp +++ b/DataTreeCore.cpp @@ -131,10 +131,8 @@ CDataMember * CDataMember::CreateChild( const char * Name, const int Len ) bool CDataMember::Clear() { // Clear value - if (Value) { + if (Value) free( Value ); - Value = NULL; - } // Clear children while (FirstChild) { @@ -142,7 +140,9 @@ bool CDataMember::Clear() } // Reset value - Type = jtNull; + Type = jtNull; + Value = NULL; + Len = 0; return true; } //--------------------------------------------------------------------------- diff --git a/FunctionCore-ChannelBuffer.h b/FunctionCore-ChannelBuffer.h deleted file mode 100644 index 7af897d..0000000 --- a/FunctionCore-ChannelBuffer.h +++ /dev/null @@ -1,222 +0,0 @@ -/* - * FunctionCore.h - * - * Created on: 18 May 2016 - * Author: wentzelc - */ - -#ifndef REDACORE_FUNCTIONCORE_H_ -#define REDACORE_FUNCTIONCORE_H_ - -// Standard C/C++ Libraries -/* none */ - -// redA Libraries -#include "LogCore.h" -#include "DataTreeCore.h" -#include "CharBufferCore.h" -#include "ItemBufferCore.h" - -//--------------------------------------------------------------------------- - -// Preview -typedef struct SChannel TChannel; -typedef struct SChannelLink TChannelLink; -typedef struct SProcessBuffer TProcessBuffer; -typedef struct SProcessItem TProcessItem; - -class CFunctionCore; - -//--------------------------------------------------------------------------- - -struct SChannel -{ - char * Name; - - TChannelLink * FirstInput; - TChannelLink * FirstOutput; - - bool InputEnabled; - bool OutputEnabled; - - TProcessBuffer * InputBuffer; - - TChannel * Next; -}; -//--------------------------------------------------------------------------- - -struct SChannelLink -{ - CFunctionCore * Function; - char * Name; - - SChannelLink * Next; -}; -//--------------------------------------------------------------------------- - -struct SProcessBuffer -{ - // Process buffer - char * Name; // Name of buffer; - CItemBufferCore Buffer; // Message buffer - unsigned long LastRefNo; // Last RefNo for item - - // Input parameters - bool PassthroughCallback; // Send Callback once processed message has been passed on, else once processed - - // Output parameters - TChannel * OutputChannel; // Channel to which to pass buffer output - bool OutputCallback; // Expect callback on output? - - TProcessBuffer * Next; // Next buffer in list -}; -//--------------------------------------------------------------------------- - -struct SProcessItem -{ - // Input Parameters - TChannel * SourceChannel; - char * SourceRef; - bool SourceCallback; - - // Processing Parameters - bool Processed; - bool Completed; - - // Output Parameters - timeval CallbackTimer; - TChannelLink FirstOutput; - - // Input Data - void * DataIn; - int DataInLen; - - // Output Data - void * DataOut; - int DateOutLen; -}; -//--------------------------------------------------------------------------- - -class CFunctionCore_not_used -{ -protected: - // Function Definition - char * Name; - char * Type; - - // Configuration - CDataTree * DataTree; - TDataMember * ConfigMember; - TDataMember * LinkConfigMember; - - // Channels - TChannel * FirstChannel; - - // Processing Queues - TProcessBuffer * FirstProcessBuffer; - - // Logging - CLogCore * Log; - EDebugLevel LogLevel; - int LogOutput; - - // Stored Output - char * StoredOutput; - int StoredOutputLen; - - // Manage Channel - inline TChannel * GetChannel( const char * Name ) { - if (!Name) return NULL; - TChannel * Channel = FirstChannel; - while (Channel && strcmp( Name, Channel->Name )) - Channel = Channel->Next; - return Channel; - } - - // Load configuration - virtual bool LoadConfigData(); - virtual bool LoadChannelLinkData(); - - // Data Input/Output - virtual int Output( const TChannel * Channel, const char * Data, int Len ); - virtual bool PullInput( TChannel * Channel ); - - // Processing Queue - TProcessItem * CreateProcessItem(); - -public: - // Life cycle - CFunctionCore_not_used( const char * pName, const char * Type ); - virtual ~CFunctionCore_not_used(); - - // Load Configuration - virtual bool Init(); - - bool InitConfig( const char * pConfigPath ); - bool InitConfig( TDataMember * pConfigMember ); - - bool InitChannelLinks( const char * pLinkConfigPath ); - bool InitChannelLinks( TDataMember *pLinkConfigMember ); - - // Set Parameters Manually - bool SetLogParam( EDebugLevel pDebugLevel, int pOutputDisplay ); - bool SetLogLevel( EDebugLevel pDebugLevel ); - - // Miscellaneous - inline const char * GetName() { return Name; }; - inline const char * GetType() { return Type; }; - - // Manage Channels - virtual TChannel * AddChannel( const char * ChannelName, const bool pInputEnable = true, const bool pOutputEnabled = true ); - inline bool SetChannelOutEnable( const char * ChannelName, const bool pOutputEnable ) { - TChannel * Channel = GetChannel( ChannelName ); - if (!Channel) return false; - Channel->OutputEnabled = pOutputEnable; - return true; - } - inline bool SetChannelInEnable( const char * ChannelName, const bool pInputEnable ) { - TChannel * Channel = GetChannel( ChannelName ); - if (!Channel) return false; - Channel->InputEnabled = pInputEnable; - return true; - } - inline bool isInputEnabled( const char * ChannelName ) { - TChannel * Channel = GetChannel( ChannelName ); - return ((Channel)? Channel->InputEnabled : false); - } - inline bool isOutputEnabled( const char * ChannelName ) { - TChannel * Channel = GetChannel( ChannelName ); - return ((Channel)? Channel->OutputEnabled : false); - } - - // Pushing Data Output -> Input - virtual int Output( const char * ChannelName, const char * Data, int Len = -1 ); - virtual int Input( const char * ChannelName, const char * Data, int Len = -1 ); - - // Pulling Data Input <- Output - virtual bool PullInput( const char * ChannelName ); - virtual bool PullOutput( const char * ChannelName, char ** Data, int * Len = NULL ); - - // Manages Process Buffers - TProcessBuffer * CreateProcessBuffer( const char * Name ); - TProcessBuffer * GetProcessBuffer( const char * Name ); - - TProcessItem * CreateProcessItem( const char * BufferName ); - TProcessItem * GetProcessItem( const char BufferName, const int Ref ); - bool DestroyProcessItem( const char * BufferName, const int Ref ); - - // Queued Output - virtual bool OutputMessage( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo, bool Callback, const char * Data, int Len = -1 ); - virtual bool OutputMessageCallback( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo, const bool Success, const char * Error ); - virtual bool InputMessage( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo, bool Callback, const char * Data, int Len = -1 ); - virtual bool InputMessageCancel( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo ); - - // Automated Data Input/Output - virtual bool LinkInputChannel( const char * ChannelName, const char * OutFunctionName, const char * OutChannelName, bool Bidirectional ); - virtual bool LinkOutputChannel( const char * ChannelName, const char * InFunctionName, const char * InChannelName, bool Bidirectional ); - virtual bool Process() = 0; -}; - -//--------------------------------------------------------------------------- - -#endif /* REDACORE_FUNCTIONCORE_H_ */ diff --git a/SelectableBare.cpp b/SelectableBare.cpp index b0603c9..04be540 100644 --- a/SelectableBare.cpp +++ b/SelectableBare.cpp @@ -873,82 +873,3 @@ bool CSelectableBare::Process() return true; } //--------------------------------------------------------------------------- - -bool CSelectableBare::BuildArgs( const char * ExecPath, int &Count, char * Args[] ) -{ - bool ParamStarted = false; - bool OpenQuotes = false; - char * MatchPos = NULL; - char * StartPos = NULL; - int Len; - - // Validate - if (!ExecPath || !*ExecPath) { - return false; - } - - // Split params - MatchPos = (char*)ExecPath; - for (;;) - { - // Look for whitespace - if (!ParamStarted) - { - // Quotes starts quoted parameter - if (*MatchPos == '"') { - ParamStarted = true; - OpenQuotes = true; - StartPos = MatchPos+1; // Skip starting quote - } - // Non-whitespace starts normal parameter - else if ((*MatchPos != ' ') && (*MatchPos != 0)) { - ParamStarted = true; - StartPos = MatchPos; - } - } - else if (OpenQuotes) - { - // Another quote ends parameter - if (*MatchPos == '"') { - Len = MatchPos-StartPos-1; // Skip end quote - Args[Count] = (char*)malloc( Len+1 ); - strncpy( Args[Count], StartPos, Len ); - Args[Count][Len] = 0; - Count++; - ParamStarted = false; - OpenQuotes = false; - } - } - else - { - // Whitespace ends parameter - if ((*MatchPos == ' ') || (*MatchPos == 0)) { - Len = MatchPos-StartPos; - Args[Count] = (char*)malloc( Len+1 ); - strncpy( Args[Count], StartPos, Len ); - Args[Count][Len] = 0; - Count++; - ParamStarted = false; - } - } - // Next char, unless NULL - if (*MatchPos) - MatchPos++; - else - break; - } - - // Check all parameters closed - if (ParamStarted) { - Count = 0; - Args[0] = NULL; - return false; - } - - // Set last Param to NULL - Args[Count] = NULL; - - return true; -} -//--------------------------------------------------------------------------- - diff --git a/SelectableCore.cpp b/SelectableCore.cpp index 99575b4..1f9537b 100644 --- a/SelectableCore.cpp +++ b/SelectableCore.cpp @@ -1254,7 +1254,6 @@ THandle * CSelectableCore::OpenTCPremoteSocket( THandle * Handle ) strcpy( ClientAddress, inet_ntoa(address.sin_addr) ); sprintf( ClientPort, "%d", ntohs(address.sin_port) ); - // Get end of client list RemoteClient = &FirstHandle; while (*RemoteClient) { @@ -2455,3 +2454,80 @@ bool CSelectableCore::ReadSerialConfig( THandle * Handle ) } //--------------------------------------------------------------------------- +bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[] ) +{ + bool ParamStarted = false; + bool OpenQuotes = false; + char * MatchPos = NULL; + char * StartPos = NULL; + int Len; + + // Validate + if (!ExecPath || !*ExecPath) { + return false; + } + + // Split params + MatchPos = (char*)ExecPath; + for (;;) + { + // Look for whitespace + if (!ParamStarted) + { + // Quotes starts quoted parameter + if (*MatchPos == '"') { + ParamStarted = true; + OpenQuotes = true; + StartPos = MatchPos+1; // Skip starting quote + } + // Non-whitespace starts normal parameter + else if ((*MatchPos != ' ') && (*MatchPos != 0)) { + ParamStarted = true; + StartPos = MatchPos; + } + } + else if (OpenQuotes) + { + // Another quote ends parameter + if (*MatchPos == '"') { + Len = MatchPos-StartPos-1; // Skip end quote + Args[Count] = (char*)malloc( Len+1 ); + strncpy( Args[Count], StartPos, Len ); + Args[Count][Len] = 0; + Count++; + ParamStarted = false; + OpenQuotes = false; + } + } + else + { + // Whitespace ends parameter + if ((*MatchPos == ' ') || (*MatchPos == 0)) { + Len = MatchPos-StartPos; + Args[Count] = (char*)malloc( Len+1 ); + strncpy( Args[Count], StartPos, Len ); + Args[Count][Len] = 0; + Count++; + ParamStarted = false; + } + } + // Next char, unless NULL + if (*MatchPos) + MatchPos++; + else + break; + } + + // Check all parameters closed + if (ParamStarted) { + Count = 0; + Args[0] = NULL; + return false; + } + + // Set last Param to NULL + Args[Count] = NULL; + + return true; +} +//--------------------------------------------------------------------------- diff --git a/SelectableCore.h b/SelectableCore.h index 16df503..087caf7 100644 --- a/SelectableCore.h +++ b/SelectableCore.h @@ -217,9 +217,6 @@ protected: // Buffer operations virtual bool ProcessInputBuffer( THandle * Handle, bool Force ); - // Specific operations - bool BuildArgs( const char * ExecPath, int &Count, char * Args[] ); - // Convert string to lower case inline char * strlcase( char * Str ) { for (char * Ch = Str; *Ch; Ch++ ) @@ -295,7 +292,7 @@ public: // Function Interface virtual int Input( const char * ChannelName, const char * Buffer, int BufLen = -1 ); - int OutputHandle( THandle * Handle, const char * Data, int Len ); + virtual int OutputHandle( THandle * Handle, const char * Data, int Len ); virtual bool Process(); }; @@ -319,6 +316,7 @@ protected: // Socket Operations bool ResolveAddress( THandle * Handle, bool DelayResolve ); + THandle * OpenUDPserverSocket( THandle * Handle, bool DelayResolve ); THandle * OpenUDPremoteSocket( THandle * Handle, char * RemoteAddr, char * RemotePort ); THandle * OpenUDPclientSocket( THandle * Handle, bool DelayResolve ); @@ -330,6 +328,9 @@ protected: int ReadFromUDP( THandle * Handle, char * RemoteAddr, char * RemotePort, char * Data, int MaxLen ); int WriteToUDP( THandle * Handle, const char * Data, int Len, bool Force ); + // Specific operations + bool BuildArgs( const char * ExecPath, int &Count, char * Args[] ); + public: // Life Cycle CSelectableCore( const char * Name, const char * Type = TYPE_SELECTABLE ); @@ -353,7 +354,7 @@ public: virtual bool Write( THandle * Handle ); // Function Interface - int OutputHandle( THandle * Handle, const char * Data, int Len ); + virtual int OutputHandle( THandle * Handle, const char * Data, int Len ); virtual bool Process(); }; diff --git a/UtilCore.cpp b/UtilCore.cpp index 0f079d1..cbbfd11 100644 --- a/UtilCore.cpp +++ b/UtilCore.cpp @@ -40,7 +40,10 @@ char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf, con // Remove special char for (int i=0; i 126)) || + if ((Bytes[i] == '\r') || (Bytes[i] == '\n')) { + *BufPos = (NoCrLf)? SpecChar : Bytes[i]; + } + else if (((Bytes[i] < 32) || (Bytes[i] > 126)) || (NoCrLf && ((Bytes[i] == '\r') || (Bytes[i] == '\n')))) { *BufPos = SpecChar; }