-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.cpp
123 lines (98 loc) · 3.25 KB
/
helper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* SPDX-License-Identifier: GPLv3-or-later */
/* Copyright (c) 2020 Project WomoLIN */
/* Author Myron Franze <[email protected]> */
#include "interface/helper.h"
#include <iostream>
namespace siguni::helper
{
int CSignalStrings::FindFirstCharacter( const std::string & attSignalMessage,
const char attCharacter,
const size_t attPos )
{
auto pos = attSignalMessage.find( attCharacter, attPos );
if ( std::string::npos == pos )
{
return -1;
}
else
{
return pos;
}
}
int CSignalStrings::FindLastCharacter( const std::string & attSignalMessage,
const char attCharacter )
{
int pos = -1;
int lastPos { -1 };
do
{
pos = FindFirstCharacter( attSignalMessage, attCharacter, pos+1 );
if( pos >= 0 )
{
lastPos = pos;
}
}while( pos >= 0 );
return lastPos;
}
bool CSignalStrings::SplitKeyValue( const std::string & attSignalMessage,
const char attDelimiter,
std::string & attKey,
std::string & attValue )
{
auto firstPos = attSignalMessage.find( attDelimiter );
if ( std::string::npos == firstPos ) {
return false;
}
auto secondPos = attSignalMessage.substr( firstPos + 1 ).find( attDelimiter );
if ( std::string::npos != secondPos ) {
return false; // found more than one delimiter
}
// extract key and value
attKey = attSignalMessage.substr( 0, firstPos );
attValue = attSignalMessage.substr( firstPos + 1 );
return true;
}
std::vector<std::string>
CSignalStrings::GetValueItems( std::string attString,
const char attDelimiter )
{
std::vector<std::string> items;
size_t pos = 0;
std::string token;
while ( ( pos = attString.find( attDelimiter ) ) != std::string::npos) {
items.push_back( attString.substr( 0, pos ) );
attString.erase( 0, pos + 1 );
}
items.push_back( attString );
return items;
}
bool CSignalStrings::CompareTwoStringVectors( std::vector<std::string> attStr1,
std::vector<std::string> attStr2 )
{
sort( attStr1.begin(), attStr1.end() );
sort( attStr2.begin(), attStr2.end() );
if( attStr1.size() != attStr2.size() ){
return false;
}
for ( unsigned int i=0; i<attStr1.size(); i++ )
{
if( 0 != attStr1[i].compare( attStr2[i] ) )
{
return false;
}
}
return true;
}
std::string CSignalStrings::CreateStringFromVector( std::vector<std::string> attStrVector,
const char attDelimiter )
{
std::string vectorString;
vectorString += attStrVector[0];
for ( unsigned int i=1; i<attStrVector.size(); i++ )
{
vectorString += attDelimiter;
vectorString += attStrVector[i];
}
return vectorString;
}
}