TNB Library
TnbStrEx.h
[詳解]
1#pragma once
13#include "TnbStr.h"
14
15
16
17//T-TestCaseコードカバレッジDisable
18#pragma comment(user, "T-Coverage Disable")
19
20
21
22//TNB Library
23namespace TNB
24{
25
26
27
36{
37 CStr str;
38 #ifndef _WIN32_WCE
39 DWORD dwLength = MAX_COMPUTERNAME_LENGTH + 1;
40 ::GetComputerName(str.GetBuffer(dwLength), &dwLength);
41 str.ReleaseBuffer();
42 #else
43 str = _T("CE");
44 #endif
45 return str;
46}
47
48
49
57inline CStr GetProcessPath(void)
58{
59 CStr str;
60 DWORD dwRc = ::GetModuleFileName(NULL, str.GetBuffer(MAX_PATH), MAX_PATH);
61 str.ReleaseBuffer();
62 if ( dwRc != 0 )
63 {
64// INT_PTR p1 = str.ReverseFind('\\');
65// INT_PTR p2 = str.ReverseFind('/');
66// INT_PTR p0 = (p1 < p2) ? p2 : p1;
67 INT_PTR p = str.ReverseFindOneOf(_T("\\/"));
68 if ( p >= 0 )
69 {
70 return str.Left(p);
71 }
72 }
73 return str;
74}
75
76
77
85inline CStr GetProcessName(void)
86{
87 CStr str;
88 DWORD dwRc = ::GetModuleFileName(NULL, str.GetBuffer(MAX_PATH), MAX_PATH);
89 str.ReleaseBuffer();
90 if ( dwRc != 0 )
91 {
92// INT_PTR p1 = str.ReverseFind('\\');
93// INT_PTR p2 = str.ReverseFind('/');
94// INT_PTR p0 = (p1 < p2) ? p2 : p1;
95 INT_PTR p = str.ReverseFindOneOf(_T("\\/"));
96 if ( p >= 0 )
97 {
98 return str.Mid(static_cast<INDEX>(p + 1));
99 }
100 }
101 return str;
102}
103
104
105
116inline CStr LoadStr(UINT nID, HINSTANCE hInst = NULL)
117{
118 if ( hInst == NULL )
119 {
121 }
122 CStr str;
123 int iLen = 256;
124 while ( true )
125 {
126 int r = ::LoadString(hInst, nID, str.GetBuffer(iLen), iLen);
127 str.ReleaseBuffer();
128 if ( r == 0 )
129 {
130 str.Empty();
131 break;
132 }
133 if ( r < iLen - 2 )
134 {
135 break;
136 }
137 iLen *= 2;
138 }
139 return str;
140}
141
142
143
161inline CAscii LocalMapString(LPCSTR lpsz, DWORD dwFlags)
162{
163 // 半角→全角変換で最大2倍のサイズになる
164 size_t iLen = (STRLIB::GetLen(lpsz) + 1) * 2;
165 CWorkMemT<char> work(iLen);
166 int iRc = ::LCMapStringA(::GetUserDefaultLCID(),
167 dwFlags,
168 lpsz, -1, // 入力文字列と長さ(NULL終端なら長さに-1指定可能)
169 work, ToInt(iLen));// 変換後の文字列を格納するバッファとバッファ長
170 if ( iRc <= 0 )
171 {
172 work.Free();
173 }
174 return CAscii(work);
175}
176
177
178
196inline CUnicode LocalMapString(LPCWSTR lpsz, DWORD dwFlags)
197{
198 size_t iLen = STRLIB::GetLen(lpsz) + 1;
199 CWorkMemT<WCHAR> work(iLen);
200 int iRc = ::LCMapStringW(::GetUserDefaultLCID(),
201 dwFlags,
202 lpsz, -1, // 入力文字列と長さ(NULL終端なら長さに-1指定可能)
203 work, ToInt(iLen));// 変換後の文字列を格納するバッファとバッファ長
204 if ( iRc <= 0 )
205 {
206 work.Free();
207 }
208 return CUnicode(work);
209}
210
211
212
221inline CAscii LowerString(LPCSTR lpsz)
222{
223 return LocalMapString(lpsz, LCMAP_LOWERCASE);
224}
225
226
227
236inline CUnicode LowerString(LPCWSTR lpsz)
237{
238 return LocalMapString(lpsz, LCMAP_LOWERCASE);
239}
240
241
242
251inline CAscii UpperString(LPCSTR lpsz)
252{
253 return LocalMapString(lpsz, LCMAP_UPPERCASE);
254}
255
256
257
266inline CUnicode UpperString(LPCWSTR lpsz)
267{
268 return LocalMapString(lpsz, LCMAP_UPPERCASE);
269}
270
271
272
281inline CStr SystemErrorToMessageText(DWORD dwError, bool withRowValue = false)
282{
283 LPTSTR lpBuffer = NULL;
284 const DWORD dwFlag = FORMAT_MESSAGE_ALLOCATE_BUFFER
285 | FORMAT_MESSAGE_FROM_SYSTEM
286 | FORMAT_MESSAGE_IGNORE_INSERTS;
287 DWORD r = ::FormatMessage(
288 dwFlag,
289 NULL,
290 dwError,
291 LANG_USER_DEFAULT,
292 reinterpret_cast<LPTSTR>(&lpBuffer),
293 0,
294 NULL);
295 CStr strErrMessage;
296 if ( r == 0 )
297 {
298 strErrMessage = _T("Unknown");
299 }
300 else
301 {
302 strErrMessage = lpBuffer;
303 strErrMessage.TrimRight(_T("\n\r"));
304 ::LocalFree(lpBuffer);
305 }
306 if ( withRowValue )
307 {
308 strErrMessage += CStr::Fmt(_T("(%d)"), dwError);
309 }
310 return strErrMessage;
311}
312
313
314
315}; //TNB
316
317
318
319//T-TestCaseコードカバレッジEnable
320#pragma comment(user,"T-Coverage Enable")
321
文字列管理関係のヘッダ
INT_PTR ReverseFindOneOf(const TYP *lpsz) const
[確認] 検索(後ろから)
Definition: TnbStr.h:621
CStrT Left(size_t iSize) const
[作成] 範囲取得.
Definition: TnbStr.h:801
void ReleaseBuffer(void)
[操作] 割り当てたバッファを開放.
Definition: TnbStr.h:954
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
Definition: TnbStr.h:1206
CStrT & TrimRight(TYP t=' ')
[処理] 末尾から文字をトリム.
Definition: TnbStr.h:990
void Empty(void)
[削除] 空化
Definition: TnbStr.h:197
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
Definition: TnbStr.h:766
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
Definition: TnbStr.h:914
void Free(void)
[設定] 解放.
Definition: TnbDef.h:652
CStr GetComputerName(void)
[取得] PC名取得
Definition: TnbStrEx.h:35
size_t GetLen(LPCSTR lpsz)
[計算] 文字列長計算(ASCII/SJIS用)
Definition: TnbStrLib.h:44
TNB::CStrT< char > CAscii
ASCII文字列クラス
Definition: TnbStr.h:1758
TNB::CStrT< WCHAR > CUnicode
UNICODE文字列クラス
Definition: TnbStr.h:1771
CAscii UpperString(LPCSTR lpsz)
[変換] 大文字変換(ASCII/SJIS用)
Definition: TnbStrEx.h:251
CAscii LowerString(LPCSTR lpsz)
[変換] 小文字変換(ASCII/SJIS用)
Definition: TnbStrEx.h:221
CStr GetProcessName(void)
[取得] プロセスネーム取得.
Definition: TnbStrEx.h:85
CAscii LocalMapString(LPCSTR lpsz, DWORD dwFlags)
[変換] 文字変換(ASCII/SJIS用)
Definition: TnbStrEx.h:161
CStr GetProcessPath(void)
[取得] プロセスのパス取得.
Definition: TnbStrEx.h:57
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
CStr LoadStr(UINT nID, HINSTANCE hInst=NULL)
[取得] リソース文字列取得
Definition: TnbStrEx.h:116
void SystemErrorToMessageText(CSimpleStr &_str, DWORD dwError)
[変換] SystemErrorコード文字列化.
Definition: TnbDef.h:981
HINSTANCE GetInstanceHandleByTnb(EInstanceType type=EI_Process)
[取得] インスタンスハンドル取得.
Definition: TnbDef.h:1341
@ EI_String
文字列リソース用
Definition: TnbDef.h:1262
TNB Library
Definition: TnbDoxyTitle.txt:2