Files
redAcore/FunctionCore.cpp
Charl Wentzel 9ace97c1a3 Major update:
- General bug fixes
- Implement search for multi-character marker search in BufferCore
  Replaced FindChar() method with FindStr() method
- Implemented LocalIO and LinkedIO in FunctionCore
- Connect LocalIO with FD in SelectableCore
- Implement Write buffer with Write Select in SelectableCore
- Improve validation checks in SelectableCore
- Use Handle ptr instead of Handle Names for Config methods
2016-05-25 14:17:40 +02:00

226 lines
5.6 KiB
C++

/*
* FunctionCore.cpp
*
* Created on: 18 May 2016
* Author: wentzelc
*/
// redA Libraries
#include "FunctionCore.h"
#include "TimingCore.h"
#include "LogCore.h"
// Standard C/C++ Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------
// Life cycle
CFunctionCore::CFunctionCore( const char * FunctionName )
{
// Set name
if (FunctionName) {
Name = (char*)malloc( strlen(FunctionName)+1 );
strcpy( Name, FunctionName );
}
else {
Name = NULL;
}
// IO Functions
FirstIO = NULL;
}
//---------------------------------------------------------------------------
CFunctionCore::~CFunctionCore()
{
TLocalIO * NextIO = NULL;
TLinkedIO * NextLinkedIO = NULL;
// Destroy pointers
if (Name) {
free( Name );
}
// Destroy IO
while (FirstIO)
{
// Destroy Parameters
if (FirstIO->Name) {
free( FirstIO->Name );
}
// Destroy Linked Inputs
while (FirstIO->FirstInput) {
if (FirstIO->FirstInput->IOName) {
free( FirstIO->FirstInput->IOName );
}
NextLinkedIO = FirstIO->FirstInput->Next;
free( FirstIO->FirstInput );
FirstIO->FirstInput = NextLinkedIO;
}
// Destroy Linked Outputs
while (FirstIO->FirstOutput) {
if (FirstIO->FirstOutput->IOName) {
free( FirstIO->FirstOutput->IOName );
}
NextLinkedIO = FirstIO->FirstOutput->Next;
free( FirstIO->FirstOutput );
FirstIO->FirstOutput = NextLinkedIO;
}
// Destroy Local IO
// Destroy Local IO
NextIO = FirstIO->Next;
free( FirstIO );
FirstIO = NextIO;
}
}
//---------------------------------------------------------------------------
TLocalIO * CFunctionCore::AddLocalIO( const char * IOName )
{
TLocalIO ** LocalIO = NULL;
// Validate
if (!IOName) {
return NULL;
}
// Check if exists
LocalIO = &FirstIO;
while (*LocalIO && strcmp( IOName, (*LocalIO)->Name )) {
LocalIO = &((*LocalIO)->Next);
}
// Create if not exist
if (!*LocalIO) {
// Create
*LocalIO = (TLocalIO*)malloc( sizeof(TLocalIO) );
memset( *LocalIO, 0, sizeof(TLocalIO) );
// Set Name
(*LocalIO)->Name = (char*)malloc( strlen(IOName)+1 );
strcpy( (*LocalIO)->Name, IOName );
}
return *LocalIO;
}
//---------------------------------------------------------------------------
// Manual Data Input/Output
int CFunctionCore::Input( const char * IOName, const char * Buffer, int MaxLen )
{
TLocalIO * LocalIO = NULL;
// Validate
if (!IOName || !Buffer) {
return 0;
}
else if (MaxLen == -1) {
MaxLen = strlen( Buffer );
}
// Get IO
if (!(LocalIO = GetLocalIO( IOName ))) {
return 0;
}
// Do something and acknowledge size received
return MaxLen;
}
//---------------------------------------------------------------------------
int CFunctionCore::Output( const char * IOName, const char * Buffer, int Len )
{
TLocalIO * LocalIO = NULL;
// Validate
if (!IOName || !Buffer) {
return 0;
} else if (Len == -1) {
Len = strlen( Buffer );
}
// Get IO
if (!(LocalIO = GetLocalIO( IOName ))) {
return 0;
}
// Do something and acknowledge size received
return Len;
}
//---------------------------------------------------------------------------
// Automated Data Input/Output
bool CFunctionCore::AddInput( const char * IOName, CFunctionCore * OutFunction, const char * OutputName )
{
TLocalIO * LocalIO = NULL;
TLinkedIO ** LinkedIO = NULL;
// Get IO
if (!(LocalIO = GetLocalIO( IOName ))) {
return false;
}
// Check if linked IO exists
LinkedIO = &(LocalIO->FirstInput);
while (*LinkedIO && (((*LinkedIO)->Function != OutFunction) || strcmp( (*LinkedIO)->IOName, OutputName ) )) {
LinkedIO = &((*LinkedIO)->Next);
}
// Create if not found
if (!*LinkedIO)
{
// Create
*LinkedIO = (TLinkedIO*)malloc( sizeof(TLinkedIO) );
memset( *LinkedIO, 0, sizeof( TLinkedIO ));
// Set Parameters
(*LinkedIO)->Function = OutFunction;
(*LinkedIO)->IOName = (char*)malloc( strlen(OutputName)+1 );
strcpy( (*LinkedIO)->IOName, OutputName );
}
return true;
}
//---------------------------------------------------------------------------
bool CFunctionCore::AddOutput( const char * IOName, CFunctionCore * InFunction, const char * InputName )
{
TLocalIO * LocalIO = NULL;
TLinkedIO ** LinkedIO = NULL;
// Get IO
if (!(LocalIO = GetLocalIO( IOName ))) {
return false;
}
// Check if linked IO exists
LinkedIO = &(LocalIO->FirstOutput);
while (*LinkedIO && (((*LinkedIO)->Function != InFunction) || strcmp( (*LinkedIO)->IOName, InputName ) )) {
LinkedIO = &((*LinkedIO)->Next);
}
// Create if not found
if (!*LinkedIO)
{
// Create
*LinkedIO = (TLinkedIO*)malloc( sizeof(TLinkedIO) );
memset( *LinkedIO, 0, sizeof( TLinkedIO ));
// Set Parameters
(*LinkedIO)->Function = InFunction;
(*LinkedIO)->IOName = (char*)malloc( strlen(InputName)+1 );
strcpy( (*LinkedIO)->IOName, InputName );
}
return true;
}
//---------------------------------------------------------------------------