TNB Library
TnbExInifileAccessor.h
[詳解]
1#pragma once
11#ifdef _WIN32_WCE
12 #error not support "ExInifileAccessor"
13#endif // _WIN32_WCE
14
15
16
17#include "TnbInifileAccessor.h"
18
19
20
21//TNB Library
22namespace TNB
23{
24
25
26
51{
52 DEFSUPER(CInifileAccessor);
53 enum { E_BufferSize = 65536 };
59 static EKind ms_CheckKind(LPCTSTR lpsz)
60 {
61 if ( lpsz[0] != '_' || lpsz[2] != '_' )
62 {
63 return EK_Nothing;
64 }
65 switch ( lpsz[1] )
66 {
67 case EK_String: // 文字列
68 case EK_PluralStrings: // 複数の文字列群
69 case EK_Binary: // バイナリ
70 case EK_Dword: // 32Bit Unsigned
71 case EK_Longlong: // 64Bit Signed
72 break;
73 default:
74 return EK_Nothing;
75 }
76 return static_cast<EKind>(lpsz[1]);
77 }
78
84 static CStr ms_ToWritingStr(LPCTSTR lpsz)
85 {
86 CStr s = lpsz;
87 if ( s.IsEmpty() )
88 {
89 s = _T("@");
90 }
91 else
92 {
93 s.Replace(_T("\\"), _T("\\\\"));
94 s.Replace(_T("\n"), _T("\\n"));
95 s.Replace(_T("\r"), _T("\\r"));
96 s.Replace(_T("="), _T("\\-"));
97 s.Replace(_T(" "), _T("\\_"));
98 }
99 return s;
100 }
101
107 static CStr ms_ToOriginalStr(LPCTSTR lpsz)
108 {
109 CStr s = lpsz;
110 s.Replace(_T("\\_"), _T(" "));
111 s.Replace(_T("\\n"), _T("\n"));
112 s.Replace(_T("\\r"), _T("\r"));
113 s.Replace(_T("\\-"), _T("="));
114 s.Replace(_T("\\\\"), _T("\\"));
115 return s;
116 }
117
127 bool m_Write(EKind eKind, LPCTSTR lpszSectionName, LPCTSTR lpszKey, LPCTSTR lpszData)
128 {
129 CStr s = CStr::Fmt(_T("_%c_%s"), eKind, lpszData);
130 return !! ::WritePrivateProfileString(
131 lpszSectionName,
132 ms_ToWritingStr(lpszKey),
133 ms_ToWritingStr(s),
135 );
136 }
137
144 CStr m_Query(LPCTSTR lpszSectionName, LPCTSTR lpszKey) const
145 {
146 CStr strValue;
147 ::GetPrivateProfileString(
148 lpszSectionName,
149 ms_ToWritingStr(lpszKey),
150 _T(""),
151 strValue.GetBuffer(E_BufferSize),
152 E_BufferSize,
154 strValue.ReleaseBuffer();
155 strValue = ms_ToOriginalStr(strValue);
156 return strValue.Mid(3);
157 }
158
159public:
160
165 explicit CExInifileAccessor(LPCTSTR lpszFile = NULL) : _super(lpszFile)
166 {
167 }
168
174 virtual CStrVector EnumKeyNames(LPCTSTR lpszSectionName) const
175 {
176 CStrVector vstr = _super::EnumKeyNames(lpszSectionName);
177 loop ( i, vstr.GetSize() )
178 {
179 vstr[i] = ms_ToOriginalStr(vstr[i]);
180 }
181 return vstr;
182 }
183
190 virtual EKind GetKeyKind(LPCTSTR lpszSectionName, LPCTSTR lpszKey) const
191 {
192 TCHAR atcBuf[16];
193 DWORD dwRc = ::GetPrivateProfileString(
194 lpszSectionName,
195 ms_ToWritingStr(lpszKey),
196 _T(""),
197 atcBuf,
198 16,
200 if ( dwRc < 3 )
201 {
202 return EK_Nothing;
203 }
204 return ms_CheckKind(atcBuf);
205 }
206
214 virtual CValue QueryValue(LPCTSTR lpszSectionName, LPCTSTR lpszKey) const
215 {
216 CValue v;
217 CStr str = m_Query(lpszSectionName, lpszKey);
218 switch ( GetKeyKind(lpszSectionName, lpszKey) )
219 {
220 case EK_Nothing:
221 break;
222 case EK_String:
223 v = CValue(str);
224 break;
225 case EK_PluralStrings:
226 v = CValue(StrToStrVectorEx(str));
227 break;
228 case EK_Binary:
230 break;
231 case EK_Dword:
232 v = CValue(static_cast<DWORD>(str.ToLonglong()));
233 break;
234 case EK_Longlong:
235 v = CValue(str.ToLonglong());
236 break;
237 default:
238 ASSERT(false);
239 break;
240 }
241 return v;
242 }
243
253 virtual bool WriteValue(LPCTSTR lpszSectionName, LPCTSTR lpszKey, const IAccessor::CValue& value)
254 {
255 EKind kind = value.GetKind();
256 if ( kind == EK_Nothing )
257 {
258 return _super::WriteValue(lpszSectionName, lpszKey, value);
259 }
260 return m_Write(kind, lpszSectionName, lpszKey, value.QueryString());
261 }
262};
263
264
265
266}; // TNB
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
iniファイルアクセス関係のヘッダ
拡張iniファイルアクセスクラス
virtual CStrVector EnumKeyNames(LPCTSTR lpszSectionName) const
[取得] キー名一覧取得
virtual bool WriteValue(LPCTSTR lpszSectionName, LPCTSTR lpszKey, const IAccessor::CValue &value)
[設定] 情報設定
virtual CValue QueryValue(LPCTSTR lpszSectionName, LPCTSTR lpszKey) const
[取得] 情報取得
virtual EKind GetKeyKind(LPCTSTR lpszSectionName, LPCTSTR lpszKey) const
[取得] 情報取種取得
CExInifileAccessor(LPCTSTR lpszFile=NULL)
コンストラクタ
iniファイルアクセスクラス
CStr GetBaseFileName(void) const
[取得] 対象ファイル取得
static CByteVector HexStringToBinary(const TYP *lpszHex)
[作成] HEX文字列→バイナリ
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbStr.h:528
void ReleaseBuffer(void)
[操作] 割り当てたバッファを開放.
Definition: TnbStr.h:954
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
Definition: TnbStr.h:1206
LONGLONG ToLonglong(INDEX iOffset=0) const
[取得] 数値(LONGLONG)へ変換
Definition: TnbStr.h:889
int Replace(TYP tOld, TYP tNew)
[処理] 文字置換.
Definition: TnbStr.h:1038
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
Definition: TnbStr.h:766
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
Definition: TnbStr.h:914
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
情報アクセスの汎用値保持クラス.
Definition: TnbAccessor.h:100
CStr QueryString(void) const
[取得] 文字列情報取得
Definition: TnbAccessor.h:185
EKind GetKind(void) const
[取得] 情報取種取得
Definition: TnbAccessor.h:171
CStrVector StrToStrVectorEx(LPCTSTR lpsz, TCHAR sepaChar='^', TCHAR escChar='\\')
[変換] CStr → CStrVector
Definition: TnbStrVector.h:201
TNB Library
Definition: TnbDoxyTitle.txt:2
EKind
値型の種類.
Definition: TnbAccessor.h:80
@ EK_String
文字列
Definition: TnbAccessor.h:82
@ EK_Binary
バイナリ
Definition: TnbAccessor.h:84
@ EK_Dword
32Bit Unsigned Integer
Definition: TnbAccessor.h:85
@ EK_PluralStrings
複数の文字列群
Definition: TnbAccessor.h:83
@ EK_Nothing
存在しない
Definition: TnbAccessor.h:81
@ EK_Longlong
64Bit Signed Integer
Definition: TnbAccessor.h:86