OSDN Git Service

CLSIDの変更
[seraphyscrtools/SeraphyScriptTools.git] / ProfileSection.cpp
1 // ProfileSection.cpp : CProfileSection \82Ì\83C\83\93\83v\83\8a\83\81\83\93\83e\81[\83V\83\87\83\93
2 #include "stdafx.h"
3 #include "SeraphyScriptTools.h"
4 #include "ProfileSection.h"
5
6 /////////////////////////////////////////////////////////////////////////////
7 // CProfileSection
8
9 STDMETHODIMP CProfileSection::get_Value(VARIANT idx, VARIANT *pVal)
10 {
11         ::VariantInit(pVal);
12
13         ATL::CString szKeyname;
14         CComVariant varIdx;
15         if (SUCCEEDED(varIdx.ChangeType(VT_BSTR, &idx))) {
16                 szKeyname = varIdx.bstrVal;
17         }
18
19         DWORD siz = 1024 * 8; // 8KBytes
20         while (siz < 10 * 1024 * 1024) { // 10MBytes\82Ü\82Å
21                 ATL::CString buf;
22                 LPTSTR szReturn = buf.GetBufferSetLength(siz);
23                 DWORD rdsiz = GetPrivateProfileString(
24                         m_szSectionName, szKeyname, _TEXT(""), szReturn, siz, m_szProfilePath);
25                 if (rdsiz >= siz - 2) {
26                         // \83o\83b\83t\83@\83I\81[\83o\81[\82µ\82Ä\82¢\82é\89Â\94\\90«\82ª\82 \82é\82½\82ß\83o\83b\83t\83@\82ð\91å\82«\82­\82µ\82Ä\8dÄ\8e\8e\8ds\82·\82é
27                         siz *= 2;
28                         continue;
29                 }
30                 CComVariant ret(szReturn);
31                 return ret.Detach(pVal);
32         }
33         return E_OUTOFMEMORY;
34 }
35
36 STDMETHODIMP CProfileSection::put_Value(VARIANT idx, VARIANT newVal)
37 {
38         // \83f\83B\83t\83H\83\8b\83g\82Ì\92l\82Â\82«\8eæ\93¾
39
40         ATL::CString szKeyname;
41         CComVariant varIdx;
42         if (SUCCEEDED(varIdx.ChangeType(VT_BSTR, &idx))) {
43                 szKeyname = varIdx.bstrVal;
44         }
45
46         ATL::CString szWrite;
47         CComVariant varNew;
48         if (SUCCEEDED(varNew.ChangeType(VT_BSTR, &newVal))) {
49                 szWrite = varNew.bstrVal;
50         }
51
52         WritePrivateProfileString(
53                 m_szSectionName, szKeyname, szWrite, m_szProfilePath);
54         return S_OK;
55 }
56
57 STDMETHODIMP CProfileSection::GetValue(VARIANT idx, VARIANT def, VARIANT* pVal)
58 {
59         // \83f\83B\83t\83H\83\8b\83g\82Ì\92l\82Â\82«\8eæ\93¾
60         ::VariantInit(pVal);
61
62         ATL::CString szKeyname;
63         CComVariant varIdx;
64         if (SUCCEEDED(varIdx.ChangeType(VT_BSTR, &idx))) {
65                 szKeyname = varIdx.bstrVal;
66         }
67
68         ATL::CString szDefault;
69         CComVariant varDef;
70         if (SUCCEEDED(varDef.ChangeType(VT_BSTR, &def))) {
71                 szDefault = varDef.bstrVal;
72         }
73
74         DWORD siz = 1024 * 8; // 8KBytes
75         while (siz < 10 * 1024 * 1024) { // 10MBytes\82Ü\82Å
76                 ATL::CString buf;
77                 LPTSTR szReturn = buf.GetBufferSetLength(siz);
78                 DWORD rdsiz = GetPrivateProfileString(
79                         m_szSectionName, szKeyname, szDefault, szReturn, siz, m_szProfilePath);
80                 if (rdsiz >= siz - 2) {
81                         // \83o\83b\83t\83@\83I\81[\83o\81[\82µ\82Ä\82¢\82é\89Â\94\\90«\82ª\82 \82é\82½\82ß\83o\83b\83t\83@\82ð\91å\82«\82­\82µ\82Ä\8dÄ\8e\8e\8ds\82·\82é
82                         siz *= 2;
83                         continue;
84                 }
85                 CComVariant ret(szReturn);
86                 return ret.Detach(pVal);
87         }
88         return E_OUTOFMEMORY;
89 }
90
91 STDMETHODIMP CProfileSection::GetKeyNames(VARIANT *pVal)
92 {
93         // \83L\81[\96¼\82Ì\97ñ\8b\93\82ð\8ds\82¤
94         DWORD oldsiz = 0;
95         DWORD retsiz = 0;
96
97         ATL::CString buf;
98         LPTSTR pBuf = NULL;
99
100         DWORD siz = 4096;
101         while (siz < 10 * 1024 * 1024) { // 10MBytes\82Ü\82Å
102                 pBuf = buf.GetBufferSetLength(siz);
103                 DWORD rdsiz = retsiz = GetPrivateProfileSection(
104                         m_szSectionName, pBuf, siz, m_szProfilePath);
105                 if (rdsiz >= siz - 2) {
106                         // \83o\83b\83t\83@\83I\81[\83o\81[\82µ\82Ä\82¢\82é\89Â\94\\90«\82ª\82 \82é\82½\82ß\83o\83b\83t\83@\82ð\91å\82«\82­\82µ\82Ä\8dÄ\8e\8e\8ds\82·\82é
107                         siz *= 2;
108                         continue;
109                 }
110                 break;
111         }
112
113         // \83_\83u\83\8b\83k\83\8b\8fI\92[\95\8e\9a\97ñ\82©\82ç\81A\83A\83C\83e\83\80\90\94\82ð\83J\83E\83\93\83g\82·\82é
114         LPTSTR p = pBuf;
115         long count = 0;
116         while (*p) {
117                 count++;
118                 while (*p++);
119         }
120
121         // \88ê\8e\9f\94z\97ñ\82Ì\90\90¬
122         SAFEARRAY* pArray = SafeArrayCreateVector(VT_VARIANT, 0, count);
123         long idx = 0;
124         p = pBuf;
125         while (*p) {
126                 LPCTSTR pb = p;
127                 while (*p) {
128                         if (*p == '=') {
129                                 // \83L\81[\96¼\82¾\82¯\82É\82·\82é(value\92l\82Í\96³\8e\8b)
130                                 *p = 0;
131                         }
132                         p++;
133                 }
134                 p++; // \8e\9f\82Ì\83A\83C\83e\83\80\82Ì\90æ\93ª\82É\83A\83h\83\8c\83X\82·\82é
135
136                 // \83L\81[\96¼\82ð\94z\97ñ\82É\93o\98^\82·\82é.
137                 CComVariant tmp(pb);
138                 SafeArrayPutElement(pArray, &idx, &tmp);
139                 idx++;
140         }
141
142         // \96ß\82è\92l
143         ::VariantInit(pVal);
144         pVal->vt = VT_VARIANT | VT_ARRAY;
145         pVal->parray = pArray;
146         return S_OK;
147 }