ADTF
Loading...
Searching...
No Matches
stringlist.h
Go to the documentation of this file.
1
7#pragma once
8
9namespace A_UTILS_NS
10{
11
17template<class storageT>
19{
20 public:
24 typedef std::vector<storageT> tStringList;
26 typedef typename tStringList::const_iterator const_iterator;
28 typedef typename tStringList::const_reverse_iterator const_reverse_iterator;
30 typedef typename tStringList::iterator iterator;
32 typedef typename tStringList::reverse_iterator reverse_iterator;
33
35 static const tSize InvalidPos = g_npos;
36
37 protected:
40
41 public:
44 {
45 return m_lstList.begin();
46 }
47
49 {
50 return m_lstList.end();
51 }
52
54 {
55 return m_lstList.cbegin();
56 }
57
59 {
60 return m_lstList.cend();
61 }
62
64 {
65 return m_lstList.cbegin();
66 }
67
69 {
70 return m_lstList.cend();
71 }
72
74 {
75 return m_lstList.rbegin();
76 }
77
79 {
80 return m_lstList.rend();
81 }
82
84 {
85 return m_lstList.crbegin();
86 }
87
89 {
90 return m_lstList.crend();
91 }
92
98 {
99 }
100
107 string_list_base(const _myType& lstList)
108 {
109 Copy(lstList);
110 }
111
119 {
120 //@TODO: Could also move
121 Copy(lstList);
122 }
123
131 string_list_base(const storageT& strList)
132 {
133 _myType lstList;
134
135 tSize nTokenLen = 1;
137 tSize nPosComma = cString::InvalidPos;
138 tSize nPosSemicolon = cString::InvalidPos;
139 tSize nStartPos;
140
141 do
142 {
143 nStartPos = nPos + 1;
144 nPosComma = strList.Find(",", nStartPos);
145 nPosSemicolon = strList.Find(";", nStartPos);
146
147 if (cString::InvalidPos != nPosComma && cString::InvalidPos != nPosSemicolon)
148 {
149 nPosComma < nPosSemicolon ? nPos = nPosComma : nPos = nPosSemicolon;
150 }
151 else if (cString::InvalidPos != nPosSemicolon)
152 {
153 nPos = nPosSemicolon;
154 }
155 else
156 {
157 nPos = nPosComma;
158 }
159
160 if (nPos != cString::InvalidPos)
161 {
162 if (nPos != 0)
163 {
164 lstList.m_lstList.emplace_back(strList.Mid(nStartPos, nPos - nStartPos));
165 }
166 else
167 {
168 lstList.m_lstList.emplace_back(storageT(""));
169 }
170 nPos += nTokenLen - 1;
171 }
172 else
173 {
174 if (nStartPos < strList.GetLength())
175 {
176 lstList.m_lstList.emplace_back(strList.Mid(nStartPos));
177 }
178 else if (strList.EndsWith(",") || strList.EndsWith(";"))
179 {
180 lstList.m_lstList.emplace_back(storageT(""));
181 }
182 break;
183 }
184 } while (nPos != cString::InvalidPos);
185
186 Copy(lstList);
187 }
188
189 string_list_base(std::initializer_list<storageT> lstStrings)
190 {
191 for (const auto& itElem : lstStrings)
192 {
193 Append(itElem);
194 }
195 }
196
206 {
207 Copy(lstList);
208 return *this;
209 }
210
220 {
221 //@TODO: could be moved!!
222 Copy(lstList);
223 return *this;
224 }
225
232 {
233 Clear();
234 }
235
246 tVoid Copy(const _myType& lstList)
247 {
248 Clear();
249 m_lstList = lstList.m_lstList;
250 }
251
258 {
259 if (m_lstList.size() > 0)
260 {
261 m_lstList.clear();
262 }
263 }
264
275 tResult Append(const storageT& strString)
276 {
277 m_lstList.push_back(strString);
279 }
280
291 tResult Append(const _myType& lstString)
292 {
293 auto oIt = lstString.cbegin();
294 for (;
295 oIt != lstString.cend();
296 oIt++)
297 {
298 Append(*oIt);
299 }
300
302 }
303
315 tResult Add(const storageT& strString)
316 {
317 if (InvalidPos != Find(strString))
318 {
319 RETURN_ERROR(ERR_FAILED);
320 }
321
322 Append(strString);
324 }
325
337 tResult Add(const _myType& lstString)
338 {
339 tInt nAddedCount = 0;
340 for (auto it = lstString.begin();
341 it != lstString.end();
342 it++)
343 {
344 if (IS_OK(Add(*it)))
345 {
346 nAddedCount++;
347 }
348 }
349
350 if (nAddedCount > 0)
351 {
353 }
354 else
355 {
356 RETURN_ERROR(ERR_FAILED);
357 }
358 }
359
372 tResult Insert(tSize nIdx, const storageT& strString)
373 {
374 if (nIdx < m_lstList.size())
375 {
376 m_lstList.insert(m_lstList.begin() + nIdx, strString);
378 }
379 else
380 {
381 RETURN_ERROR(ERR_INVALID_INDEX);
382 }
383 }
384
396 {
397 if (nIdx < m_lstList.size())
398 {
399 m_lstList.erase(m_lstList.begin() + nIdx);
400 }
401 else
402 {
403 RETURN_ERROR(ERR_INVALID_INDEX);
404 }
405 return ERR_NOERROR;
406 }
407
418 static tBool fnSortLexically(const storageT& strFirst, const storageT& strSecond)
419 {
420 if (0 > strFirst.CompareNoCase(strSecond))
421 {
422 return tTrue;
423 }
424 else
425 {
426 return tFalse;
427 }
428 }
429
440 tVoid Sort(tBool bIgnoreCase = tFalse)
441 {
442 if (!bIgnoreCase)
443 {
444 std::sort(m_lstList.begin(), m_lstList.end());
445 }
446 else
447 {
448 std::sort(m_lstList.begin(), m_lstList.end(), fnSortLexically);
449 }
450 }
451
459 {
460 return m_lstList.size();
461 }
462
473 const storageT& Get(tSize nIdx, const storageT& strDefault = "") const
474 {
475 if (nIdx < GetItemCount())
476 {
477 const storageT& strRet = m_lstList[nIdx];
478 return strRet;
479 }
480 return strDefault;
481 }
482
494 tResult Set(tSize nIdx, const storageT& strValue)
495 {
496 if (InvalidPos == nIdx)
497 {
498 return ERR_INVALID_ARG;
499 }
500 else if (nIdx < GetItemCount())
501 {
502 m_lstList[nIdx] = strValue;
503 }
504 else
505 {
506 m_lstList.push_back(strValue);
507 }
509 }
510
520 tSize Find(const storageT& strString) const
521 {
522 const_iterator it = std::find(cbegin(), cend(), strString);
523 if (it != cend())
524 {
525 return std::distance(cbegin(), it);
526 }
527 else
528 {
529 return InvalidPos;
530 }
531 }
532
539 tSize Match(const storageT& strPattern) const
540 {
541 tSize nMatchType = 0; // exact
542 tSize nStart = 0;
543 tSize nLen = strPattern.GetLength();
544
545 if (!strPattern.IsEmpty())
546 {
547 const tChar* ptr = strPattern.GetPtr();
548 if (*ptr == '*')
549 {
550 nMatchType |= 1;
551 nStart = 1;
552 nLen--;
553 }
554
555 if (*(ptr + strPattern.GetLength() - 1) == '*')
556 {
557 nMatchType |= 2;
558 nLen--;
559 }
560 }
561
562 if (nMatchType == 0)
563 {
564 return Find(strPattern);
565 }
566
567 storageT strSubStr = strPattern.Mid(nStart, nLen);
568
569 storageT strItem;
570 tSize l_nIdx = 0;
571 typename tStringList::const_iterator l_poI;
572 for (l_poI = m_lstList.begin(); l_poI != m_lstList.end(); ++l_poI)
573 {
574 if (nMatchType == 1)
575 {
576 // search for "*text"
577 strItem = (*l_poI).Right(nLen);
578 if (strItem.IsEqual(strSubStr))
579 {
580 return l_nIdx;
581 }
582 }
583 else if (nMatchType == 2)
584 {
585 // search for "text*"
586 strItem = (*l_poI).Left(nLen);
587 if (strItem.IsEqual(strSubStr))
588 {
589 return l_nIdx;
590 }
591 }
592 else if (nMatchType == 3)
593 {
594 // search for "*text*"
595 strItem = (*l_poI).Mid(nStart, nLen);
596 if (strItem.Find(strSubStr) != cString::InvalidPos)
597 {
598 return l_nIdx;
599 }
600 }
601
602 l_nIdx++;
603 }
604 return InvalidPos;
605 }
606
618 tSize FindNoCase(const storageT& strString) const
619 {
620 //@TODO: could also use std::find_if
621 tSize l_nIdx = 0;
622 typename tStringList::const_iterator l_poI;
623 for (l_poI = m_lstList.begin(); l_poI != m_lstList.end(); ++l_poI)
624 {
625 if (strString.CompareNoCase(*l_poI) == 0)
626 {
627 return l_nIdx;
628 }
629 l_nIdx++;
630 }
631 return InvalidPos;
632 }
633
645 storageT Join(const storageT& strSeparator) const
646 {
647 storageT l_strString;
648 tBool bFirst = tTrue;
649
650 typename tStringList::const_iterator l_poI;
651 for (l_poI = m_lstList.begin(); l_poI != m_lstList.end(); ++l_poI)
652 {
653 if (!bFirst)
654 {
655 l_strString.Append(strSeparator);
656 }
657 else
658 {
659 bFirst = false;
660 }
661
662 l_strString.Append(*l_poI);
663 }
664
665 return l_strString;
666 }
667
677 {
678 return m_lstList.empty();
679 }
680
692 storageT& operator[](tSize nIdx)
693 {
694 return m_lstList[nIdx];
695 }
696
698 const storageT& operator[](tSize nIdx) const
699 {
700 return m_lstList[nIdx];
701 }
702
703 bool operator==(const _myType& oList) const
704 {
705 if (oList.GetItemCount() == GetItemCount())
706 {
707 _myType::const_iterator itRHS = oList.cbegin();
708 for (_myType::const_iterator itLHS = cbegin(); itLHS != cend();)
709 {
710 if (*itRHS != *itLHS)
711 {
712 return tFalse;
713 }
714 itRHS++;
715 itLHS++;
716 }
717 return tTrue;
718 }
719 else
720 {
721 return tFalse;
722 }
723 }
724};
725
726using cStringList = string_list_base<cString>;
727
728} // namespace A_UTILS_NS
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
void tVoid
The tVoid is always the definition for the void (non-type).
int tInt
type definition for signed integer value (platform and compiler dependent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
size_t tSize
type definition for a array size values, map size values etc.
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
forward declaration
Definition stringlist.h:19
string_list_base(const _myType &lstList)
Constructor that duplicates an existing string list.
Definition stringlist.h:107
tSize Match(const storageT &strPattern) const
Returns the index of the string matching a pattern.
Definition stringlist.h:539
tResult Append(const _myType &lstString)
This function appends the given list of strings to the list.
Definition stringlist.h:291
tSize Find(const storageT &strString) const
This function returns the position of a string inside the list.
Definition stringlist.h:520
const storageT & Get(tSize nIdx, const storageT &strDefault="") const
This function retrieves a string value from the list.
Definition stringlist.h:473
const_iterator begin() const
Return const_iterator to beginning.
Definition stringlist.h:53
tResult Append(const storageT &strString)
This function appends one string to the list.
Definition stringlist.h:275
const_iterator cbegin() const
Return iterator to beginning.
Definition stringlist.h:63
tVoid Copy(const _myType &lstList)
This function copies the content of an existing string list to this cStringList object.
Definition stringlist.h:246
tVoid Sort(tBool bIgnoreCase=tFalse)
This function arranges the list elements.
Definition stringlist.h:440
tSize FindNoCase(const storageT &strString) const
This function returns the position of a string inside the list.
Definition stringlist.h:618
string_list_base & operator=(_myType &&lstList)
The cStringList assignment (=) operator reinitializes existing string lists with an existing object.
Definition stringlist.h:219
tResult Delete(tSize nIdx)
This function removes one item from the list.
Definition stringlist.h:395
tStringList::const_iterator const_iterator
short typedefinition for cString lists iterators
Definition stringlist.h:26
string_list_base(const storageT &strList)
Constructor that creates string list from comma or semicolon-delimited string.
Definition stringlist.h:131
tResult Insert(tSize nIdx, const storageT &strString)
This function inserts a string to the list at a specified position.
Definition stringlist.h:372
storageT Join(const storageT &strSeparator) const
This function concatenates all list elements to one single cString object.
Definition stringlist.h:645
reverse_iterator rend()
Return reverse iterator to reverse end.
Definition stringlist.h:78
std::vector< storageT > tStringList
list of containing cStrings
Definition stringlist.h:24
tResult Add(const storageT &strString)
This function appends one string to the list only if the strString is not yet part of the list.
Definition stringlist.h:315
tStringList::reverse_iterator reverse_iterator
short typedefinition for cString lists reverse iterators
Definition stringlist.h:32
const_reverse_iterator crbegin() const
Return reverse const_iterator to reverse beginning.
Definition stringlist.h:83
tStringList::iterator iterator
short typedefinition for cString lists iterators
Definition stringlist.h:30
const_iterator cend() const
Return iterator to end.
Definition stringlist.h:68
tResult Set(tSize nIdx, const storageT &strValue)
This function stores a string value into the list.
Definition stringlist.h:494
string_list_base(_myType &&lstList)
Constructor that duplicates an existing string list.
Definition stringlist.h:118
tVoid Clear()
This function cleans up the list and frees all allocated memory blocks.
Definition stringlist.h:257
static tBool fnSortLexically(const storageT &strFirst, const storageT &strSecond)
Checks if two strings are sorted lexically.
Definition stringlist.h:418
tSize GetItemCount() const
This function returns the number of items the list contains.
Definition stringlist.h:458
string_list_base()
Constructor that initializes an empty cStringList object.
Definition stringlist.h:97
tBool IsEmpty() const
This function checks if the string object is empty.
Definition stringlist.h:676
const_reverse_iterator crend() const
Return reverse const_iterator to reverse end.
Definition stringlist.h:88
virtual ~string_list_base()
Destructor.
Definition stringlist.h:231
tStringList::const_reverse_iterator const_reverse_iterator
short typedefinition for cString lists reverse iterators
Definition stringlist.h:28
iterator end()
Return iterator to end.
Definition stringlist.h:48
const_iterator end() const
Return const_iterator to end.
Definition stringlist.h:58
reverse_iterator rbegin()
Return reverse iterator to reverse beginning.
Definition stringlist.h:73
string_list_base & operator=(const _myType &lstList)
The cStringList assignment (=) operator reinitializes existing string lists with an existing object.
Definition stringlist.h:205
iterator begin()
Return iterator to beginning.
Definition stringlist.h:43
storageT & operator[](tSize nIdx)
This function retrieves a string value from the list.
Definition stringlist.h:692
tResult Add(const _myType &lstString)
This function appends strings of the given list to the list only if the string is not yet part of the...
Definition stringlist.h:337
const storageT & operator[](tSize nIdx) const
This function retrieves a string value from the list.
Definition stringlist.h:698
string_list_base< storageT > _myType
definition of my type
Definition stringlist.h:22
#define tFalse
Value for tBool.
Definition constants.h:60
#define tTrue
Value for tBool.
Definition constants.h:62
ADTF A_UTIL Namespace - Within adtf this is used as util or adtf_util.
Definition d_ptr.h:11
tBool operator==(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.