ADTF
stringlist.h
Go to the documentation of this file.
1 
7 #ifndef _STRINGLIST_CLASS_HEADER_
8 #define _STRINGLIST_CLASS_HEADER_
9 
10 
11 namespace A_UTILS_NS
12 {
13 
19 template<class storageT>
20 class DOEXPORT string_list_base
21 {
22  public:
26  typedef std::vector<storageT> tStringList;
28  typedef typename tStringList::const_iterator const_iterator;
30  typedef typename tStringList::const_reverse_iterator const_reverse_iterator;
32  typedef typename tStringList::iterator iterator;
34  typedef typename tStringList::reverse_iterator reverse_iterator;
35 
37  static const tSize InvalidPos = g_npos;
38 
39  protected:
42 
43  public:
46  {
47  return m_lstList.begin();
48  }
51  {
52  return m_lstList.end();
53  }
56  {
57  return m_lstList.cbegin();
58  }
61  {
62  return m_lstList.cend();
63  }
66  {
67  return m_lstList.cbegin();
68  }
71  {
72  return m_lstList.cend();
73  }
76  {
77  return m_lstList.rbegin();
78  }
81  {
82  return m_lstList.rend();
83  }
86  {
87  return m_lstList.crbegin();
88  }
91  {
92  return m_lstList.crend();
93  }
94 
100  {
101  }
102 
109  string_list_base(const _myType& lstList)
110  {
111  Copy(lstList);
112  }
113 
121  {
122  //@TODO: Could also move
123  Copy(lstList);
124  }
125 
133  string_list_base(const storageT& strList)
134  {
135  _myType lstList;
136 
137  tSize nTokenLen = 1;
138  tSize nPos = cString::InvalidPos;
139  tSize nPosComma = cString::InvalidPos;
140  tSize nPosSemicolon = cString::InvalidPos;
141  tSize nStartPos;
142 
143  do
144  {
145  nStartPos = nPos + 1;
146  nPosComma = strList.Find(",", nStartPos);
147  nPosSemicolon = strList.Find(";", nStartPos);
148 
149  if (cString::InvalidPos != nPosComma && cString::InvalidPos != nPosSemicolon)
150  {
151  nPosComma < nPosSemicolon ? nPos = nPosComma : nPos = nPosSemicolon;
152  }
153  else if (cString::InvalidPos != nPosSemicolon)
154  {
155  nPos = nPosSemicolon;
156  }
157  else
158  {
159  nPos = nPosComma;
160  }
161 
162  if (nPos != cString::InvalidPos)
163  {
164  if (nPos != 0)
165  {
166  lstList.m_lstList.emplace_back(strList.Mid(nStartPos, nPos - nStartPos));
167  }
168  else
169  {
170  lstList.m_lstList.emplace_back(storageT(""));
171  }
172  nPos += nTokenLen - 1;
173  }
174  else
175  {
176  if (nStartPos < strList.GetLength())
177  {
178  lstList.m_lstList.emplace_back(strList.Mid(nStartPos));
179  }
180  else if (strList.EndsWith(",") || strList.EndsWith(";"))
181  {
182  lstList.m_lstList.emplace_back(storageT(""));
183  }
184  break;
185  }
186  } while (nPos != cString::InvalidPos);
187 
188  Copy(lstList);
189  }
190 
191  string_list_base(std::initializer_list<storageT> lstStrings)
192  {
193  for (const auto& itElem : lstStrings)
194  {
195  Append(itElem);
196  }
197  }
198 
208  {
209  Copy(lstList);
210  return *this;
211  }
212 
222  {
223  //@TODO: could be moved!!
224  Copy(lstList);
225  return *this;
226  }
227 
234  {
235  Clear();
236  }
237 
248  tVoid Copy(const _myType& lstList)
249  {
250  Clear();
251  m_lstList = lstList.m_lstList;
252  }
253 
260  {
261  if (m_lstList.size() > 0)
262  {
263  m_lstList.clear();
264  }
265  }
266 
277  tResult Append(const storageT& strString)
278  {
279  m_lstList.push_back(strString);
281  }
282 
293  tResult Append(const _myType& lstString)
294  {
295  auto oIt = lstString.cbegin();
296  for (;
297  oIt != lstString.cend();
298  oIt++)
299  {
300  Append(*oIt);
301  }
302 
304  }
305 
317  tResult Add(const storageT& strString)
318  {
319  if (InvalidPos != Find(strString))
320  {
321  RETURN_ERROR(ERR_FAILED);
322  }
323 
324  Append(strString);
326  }
327 
339  tResult Add(const _myType& lstString)
340  {
341  tInt nAddedCount = 0;
342  for (auto it = lstString.begin();
343  it != lstString.end();
344  it++)
345  {
346  if (IS_OK(Add(*it)))
347  {
348  nAddedCount++;
349  }
350  }
351 
352  if (nAddedCount > 0)
353  {
355  }
356  else
357  {
358  RETURN_ERROR(ERR_FAILED);
359  }
360  }
361 
374  tResult Insert(tSize nIdx, const storageT& strString)
375  {
376  if (nIdx < m_lstList.size())
377  {
378  m_lstList.insert(m_lstList.begin() + nIdx, strString);
380  }
381  else
382  {
383  RETURN_ERROR(ERR_INVALID_INDEX);
384  }
385  }
386 
398  {
399  if (nIdx < m_lstList.size())
400  {
401  m_lstList.erase(m_lstList.begin() + nIdx);
402  }
403  else
404  {
405  RETURN_ERROR(ERR_INVALID_INDEX);
406  }
407  return ERR_NOERROR;
408  }
409 
420  static tBool fnSortLexically(const storageT& strFirst, const storageT& strSecond)
421  {
422  if (0 > strFirst.CompareNoCase(strSecond))
423  {
424  return tTrue;
425  }
426  else
427  {
428  return tFalse;
429  }
430  }
431 
442  tVoid Sort(tBool bIgnoreCase = tFalse)
443  {
444  if (!bIgnoreCase)
445  {
446  std::sort(m_lstList.begin(), m_lstList.end());
447  }
448  else
449  {
450  std::sort(m_lstList.begin(), m_lstList.end(), fnSortLexically);
451  }
452  }
453 
461  {
462  return m_lstList.size();
463  }
464 
475  const storageT& Get(tSize nIdx, const storageT& strDefault = "") const
476  {
477  if (nIdx < GetItemCount())
478  {
479  const storageT& strRet = m_lstList[nIdx];
480  return strRet;
481  }
482  return strDefault;
483  }
484 
496  tResult Set(tSize nIdx, const storageT& strValue)
497  {
498  if (InvalidPos == nIdx)
499  {
500  return ERR_INVALID_ARG;
501  }
502  else if (nIdx < GetItemCount())
503  {
504  m_lstList[nIdx] = strValue;
505  }
506  else
507  {
508  m_lstList.push_back(strValue);
509  }
511  }
512 
522  tSize Find(const storageT& strString) const
523  {
524  const_iterator it = std::find(cbegin(), cend(), strString);
525  if (it != cend())
526  {
527  return std::distance(cbegin(), it);
528  }
529  else
530  {
531  return InvalidPos;
532  }
533  }
534 
541  tSize Match(const storageT& strPattern) const
542  {
543  tSize nMatchType = 0; // exact
544  tSize nStart = 0;
545  tSize nLen = strPattern.GetLength();
546 
547  if (!strPattern.IsEmpty())
548  {
549  const tChar* ptr = strPattern.GetPtr();
550  if (*ptr == '*')
551  {
552  nMatchType |= 1;
553  nStart = 1;
554  nLen--;
555  }
556 
557  if (*(ptr + strPattern.GetLength() - 1) == '*')
558  {
559  nMatchType |= 2;
560  nLen--;
561  }
562  }
563 
564  if (nMatchType == 0)
565  {
566  return Find(strPattern);
567  }
568 
569  storageT strSubStr = strPattern.Mid(nStart, nLen);
570 
571  storageT strItem;
572  tSize l_nIdx = 0;
573  typename tStringList::const_iterator l_poI;
574  for (l_poI = m_lstList.begin(); l_poI != m_lstList.end(); ++l_poI)
575  {
576  if (nMatchType == 1)
577  {
578  // search for "*text"
579  strItem = (*l_poI).Right(nLen);
580  if (strItem.IsEqual(strSubStr))
581  {
582  return l_nIdx;
583  }
584  }
585  else if (nMatchType == 2)
586  {
587  // search for "text*"
588  strItem = (*l_poI).Left(nLen);
589  if (strItem.IsEqual(strSubStr))
590  {
591  return l_nIdx;
592  }
593  }
594  else if (nMatchType == 3)
595  {
596  // search for "*text*"
597  strItem = (*l_poI).Mid(nStart, nLen);
598  if (strItem.Find(strSubStr) != cString::InvalidPos)
599  {
600  return l_nIdx;
601  }
602  }
603 
604  l_nIdx++;
605  }
606  return InvalidPos;
607  }
608 
620  tSize FindNoCase(const storageT& strString) const
621  {
622  //@TODO: could also use std::find_if
623  tSize l_nIdx = 0;
624  typename tStringList::const_iterator l_poI;
625  for (l_poI = m_lstList.begin(); l_poI != m_lstList.end(); ++l_poI)
626  {
627  if (strString.CompareNoCase(*l_poI) == 0)
628  {
629  return l_nIdx;
630  }
631  l_nIdx++;
632  }
633  return InvalidPos;
634  }
635 
647  storageT Join(const storageT& strSeparator) const
648  {
649  storageT l_strString;
650  tBool bFirst = tTrue;
651 
652  typename tStringList::const_iterator l_poI;
653  for (l_poI = m_lstList.begin(); l_poI != m_lstList.end(); ++l_poI)
654  {
655  if (!bFirst)
656  {
657  l_strString.Append(strSeparator);
658  }
659  else
660  {
661  bFirst = false;
662  }
663 
664  l_strString.Append(*l_poI);
665  }
666 
667  return l_strString;
668  }
669 
678  tBool IsEmpty() const
679  {
680  return m_lstList.empty();
681  }
682 
694  storageT& operator[](tSize nIdx)
695  {
696  return m_lstList[nIdx];
697  }
698 
700  const storageT& operator[](tSize nIdx) const
701  {
702  return m_lstList[nIdx];
703  }
704 
705  bool operator==(const _myType& oList) const
706  {
707  if (oList.GetItemCount() == GetItemCount())
708  {
709  _myType::const_iterator itRHS = oList.cbegin();
710  for (_myType::const_iterator itLHS = cbegin(); itLHS != cend();)
711  {
712  if (*itRHS != *itLHS)
713  {
714  return tFalse;
715  }
716  itRHS++;
717  itLHS++;
718  }
719  return tTrue;
720  }
721  else
722  {
723  return tFalse;
724  }
725  }
726 };
727 
728 using cStringList = string_list_base<cString>;
729 
730 } // namespace A_UTILS_NS
731 
732 
733 
734 #endif // _STRINGLIST_CLASS_HEADER_
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.
#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.
static const tSize InvalidPos
used to identicate out of range, invalidpos or default length
Definition: string.h:56
forward declaration
Definition: stringlist.h:21
string_list_base(const _myType &lstList)
Constructor that duplicates an existing string list.
Definition: stringlist.h:109
tSize Match(const storageT &strPattern) const
Returns the index of the string matching a pattern.
Definition: stringlist.h:541
tResult Append(const _myType &lstString)
This function appends the given list of strings to the list.
Definition: stringlist.h:293
tSize Find(const storageT &strString) const
This function returns the position of a string inside the list.
Definition: stringlist.h:522
storageT & operator[](tSize nIdx)
This function retrieves a string value from the list.
Definition: stringlist.h:694
const_iterator begin() const
Return const_iterator to beginning.
Definition: stringlist.h:55
tResult Append(const storageT &strString)
This function appends one string to the list.
Definition: stringlist.h:277
const_iterator cbegin() const
Return iterator to beginning.
Definition: stringlist.h:65
tVoid Copy(const _myType &lstList)
This function copies the content of an existing string list to this cStringList object.
Definition: stringlist.h:248
tVoid Sort(tBool bIgnoreCase=tFalse)
This function arranges the list elements.
Definition: stringlist.h:442
tSize FindNoCase(const storageT &strString) const
This function returns the position of a string inside the list.
Definition: stringlist.h:620
tResult Delete(tSize nIdx)
This function removes one item from the list.
Definition: stringlist.h:397
tStringList::const_iterator const_iterator
short typedefinition for cString lists iterators
Definition: stringlist.h:28
string_list_base(const storageT &strList)
Constructor that creates string list from comma or semicolon-delimited string.
Definition: stringlist.h:133
tResult Insert(tSize nIdx, const storageT &strString)
This function inserts a string to the list at a specified position.
Definition: stringlist.h:374
storageT Join(const storageT &strSeparator) const
This function concatenates all list elements to one single cString object.
Definition: stringlist.h:647
reverse_iterator rend()
Return reverse iterator to reverse end.
Definition: stringlist.h:80
std::vector< storageT > tStringList
list of containing cStrings
Definition: stringlist.h:26
const storageT & Get(tSize nIdx, const storageT &strDefault="") const
This function retrieves a string value from the list.
Definition: stringlist.h:475
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:317
tStringList::reverse_iterator reverse_iterator
short typedefinition for cString lists reverse iterators
Definition: stringlist.h:34
const storageT & operator[](tSize nIdx) const
This function retrieves a string value from the list.
Definition: stringlist.h:700
const_reverse_iterator crbegin() const
Return reverse const_iterator to reverse beginning.
Definition: stringlist.h:85
tStringList::iterator iterator
short typedefinition for cString lists iterators
Definition: stringlist.h:32
const_iterator cend() const
Return iterator to end.
Definition: stringlist.h:70
tResult Set(tSize nIdx, const storageT &strValue)
This function stores a string value into the list.
Definition: stringlist.h:496
string_list_base(_myType &&lstList)
Constructor that duplicates an existing string list.
Definition: stringlist.h:120
tVoid Clear()
This function cleans up the list and frees all allocated memory blocks.
Definition: stringlist.h:259
static tBool fnSortLexically(const storageT &strFirst, const storageT &strSecond)
Checks if two strings are sorted lexically.
Definition: stringlist.h:420
tStringList m_lstList
Internally used string list.
Definition: stringlist.h:41
tSize GetItemCount() const
This function returns the number of items the list contains.
Definition: stringlist.h:460
string_list_base()
Constructor that initializes an empty cStringList object.
Definition: stringlist.h:99
tBool IsEmpty() const
This function checks if the string object is empty.
Definition: stringlist.h:678
string_list_base & operator=(const _myType &lstList)
The cStringList assignment (=) operator reinitializes existing string lists with an existing object.
Definition: stringlist.h:207
const_reverse_iterator crend() const
Return reverse const_iterator to reverse end.
Definition: stringlist.h:90
virtual ~string_list_base()
Destructor.
Definition: stringlist.h:233
string_list_base & operator=(_myType &&lstList)
The cStringList assignment (=) operator reinitializes existing string lists with an existing object.
Definition: stringlist.h:221
tStringList::const_reverse_iterator const_reverse_iterator
short typedefinition for cString lists reverse iterators
Definition: stringlist.h:30
iterator end()
Return iterator to end.
Definition: stringlist.h:50
const_iterator end() const
Return const_iterator to end.
Definition: stringlist.h:60
reverse_iterator rbegin()
Return reverse iterator to reverse beginning.
Definition: stringlist.h:75
iterator begin()
Return iterator to beginning.
Definition: stringlist.h:45
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:339
string_list_base< storageT > _myType
definition of my type
Definition: stringlist.h:24
#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 adtf::util or adtf_util.
Definition: d_ptr.h:11
tBool operator==(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.