Files
redAcore/EventBufferCore.cpp
Charl Wentzel 3e40f7a86d 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()
2019-01-08 08:35:21 +02:00

313 lines
8.6 KiB
C++

/*
* EventBufferCore.h
* *
* Created on: 20 Aug 2017
* Author: wentzelc
*/
// Standard C/C++ Libraries
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
// redA Libraries
#include "EventBufferCore.h"
//---------------------------------------------------------------------------
//***** Function Prototypes *****//
TEventEntry * CreateEvent()
{
TEventEntry * Event;
// Create structure
Event = (TEventEntry *)malloc( sizeof(TEventEntry) );
// Set default values
memset( Event, 0x0, sizeof(TEventEntry));
// Return empty structure
return Event;
}
//---------------------------------------------------------------------------
TEventEntry * CreateEvent( const char * EventType, const char * Parent, const char * DeviceName, const char * ParamName,
long Value, const char * DateTimeStr, const char * SourceName )
{
char TextValue[30];
// Create text from value
sprintf( TextValue, "%ld", Value );
// Create Event
return CreateEvent( EventType, Parent, DeviceName, ParamName, TextValue, DateTimeStr, SourceName );
}
//---------------------------------------------------------------------------
TEventEntry * CreateEvent( const char * EventType, const char * Parent, const char * DeviceName, const char * ParamName,
double Value, const char * DateTimeStr, const char * SourceName )
{
char TextValue[30];
// Create text from value
sprintf( TextValue, "%lf", Value );
// Create Event
return CreateEvent( EventType, Parent, DeviceName, ParamName, TextValue, DateTimeStr, SourceName );
}
//---------------------------------------------------------------------------
TEventEntry * CreateEvent( const char * EventType, const char * Parent, const char * DeviceName, const char * ParamName,
const char * Value, const char * DateTimeStr, const char * SourceName )
{
TEventEntry * Event;
// 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();
// Set Event params
Event->EventType = (EventType)? strdup( EventType ) : strdup( "" );
Event->Parent = (Parent)? strdup( Parent ) : strdup( "" );
Event->DeviceName = (DeviceName)? strdup( DeviceName ) : strdup( "" );
Event->ParamName = (ParamName)? strdup( ParamName ) : strdup( "" );
Event->Value = (Value)? strdup( Value ) : strdup( "" );
Event->SourceName = (SourceName)? strdup( SourceName ) : strdup( "" );
// Set Date and time
strncpy( Event->DateTime, DateTimeStr, 19);
Event->DateTime[19] = 0;
// Return Event
return Event;
}
//---------------------------------------------------------------------------
TEventEntry * CopyEvent( TEventEntry * Event )
{
TEventEntry * EventCopy;
// Check if valid pointer
if (!Event)
return NULL;
// Create new poitner
EventCopy = (TEventEntry*)malloc(sizeof(TEventEntry));
// Copy event no
EventCopy->EventNo = Event->EventNo;
EventCopy->EventType = (Event->EventType)? strdup( Event->EventType ) : NULL;
EventCopy->Parent = (Event->Parent)? strdup( Event->Parent ) : NULL;
EventCopy->DeviceName = (Event->DeviceName)? strdup( Event->DeviceName ) : NULL;
EventCopy->ParamName = (Event->ParamName)? strdup( Event->ParamName ) : NULL;
EventCopy->Value = (Event->Value)? strdup( Event->Value ) : NULL;
EventCopy->SourceName = (Event->SourceName)? strdup( Event->SourceName ) : NULL;
strcpy( EventCopy->DateTime, Event->DateTime );
// Return copy
return EventCopy;
}
//---------------------------------------------------------------------------
bool DestroyEvent( TEventEntry ** Event )
{
// Check if valid pointer
if (!Event || !*Event)
return false;
// Destroy pointers
if ((*Event)->EventType)
free( (*Event)->EventType );
if ((*Event)->Parent)
free( (*Event)->Parent );
if ((*Event)->DeviceName)
free( (*Event)->DeviceName );
if ((*Event)->ParamName)
free( (*Event)->ParamName );
if ((*Event)->Value)
free( (*Event)->Value );
if ((*Event)->SourceName)
free( (*Event)->SourceName );
// Destroy structure
free( *Event );
*Event = NULL;
return true;
}
//---------------------------------------------------------------------------
CEventBuffer::CEventBuffer( const char * BufferName ) :
CItemBuffer( false )
{
// Clear pointers
CurrentEvent = NULL;
PeerBuffer = NULL;
EventCount = 0;
FirstEventType = NULL;
// Set name
if (!BufferName || !BufferName[0])
strcpy( Name, "" );
else if (strlen(BufferName) > 30)
strncpy( Name, BufferName, 30 );
else
strcpy( Name, BufferName );
}
//---------------------------------------------------------------------------
CEventBuffer::~CEventBuffer()
{
TBufferItem * NextItem;
TEventType * NextEventType;
// Free Event List
while (FirstItem)
{
NextItem = FirstItem->NextItem;
DestroyItem( &FirstItem );
FirstItem = NextItem;
}
// Free filters
while (FirstEventType)
{
NextEventType = FirstEventType->Next;
free( FirstEventType->Name );
FirstEventType = NextEventType;
}
}
//---------------------------------------------------------------------------
bool CEventBuffer::SetEventFilter( int n, ... )
{
va_list EventList;
char * EventName;
TEventType ** EventType;
// Get end of list
EventType = &FirstEventType;
while (*EventType)
EventType = &((*EventType)->Next);
// Process Event Types
va_start( EventList, n );
for (int i = 0; i < n; i++)
{
// Create entry
*EventType = (TEventType*)malloc( sizeof(TEventType) );
// Copy name
EventName = va_arg(EventList, char*);
(*EventType)->Name = strdup( EventName );
// Next
(*EventType)->Next = NULL;
EventType = &((*EventType)->Next);
}
va_end( EventList );
return true;
}
//---------------------------------------------------------------------------
bool CEventBuffer::PassEventFilter( TEventEntry * Event )
{
TEventType * EventType;
// Check if valid event
if (!Event)
return false;
// If not list supplied, then all pass
if (!FirstEventType)
return true;
// Check event name against list
EventType = FirstEventType;
while (EventType) {
if (!strcasecmp( Event->EventType, EventType->Name ))
return true;
EventType = EventType->Next;
}
return false;
}
//---------------------------------------------------------------------------
bool CEventBuffer::SetPeerBuffer( CEventBuffer *pPeerBuffer )
{
PeerBuffer = pPeerBuffer;
return true;
}
//---------------------------------------------------------------------------
bool CEventBuffer::ClearCurrentEvent()
{
CurrentEvent = NULL;
return true;
}
//---------------------------------------------------------------------------
bool CEventBuffer::AddEvent( TEventEntry * Event, unsigned int EventNo )
{
bool result = true;
TEventEntry * EventCopy = NULL;
// Validate Event
if (PassEventFilter(Event))
{
// Save copy of event entry to Buffer
EventCopy = CopyEvent( Event );
EventCopy->EventNo = (!EventNo)? ++EventCount : EventNo;
result = Push( (void*)EventCopy, sizeof(TEventEntry) );
}
// Add to Peer Buffers, or destroy if last peer buffer
if (PeerBuffer)
PeerBuffer->AddEvent( Event );
else
DestroyEvent( &Event );
return result;
}
//---------------------------------------------------------------------------
bool CEventBuffer::DestroyEntry( void ** Entry )
{
// Destroy event
DestroyEvent( (TEventEntry**)Entry );
return true;
}
//---------------------------------------------------------------------------
TEventEntry * CEventBuffer::GetNextEvent( bool &Error )
{
// Return entry as Event entry
Error = false;
CurrentEvent = (TEventEntry*)Peek();
return CurrentEvent;
}
//---------------------------------------------------------------------------
bool CEventBuffer::ClearLastEvent( bool FreeEvent )
{
bool result;
// Accept Entry as void entry
result = Delete();
CurrentEvent = NULL;
return result;
}
//---------------------------------------------------------------------------C