OSDN Git Service

20021009版ソース
[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::InterfaceSupportsErrorInfo(REFIID riid)
10 {
11         static const IID* arr[] = 
12         {
13                 &IID_ISeraphyScriptTools_ProfileSection
14         };
15         for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
16         {
17                 if (IsEqualGUID(*arr[i],riid))
18                         return S_OK;
19         }
20         return S_FALSE;
21 }
22
23 STDMETHODIMP CProfileSection::get_Value(VARIANT idx, VARIANT *pVal)
24 {
25         ::VariantInit(pVal);
26         CComVariant varIdx;
27         CHAR szKeyname[MAX_PATH] = { 0 };
28         const int siz = 1024 * 8; // 8KBytes
29         LPSTR szReturn = new CHAR[siz];
30         if(varIdx.ChangeType(VT_BSTR,&idx) == S_OK){
31                 WideCharToMultiByte(GetACP(),0,varIdx.bstrVal,-1,szKeyname,MAX_PATH,NULL,NULL);
32         }
33         if(GetPrivateProfileString(m_szSectionName,szKeyname,"",szReturn,siz,m_szProfilePath)){
34                 CComVariant ret((LPCSTR)szReturn);
35                 ret.Detach(pVal);
36         }
37         delete[]szReturn;
38         return S_OK;
39 }
40
41 STDMETHODIMP CProfileSection::put_Value(VARIANT idx, VARIANT newVal)
42 {
43         // \83f\83B\83t\83H\83\8b\83g\82Ì\92l\82Â\82«\8eæ\93¾
44         CComVariant varIdx,varNew;
45         CHAR szKeyname[MAX_PATH] = { 0 };
46         const int siz = 1024 * 8; // 8KBytes
47         LPSTR szWrite = new CHAR[siz+1];
48         if(varIdx.ChangeType(VT_BSTR,&idx) == S_OK){
49                 WideCharToMultiByte(GetACP(),0,varIdx.bstrVal,-1,szKeyname,MAX_PATH,NULL,NULL);
50         }
51         if(varNew.ChangeType(VT_BSTR,&newVal) == S_OK){
52                 int cnt = WideCharToMultiByte(GetACP(),0,varNew.bstrVal,-1,szWrite,siz,NULL,NULL);
53                 szWrite[cnt] = 0;
54         }
55         WritePrivateProfileString(m_szSectionName,szKeyname,szWrite,m_szProfilePath);
56         delete[]szWrite;
57         return S_OK;
58 }
59
60 STDMETHODIMP CProfileSection::GetValue(VARIANT idx, VARIANT def,VARIANT* pVal)
61 {
62         // \83f\83B\83t\83H\83\8b\83g\82Ì\92l\82Â\82«\8eæ\93¾
63         ::VariantInit(pVal);
64         CComVariant varIdx,varDef;
65         CHAR szKeyname[MAX_PATH] = { 0 };
66         CHAR szDefault[MAX_PATH] = { 0 };
67         const int siz = 1024 * 8; // 8KBytes
68         LPSTR szReturn = new CHAR[siz];
69         if(varIdx.ChangeType(VT_BSTR,&idx) == S_OK){
70                 WideCharToMultiByte(GetACP(),0,varIdx.bstrVal,-1,szKeyname,MAX_PATH,NULL,NULL);
71         }
72         if(varDef.ChangeType(VT_BSTR,&def) == S_OK){
73                 WideCharToMultiByte(GetACP(),0,varDef.bstrVal,-1,szDefault,MAX_PATH,NULL,NULL);
74         }
75         if(GetPrivateProfileString(m_szSectionName,szKeyname,szDefault,szReturn,siz,m_szProfilePath)){
76                 CComVariant ret((LPCSTR)szReturn);
77                 ret.Detach(pVal);
78         }
79         delete[]szReturn;
80         return S_OK;
81 }
82
83 STDMETHODIMP CProfileSection::GetKeyNames(VARIANT *pVal)
84 {
85         // \83L\81[\96¼\82Ì\97ñ\8b\93\82ð\8ds\82¤
86         DWORD siz = 4096;
87         DWORD oldsiz = 0;
88         DWORD retsiz = 0;
89         LPSTR pBuf = new CHAR[siz];
90         while(  (retsiz  = GetPrivateProfileSection(m_szSectionName,pBuf,siz,m_szProfilePath)) &&
91                         (retsiz == (siz - 2)) && ( oldsiz != retsiz ) ){
92                 oldsiz = siz;
93                 siz *= 2;
94                 delete []pBuf;
95                 pBuf = new CHAR[siz];
96         }
97         // \83J\83E\83\93\83g\82·\82é
98         LPSTR p = pBuf;
99         long count = 0;
100         while(*p){
101                 count++;
102                 while(*p++);
103         }
104         // \88ê\8e\9f\94z\97ñ\82Ì\90\90¬
105         SAFEARRAY* pArray = SafeArrayCreateVector(VT_VARIANT,0,count);
106         long idx = 0;
107         p = pBuf;
108         while(*p){
109                 CComVariant tmp;
110                 LPCSTR pb = p;
111                 while(*p){
112                         if(*p == '='){
113                                 *p = 0;
114                         }
115                         p++;
116                 }
117                 p++;
118                 tmp = (LPCSTR)pb;
119                 SafeArrayPutElement(pArray,&idx,&tmp);
120                 tmp.Clear();
121                 idx++;
122         }
123         delete[]pBuf;
124         // \96ß\82è\92l
125         ::VariantInit(pVal);
126         pVal->vt     = VT_VARIANT | VT_ARRAY;
127         pVal->parray = pArray;
128         return S_OK;
129 }