Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cArrayString.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cArrayString_H
7 #define _INC_cArrayString_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cArrayRef.h"
13 #include "cString.h"
14 
15 namespace Gray
16 {
17  template< typename _TYPE_CH = TCHAR >
18  class GRAYCORE_LINK cArrayString : public cArrayTyped < cStringT<_TYPE_CH>, const _TYPE_CH* >
19  {
22  typedef cStringT<_TYPE_CH> STR_t;
23  typedef cArrayTyped< cStringT<_TYPE_CH>, const _TYPE_CH* > SUPER_t;
25  public:
26  static const ITERATE_t k_MaxDefault = 32;
27  static const ITERATE_t k_MaxElements = 64 * 1024;
28  public:
29  cArrayString() noexcept
30  {
31  }
32  cArrayString(const _TYPE_CH** ppStr, ITERATE_t iCount)
33  {
34  SetStrings(ppStr, iCount);
35  }
36  explicit cArrayString(const cArrayString& a)
37  {
38  this->SetCopy(a);
39  }
40  cArrayString(THIS_t&& ref) noexcept
41  : SUPER_t(ref)
42  {
44  }
45 
46  inline ~cArrayString()
47  {
48  }
49  void SetStrings(const _TYPE_CH** ppStr, ITERATE_t iCount)
50  {
52  this->RemoveAll();
53  ASSERT(iCount < k_MaxElements); // reasonable max.
54  for (ITERATE_t i = 0; i < iCount; i++)
55  {
56  this->Add(ppStr[i]);
57  }
58  }
60  {
62  this->RemoveAll();
63  ASSERT(iCount < k_MaxElements); // reasonable max.
64  for (ITERATE_t i = 0; i < iCount; i++)
65  {
66  this->Add(ppStr[i]);
67  }
68  }
69  ITERATE_t _cdecl AddFormat(const _TYPE_CH* pszFormat, ...)
70  {
71  cStringT<_TYPE_CH> sTmp;
72  va_list vargs;
73  va_start(vargs, pszFormat);
74  sTmp.FormatV(pszFormat, vargs);
75  va_end(vargs);
76  return this->Add(sTmp);
77  }
78  ITERATE_t AddTable(const _TYPE_CH* const* ppszTable, size_t iElemSize = sizeof(_TYPE_CH*))
79  {
80  ITERATE_t i = 0;
81  for (; *ppszTable != nullptr; i++)
82  {
83  ASSERT(i < k_MaxElements); // reasonable max.
84  this->Add(*ppszTable);
85  ppszTable = (const _TYPE_CH* const*)(((const BYTE*)ppszTable) + iElemSize);
86  }
87  return i;
88  }
89  ITERATE_t AddUniqueMax(const _TYPE_CH* pszStr, ITERATE_t iMax = k_MaxDefault)
90  {
93 
94  if (StrT::IsNullOrEmpty(pszStr) || iMax < 1)
95  return k_ITERATE_BAD;
96  ITERATE_t iQty = this->GetSize();
97  for (ITERATE_t i = 0; i < iQty; i++)
98  {
99  if (!StrT::CmpI(this->GetAt(i).get_CPtr(), pszStr)) // dupe
100  {
101  return i;
102  }
103  }
104  while (iQty >= iMax)
105  {
106  this->RemoveAt(0); // roll off extras from head.
107  iQty--;
108  }
109  this->AddTail(pszStr);
110  return iQty;
111  }
112  ITERATE_t FindCmpI(const _TYPE_CH* pszFind) const
113  {
115  ITERATE_t iQty = this->GetSize();
116  for (ITERATE_t i = 0; i < iQty; i++)
117  {
118  if (!StrT::CmpI(this->GetAt(i).get_CPtr(), pszFind))
119  return i;
120  }
121  return k_ITERATE_BAD;
122  }
123  ITERATE_t FindStrIR(const _TYPE_CH* pszSearch) const
124  {
126  ITERATE_t iQty = this->GetSize();
127  for (ITERATE_t i = 0; i < iQty; i++)
128  {
129  if (StrT::FindStrI(pszSearch, this->GetAt(i).get_CPtr()) != nullptr)
130  return i;
131  }
132  return k_ITERATE_BAD;
133  }
134 
135  _TYPE_CH** get_PPStr() const
136  {
137  return (_TYPE_CH**)this->GetData();
138  }
140  {
141  if (!SUPER_t::IsValidIndex(i))
142  return "";
143  return SUPER_t::GetAt(i);
144  }
145 
146  ITERATE_t SetStrSep(const _TYPE_CH* pszStr, _TYPE_CH chSep = ',')
147  {
149  _TYPE_CH szSep[2];
150  szSep[0] = chSep;
151  szSep[1] = '\0';
152  _TYPE_CH szTmp[StrT::k_LEN_MAX];
153  _TYPE_CH* aCmds[128];
154  ITERATE_t iStrings = StrT::ParseCmdsTmp(szTmp, STRMAX(szTmp), pszStr, aCmds, _countof(aCmds), szSep);
155  SetStrings((const _TYPE_CH**)aCmds, iStrings);
156  return iStrings;
157  }
158 
159  STR_t GetStrSep(_TYPE_CH chSep = ',', ITERATE_t iMax = 0x7FFF) const
160  {
162  if (iMax > this->GetSize())
163  iMax = this->GetSize();
164  STR_t sRet;
165  for (ITERATE_t i = 0; i < iMax; i++)
166  {
167  sRet += SUPER_t::GetAt(i);
168  sRet += chSep;
169  }
170  return sRet;
171  }
172  };
173 
176 
177 }
178 
179 #endif // _INC_cArrayString_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define STRMAX(x)
Get Max size of static string space. minus the '\0' terminator character.
Definition: StrConst.h:33
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define _countof(a)
Definition: cKernel.h:35
Definition: cArray.h:99
void FormatV(const _TYPE_CH *pszStr, va_list args)
Definition: cString.cpp:264
Definition: cArrayString.h:19
ITERATE_t _cdecl AddFormat(const _TYPE_CH *pszFormat,...)
Definition: cArrayString.h:69
ITERATE_t AddUniqueMax(const _TYPE_CH *pszStr, ITERATE_t iMax=k_MaxDefault)
Definition: cArrayString.h:89
_TYPE_CH ** get_PPStr() const
Definition: cArrayString.h:135
cArrayString(const _TYPE_CH **ppStr, ITERATE_t iCount)
Definition: cArrayString.h:32
cArrayString(THIS_t &&ref) noexcept
Definition: cArrayString.h:40
STR_t GetStrSep(_TYPE_CH chSep=',', ITERATE_t iMax=0x7FFF) const
Definition: cArrayString.h:159
ITERATE_t FindCmpI(const _TYPE_CH *pszFind) const
Definition: cArrayString.h:112
cArrayString(const cArrayString &a)
Definition: cArrayString.h:36
void SetStrings(const _TYPE_CH **ppStr, ITERATE_t iCount)
Definition: cArrayString.h:49
void SetStrings(cStringT< _TYPE_CH > *ppStr, ITERATE_t iCount)
Definition: cArrayString.h:59
ITERATE_t AddTable(const _TYPE_CH *const *ppszTable, size_t iElemSize=sizeof(_TYPE_CH *))
Definition: cArrayString.h:78
cArrayString() noexcept
Definition: cArrayString.h:29
ITERATE_t FindStrIR(const _TYPE_CH *pszSearch) const
Definition: cArrayString.h:123
STR_t GetAtCheck(ITERATE_t i) const
Definition: cArrayString.h:139
ITERATE_t SetStrSep(const _TYPE_CH *pszStr, _TYPE_CH chSep=',')
Definition: cArrayString.h:146
~cArrayString()
Definition: cArrayString.h:46
Definition: cArray.h:437
Definition: cString.h:381
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
cArrayString< wchar_t > cArrayStringW
Definition: cArrayString.h:175
int ITERATE_t
like size_t but signed
Definition: Index.h:28
const ITERATE_t k_ITERATE_BAD
Definition: Index.h:30
cArrayString< char > cArrayStringA
Definition: cArrayString.h:174
static const StrLen_t k_LEN_MAX
arbitrary max size for Format() etc. NOTE: _MSC_VER says stack frame should be at least 16384
Definition: StrT.h:75
static bool IsNullOrEmpty(const TYPE *pszStr) noexcept
Definition: StrT.h:102
static __DECL_IMPORT ITERATE_t __stdcall ParseCmdsTmp(TYPE *pszTmp, StrLen_t iTmpSizeMax, const TYPE *pszCmdLine, TYPE **ppCmds, ITERATE_t iCmdQtyMax, const TYPE *pszSep=nullptr, STRP_MASK_t uFlags=STRP_DEF)
static __DECL_IMPORT TYPE *__stdcall FindStrI(const TYPE *pszStr, const TYPE *pszFind, StrLen_t iLenMax=StrT::k_LEN_MAX)
static __DECL_IMPORT COMPARE_t __stdcall CmpI(const TYPE *pszStr1, const TYPE *pszStr2)