Important Update:
- CLiteProtocol: - Set input string on methods to const char * - Simplify checking for verification - Ensure Length parameter can be left out on all methods - Add quick functions for structured commands: To, From, Command - CLogCore: - Use only "\n", not "\r\n" - SignalCore: - Use std::endl instead of "\r\n"
This commit is contained in:
@@ -95,7 +95,7 @@ bool CLiteProtocol::UpdateCommandStr()
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CLiteProtocol::VerifyStr( char * pString, int pLength, int &ParamCount, char * pError )
|
||||
bool CLiteProtocol::VerifyStr( const char * pString, int pLength, int &ParamCount, char * pError )
|
||||
{
|
||||
char * Mark;
|
||||
long tmpLen;
|
||||
@@ -230,7 +230,7 @@ char* CLiteProtocol::GetCommandStr( char ** pString, int * pLength )
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CLiteProtocol::SetCommandStr( char * pString, int pLength, char * Error )
|
||||
bool CLiteProtocol::SetCommandStr( const char * pString, const int pLength )
|
||||
{
|
||||
int ParamCount;
|
||||
|
||||
@@ -343,21 +343,24 @@ bool CLiteProtocol::VerifyCommandStr()
|
||||
|
||||
bool CLiteProtocol::AppendParam( const char * pString, const int pLength )
|
||||
{
|
||||
int StringLen;
|
||||
|
||||
// Check if valid
|
||||
if (!pString)
|
||||
return false;
|
||||
|
||||
// Increase string length if required
|
||||
IncCommandStr( Length + pLength+1 );
|
||||
StringLen = (pLength < 0)? strlen(pString) : pLength;
|
||||
IncCommandStr( Length + StringLen+1 );
|
||||
|
||||
// Copy string
|
||||
memcpy( &CommandStr[CurrPos], (char*)pString, pLength );
|
||||
CommandStr[CurrPos+pLength] = Separator;
|
||||
Length = Length + pLength + 1; // Allow for separator character
|
||||
memcpy( &CommandStr[CurrPos], (char*)pString, StringLen );
|
||||
CommandStr[CurrPos+StringLen] = Separator;
|
||||
Length = Length + StringLen + 1; // Allow for separator character
|
||||
|
||||
// Move pointer to end and set end char
|
||||
CurrParam++;
|
||||
CurrPos = CurrPos + pLength + 1; // Allow for separator character
|
||||
CurrPos = CurrPos + StringLen + 1; // Allow for separator character
|
||||
CommandStr[CurrPos] = EndChar;
|
||||
|
||||
// Update references
|
||||
@@ -391,8 +394,8 @@ bool CLiteProtocol::AppendParam( double pValue, int pDigits )
|
||||
char ConvStr[10];
|
||||
char TempStr[20];
|
||||
|
||||
// Check if command sting exists
|
||||
if (!CommandStr)
|
||||
// Check if command sting valid
|
||||
if (!Verified)
|
||||
return false;
|
||||
|
||||
// Create string representation
|
||||
@@ -415,7 +418,7 @@ char const * CLiteProtocol::GetParamNo( int ParamNo, char ** pString, int * pLen
|
||||
char * Mark = NULL;
|
||||
|
||||
// Check if valid parameter no
|
||||
if (!CommandStr || !CommandStr[0] || !Length || (ParamNo < 1) || (ParamNo > NoOfParams))
|
||||
if (!Verified || (ParamNo < 1) || (ParamNo > NoOfParams))
|
||||
return NULL;
|
||||
|
||||
// Set Starting point
|
||||
@@ -444,8 +447,8 @@ char const * CLiteProtocol::GetParamNo( int ParamNo, char ** pString, int * pLen
|
||||
|
||||
bool CLiteProtocol::GotoParam( int ParamNo )
|
||||
{
|
||||
// Check if command sting exists
|
||||
if (!CommandStr || (ParamNo < 1))
|
||||
// Check if command sting valid
|
||||
if (!Verified || (ParamNo < 1) || (ParamNo > NoOfParams+1))
|
||||
return false;
|
||||
|
||||
// Goto Param one
|
||||
@@ -464,12 +467,8 @@ bool CLiteProtocol::NextParam()
|
||||
int ParamLen;
|
||||
char * Mark = NULL;
|
||||
|
||||
// Check if command sting exists
|
||||
if (!CommandStr || !CommandStr[0] || !Length)
|
||||
return false;
|
||||
|
||||
// Check if next param exists
|
||||
if (CurrParam == NoOfParams)
|
||||
// Check if command sting valid
|
||||
if (!Verified || (CurrParam == NoOfParams))
|
||||
return false;
|
||||
|
||||
// Get Current parameter length
|
||||
@@ -491,7 +490,7 @@ char const * CLiteProtocol::GetParam( char ** pString, int * pLength )
|
||||
char * Mark = NULL;
|
||||
|
||||
// Check if valid parameter no
|
||||
if (!CommandStr || !CommandStr[0] || !Length)
|
||||
if (!Verified)
|
||||
{
|
||||
if (pString) *pString = NULL;
|
||||
if (pLength) pLength = 0;
|
||||
@@ -511,3 +510,45 @@ char const * CLiteProtocol::GetParam( char ** pString, int * pLength )
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Create command
|
||||
bool CLiteProtocol::CreateCommand( char const * From, char const * To, char const * Command )
|
||||
{
|
||||
// Create new command
|
||||
if (!NewCommandStr())
|
||||
return false;
|
||||
|
||||
// Insert Parameters in correct order
|
||||
if (!AppendParam( From ) || !AppendParam( To ) || !AppendParam( Command ))
|
||||
{
|
||||
ClearCommandStr();
|
||||
return false;
|
||||
};
|
||||
|
||||
// All is well
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Read command
|
||||
bool CLiteProtocol::ReadCommand( char ** From, char ** To, char ** Command, int * DataParams,
|
||||
int * FromLen, int * ToLen, int * CommandLen )
|
||||
{
|
||||
// Check if parameters available
|
||||
if (!Verified || (NoOfParams < 3))
|
||||
return false;
|
||||
|
||||
// Read parameters one by one
|
||||
GotoParam( 1 );
|
||||
GetParam( From, FromLen );
|
||||
NextParam();
|
||||
GetParam( To, ToLen );
|
||||
NextParam();
|
||||
GetParam( Command, CommandLen );
|
||||
|
||||
if (DataParams)
|
||||
*DataParams = NoOfParams - 3;
|
||||
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user