From 0046edc25146674998e26a21557e5829e2770670 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Mon, 15 Apr 2019 17:26:52 +0200 Subject: [PATCH] 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 --- CMakeLists.txt | 2 +- LogCore.cpp | 26 ++------ UtilCore.cpp | 178 +++++++++++++++++++++++++++++++++++++++++++++++++ UtilCore.h | 39 +++++++++++ 4 files changed, 224 insertions(+), 21 deletions(-) create mode 100644 UtilCore.cpp create mode 100644 UtilCore.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c39d4d..9bfeabf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ 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 ApplicationCore.cpp FunctionCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp WatchdogCore.cpp DeviceCore.cpp ) diff --git a/LogCore.cpp b/LogCore.cpp index fa83d9b..0b1cf58 100644 --- a/LogCore.cpp +++ b/LogCore.cpp @@ -12,6 +12,7 @@ // redA Libraries #include "LogCore.h" #include "DateTimeCore.h" +#include "UtilCore.h" //--------------------------------------------------------------------------- @@ -176,18 +177,9 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short } else if (OutFormat == loNormal) { - // Ignore \r\n - for (int i=0; i 126)) { - if (((Buffer[i] == '\r') || (Buffer[i] == '\n')) && !NoCrLf) - fprintf( OutputFile, "%c", Buffer[i] ); - else - fprintf( OutputFile, "." ); - } - else { - fprintf( OutputFile, "%c", Buffer[i] ); - } - } + // Remove special char + fprintf( OutputFile, "%s", BytesToSafeStr(Buffer, Len, NoCrLf, '.') ); + // Add EOL if not present or ignored if (NoCrLf || (Buffer[Len-1] != '\n')) { fprintf( OutputFile, "\n" ); @@ -196,19 +188,13 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short else if (OutFormat == loHex) { // Print Hex values of individual bytes - for (int i=0; i +#include +#include +#include + +// 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 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= '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_ */