Important Update:

- Implement Handle Address renaming:
- Restructure JSON config, with address list and rename list
- SelectableCore:
  - Add method GetHandleAddress() - lookup renamed handle
  - Add init values to all struct and classes declarations
- Replace malloc()/strcpy() sequences with strdup()
This commit is contained in:
Charl Wentzel
2019-01-08 08:35:21 +02:00
parent c5da842ac1
commit 3e40f7a86d
7 changed files with 128 additions and 232 deletions

View File

@@ -104,8 +104,7 @@ void CApplication::GetProcessName( char ** ProcessName, char * pFilePath )
if (TempStr[0] == '/') TempStr++; if (TempStr[0] == '/') TempStr++;
// Copy Process Name // Copy Process Name
*ProcessName = (char*)malloc( strlen(TempStr)+1 ); *ProcessName = strdup( TempStr );
strcpy( *ProcessName, TempStr );
// Remove extension // Remove extension
TempStr = strrchr( *ProcessName, '.' ); TempStr = strrchr( *ProcessName, '.' );
@@ -163,7 +162,7 @@ bool CApplication::LoadConfig()
} }
// Load Address List // Load Address List
if (!(AddressFile = (char*)Config->GetChStr( "Application/AddressList" ))) { if (!(AddressFile = (char*)Config->GetChStr( "Application/Addresses/List" ))) {
if (Log) Log->Message( dlLow, dlLow, "%s: No Address List file specified", ProcessName ); if (Log) Log->Message( dlLow, dlLow, "%s: No Address List file specified", ProcessName );
} }
else if (!JSONparser->ReadFromFile( "AddressList", AddressFile )) { else if (!JSONparser->ReadFromFile( "AddressList", AddressFile )) {
@@ -235,7 +234,7 @@ bool CApplication::InitApplication()
bool CApplication::InitFunction( CFunctionCore * Function ) bool CApplication::InitFunction( CFunctionCore * Function )
{ {
char FunctionPath[100]; char FunctionPath[100];
CDataMember * FunctionConfig = NULL; CDataMember * FunctionConfig;
// Validate // Validate
if (!Function) if (!Function)
@@ -274,7 +273,7 @@ bool CApplication::InitFunctions()
return false; return false;
} }
// Process each Channel // Process each Function
FunctionConfig = FunctionList->GetFirstChild(); FunctionConfig = FunctionList->GetFirstChild();
while (FunctionConfig) while (FunctionConfig)
{ {
@@ -358,8 +357,7 @@ bool CApplication::AddFunctionType( const char * Type, FFuncConstructor Construc
// Add new Type // Add new Type
*FunctionType = (TFunctionType*)calloc( sizeof(TFunctionType), 1 ); *FunctionType = (TFunctionType*)calloc( sizeof(TFunctionType), 1 );
(*FunctionType)->Name = (char*)malloc( strlen(Type)+1 ); (*FunctionType)->Name = strdup( Type );
strcpy( (*FunctionType)->Name, Type );
(*FunctionType)->Constructor = Constructor; (*FunctionType)->Constructor = Constructor;
return true; return true;

View File

@@ -410,8 +410,7 @@ TDevice * CDeviceCore::AddDeviceType( const char * DeviceTypeName )
else { else {
*DeviceType = new TDevice; *DeviceType = new TDevice;
(*DeviceType)->Type = (char *)malloc( strlen( DeviceTypeName )+1 ); (*DeviceType)->Type = strdup( DeviceTypeName );
strcpy( (*DeviceType)->Type, DeviceTypeName );
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Added device type '%s'", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Added device type '%s'",
ProcessName, Name, DeviceTypeName ); ProcessName, Name, DeviceTypeName );
@@ -454,20 +453,15 @@ TDevice * CDeviceCore::AddDevice( const char * DeviceName, const char * DeviceTy
else { else {
*Device = new TDevice; *Device = new TDevice;
(*Device)->Name = (char *)malloc( strlen( DeviceName )+1 ); (*Device)->Name = strdup( DeviceName );
strcpy( (*Device)->Name, DeviceName );
(*Device)->ID = DeviceID; (*Device)->ID = DeviceID;
if (DeviceAddress) { if (DeviceAddress) {
(*Device)->Address = (char *)malloc( strlen( DeviceAddress )+1 ); (*Device)->Address = strdup( DeviceAddress );
strcpy( (*Device)->Address, DeviceAddress );
} }
if (DeviceType && *DeviceType) { if (DeviceType && *DeviceType) {
(*Device)->Type = (char *)malloc( strlen( DeviceType )+1 ); (*Device)->Type = strdup( DeviceType );
strcpy( (*Device)->Type, DeviceType );
(*Device)->Template = GetDeviceType( DeviceType ); (*Device)->Template = GetDeviceType( DeviceType );
} }
@@ -540,8 +534,7 @@ TDeviceParam * CDeviceCore::AddDeviceParam( TDevice * Device, const char * Param
*Param = new TDeviceParam; *Param = new TDeviceParam;
// Set Name // Set Name
(*Param)->Name = (char *)malloc( strlen( ParamName )+1 ); (*Param)->Name = strdup( ParamName );
strcpy( (*Param)->Name, ParamName );
(*Param)->DataType = DataType; (*Param)->DataType = DataType;
(*Param)->Device = Device; (*Param)->Device = Device;
@@ -683,9 +676,7 @@ TDeviceParamGroup * CDeviceCore::AddParamGroup( TDevice * Device, const char * G
else { else {
*ParamGroup = new TDeviceParamGroup; *ParamGroup = new TDeviceParamGroup;
(*ParamGroup)->Name = (char *)malloc( strlen( GroupName )+1 ); (*ParamGroup)->Name = strdup( GroupName );
strcpy( (*ParamGroup)->Name, GroupName );
(*ParamGroup)->Device = Device; (*ParamGroup)->Device = Device;
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Added param group '%s/%s'", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Added param group '%s/%s'",
@@ -1197,8 +1188,7 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int
{ {
// Ensure string is zero terminated // Ensure string is zero terminated
if (Value[Len] != 0) { if (Value[Len] != 0) {
TempStr = (char *)malloc( Len+1 ); TempStr = strndup( Value, Len );
memcpy( TempStr, Value, Len );
TempStr[Len] = 0; TempStr[Len] = 0;
UseTempStr = true; UseTempStr = true;
} }

View File

@@ -73,58 +73,18 @@ TEventEntry * CreateEvent( const char * EventType, const char * Parent, const
// Create Blank Event Entry // Create Blank Event Entry
Event = CreateEvent(); Event = CreateEvent();
// Set Event type // Set Event params
Event->EventType = (char*)malloc( sizeof(char)*(strlen(EventType)+1) ); Event->EventType = (EventType)? strdup( EventType ) : strdup( "" );
strcpy( Event->EventType, EventType ); Event->Parent = (Parent)? strdup( Parent ) : strdup( "" );
Event->DeviceName = (DeviceName)? strdup( DeviceName ) : strdup( "" );
// Set Parent Name Event->ParamName = (ParamName)? strdup( ParamName ) : strdup( "" );
if (Parent && Parent[0]) Event->Value = (Value)? strdup( Value ) : strdup( "" );
{ Event->SourceName = (SourceName)? strdup( SourceName ) : strdup( "" );
Event->Parent = (char*)malloc( sizeof(char)*(strlen(Parent)+1) );
strcpy( Event->Parent, Parent );
}
else
{
Event->Parent = (char*)malloc(1);
Event->Parent[0] = 0;
}
// Set Device name
Event->DeviceName = (char*)malloc( sizeof(char)*(strlen(DeviceName)+1) );
strcpy( Event->DeviceName, DeviceName );
// Set Param name
if ((!ParamName || !ParamName[0]))
{
Event->ParamName = (char*)malloc(1);
Event->ParamName[0] = 0;
}
else
{
Event->ParamName = (char*)malloc( sizeof(char)*(strlen(ParamName)+1) );
strcpy( Event->ParamName, ParamName );
}
// Set Value
if ((!Value || !Value[0]))
{
Event->Value = (char*)malloc(1);
Event->Value[0] = 0;
}
else
{
Event->Value = (char*)malloc( sizeof(char)*(strlen(Value)+1) );
strcpy( Event->Value, Value );
}
// Set Date and time // Set Date and time
strncpy( Event->DateTime, DateTimeStr, 19); strncpy( Event->DateTime, DateTimeStr, 19);
Event->DateTime[19] = 0; Event->DateTime[19] = 0;
// Set source
Event->SourceName = (char*)malloc( sizeof(char)*(strlen(SourceName)+1) );
strcpy( Event->SourceName, SourceName );
// Return Event // Return Event
return Event; return Event;
} }
@@ -144,57 +104,15 @@ TEventEntry * CopyEvent( TEventEntry * Event )
// Copy event no // Copy event no
EventCopy->EventNo = Event->EventNo; EventCopy->EventNo = Event->EventNo;
// Copy pointer content EventCopy->EventType = (Event->EventType)? strdup( Event->EventType ) : NULL;
if (Event->EventType) EventCopy->Parent = (Event->Parent)? strdup( Event->Parent ) : NULL;
{ EventCopy->DeviceName = (Event->DeviceName)? strdup( Event->DeviceName ) : NULL;
EventCopy->EventType = (char*)malloc( strlen(Event->EventType)+1 ); EventCopy->ParamName = (Event->ParamName)? strdup( Event->ParamName ) : NULL;
strcpy( EventCopy->EventType, Event->EventType ); EventCopy->Value = (Event->Value)? strdup( Event->Value ) : NULL;
} EventCopy->SourceName = (Event->SourceName)? strdup( Event->SourceName ) : NULL;
else
EventCopy->EventType = NULL;
if (Event->Parent)
{
EventCopy->Parent = (char*)malloc( strlen(Event->Parent)+1 );
strcpy( EventCopy->Parent, Event->Parent );
}
else
EventCopy->Parent = NULL;
if (Event->DeviceName)
{
EventCopy->DeviceName = (char*)malloc( strlen(Event->DeviceName)+1 );
strcpy( EventCopy->DeviceName, Event->DeviceName );
}
else
EventCopy->DeviceName = NULL;
if (Event->ParamName)
{
EventCopy->ParamName = (char*)malloc( strlen(Event->ParamName)+1 );
strcpy( EventCopy->ParamName, Event->ParamName );
}
else
EventCopy->ParamName = NULL;
if (Event->Value)
{
EventCopy->Value = (char*)malloc( strlen(Event->Value)+1 );
strcpy( EventCopy->Value, Event->Value );
}
else
EventCopy->Value = NULL;
strcpy( EventCopy->DateTime, Event->DateTime ); strcpy( EventCopy->DateTime, Event->DateTime );
if (Event->SourceName)
{
EventCopy->SourceName = (char*)malloc( strlen(Event->SourceName)+1 );
strcpy( EventCopy->SourceName, Event->SourceName );
}
else
EventCopy->SourceName = NULL;
// Return copy // Return copy
return EventCopy; return EventCopy;
} }
@@ -290,8 +208,7 @@ bool CEventBuffer::SetEventFilter( int n, ... )
// Copy name // Copy name
EventName = va_arg(EventList, char*); EventName = va_arg(EventList, char*);
(*EventType)->Name = (char*)malloc( strlen(EventName)+1 ); (*EventType)->Name = strdup( EventName );
strcpy( (*EventType)->Name, EventName );
// Next // Next
(*EventType)->Next = NULL; (*EventType)->Next = NULL;

View File

@@ -80,10 +80,8 @@ TFileHandle * CFileCore::AddFile( const char * Name, const char * Path, bool Ap
*FileHandle = (TFileHandle*)calloc( 1, sizeof(TFileHandle) ); *FileHandle = (TFileHandle*)calloc( 1, sizeof(TFileHandle) );
// Set name & Path // Set name & Path
(*FileHandle)->Name = (char*)malloc( strlen(Name)+1 ); (*FileHandle)->Name = strdup( Name );
strcpy( (*FileHandle)->Name, Name ); (*FileHandle)->Path = strdup( Path );
(*FileHandle)->Path = (char*)malloc( strlen(Path)+1 );
strcpy( (*FileHandle)->Path, Path );
} }
// Create Channel if necessary // Create Channel if necessary

View File

@@ -31,8 +31,7 @@ CFunctionCore::CFunctionCore( const char * pName, const char * pType ) : Type( p
{ {
// Set name // Set name
if (pName) { if (pName) {
Name = (char*)malloc( strlen(pName)+1 ); Name = strdup( pName );
strcpy( Name, pName );
} }
// Logging // Logging
@@ -195,8 +194,7 @@ TChannel * CFunctionCore::AddChannel( const char * ChannelName, const bool pInpu
*Channel = new TChannel; *Channel = new TChannel;
// Set Name // Set Name
(*Channel)->Name = (char*)malloc( strlen(ChannelName)+1 ); (*Channel)->Name = strdup( ChannelName );
strcpy( (*Channel)->Name, ChannelName );
// Log Event // Log Event
if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Channel '%s' - Created", if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Channel '%s' - Created",
@@ -237,8 +235,7 @@ bool CFunctionCore::LinkInputChannel( const char * ChannelName, const char * Out
// Set Parameters // Set Parameters
(*LinkedChannel)->Function = OutFunction; (*LinkedChannel)->Function = OutFunction;
(*LinkedChannel)->Name = (char*)malloc( strlen(OutChannelName)+1 ); (*LinkedChannel)->Name = strdup( OutChannelName );
strcpy( (*LinkedChannel)->Name, OutChannelName );
// Log Event // Log Event
if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Input Linked - '%s'/'%s' <-- '%s'/'%s'", if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Input Linked - '%s'/'%s' <-- '%s'/'%s'",
@@ -282,8 +279,7 @@ bool CFunctionCore::LinkOutputChannel( const char * ChannelName, const char * In
// Set Parameters // Set Parameters
(*LinkedChannel)->Function = InFunction; (*LinkedChannel)->Function = InFunction;
(*LinkedChannel)->Name = (char*)malloc( strlen(InChannelName)+1 ); (*LinkedChannel)->Name = strdup( InChannelName );
strcpy( (*LinkedChannel)->Name, InChannelName );
// Log Event // Log Event
if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Output Linked - '%s'/'%s' --> '%s'/'%s'", if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Output Linked - '%s'/'%s' --> '%s'/'%s'",

View File

@@ -44,9 +44,6 @@ CSelectableCore::CSelectableCore( const char * pName, const char * pType ) : CFu
{ {
// Quick access // Quick access
Selector = Application->Selector; Selector = Application->Selector;
// Handles
FirstHandle = NULL;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -98,6 +95,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
Handle = CreateHandle( HandleConfig->GetName(), false ); Handle = CreateHandle( HandleConfig->GetName(), false );
Handle->Channel = GetChannel( HandleConfig->GetChStr( "Channel" ) ); Handle->Channel = GetChannel( HandleConfig->GetChStr( "Channel" ) );
// Load handle specifics
Type = (char*)HandleConfig->GetChStr( "Type", "TCPclient", true ); Type = (char*)HandleConfig->GetChStr( "Type", "TCPclient", true );
if (!strcasecmp( Type, "Serial" )) if (!strcasecmp( Type, "Serial" ))
{ {
@@ -142,7 +140,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
else if (!strcasecmp( Type, "LinePrinter" )) else if (!strcasecmp( Type, "LinePrinter" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
} else { } else {
Address = (char*)HandleConfig->GetChStr( "Port/Address", NULL, true ); // Get default value Address = (char*)HandleConfig->GetChStr( "Port/Address", NULL, true ); // Get default value
@@ -151,7 +149,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
else if (!strcasecmp( Type, "UNIXserver" )) else if (!strcasecmp( Type, "UNIXserver" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
} else { } else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
@@ -161,7 +159,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
else if (!strcasecmp( Type, "UNIXclient" )) else if (!strcasecmp( Type, "UNIXclient" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
} else { } else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
@@ -170,7 +168,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
else if (!strcasecmp( Type, "UDPserver" )) else if (!strcasecmp( Type, "UDPserver" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
@@ -182,7 +180,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
else if (!strcasecmp( Type, "UDPclient" )) else if (!strcasecmp( Type, "UDPclient" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
@@ -194,7 +192,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
else if (!strcasecmp( Type, "TCPserver" )) else if (!strcasecmp( Type, "TCPserver" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
@@ -207,7 +205,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
else if (!strcasecmp( Type, "TCPclient" )) else if (!strcasecmp( Type, "TCPclient" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
@@ -243,6 +241,27 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CDataMember * CSelectableCore::GetHandleAddress( THandle * Handle, const char * HandleRef )
{
CDataMember * AddressDef = NULL;
char NamePath[100];
char * Address;
// Handle renamed?
sprintf( NamePath, "Application/Addresses/Rename/%s", HandleRef );
if (!(Address = (char*)Application->Config->GetChStr( NamePath, NULL, false ))) {
Address = (char*)HandleRef;
}
// Get address def
if ((AddressDef = Application->AddressList->GetChild( Address ))) {
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Use address '%s' ('%s')",
ProcessName, Name, Handle->Name, Address, HandleRef );
}
return AddressDef;
}
//---------------------------------------------------------------------------
THandle * CSelectableCore::CreateHandle( const char * HandleName, bool CreateChannel ) THandle * CSelectableCore::CreateHandle( const char * HandleName, bool CreateChannel )
{ {
THandle ** Handle = NULL; THandle ** Handle = NULL;
@@ -260,8 +279,7 @@ THandle * CSelectableCore::CreateHandle( const char * HandleName, bool CreateCh
// Set name // Set name
if (HandleName) { if (HandleName) {
(*Handle)->Name = (char*)malloc( strlen(HandleName)+1 ); (*Handle)->Name = strdup( HandleName );
strcpy( (*Handle)->Name, HandleName );
} }
// Set File Descriptor // Set File Descriptor
@@ -354,14 +372,9 @@ bool CSelectableCore::SetSerialHandle( THandle * Handle, const char * FileName )
// Set Type // Set Type
Handle->Type = ctSerial; Handle->Type = ctSerial;
// Clear File Name // Set path
if (Handle->Path) { if (Handle->Path) free( Handle->Path );
free( Handle->Path ); Handle->Path = strdup( FileName );
}
// Set name
Handle->Path = (char*)malloc( strlen(FileName)+1 );
strcpy( Handle->Path, FileName );
// Log event // Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as Port [%s]", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as Port [%s]",
@@ -399,14 +412,9 @@ bool CSelectableCore::SetLinePrinterHandle( THandle * Handle, const char * FileN
// Set Type // Set Type
Handle->Type = ctLinePrinter; Handle->Type = ctLinePrinter;
// Clear File Name // Set Path
if (Handle->Path) { if (Handle->Path) free( Handle->Path );
free( Handle->Path ); Handle->Path = strdup( FileName );
}
// Set name
Handle->Path = (char*)malloc( strlen(FileName)+1 );
strcpy( Handle->Path, FileName );
// Log event // Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as Port [%s]", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as Port [%s]",
@@ -425,14 +433,9 @@ bool CSelectableCore::SetForkPipeHandle( THandle * Handle, const char * ExecPath
// Set Type // Set Type
Handle->Type = ctForkPipe; Handle->Type = ctForkPipe;
// Clear File Name // Set path
if (Handle->Path) { if (Handle->Path) free( Handle->Path );
free( Handle->Path ); Handle->Path = strdup( ExecPath );
}
// Set name
Handle->Path = (char*)malloc( strlen(ExecPath)+1 );
strcpy( Handle->Path, ExecPath );
// Log event // Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as ForkPipe [%s]", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as ForkPipe [%s]",
@@ -459,8 +462,7 @@ bool CSelectableCore::SetUnixHandle( THandle * Handle, EConnectType Type, const
} }
// Set name // Set name
Handle->Path = (char*)malloc( strlen(FileName)+1 ); Handle->Path = strdup( FileName );
strcpy( Handle->Path, FileName );
Handle->Queue = Queue; Handle->Queue = Queue;
// Log event // Log event
@@ -492,11 +494,8 @@ bool CSelectableCore::SetSocketHandle( THandle * Handle, EConnectType Type, con
freeaddrinfo( Handle->AddressList ); freeaddrinfo( Handle->AddressList );
// Set HostName & Port // Set HostName & Port
Handle->HostName = (char*)malloc( strlen(HostName)+1 ); Handle->HostName = strdup( HostName );
strcpy( Handle->HostName, HostName ); Handle->PortName = strdup( PortName );
Handle->PortName = (char*)malloc( strlen(PortName)+1 );
strcpy( Handle->PortName, PortName );
Handle->AddressList = NULL; Handle->AddressList = NULL;
Handle->AddressInfo = NULL; Handle->AddressInfo = NULL;
@@ -597,8 +596,7 @@ bool CSelectableCore::SetInBuffer( THandle * Handle, int InBufSize, int InTimeou
// Set Input Markers // Set Input Markers
if (InMarkerLen && InMarker) { if (InMarkerLen && InMarker) {
Handle->InMarkerLen = InMarkerLen; Handle->InMarkerLen = InMarkerLen;
Handle->InMarker = (char *)malloc( InMarkerLen+1 ); Handle->InMarker = strndup( InMarker, InMarkerLen );
memcpy( Handle->InMarker, InMarker, InMarkerLen );
Handle->InMarker[InMarkerLen] = 0; Handle->InMarker[InMarkerLen] = 0;
} }
@@ -2906,8 +2904,7 @@ bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[
// Another quote ends parameter // Another quote ends parameter
if (*MatchPos == '"') { if (*MatchPos == '"') {
Len = MatchPos-StartPos-1; // Skip end quote Len = MatchPos-StartPos-1; // Skip end quote
Args[Count] = (char*)malloc( Len+1 ); Args[Count] = strndup( StartPos, Len );
strncpy( Args[Count], StartPos, Len );
Args[Count][Len] = 0; Args[Count][Len] = 0;
Count++; Count++;
ParamStarted = false; ParamStarted = false;
@@ -2919,8 +2916,7 @@ bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[
// Whitespace ends parameter // Whitespace ends parameter
if ((*MatchPos == ' ') || (*MatchPos == 0)) { if ((*MatchPos == ' ') || (*MatchPos == 0)) {
Len = MatchPos-StartPos; Len = MatchPos-StartPos;
Args[Count] = (char*)malloc( Len+1 ); Args[Count] = strndup( StartPos, Len );
strncpy( Args[Count], StartPos, Len );
Args[Count][Len] = 0; Args[Count][Len] = 0;
Count++; Count++;
ParamStarted = false; ParamStarted = false;

View File

@@ -55,16 +55,16 @@ typedef void (*FHandleCallback)( CSelectableCore * Function, THandle * Handle, E
// List of Handles for Select Object // List of Handles for Select Object
struct SSelectHandle { struct SSelectHandle {
// File Descriptor // File Descriptor
int FD; int FD = -1;
bool Read; bool Read = false;
bool Write; bool Write = false;
// Event Object // Event Object
THandle * Handle; THandle * Handle = NULL;
CSelectableCore * Function; CSelectableCore * Function = NULL;
// List // List
TSelectHandle * Next; TSelectHandle * Next = NULL;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -72,63 +72,63 @@ struct SSelectHandle {
// List or Handles for Selectable Function Object // List or Handles for Selectable Function Object
struct SHandle { struct SHandle {
// Description // Description
char * Name; char * Name = NULL;
EConnectType Type; EConnectType Type = ctNone;
// State // State
int FD; int FD = -1;
EConnectState State; EConnectState State = csNone;
bool AutoManage; bool AutoManage = false;
bool Persistent; bool Persistent = false;
timeval LastAction; timeval LastAction = {0,0};
long ReopenDelay; // millisecs before trying to re-open socket long ReopenDelay = 1000; // millisecs before trying to re-open socket
long CloseTimeout; // millisecs of no traffic before closing socket long CloseTimeout = 1000; // millisecs of no traffic before closing socket
// Callback functions // Callback functions
FHandleCallback StateCallback[ 7 ]; FHandleCallback StateCallback[ 7 ] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
// Type specific parameters // Type specific parameters
char * Path; // Port (file)name or Exec path char * Path = NULL; // Port (file)name or Exec path
// Fork config // Fork config
pid_t ChildPID; // Forked child PID pid_t ChildPID; // Forked child PID
// Socket config // Socket config
char * HostName; // Host name or IP adddress char * HostName = NULL; // Host name or IP adddress
char * PortName; // Socket port no or protocol, e.g. "80" or "HTTP" char * PortName = NULL; // Socket port no or protocol, e.g. "80" or "HTTP"
struct addrinfo * AddressList; // List of resolved IP Addresses for host name struct addrinfo * AddressList = NULL; // List of resolved IP Addresses for host name
struct addrinfo * AddressInfo; // Current selected IP Address struct addrinfo * AddressInfo = NULL; // Current selected IP Address
bool AddressFailed; // Indicate failure to connect to address bool AddressFailed = false; // Indicate failure to connect to address
short Queue; // Max waiting connections short Queue = 2; // Max waiting connections
long ResolveDelay; // Delay before resolving hostname via DNS long ResolveDelay = 0; // Delay before resolving hostname via DNS
// Serial Port config // Serial Port config
bool SerialConfig; bool SerialConfig = false;
int InBaudrate; int InBaudrate = 0;
int OutBaudrate; int OutBaudrate = 0;
short DataBits; short DataBits = 0;
short Parity; short Parity = NO_PARITY;
short StopBits; short StopBits = 0;
short FlowCtrl; short FlowCtrl = NO_FLOWCTRL;
int DataWait; int DataWait = 0;
// Buffers // Buffers
CRollingBuffer * InBuffer; CRollingBuffer * InBuffer = NULL;
CRollingBuffer * OutBuffer; CRollingBuffer * OutBuffer = NULL;
// Input Markers // Input Markers
char * InMarker; char * InMarker = NULL;
int InMarkerLen; int InMarkerLen = 0;
// Input Timer // Input Timer
timeval InStart; timeval InStart = {0,0};
long InTimeout; // millisecs long InTimeout = 0; // millisecs
// List / Tree // List / Tree
TChannel * Channel; TChannel * Channel = NULL;
THandle * Parent; THandle * Parent = NULL;
THandle * Next; THandle * Next = NULL;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -137,7 +137,7 @@ class CSelect
{ {
protected: protected:
// List // List
TSelectHandle * FirstHandle; TSelectHandle * FirstHandle = NULL;
// Select Variables // Select Variables
fd_set ReadTestFDS; fd_set ReadTestFDS;
@@ -147,12 +147,12 @@ protected:
fd_set WriteFDS; fd_set WriteFDS;
// Configuration // Configuration
int MaxFD; int MaxFD = 0;
timeval Timeout; timeval Timeout = {0,0};
// Output // Output
CLogCore * Log; CLogCore * Log = NULL;
EDebugLevel LogLevel; EDebugLevel LogLevel = dlNone;
public: public:
// Life Cycle // Life Cycle
@@ -184,10 +184,10 @@ class CSelectableCore : public CFunctionCore
{ {
protected: protected:
// FDs // FDs
THandle * FirstHandle; THandle * FirstHandle = NULL;
// Select interface // Select interface
CSelect * Selector; CSelect * Selector = NULL;
// Managing File Handles // Managing File Handles
bool RemoveHandle( THandle * Handle ); bool RemoveHandle( THandle * Handle );
@@ -278,6 +278,7 @@ public:
// General port parameters // General port parameters
THandle * CreateHandle( const char * HandleName, bool CreateChannel ); THandle * CreateHandle( const char * HandleName, bool CreateChannel );
virtual CDataMember * GetHandleAddress( THandle * Handle, const char * HandleRef );
bool SetCallback( THandle * Handle, EConnectState pState, FHandleCallback pCallback ); bool SetCallback( THandle * Handle, EConnectState pState, FHandleCallback pCallback );
bool SetAutoManage( THandle * Handle, bool AutoManage, bool Persistent, int ReopenDelay = 0, int CloseTimeout = 0 ); bool SetAutoManage( THandle * Handle, bool AutoManage, bool Persistent, int ReopenDelay = 0, int CloseTimeout = 0 );