CharBuffer search fixed & Log/Util buffer changed:
- Char/ShiftBuffer: Fixed multi-char string search
- UtilCore: Always pass output buffer & remove own buffer for conversions
- UtilCore: Remove optional parameters to force passing of buffer
- LogCore: Add TempBuffer for output conversion (using UtilCore)
- ApplicationCore: Allow setting size of TempBuffer via config
- Optimize use of {}
This commit is contained in:
@@ -32,7 +32,7 @@ CApplication::CApplication( EDebugLevel pLogLevel )
|
||||
AddressFile = NULL;
|
||||
|
||||
// Create output logger
|
||||
Log = new CLogCore( stdout );
|
||||
Log = new CLogCore( stdout, 5000 );
|
||||
LogLevel = pLogLevel;
|
||||
LogOutput = loNormal;
|
||||
|
||||
@@ -193,9 +193,10 @@ bool CApplication::SaveConfig()
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CApplication::SetLogParam( EDebugLevel pDebugLevel, int pOutputDisplay )
|
||||
bool CApplication::SetLogParam( int pLogBufferSize, EDebugLevel pDebugLevel, int pOutputDisplay )
|
||||
{
|
||||
// Output
|
||||
LogBufSize = pLogBufferSize;
|
||||
LogLevel = pDebugLevel;
|
||||
LogOutput = pOutputDisplay;
|
||||
|
||||
@@ -208,6 +209,7 @@ bool CApplication::InitApplication()
|
||||
CDataMember * CoreConfig;
|
||||
CDataMember * LogConfig;
|
||||
CDataMember * SelectConfig;
|
||||
int pLogSize;
|
||||
EDebugLevel pLogLevel;
|
||||
int pLogOutput;
|
||||
|
||||
@@ -216,14 +218,14 @@ bool CApplication::InitApplication()
|
||||
|
||||
// Configure Logging/Debugging
|
||||
LogConfig = CoreConfig->GetChild( "Log", true );
|
||||
pLogSize = Log->ReadLogBufSize( LogConfig );
|
||||
pLogLevel = Log->ReadLogLevel( LogConfig );
|
||||
pLogOutput = Log->ReadLogOutput( LogConfig );
|
||||
SetLogParam( pLogLevel, pLogOutput );
|
||||
SetLogParam( pLogSize, pLogLevel, pLogOutput );
|
||||
|
||||
// Configure Selector
|
||||
if ((SelectConfig = CoreConfig->GetChild( "Selector" ))) {
|
||||
if ((SelectConfig = CoreConfig->GetChild( "Selector" )))
|
||||
Selector = new CSelect( (int)SelectConfig->GetChInt( "Wait", 5, true ), LogLevel );
|
||||
}
|
||||
|
||||
// Show status
|
||||
if (Log) Log->Message( LogLevel, dlLow, "%s: Application Core configured.", ProcessName );
|
||||
@@ -276,8 +278,7 @@ bool CApplication::InitFunctions()
|
||||
|
||||
// Process each Function
|
||||
FunctionConfig = FunctionList->GetFirstChild();
|
||||
while (FunctionConfig)
|
||||
{
|
||||
while (FunctionConfig) {
|
||||
// Get function parameters
|
||||
Type = (char*)FunctionConfig->GetChStr( "Type", "Custom", true );
|
||||
|
||||
@@ -325,8 +326,7 @@ bool CApplication::InitFunctionLinks()
|
||||
|
||||
// Process each Channel
|
||||
FunctionItem = FirstFunction;
|
||||
while (FunctionItem)
|
||||
{
|
||||
while (FunctionItem) {
|
||||
// Build links for function
|
||||
if ((LinkConfig = LinkList->GetChild( FunctionItem->Function->GetName() )))
|
||||
FunctionItem->Function->InitChannelLinks( LinkConfig );
|
||||
@@ -412,9 +412,8 @@ CDataMember * CApplication::GetAddress( const char * SearchAddress )
|
||||
|
||||
// Address renamed?
|
||||
sprintf( NamePath, "Application/Addresses/Rename/%s", SearchAddress );
|
||||
if (!(Address = (char*)Config->GetChStr( NamePath, NULL, false ))) {
|
||||
if (!(Address = (char*)Config->GetChStr( NamePath, NULL, false )))
|
||||
Address = (char*)SearchAddress;
|
||||
}
|
||||
|
||||
// Get address def
|
||||
AddressDef = AddressList->GetChild( Address );
|
||||
@@ -429,13 +428,11 @@ bool CApplication::Run( bool TerminateOnError )
|
||||
TFunctionItem * FunctionItem;
|
||||
|
||||
// Check for FD Events/States
|
||||
if (Selector) {
|
||||
if (Selector)
|
||||
Selector->Test();
|
||||
}
|
||||
|
||||
// Process Functions
|
||||
for (FunctionItem = FirstFunction; FunctionItem; FunctionItem = FunctionItem->Next )
|
||||
{
|
||||
for (FunctionItem = FirstFunction; FunctionItem; FunctionItem = FunctionItem->Next ) {
|
||||
ProcessTerminate = !FunctionItem->Function->Process();
|
||||
if (TerminateOnError) {
|
||||
if (ProcessTerminate) // Raise terminate flag for other processes
|
||||
|
||||
@@ -63,6 +63,7 @@ protected:
|
||||
TFunctionItem * FirstFunction;
|
||||
|
||||
// Output
|
||||
int LogBufSize;
|
||||
EDebugLevel LogLevel;
|
||||
int LogOutput;
|
||||
|
||||
@@ -87,7 +88,7 @@ public:
|
||||
bool SaveConfig();
|
||||
|
||||
// Manually set configuration
|
||||
bool SetLogParam( EDebugLevel pDebugLevel, int pOutputDisplay );
|
||||
bool SetLogParam( int pLogBufferSize, EDebugLevel pDebugLevel, int pOutputDisplay );
|
||||
inline void WriteToScreen( const char * BasePath, const int Indent = 2 ) { JSONparser->WriteToScreen( BasePath, Indent ); };
|
||||
|
||||
// Init application
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
CCharBuffer::CCharBuffer( int BufferSize ) : BufSize( BufferSize )
|
||||
{
|
||||
// Create Buffer
|
||||
if (BufSize)
|
||||
{
|
||||
if (BufSize) {
|
||||
// Create Buffer
|
||||
Buffer = (char *)malloc( BufSize+1 );
|
||||
memset( Buffer, 0, BufSize+1 );
|
||||
@@ -44,12 +43,10 @@ CCharBuffer::CCharBuffer( int BufferSize ) : BufSize( BufferSize )
|
||||
CCharBuffer::~CCharBuffer()
|
||||
{
|
||||
// Destroy Buffer
|
||||
if (Buffer) {
|
||||
if (Buffer)
|
||||
free( Buffer );
|
||||
}
|
||||
if (OutBuffer) {
|
||||
if (OutBuffer)
|
||||
free( OutBuffer );
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -125,17 +122,14 @@ char CRollingBuffer::PeekChar( int Pos )
|
||||
char CharValue = 0;
|
||||
|
||||
// Validate buffer and Pos
|
||||
if (!BufSize || (Pos < 0) || (Pos > BufLen)) {
|
||||
if (!BufSize || (Pos < 0) || (Pos > BufLen))
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get char
|
||||
if (BufStart + Pos < BufSize) {
|
||||
if (BufStart + Pos < BufSize)
|
||||
CharValue = Buffer[ BufStart+Pos ];
|
||||
}
|
||||
else {
|
||||
else
|
||||
CharValue = Buffer[ BufStart+Pos-BufSize ];
|
||||
}
|
||||
|
||||
// Return char
|
||||
return CharValue;
|
||||
@@ -187,17 +181,15 @@ int CRollingBuffer::Peek( char ** Data, int PeekPos, int MaxLen )
|
||||
}
|
||||
|
||||
// Get buffer start position
|
||||
if (BufStart + StartPos < BufSize) {
|
||||
if (BufStart + StartPos < BufSize)
|
||||
StartPos = BufStart + PeekPos;
|
||||
}
|
||||
else {
|
||||
else
|
||||
StartPos = BufStart + PeekPos - BufSize;
|
||||
}
|
||||
|
||||
// Validate MaxLen
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize)) {
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize))
|
||||
MaxLen = BufSize;
|
||||
}
|
||||
|
||||
BytesReturned = (MaxLen > BufLen - PeekPos)? (BufLen - PeekPos) : MaxLen;
|
||||
|
||||
// Copy Data from Buffer
|
||||
@@ -232,17 +224,15 @@ int CRollingBuffer::PeekCopy( char ** Data, int PeekPos, int MaxLen )
|
||||
}
|
||||
|
||||
// Get start position
|
||||
if (BufStart + StartPos < BufSize) {
|
||||
if (BufStart + StartPos < BufSize)
|
||||
StartPos = BufStart + PeekPos;
|
||||
}
|
||||
else {
|
||||
else
|
||||
StartPos = BufStart + PeekPos - BufSize;
|
||||
}
|
||||
|
||||
// Validate MaxLen
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize)) {
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize))
|
||||
MaxLen = BufSize;
|
||||
}
|
||||
|
||||
BytesReturned = (MaxLen > BufLen - PeekPos)? (BufLen - PeekPos) : MaxLen;
|
||||
|
||||
// Allocate memory
|
||||
@@ -266,10 +256,11 @@ int CRollingBuffer::PeekCopy( char ** Data, int PeekPos, int MaxLen )
|
||||
// Look for first occurrence of character in buffer
|
||||
bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundPos, int StartPos )
|
||||
{
|
||||
const char * CheckPos = NULL;
|
||||
const char * CheckLimit = NULL;
|
||||
const char * MatchPos = NULL;
|
||||
const char * MatchLimit = NULL;
|
||||
const char * CheckChar = NULL; // Current position in haystack
|
||||
const char * CheckLimit = NULL; // Roll-over point in haystack
|
||||
const char * CheckStart = NULL; // Reference to where match starts in haystack
|
||||
const char * MatchChar = NULL; // Current match position in needle
|
||||
const char * MatchLimit = NULL; // End of needle
|
||||
|
||||
// Check if buffer exists
|
||||
if (!BufSize || !SearchStr || !SearchLen) {
|
||||
@@ -283,43 +274,45 @@ bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundP
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get Search start point and limit
|
||||
CheckPos = (BufStart + StartPos < BufSize)? &Buffer[ BufStart+StartPos ] : &Buffer[ (BufStart+StartPos)-BufSize ];
|
||||
// Get Search start point and roll-over of haystack
|
||||
CheckChar = (BufStart + StartPos < BufSize)? &Buffer[ BufStart+StartPos ] : &Buffer[ (BufStart+StartPos)-BufSize ];
|
||||
CheckLimit = &Buffer[BufSize-1];
|
||||
|
||||
// Set Match start point and limit
|
||||
MatchPos = SearchStr;
|
||||
// Set Match start point and limit of needle
|
||||
MatchChar = SearchStr;
|
||||
MatchLimit = &SearchStr[SearchLen-1];
|
||||
|
||||
// Search for char
|
||||
// Search for needle in haystack
|
||||
FoundPos = StartPos;
|
||||
while (FoundPos < BufLen)
|
||||
{
|
||||
// Check if char match
|
||||
if (*CheckPos == *MatchPos) {
|
||||
if (MatchPos == MatchLimit) {
|
||||
// Full match found
|
||||
if (*CheckChar == *MatchChar) {
|
||||
if (MatchChar == MatchLimit) {
|
||||
// Whole needle found
|
||||
FoundPos -= SearchLen-1;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Next char to match
|
||||
MatchPos++;
|
||||
// More needle to find
|
||||
if (MatchChar == SearchStr)
|
||||
CheckStart = CheckChar; // Mark start of match
|
||||
MatchChar++; // Continue matching needle
|
||||
}
|
||||
}
|
||||
else if (MatchPos != SearchStr){
|
||||
// Reset match point
|
||||
FoundPos -= MatchPos - SearchStr;
|
||||
MatchPos = SearchStr;
|
||||
else if (MatchChar != SearchStr){
|
||||
// Only partial needle found - reset match search
|
||||
FoundPos -= MatchChar - SearchStr;
|
||||
CheckChar = CheckStart;
|
||||
MatchChar = SearchStr;
|
||||
}
|
||||
|
||||
// Next char
|
||||
FoundPos++;
|
||||
if (CheckPos == CheckLimit) {
|
||||
CheckPos = Buffer; // Roll-over, Start at beginning of buffer
|
||||
} else {
|
||||
CheckPos++;
|
||||
}
|
||||
if (CheckChar == CheckLimit)
|
||||
CheckChar = Buffer; // Roll-over, Start at beginning of haystack
|
||||
else
|
||||
CheckChar++;
|
||||
}
|
||||
|
||||
// Not Found
|
||||
@@ -334,19 +327,17 @@ int CRollingBuffer::Clear( int ClearLen )
|
||||
int BytesCleared;
|
||||
|
||||
// Validate ClearLen
|
||||
if (ClearLen == -1) {
|
||||
if (ClearLen == -1)
|
||||
BytesCleared = BufLen;
|
||||
}
|
||||
else {
|
||||
else
|
||||
BytesCleared = (ClearLen > BufLen)? BufLen : ClearLen;
|
||||
}
|
||||
|
||||
// Update Buffer pointers
|
||||
if (BufStart + BytesCleared < BufSize) {
|
||||
if (BufStart + BytesCleared < BufSize)
|
||||
BufStart += BytesCleared;
|
||||
} else {
|
||||
else
|
||||
BufStart = (BytesCleared-(BufSize-BufStart)); // Copy roll-over from start of buffer
|
||||
}
|
||||
|
||||
BufLen -= BytesCleared;
|
||||
|
||||
return BytesCleared;
|
||||
@@ -364,19 +355,16 @@ int CRollingBuffer::Push( bool Overwrite, const char * Data, int Len )
|
||||
int DataRemain;
|
||||
|
||||
// Validate Buffer and Len
|
||||
if (!BufSize || !Data || !Len) {
|
||||
if (!BufSize || !Data || !Len)
|
||||
return 0;
|
||||
}
|
||||
else if (Len == -1) {
|
||||
else if (Len == -1)
|
||||
Len = strlen( Data );
|
||||
}
|
||||
|
||||
// Prevent overwrite
|
||||
DataRemain = (!Overwrite && (Len > BufSize-BufLen))? BufSize-BufLen : Len;
|
||||
|
||||
// Read Data into buffer
|
||||
while (DataRemain)
|
||||
{
|
||||
while (DataRemain) {
|
||||
// Read from file descriptor
|
||||
BufRemain = BufSize - BufEnd;
|
||||
BytesPushed = ((BufRemain > DataRemain)? DataRemain : BufRemain);
|
||||
@@ -414,16 +402,14 @@ int CRollingBuffer::ReadFromFD( int Handle, int MaxRead, bool Overwrite )
|
||||
bool Error = false;
|
||||
|
||||
// Check if buffer created
|
||||
if (!BufSize) {
|
||||
if (!BufSize)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read file descriptor into buffer
|
||||
if (MaxRead == -1)
|
||||
MaxRead = BufSize;
|
||||
DataRemain = ((!Overwrite && (MaxRead > BufSize-BufLen)))? BufSize-BufLen : MaxRead;
|
||||
while (DataRemain)
|
||||
{
|
||||
while (DataRemain) {
|
||||
// Read from file descriptor
|
||||
BufRemain = BufSize - BufEnd;
|
||||
BytesRead = read( Handle, &Buffer[BufEnd], ((BufRemain > DataRemain)? DataRemain : BufRemain) );
|
||||
@@ -448,9 +434,8 @@ int CRollingBuffer::ReadFromFD( int Handle, int MaxRead, bool Overwrite )
|
||||
BufStart = BufEnd;
|
||||
}
|
||||
|
||||
if (DataRemain) {
|
||||
if (DataRemain)
|
||||
usleep( 500 );
|
||||
}
|
||||
}
|
||||
return (Error)? -TotalRead : TotalRead; // Report negative total on error
|
||||
}
|
||||
@@ -467,13 +452,11 @@ int CRollingBuffer::WriteToFD( int Handle, int MaxLen )
|
||||
bool Error = false;
|
||||
|
||||
// Check if buffer created
|
||||
if (!BufSize) {
|
||||
if (!BufSize)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read Data into buffer
|
||||
while (DataRemain)
|
||||
{
|
||||
while (DataRemain) {
|
||||
// Read from file descriptor
|
||||
BufRemain = BufSize - ReadPos;
|
||||
BytesWritten = write( Handle, &Buffer[ReadPos], ((BufRemain > DataRemain)? DataRemain : BufRemain) );
|
||||
@@ -489,13 +472,11 @@ int CRollingBuffer::WriteToFD( int Handle, int MaxLen )
|
||||
|
||||
// Update Buffer Pointers
|
||||
ReadPos += BytesWritten;
|
||||
if (ReadPos >= BufSize) {
|
||||
if (ReadPos >= BufSize)
|
||||
ReadPos = 0; // Rolling over end of buffer, start at beginning
|
||||
}
|
||||
|
||||
if (DataRemain) {
|
||||
if (DataRemain)
|
||||
usleep( 500 );
|
||||
}
|
||||
}
|
||||
|
||||
return (Error)? -TotalWritten : TotalWritten; // Report negative total on error
|
||||
@@ -515,9 +496,8 @@ CShiftBuffer::CShiftBuffer( int BufferSize ) :
|
||||
char CShiftBuffer::PeekChar( int Pos )
|
||||
{
|
||||
// Validate buffer and Pos
|
||||
if (!BufSize || (Pos < 0) || (Pos > BufLen)) {
|
||||
if (!BufSize || (Pos < 0) || (Pos > BufLen))
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Return char
|
||||
return Buffer[Pos];
|
||||
@@ -552,9 +532,9 @@ int CShiftBuffer::Peek( char ** Data, int PeekPos, int MaxLen )
|
||||
}
|
||||
|
||||
// Calculate bytes to return
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize)) {
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize))
|
||||
MaxLen = BufSize;
|
||||
}
|
||||
|
||||
BytesReturned = (MaxLen > BufLen - PeekPos)? (BufLen - PeekPos) : MaxLen;
|
||||
|
||||
// Copy Data from Buffer
|
||||
@@ -581,9 +561,9 @@ int CShiftBuffer::PeekCopy( char ** Data, int PeekPos, int MaxLen )
|
||||
}
|
||||
|
||||
// Calculate bytes to return
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize)) {
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize))
|
||||
MaxLen = BufSize;
|
||||
}
|
||||
|
||||
BytesReturned = (MaxLen > BufLen - PeekPos)? (BufLen - PeekPos) : MaxLen;
|
||||
|
||||
// Allocate memory
|
||||
@@ -601,10 +581,11 @@ int CShiftBuffer::PeekCopy( char ** Data, int PeekPos, int MaxLen )
|
||||
// Look for first occurrence of character in buffer
|
||||
bool CShiftBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundPos, int StartPos )
|
||||
{
|
||||
char * CheckPos = NULL;
|
||||
char * CheckLimit = NULL;
|
||||
char * MatchPos = NULL;
|
||||
char * MatchLimit = NULL;
|
||||
const char * CheckChar = NULL; // Current position in haystack
|
||||
const char * CheckLimit = NULL; // End of haystack
|
||||
const char * CheckStart = NULL; // Reference to where match starts in haystack
|
||||
const char * MatchChar = NULL; // Current match position in needle
|
||||
const char * MatchLimit = NULL; // End of needle
|
||||
|
||||
// Check if buffer exists
|
||||
if (!BufSize || !SearchStr || !SearchLen) {
|
||||
@@ -618,42 +599,44 @@ bool CShiftBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundPos
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get Search start point and limit
|
||||
CheckPos = &Buffer[StartPos];
|
||||
// Get Search start point and end of haystack
|
||||
CheckChar = &Buffer[StartPos];
|
||||
CheckLimit = &Buffer[BufLen-1];
|
||||
|
||||
// Set Match start point and limit
|
||||
MatchPos = (char*)SearchStr;
|
||||
// Set Match start point and limit of needle
|
||||
MatchChar = (char*)SearchStr;
|
||||
MatchLimit = (char*)&SearchStr[SearchLen-1];
|
||||
|
||||
// Search for char
|
||||
FoundPos = StartPos;
|
||||
while (FoundPos < BufLen)
|
||||
{
|
||||
while (FoundPos < BufLen) {
|
||||
// Check if char match
|
||||
if (*CheckPos == *MatchPos) {
|
||||
if (MatchPos == MatchLimit) {
|
||||
// Full match found
|
||||
if (*CheckChar == *MatchChar) {
|
||||
if (MatchChar == MatchLimit) {
|
||||
// Whole needle found
|
||||
FoundPos -= SearchLen-1;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Next char to match
|
||||
MatchPos++;
|
||||
// More needle to find
|
||||
if (MatchChar == SearchStr)
|
||||
CheckStart = CheckChar; // Mark start of match
|
||||
MatchChar++; // Continue matching needle
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Reset match point
|
||||
MatchPos = (char*)SearchStr;
|
||||
else if (MatchChar != SearchStr){
|
||||
// Only partial needle found - reset match search
|
||||
FoundPos -= MatchChar - SearchStr;
|
||||
CheckChar = CheckStart;
|
||||
MatchChar = SearchStr;
|
||||
}
|
||||
|
||||
// Next char
|
||||
FoundPos++;
|
||||
if (CheckPos == CheckLimit) {
|
||||
if (CheckChar == CheckLimit) // End of haystack reached
|
||||
break;
|
||||
} else {
|
||||
CheckPos++;
|
||||
}
|
||||
else
|
||||
CheckChar++;
|
||||
}
|
||||
|
||||
// Not Found
|
||||
@@ -668,8 +651,7 @@ int CShiftBuffer::Clear( int ClearLen )
|
||||
int BytesCleared;
|
||||
|
||||
// Validate ClearLen
|
||||
if ((ClearLen == -1) || (ClearLen >= BufLen))
|
||||
{
|
||||
if ((ClearLen == -1) || (ClearLen >= BufLen)) {
|
||||
// Clear all data
|
||||
BufLen = 0;
|
||||
Buffer[0] = 0;
|
||||
@@ -695,17 +677,14 @@ int CShiftBuffer::Push( bool Overwrite, const char * Data, int Len )
|
||||
int BufRemain = 0;
|
||||
|
||||
// Validate Buffer and Len
|
||||
if (!BufSize || !Data || !Len) {
|
||||
if (!BufSize || !Data || !Len)
|
||||
return 0;
|
||||
}
|
||||
else if (Len == -1) {
|
||||
else if (Len == -1)
|
||||
Len = strlen( Data );
|
||||
}
|
||||
|
||||
// Copy Data to buffer
|
||||
BufRemain = BufSize - BufLen;
|
||||
if (Overwrite && (Len > BufRemain))
|
||||
{
|
||||
if (Overwrite && (Len > BufRemain)) {
|
||||
if (Len < BufSize) {
|
||||
memcpy( Buffer, &Buffer[Len-BufRemain], BufLen-(Len-BufRemain) ); // Shift remaining old data left
|
||||
memcpy( &Buffer[BufSize-Len], Data, Len ); // Copy new data
|
||||
@@ -720,8 +699,7 @@ int CShiftBuffer::Push( bool Overwrite, const char * Data, int Len )
|
||||
BufLen = BufSize;
|
||||
Buffer[BufLen] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// Copy first portion of new data to buffer
|
||||
BytesPushed = (Len > BufRemain)? BufRemain : Len;
|
||||
memcpy( &Buffer[BufLen], Data, BytesPushed );
|
||||
@@ -744,16 +722,14 @@ int CShiftBuffer::ReadFromFD( int Handle, int MaxRead, bool Overwrite )
|
||||
bool Error = false;
|
||||
|
||||
// Check if buffer created
|
||||
if (!BufSize) {
|
||||
if (!BufSize)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read file descriptor into buffer
|
||||
if (MaxRead == -1)
|
||||
MaxRead = BufSize;
|
||||
DataRemain = ((!Overwrite && (MaxRead > BufSize-BufLen)))? BufSize-BufLen : MaxRead;
|
||||
while (DataRemain)
|
||||
{
|
||||
while (DataRemain) {
|
||||
// Read from file descriptor
|
||||
BytesRead = read( Handle, &Buffer[BufLen], DataRemain );
|
||||
if (BytesRead <= 0) {
|
||||
@@ -770,9 +746,8 @@ int CShiftBuffer::ReadFromFD( int Handle, int MaxRead, bool Overwrite )
|
||||
// Zero terminate
|
||||
Buffer[BufLen] = 0;
|
||||
|
||||
if (DataRemain) {
|
||||
if (DataRemain)
|
||||
usleep( 500 );
|
||||
}
|
||||
}
|
||||
return (Error)? -TotalRead : TotalRead; // Report negative total on error
|
||||
}
|
||||
@@ -789,13 +764,11 @@ int CShiftBuffer::WriteToFD( int Handle, int MaxLen )
|
||||
bool Error = false;
|
||||
|
||||
// Check if buffer created
|
||||
if (!BufSize) {
|
||||
if (!BufSize)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read Data into buffer
|
||||
while (DataRemain)
|
||||
{
|
||||
while (DataRemain) {
|
||||
// Read from file descriptor
|
||||
BufRemain = BufSize - ReadPos;
|
||||
BytesWritten = write( Handle, &Buffer[ReadPos], ((BufRemain > DataRemain)? DataRemain : BufRemain) );
|
||||
@@ -811,13 +784,11 @@ int CShiftBuffer::WriteToFD( int Handle, int MaxLen )
|
||||
|
||||
// Update Buffer Pointers
|
||||
ReadPos += BytesWritten;
|
||||
if (ReadPos >= BufSize) {
|
||||
if (ReadPos >= BufSize)
|
||||
ReadPos = 0; // Rolling over end of buffer, start at beginning
|
||||
}
|
||||
|
||||
if (DataRemain) {
|
||||
if (DataRemain)
|
||||
usleep( 500 );
|
||||
}
|
||||
}
|
||||
|
||||
return (Error)? -TotalWritten : TotalWritten; // Report negative total on error
|
||||
|
||||
82
LogCore.cpp
82
LogCore.cpp
@@ -17,28 +17,44 @@
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Global vars
|
||||
char LogStr[10000]; // Temporary var to create log messages, globally available to save on memory operations
|
||||
|
||||
const char * LogOutputName[] = { "None", "Raw", "Normal", "Hex", "Bin" };
|
||||
const char * LogOutputName[] = { "None", "Raw", "Normal", "Hex", "Bin" };
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
CLogCore::CLogCore( FILE * pOutputFile )
|
||||
CLogCore::CLogCore( FILE * pOutputFile, int pBufferSize )
|
||||
{
|
||||
OutputFile = pOutputFile;
|
||||
BufferSize = pBufferSize;
|
||||
TempBuffer = (char*)malloc( BufferSize * sizeof(char) );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
int CLogCore::ReadLogBufSize( CDataMember * LogConfig )
|
||||
{
|
||||
int NewBufSize;
|
||||
|
||||
// Get buffer size
|
||||
NewBufSize = LogConfig->GetChInt( "BufferSize", BufferSize, true );
|
||||
|
||||
// Update buffer if size different
|
||||
if ((NewBufSize > 0) && (NewBufSize != BufferSize)) {
|
||||
free( TempBuffer );
|
||||
BufferSize = NewBufSize;
|
||||
TempBuffer = (char*)malloc( BufferSize * sizeof(char) );
|
||||
}
|
||||
return BufferSize;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
EDebugLevel CLogCore::ReadLogLevel( CDataMember * LogConfig )
|
||||
{
|
||||
char * TempStr;
|
||||
const char * TempStr;
|
||||
EDebugLevel LogLevel;
|
||||
|
||||
// Get debug level
|
||||
LogLevel = dlNone;
|
||||
TempStr = (char*)LogConfig->GetChStr( "Level", "Medium", true );
|
||||
if (TempStr)
|
||||
{
|
||||
TempStr = LogConfig->GetChStr( "Level", "Medium", true );
|
||||
if (TempStr) {
|
||||
if (!strcasecmp( TempStr, "None" ))
|
||||
LogLevel = dlNone;
|
||||
else if (!strcasecmp( TempStr, "Low" ))
|
||||
@@ -58,16 +74,15 @@ EDebugLevel CLogCore::ReadLogLevel( CDataMember * LogConfig )
|
||||
int CLogCore::ReadLogOutput( CDataMember * LogConfig )
|
||||
{
|
||||
CDataMember * ItemConfig;
|
||||
char * TempStr;
|
||||
const char * TempStr;
|
||||
|
||||
ELogOutput LogOutput = loNone;
|
||||
int LogOutOpt = 0;
|
||||
|
||||
// Get Output format
|
||||
LogOutput = loNone;
|
||||
TempStr = (char*)LogConfig->GetChStr( "Output", "Normal", true );
|
||||
if (TempStr)
|
||||
{
|
||||
TempStr = LogConfig->GetChStr( "Output", "Normal", true );
|
||||
if (TempStr) {
|
||||
if (!strcasecmp( TempStr, "None" )) // Do not print output (may use option OUT_COUNT)
|
||||
LogOutput = loNone;
|
||||
else if (!strcasecmp( TempStr, "Raw" )) // Print all output as is
|
||||
@@ -84,10 +99,8 @@ int CLogCore::ReadLogOutput( CDataMember * LogConfig )
|
||||
|
||||
// Set debug output
|
||||
ItemConfig = LogConfig->GetChElement( "Options", 0, true );
|
||||
while (ItemConfig)
|
||||
{
|
||||
if ((TempStr = (char*)ItemConfig->GetStr( "Normal" )))
|
||||
{
|
||||
while (ItemConfig) {
|
||||
if ((TempStr = (char*)ItemConfig->GetStr( "Normal" ))) {
|
||||
if (!strcasecmp( TempStr, "Count" )) // Print number of characters
|
||||
LogOutOpt |= OUT_COUNT;
|
||||
else if (!strcasecmp( TempStr, "NoCRLF" )) // With Normal, replace CR/LF with "."
|
||||
@@ -111,14 +124,12 @@ bool CLogCore::Message( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const char
|
||||
return false;
|
||||
|
||||
// Check debug level
|
||||
if (!OutputFile || (MsgLevel > DebugLevel)) {
|
||||
if (!OutputFile || (MsgLevel > DebugLevel))
|
||||
return true;
|
||||
}
|
||||
|
||||
// Show Date & Time
|
||||
if (OutputFile->_fileno > 3) {
|
||||
if (OutputFile->_fileno > 3)
|
||||
fprintf( OutputFile, "%s", GetDateTimeStr( "/", ":" ));
|
||||
}
|
||||
|
||||
// Print formated message
|
||||
va_start( ArgPtr, Format );
|
||||
@@ -141,18 +152,16 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short
|
||||
return false;
|
||||
|
||||
// Check debug level
|
||||
if (!OutputFile || (MsgLevel > DebugLevel)) {
|
||||
if (!OutputFile || (MsgLevel > DebugLevel))
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get length
|
||||
if (Len == -1)
|
||||
Len = strlen( Buffer );
|
||||
|
||||
// Show date & time
|
||||
if (OutputFile->_fileno > 3) {
|
||||
if (OutputFile->_fileno > 3)
|
||||
fprintf( OutputFile, "%s", GetDateTimeStr( "/", ":" ));
|
||||
}
|
||||
|
||||
// Show Lead
|
||||
if (Format && *Format) {
|
||||
@@ -162,39 +171,32 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short
|
||||
}
|
||||
|
||||
// Show byte count
|
||||
if (ShowCount)
|
||||
{
|
||||
// Print byte count
|
||||
if (ShowCount) {
|
||||
fprintf( OutputFile, " [%d] ", Len );
|
||||
if (!OutFormat) fprintf( OutputFile, "\n" ); // EOL if only count wanted
|
||||
}
|
||||
|
||||
// Show output
|
||||
if (OutFormat == loRaw)
|
||||
{
|
||||
if (OutFormat == loRaw) {
|
||||
// Print entire buffer as is (line feeds included)
|
||||
fprintf( OutputFile, "%.*s\n", Len, Buffer );
|
||||
}
|
||||
else if (OutFormat == loNormal)
|
||||
{
|
||||
else if (OutFormat == loNormal) {
|
||||
// Remove special char
|
||||
fprintf( OutputFile, "%s", BytesToSafeStr(Buffer, Len, NoCrLf, '.') );
|
||||
fprintf( OutputFile, "%s", BytesToSafeStr(Buffer, Len, NoCrLf, '.', TempBuffer) );
|
||||
|
||||
// Add EOL if not present or ignored
|
||||
if (NoCrLf || (Buffer[Len-1] != '\n')) {
|
||||
if (NoCrLf || (Buffer[Len-1] != '\n'))
|
||||
fprintf( OutputFile, "\n" );
|
||||
}
|
||||
}
|
||||
else if (OutFormat == loHex)
|
||||
{
|
||||
else if (OutFormat == loHex) {
|
||||
// Print Hex values of individual bytes
|
||||
fprintf( OutputFile, "%s", BytesToHexStr(Buffer, Len, " ") );
|
||||
fprintf( OutputFile, "%s", BytesToHexStr(Buffer, Len, " ", TempBuffer) );
|
||||
fprintf( OutputFile, "\n" );
|
||||
}
|
||||
else if (OutFormat == loBin)
|
||||
{
|
||||
else if (OutFormat == loBin) {
|
||||
// Print each byte as 8-bit binary
|
||||
fprintf( OutputFile, "%s", BytesToBinStr(Buffer, Len, " ") );
|
||||
fprintf( OutputFile, "%s", BytesToBinStr(Buffer, Len, " ", TempBuffer) );
|
||||
fprintf( OutputFile, "\n" );
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -31,14 +31,19 @@ typedef enum { dlNone = 0, dlLow = 1, dlMedium = 2, dlHigh = 3 } EDebugLevel;
|
||||
class CLogCore
|
||||
{
|
||||
private:
|
||||
FILE * OutputFile;
|
||||
FILE * OutputFile;
|
||||
char * TempBuffer;
|
||||
int BufferSize;
|
||||
|
||||
public:
|
||||
CLogCore( FILE * pOutputFile );
|
||||
CLogCore( FILE * pOutputFile, int pBufferSize = 5000 );
|
||||
|
||||
// Configuration file
|
||||
int ReadLogBufSize( CDataMember * LogConfig );
|
||||
EDebugLevel ReadLogLevel( CDataMember * LogConfig );
|
||||
int ReadLogOutput( CDataMember * LogConfig );
|
||||
|
||||
// Log output
|
||||
bool Message( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const char * Format, ... );
|
||||
bool Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short OutputFormat, const char * Buffer, int Len, const char * Format, ... );
|
||||
};
|
||||
|
||||
100
UtilCore.cpp
100
UtilCore.cpp
@@ -16,27 +16,15 @@
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Variable used to temp values with
|
||||
static char ReturnStr[10000];
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf, const char SpecChar, char * OutBuf )
|
||||
{
|
||||
char * TempBuf;
|
||||
char * BufPos;
|
||||
|
||||
// Select/create output buffer
|
||||
if (!OutBuf) {
|
||||
TempBuf = ReturnStr;
|
||||
}
|
||||
else {
|
||||
// if (!*OutBuf)
|
||||
// *OutBuf = (char*)malloc( Len+1 );
|
||||
// TempBuf = *OutBuf;
|
||||
TempBuf = OutBuf;
|
||||
}
|
||||
BufPos = TempBuf;
|
||||
// Validate output buffer
|
||||
if (!OutBuf)
|
||||
return NULL;
|
||||
else
|
||||
BufPos = OutBuf;
|
||||
|
||||
// Remove special char
|
||||
for (int i=0; i<Len; i++) {
|
||||
@@ -53,28 +41,21 @@ char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf, con
|
||||
BufPos++;
|
||||
}
|
||||
*BufPos = 0;
|
||||
return TempBuf;
|
||||
return OutBuf;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
char * BytesToHexStr( const char * Bytes, const int Len, const char * Separator, char * OutBuf )
|
||||
{
|
||||
char * TempBuf;
|
||||
char * BufPos;
|
||||
bool First = true;
|
||||
char SepLen = (Separator)? strlen(Separator) : 0;
|
||||
|
||||
// Select/create output buffer
|
||||
if (!OutBuf) {
|
||||
TempBuf = ReturnStr;
|
||||
}
|
||||
else {
|
||||
// if (!*OutBuf)
|
||||
// *OutBuf = (char*)malloc( Len*(2+SepLen)+1 );
|
||||
// TempBuf = *OutBuf;
|
||||
TempBuf = OutBuf;
|
||||
}
|
||||
BufPos = TempBuf;
|
||||
// Validate output buffer
|
||||
if (!OutBuf)
|
||||
return NULL;
|
||||
else
|
||||
BufPos = OutBuf;
|
||||
|
||||
// Print Hex values of individual bytes
|
||||
for (int i=0; i<Len; i++) {
|
||||
@@ -83,28 +64,21 @@ char * BytesToHexStr( const char * Bytes, const int Len, const char * Separator,
|
||||
First = false;
|
||||
}
|
||||
*BufPos = 0;
|
||||
return TempBuf;
|
||||
return OutBuf;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator, char * OutBuf )
|
||||
{
|
||||
char * TempBuf;
|
||||
char * BufPos;
|
||||
bool First = true;
|
||||
char SepLen = (Separator)? strlen(Separator) : 0;
|
||||
|
||||
// Select/create output buffer
|
||||
if (!OutBuf) {
|
||||
TempBuf = ReturnStr;
|
||||
}
|
||||
else {
|
||||
// if (!*OutBuf)
|
||||
// *OutBuf = (char*)malloc( Len*(8+SepLen)+1 );
|
||||
// TempBuf = *OutBuf;
|
||||
TempBuf = OutBuf;
|
||||
}
|
||||
BufPos = TempBuf;
|
||||
// Validate output buffer
|
||||
if (!OutBuf)
|
||||
return NULL;
|
||||
else
|
||||
BufPos = OutBuf;
|
||||
|
||||
// Print each byte as 8-bit binary
|
||||
for (int i=0; i<Len; i++) {
|
||||
@@ -115,29 +89,22 @@ char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator,
|
||||
First = false;
|
||||
}
|
||||
*BufPos = 0;
|
||||
return TempBuf;
|
||||
return OutBuf;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
char * IntToBinStr( const int Bytes, const int Len, const char * Separator, char * OutBuf )
|
||||
{
|
||||
char * TempBuf;
|
||||
char * BufPos;
|
||||
bool First = true;
|
||||
char SepLen = (Separator)? strlen(Separator) : 0;
|
||||
u_int8_t Byte;
|
||||
|
||||
// Select/create output buffer
|
||||
if (!OutBuf) {
|
||||
TempBuf = ReturnStr;
|
||||
}
|
||||
else {
|
||||
// if (!*OutBuf)
|
||||
// *OutBuf = (char*)malloc( Len*(8+SepLen)+1 );
|
||||
// TempBuf = *OutBuf;
|
||||
TempBuf = OutBuf;
|
||||
}
|
||||
BufPos = TempBuf;
|
||||
// Validate output buffer
|
||||
if (!OutBuf)
|
||||
return NULL;
|
||||
else
|
||||
BufPos = OutBuf;
|
||||
|
||||
// Print each byte as 8-bit binary
|
||||
for (int i=Len-1; i>=0; i--) {
|
||||
@@ -149,28 +116,21 @@ char * IntToBinStr( const int Bytes, const int Len, const char * Separator, char
|
||||
First = false;
|
||||
}
|
||||
*BufPos = 0;
|
||||
return TempBuf;
|
||||
return OutBuf;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
char * HexStrToBytes( const char * Str, const int Len, const char * Separator, char * OutBuf )
|
||||
{
|
||||
char * TempBuf;
|
||||
u_int8_t * BufPos;
|
||||
char * StrPos = (char*)Str;
|
||||
char SepLen = (Separator)? strlen(Separator) : 0;
|
||||
|
||||
// Select/create output buffer
|
||||
if (!OutBuf) {
|
||||
TempBuf = ReturnStr;
|
||||
}
|
||||
else {
|
||||
// if (!*OutBuf)
|
||||
// *OutBuf = (char*)malloc( Len/(2+SepLen)+1 );
|
||||
// TempBuf = *OutBuf;
|
||||
TempBuf = OutBuf;
|
||||
}
|
||||
BufPos = (u_int8_t*)TempBuf;
|
||||
// Validate output buffer
|
||||
if (!OutBuf)
|
||||
return NULL;
|
||||
else
|
||||
BufPos = (u_int8_t*)OutBuf;
|
||||
|
||||
// Print Hex values of individual bytes
|
||||
while ((StrPos - Str < Len) && isxdigit(StrPos[0]) && isxdigit(StrPos[1])) {
|
||||
@@ -185,7 +145,7 @@ char * HexStrToBytes( const char * Str, const int Len, const char * Separator, c
|
||||
}
|
||||
}
|
||||
*BufPos = 0;
|
||||
return TempBuf;
|
||||
return OutBuf;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
12
UtilCore.h
12
UtilCore.h
@@ -17,11 +17,11 @@
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Convert raw bytes to string
|
||||
char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf = false, const char SpecChar = '.', char * OutBuf = NULL );
|
||||
char * BytesToHexStr( const char * Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
||||
char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
||||
char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf, const char SpecChar, char * OutBuf );
|
||||
char * BytesToHexStr( const char * Bytes, const int Len, const char * Separator, char * OutBuf );
|
||||
char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator, char * OutBuf );
|
||||
|
||||
char * IntToBinStr( const int Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
||||
char * IntToBinStr( const int Bytes, const int Len, const char * Separator, char * OutBuf );
|
||||
|
||||
// Convert string to raw bytes
|
||||
inline u_int8_t HexToInt( char Digit ) {
|
||||
@@ -30,8 +30,8 @@ inline u_int8_t HexToInt( char Digit ) {
|
||||
else if ((Digit >= 'a') && (Digit <= 'f')) return (Digit - 'a'+10);
|
||||
else return 0;
|
||||
}
|
||||
char * HexStrToBytes( const char * Str, const int Len, const char * Separator = NULL, char * OutBuf = NULL );
|
||||
char * BinStrToBytes( const char * Str, const int Len, const char * Separator = NULL, char * OutBuf = NULL );
|
||||
char * HexStrToBytes( const char * Str, const int Len, const char * Separator, char * OutBuf );
|
||||
char * BinStrToBytes( const char * Str, const int Len, const char * Separator, char * OutBuf );
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user