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