Important Update:
- Implemented new Util library: - Convert Bytes to Hex/Bin/Safe string - Convert HexString to Bytes - Search String for substring function - Use Util library in LogCore for String conversions
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
PROJECT(lib_redAcore)
|
PROJECT(lib_redAcore)
|
||||||
|
|
||||||
ADD_LIBRARY(redAcore DateTimeCore.cpp LogCore.cpp SignalCore.cpp DataTreeCore.cpp JSONparseCore.cpp CharBufferCore.cpp
|
ADD_LIBRARY(redAcore DateTimeCore.cpp UtilCore.cpp LogCore.cpp SignalCore.cpp DataTreeCore.cpp JSONparseCore.cpp CharBufferCore.cpp
|
||||||
LiteProtocolCore.cpp ItemBufferCore.cpp EventBufferCore.cpp
|
LiteProtocolCore.cpp ItemBufferCore.cpp EventBufferCore.cpp
|
||||||
ApplicationCore.cpp FunctionCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp WatchdogCore.cpp DeviceCore.cpp )
|
ApplicationCore.cpp FunctionCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp WatchdogCore.cpp DeviceCore.cpp )
|
||||||
|
|||||||
26
LogCore.cpp
26
LogCore.cpp
@@ -12,6 +12,7 @@
|
|||||||
// redA Libraries
|
// redA Libraries
|
||||||
#include "LogCore.h"
|
#include "LogCore.h"
|
||||||
#include "DateTimeCore.h"
|
#include "DateTimeCore.h"
|
||||||
|
#include "UtilCore.h"
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -176,18 +177,9 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short
|
|||||||
}
|
}
|
||||||
else if (OutFormat == loNormal)
|
else if (OutFormat == loNormal)
|
||||||
{
|
{
|
||||||
// Ignore \r\n
|
// Remove special char
|
||||||
for (int i=0; i<Len; i++) {
|
fprintf( OutputFile, "%s", BytesToSafeStr(Buffer, Len, NoCrLf, '.') );
|
||||||
if ((Buffer[i] < 32) || (Buffer[i] > 126)) {
|
|
||||||
if (((Buffer[i] == '\r') || (Buffer[i] == '\n')) && !NoCrLf)
|
|
||||||
fprintf( OutputFile, "%c", Buffer[i] );
|
|
||||||
else
|
|
||||||
fprintf( OutputFile, "." );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
fprintf( OutputFile, "%c", Buffer[i] );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 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" );
|
||||||
@@ -196,19 +188,13 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short
|
|||||||
else if (OutFormat == loHex)
|
else if (OutFormat == loHex)
|
||||||
{
|
{
|
||||||
// Print Hex values of individual bytes
|
// Print Hex values of individual bytes
|
||||||
for (int i=0; i<Len; i++) {
|
fprintf( OutputFile, "%s", BytesToHexStr(Buffer, Len, " ") );
|
||||||
fprintf( OutputFile, "%02X ", (unsigned char)Buffer[i] );
|
|
||||||
}
|
|
||||||
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
|
||||||
for (int i=0; i<Len; i++) {
|
fprintf( OutputFile, "%s", BytesToBinStr(Buffer, Len, " ") );
|
||||||
fprintf( OutputFile, "%c%c%c%c%c%c%c%c ",
|
|
||||||
(Buffer[i] & 0x80)?'1':'0', (Buffer[i] & 0x40)?'1':'0', (Buffer[i] & 0x20)?'1':'0', (Buffer[i] & 0x10)?'1':'0',
|
|
||||||
(Buffer[i] & 0x08)?'1':'0', (Buffer[i] & 0x04)?'1':'0', (Buffer[i] & 0x02)?'1':'0', (Buffer[i] & 0x01)?'1':'0' );
|
|
||||||
}
|
|
||||||
fprintf( OutputFile, "\n" );
|
fprintf( OutputFile, "\n" );
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
178
UtilCore.cpp
Normal file
178
UtilCore.cpp
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
* UtilCore.cpp
|
||||||
|
*
|
||||||
|
* Created on: 14 April 2019
|
||||||
|
* Author: wentzelc
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Standard C/C++ Libraries
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
// redA Libraries
|
||||||
|
#include "UtilCore.h"
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Variable used to temp values with
|
||||||
|
static char ReturnStr[1000];
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Remove special char
|
||||||
|
for (int i=0; i<Len; i++) {
|
||||||
|
if (((Bytes[i] < 32) || (Bytes[i] > 126)) ||
|
||||||
|
(NoCrLf && ((Bytes[i] == '\r') || (Bytes[i] == '\n')))) {
|
||||||
|
*BufPos = SpecChar;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*BufPos = Bytes[i];
|
||||||
|
}
|
||||||
|
BufPos++;
|
||||||
|
}
|
||||||
|
BufPos = 0;
|
||||||
|
return TempBuf;
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Print Hex values of individual bytes
|
||||||
|
for (int i=0; i<Len; i++) {
|
||||||
|
sprintf( BufPos, "%s%02X", ((First)? "" : Separator), (unsigned char)Bytes[i] );
|
||||||
|
BufPos += (First)? 2 : 2+SepLen;
|
||||||
|
First = false;
|
||||||
|
}
|
||||||
|
BufPos = 0;
|
||||||
|
return TempBuf;
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Print each byte as 8-bit binary
|
||||||
|
for (int i=0; i<Len; i++) {
|
||||||
|
sprintf( BufPos, "%s%c%c%c%c%c%c%c%c", ((First)? "" : Separator),
|
||||||
|
(Bytes[i] & 0x80)?'1':'0', (Bytes[i] & 0x40)?'1':'0', (Bytes[i] & 0x20)?'1':'0', (Bytes[i] & 0x10)?'1':'0',
|
||||||
|
(Bytes[i] & 0x08)?'1':'0', (Bytes[i] & 0x04)?'1':'0', (Bytes[i] & 0x02)?'1':'0', (Bytes[i] & 0x01)?'1':'0' );
|
||||||
|
BufPos += (First)? 8 : 8+SepLen;
|
||||||
|
First = false;
|
||||||
|
}
|
||||||
|
BufPos = 0;
|
||||||
|
return TempBuf;
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Print Hex values of individual bytes
|
||||||
|
while ((StrPos - Str < Len) && isxdigit(StrPos[0]) && isxdigit(StrPos[1])) {
|
||||||
|
*BufPos = (HexToInt(StrPos[0]) << 4) + HexToInt(StrPos[1]);
|
||||||
|
BufPos++;
|
||||||
|
StrPos += 2;
|
||||||
|
if (Separator) {
|
||||||
|
if (memcmp( StrPos, Separator, SepLen ))
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
StrPos += SepLen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BufPos = 0;
|
||||||
|
return TempBuf;
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
char * StrSearch( const char * Haystack, const char * Needle, const int HaystackLen, const int NeedleLen )
|
||||||
|
{
|
||||||
|
if ((Haystack == NULL) || (Needle == NULL))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
char * hPos = (char*)Haystack;
|
||||||
|
char * nPos = (char*)Needle;
|
||||||
|
int hLen = (!HaystackLen)? strlen(Haystack) : HaystackLen;
|
||||||
|
int nLen = (!NeedleLen)? strlen(Needle) : NeedleLen;
|
||||||
|
char * Mark = NULL;
|
||||||
|
|
||||||
|
while ((hPos = (char*)memchr( hPos, *nPos, (hLen-(hPos-Haystack)) ))) { // Check if first char match
|
||||||
|
Mark = hPos;
|
||||||
|
hPos++; nPos++;
|
||||||
|
while ((hPos - Haystack < hLen) && (nPos - Needle < nLen) && (*hPos == *nPos)) { // Check remain char match
|
||||||
|
hPos++; nPos++;
|
||||||
|
}
|
||||||
|
if (nPos - Needle == nLen) return Mark; // Found it!
|
||||||
|
hPos = Mark+1; // Continue search
|
||||||
|
nPos = (char*)Needle;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
39
UtilCore.h
Normal file
39
UtilCore.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* UtilCore.h
|
||||||
|
*
|
||||||
|
* Created on: 14 April 2019
|
||||||
|
* Author: wentzelc
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef REDACORE_UTILCORE_H_
|
||||||
|
#define REDACORE_UTILCORE_H_
|
||||||
|
|
||||||
|
// Standard C/C++ Libraries
|
||||||
|
/* none */
|
||||||
|
|
||||||
|
// redA Libraries
|
||||||
|
/* none */
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// 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 );
|
||||||
|
|
||||||
|
// Convert string to raw bytes
|
||||||
|
inline u_int8_t HexToInt( char Digit ) {
|
||||||
|
if ((Digit >= '0') && (Digit <= '9')) return (Digit - '0');
|
||||||
|
else if ((Digit >= 'A') && (Digit <= 'F')) return (Digit - 'A'+10);
|
||||||
|
else if ((Digit >= 'a') && (Digit <= 'f')) return (Digit - 'a'+10);
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
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 );
|
||||||
|
|
||||||
|
// Search string data
|
||||||
|
char * StrSearch( const char * Haystack, const char * Needle, const int hLen = 0, const int nLen = 0 );
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#endif /* REDACORE_UTILCORE_H_ */
|
||||||
Reference in New Issue
Block a user