TNB Library
TnbStrAdder.h
[詳解]
1#pragma once
11#include "TnbBlockLinkVector.h"
12
13
14
15//TNB Library
16namespace TNB
17{
18
19
20
33{
34public:
35
37 CStrAdder(void) : m_length(0)
38 {
39 }
40
43 {
44 Empty();
45 }
46
51 size_t GetLength(void) const
52 {
53 return m_length;
54 }
55
62 operator LPCTSTR(void) const
63 {
64 if ( m_string.GetSize() == 0 )
65 {
66 m_string.Resize(m_length + 1);
67 m_string[m_length] = 0;
68 size_t a = 0;
69 loop ( i, m_work )
70 {
71 const TParam& p = m_work[i];
72 if ( p.length == 1 )
73 {
74 m_string[a] = p.one;
75 }
76 else if ( p.length > 1 )
77 {
78 memcpy(&m_string[a], p.pMany, p.length * sizeof(TCHAR));
79 }
80 a += p.length;
81 }
82 }
83 return m_string;
84 }
85
90 void Empty(void)
91 {
92 m_length = 0;
93 loop ( i, m_work )
94 {
95 m_work[i].Empty();
96 }
97 m_work.RemoveAll();
98 m_string.Free();
99 }
100
108 int AddFormat(size_t len, LPCTSTR lpszFmt, ...)
109 {
110 va_list args;
111 va_start(args, lpszFmt);
112 TParam& p = m_New();
113 p.SetLength(max(2, len));
114 LPTSTR lpszWork = p.pMany;
115 #if _MSC_VER < 1400 || defined(_WIN32_WCE)
116 int r = _vstprintf(lpszWork, lpszFmt, args);
117 #else
118 int r = _vstprintf_s(lpszWork, len, lpszFmt, args);
119 #endif
120 if ( ToInt(len) < r )
121 {
122 ASSERT0( false, "CStrAdder::AddFormat", "用意したサイズを超えました" );
123 r = -1;
124 }
125 if ( r == 1 )
126 {
127 TCHAR c = lpszFmt[0];
128 p.Set(c);
129 }
130 else if ( r > 1 )
131 {
132 p.length = r;
133 m_length += r;
134 }
135 else
136 {
137 p.Empty();
138 size_t l = m_work.GetSize();
139 m_work.SetSize(l - 1);
140 }
141 va_end(args);
142 return r;
143 }
144
152 size_t AddFormat(LPCTSTR lpszFmt, ...)
153 {
154 va_list args;
155 va_start(args, lpszFmt);
156 CStr s;
157 s.FormatV(lpszFmt, args);
158 Add(s);
159 va_end(args);
160 return s.GetLength();
161 }
162
167 void Add(LPCTSTR lpsz)
168 {
169 TParam& p = m_New();
170 p.Set(lpsz);
171 m_length += p.length;
172 }
173
178 void operator+=(LPCTSTR lpsz)
179 {
180 Add(lpsz);
181 }
182
187 void Add(TCHAR c)
188 {
189 TParam& p = m_New();
190 p.Set(c);
191 m_length += 1;
192 }
193
198 void operator+=(TCHAR c)
199 {
200 Add(c);
201 }
202
208 void Regulate(size_t len)
209 {
210 }
211
212private:
214 struct TParam
215 {
216 size_t length;
217 union
218 {
219 TCHAR one;
220 TCHAR* pMany;
221 };
223 TParam(void) : length(0)
224 {
225 }
227 void SetLength(size_t len)
228 {
229 Empty();
230 length = len;
231 if ( length > 1 )
232 {
233 pMany = new TCHAR[length + 1];
234 }
235 }
237 void Set(LPCTSTR lpsz)
238 {
239 Empty();
240 length = STRLIB::GetLen(lpsz);
241 if ( length == 1 )
242 {
243 one = lpsz[0];
244 }
245 else if ( length > 1 )
246 {
247 pMany = new TCHAR[length + 1];
248 STRLIB::Copy(pMany, lpsz);
249 }
250 }
252 void Set(TCHAR c)
253 {
254 Empty();
255 length = 1;
256 one = c;
257 }
259 void Empty(void)
260 {
261 if ( length > 1 )
262 {
263 delete[] pMany;
264 }
265 length = 0;
266 }
267 };
269 TParam& m_New(void)
270 {
271 m_string.Free();
272 INDEX r = m_work.Add(TParam());
273 return m_work[r];
274 }
276 size_t m_length;
277 mutable CWorkMemT<TCHAR> m_string;
278};
279
280
281
282}; //TNB
配列型情報管理関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
virtual size_t GetSize(void) const
[取得] 要素数取得.
virtual bool RemoveAll(void)
[削除] 全要素削除 .
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
文字列連結専門管理
Definition: TnbStrAdder.h:33
size_t AddFormat(LPCTSTR lpszFmt,...)
[追加] 文字列追加.
Definition: TnbStrAdder.h:152
void operator+=(LPCTSTR lpsz)
[追加] 文字列追加.
Definition: TnbStrAdder.h:178
void Add(TCHAR c)
[追加] 文字追加.
Definition: TnbStrAdder.h:187
void Regulate(size_t len)
[設定] バッファ調整.
Definition: TnbStrAdder.h:208
size_t GetLength(void) const
[取得] 文字列長取得.
Definition: TnbStrAdder.h:51
~CStrAdder(void)
デストラクタ.
Definition: TnbStrAdder.h:42
void Add(LPCTSTR lpsz)
[追加] 文字列追加.
Definition: TnbStrAdder.h:167
CStrAdder(void)
コンストラクタ.
Definition: TnbStrAdder.h:37
void operator+=(TCHAR c)
[追加] 文字追加.
Definition: TnbStrAdder.h:198
int AddFormat(size_t len, LPCTSTR lpszFmt,...)
[追加] 文字列追加.
Definition: TnbStrAdder.h:108
void Empty(void)
[設定] 空っぽ化.
Definition: TnbStrAdder.h:90
void FormatV(const TYP *lpszFormat, va_list V)
[代入] 書式付き文字列代入.
Definition: TnbStr.h:349
size_t GetLength(void) const
[取得] 文字列長
Definition: TnbStr.h:518
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
void Free(void)
[設定] 解放.
Definition: TnbDef.h:652
size_t GetLen(LPCSTR lpsz)
[計算] 文字列長計算(ASCII/SJIS用)
Definition: TnbStrLib.h:44
void Copy(LPSTR _dst, LPCSTR src)
[複製] 文字列コピー(ASCII/SJIS用)
Definition: TnbStrLib.h:89
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
TNB Library
Definition: TnbDoxyTitle.txt:2
virtual bool SetSize(size_t size)
[操作] サイズ指定