source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/NonVolatile.cpp@ 479

Last change on this file since 479 was 206, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

File size: 8.3 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/common/Environment.h>
4
5#include "../BasicCompiler_Common/common.h"
6
7
8char *ReadBuffer_NonErrMsg( const string &path ){
9 extern HANDLE hHeap;
10 int i;
11 DWORD dw;
12 char *buffer;
13 HANDLE hFile;
14
15 hFile=CreateFile(path.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
16 if(hFile==INVALID_HANDLE_VALUE) return 0;
17 i=GetFileSize(hFile,0);
18 buffer=(char *)HeapAlloc(hHeap,0,i+1);
19 ReadFile(hFile,buffer,i,&dw,0);
20 buffer[dw]=0;
21 CloseHandle(hFile);
22 return buffer;
23}
24_int8 WriteBuffer( const string &path, char *buffer,int length){
25 extern HWND hOwnerEditor;
26 HANDLE hFile;
27 DWORD dw;
28 hFile=CreateFile(path.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
29 if(hFile==INVALID_HANDLE_VALUE){
30 char temporary[MAX_PATH];
31
32 sprintf(temporary,"\"%s\" ファイルへの書き込みに失敗しました。",path);
33 MessageBox(hOwnerEditor,temporary,"エラー",MB_OK|MB_ICONEXCLAMATION);
34 return 0;
35 }
36 if(length) WriteFile(hFile,buffer,length,&dw,NULL);
37 CloseHandle(hFile);
38 return 1;
39}
40
41
42///////////////////////////
43// 不揮発性のデータ管理
44///////////////////////////
45
46BOOL CNonVolatile::GetDataLine(char *name,char *parms){
47 int i,i2,length;
48 length=lstrlen(name);
49 for(i=0;;i++){
50 if(buffer[i]=='\0') break;
51 if(i==0||buffer[i]=='\r'&&buffer[i+1]=='\n'){
52 if(buffer[i]=='\r'&&buffer[i+1]=='\n') i+=2;
53 if(memcmp(buffer+i,name,length)==0&&buffer[i+length]=='='){
54 i+=length+1;
55 for(i2=0;;i++,i2++){
56 if(buffer[i]=='\r'&&buffer[i+1]=='\n'||buffer[i]=='\0'){
57 parms[i2]=0;
58 break;
59 }
60 parms[i2]=buffer[i];
61 }
62 return 1;
63 }
64 }
65 }
66 return 0;
67}
68
69BOOL CNonVolatile::GetWholeValue(char *name,int *pi32data){
70 char temporary[8192];
71 if(!GetDataLine(name,temporary)) return 0;
72 *pi32data=atoi(temporary);
73 return 1;
74}
75BOOL CNonVolatile::Get3WholeValue(char *name,int *pd1,int *pd2,int *pd3){
76 char temporary[8192];
77 if(!GetDataLine(name,temporary)) return 0;
78 sscanf(temporary,"%d,%d,%d",pd1,pd2,pd3);
79 return 1;
80}
81BOOL CNonVolatile::GetRGBValue(char *name,COLORREF *prgb){
82 char temporary[8192];
83 if(!GetDataLine(name,temporary)) return 0;
84 sscanf(temporary,"%x",prgb);
85 return 1;
86}
87BOOL CNonVolatile::GetStringValue(char *name,char *str){
88 if(!GetDataLine(name,str)) return 0;
89 return 1;
90}
91BOOL CNonVolatile::GetRectValue(char *name,RECT *prc){
92 char temporary[8192];
93 if(!GetDataLine(name,temporary)) return 0;
94 sscanf(temporary,"%d,%d,%d,%d",&prc->left,&prc->top,&prc->right,&prc->bottom);
95 return 1;
96}
97BOOL CNonVolatile::GetPointValue(char *name,POINT *pos){
98 char temporary[8192];
99 if(!GetDataLine(name,temporary)) return 0;
100 sscanf(temporary,"%d,%d",&pos->x,&pos->y);
101 return 1;
102}
103
104void CNonVolatile::load(){
105 extern HANDLE hHeap;
106 int i;
107
108 //開く
109 buffer=ReadBuffer_NonErrMsg( Jenga::Common::Environment::GetAppDir() + "\\UserSetting\\compiler.ini" );
110 if(!buffer){
111 //レジストリを読み込む
112 OldLoad();
113 return;
114 }
115
116
117 if(!GetWholeValue("IsShow_DefaultSystem_Var",&bShow_DefaultSystem_Var)) bShow_DefaultSystem_Var=0;
118 if(!GetWholeValue("IsShow_Rad_Var",&bShow_Rad_Var)) bShow_Rad_Var=0;
119 if(!GetWholeValue("IsShow_GUID_Var",&bShow_GUID_Var)) bShow_Rad_Var=0;
120
121 extern int ScreenX,ScreenY;
122 if(!GetPointValue("MainDlgPos",&MainDlgPos)){
123 MainDlgPos.x=ScreenX/2-100;
124 MainDlgPos.y=ScreenX/2-250;
125 }
126
127 if(!GetRectValue("VarDlgRect",&VarDlgRect)){
128 VarDlgRect.left=ScreenX/2-390/2;
129 VarDlgRect.top=ScreenY/2-270/2;
130 VarDlgRect.right=ScreenX/2+390/2;
131 VarDlgRect.bottom=ScreenY/2+270/2;
132 }
133
134 //ウォッチリスト
135 if(!GetWholeValue("WatchNum",&WatchNum)) WatchNum=0;
136
137 ppWatchStr=(char **)HeapAlloc(hHeap,0,WatchNum*sizeof(char *)+1);
138 for(i=0;i<WatchNum;i++){
139 char temporary[VN_SIZE];
140 sprintf(temporary,"Watch%03d",i);
141
142 char temp2[VN_SIZE];
143 GetStringValue(temporary,temp2);
144 ppWatchStr[i]=(char *)HeapAlloc(hHeap,0,lstrlen(temp2)+1);
145 lstrcpy(ppWatchStr[i],temp2);
146 }
147
148
149 HeapDefaultFree(buffer);
150}
151void CNonVolatile::OldLoad(){
152 extern HANDLE hHeap;
153 extern int ScreenX,ScreenY;
154
155 int i;
156 HKEY hKey;
157 ULONG ulSize;
158 char temporary[MAX_PATH],temp2[VN_SIZE];
159
160 if(RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Discoversoft\\BasicCompiler",0,KEY_ALL_ACCESS,&hKey)!=ERROR_SUCCESS){
161 bShow_DefaultSystem_Var=0;
162 bShow_Rad_Var=0;
163 bShow_GUID_Var=0;
164
165 MainDlgPos.x=ScreenX/2-100;
166 MainDlgPos.y=ScreenX/2-250;
167
168 VarDlgRect.left=ScreenX/2-390/2;
169 VarDlgRect.top=ScreenY/2-270/2;
170 VarDlgRect.right=ScreenX/2+390/2;
171 VarDlgRect.bottom=ScreenY/2+270/2;
172
173 WatchNum=0;
174 ppWatchStr=(char **)HeapAlloc(hHeap,0,1);
175 }
176 else{
177 ulSize=sizeof(DWORD);
178 if(RegQueryValueEx(hKey,"bShow_DefaultSystem_Var",NULL,NULL,(BYTE *)&bShow_DefaultSystem_Var,&ulSize)!=ERROR_SUCCESS)
179 bShow_DefaultSystem_Var=0;
180
181 ulSize=sizeof(DWORD);
182 if(RegQueryValueEx(hKey,"bShow_Rad_Var",NULL,NULL,(BYTE *)&bShow_Rad_Var,&ulSize)!=ERROR_SUCCESS)
183 bShow_Rad_Var=0;
184
185 ulSize=sizeof(DWORD);
186 if(RegQueryValueEx(hKey,"bShow_GUID_Var",NULL,NULL,(BYTE *)&bShow_GUID_Var,&ulSize)!=ERROR_SUCCESS)
187 bShow_GUID_Var=0;
188
189 ulSize=1024;
190 if(RegQueryValueEx(hKey,"MainDlgPos",NULL,NULL,(unsigned char *)temporary,&ulSize)!=ERROR_SUCCESS){
191 MainDlgPos.x=ScreenX/2-100;
192 MainDlgPos.y=ScreenX/2-250;
193 }
194 else sscanf(temporary,"%d %d",
195 &MainDlgPos.x,
196 &MainDlgPos.y);
197
198 ulSize=1024;
199 if(RegQueryValueEx(hKey,"VarDlgRect",NULL,NULL,(unsigned char *)temporary,&ulSize)!=ERROR_SUCCESS){
200 VarDlgRect.left=ScreenX/2-390/2;
201 VarDlgRect.top=ScreenY/2-270/2;
202 VarDlgRect.right=ScreenX/2+390/2;
203 VarDlgRect.bottom=ScreenY/2+270/2;
204 }
205 else sscanf(temporary,"%d %d %d %d",
206 &VarDlgRect.left,
207 &VarDlgRect.top,
208 &VarDlgRect.right,
209 &VarDlgRect.bottom);
210
211 ulSize=sizeof(DWORD);
212 if(RegQueryValueEx(hKey,"WatchNum",NULL,NULL,(BYTE *)&WatchNum,&ulSize)!=ERROR_SUCCESS)
213 WatchNum=0;
214 ppWatchStr=(char **)HeapAlloc(hHeap,0,WatchNum*sizeof(char *)+1);
215 for(i=0;i<WatchNum;i++){
216 sprintf(temporary,"WathNum%03d",i);
217 ulSize=VN_SIZE;
218 if(RegQueryValueEx(hKey,temporary,NULL,NULL,(unsigned char *)temp2,&ulSize)!=ERROR_SUCCESS){
219 WatchNum=i;
220 break;
221 }
222 ppWatchStr[i]=(char *)HeapAlloc(hHeap,0,lstrlen(temp2)+1);
223 lstrcpy(ppWatchStr[i],temp2);
224 }
225 }
226}
227
228void CNonVolatile::SetWholeValue(char *name,long i32data){
229 sprintf(buffer+lstrlen(buffer),"%s=%d\r\n",name,i32data);
230}
231void CNonVolatile::Set3WholeValue(char *name,long d1,long d2,long d3){
232 sprintf(buffer+lstrlen(buffer),"%s=%d,%d,%d\r\n",name,d1,d2,d3);
233}
234void CNonVolatile::SetRGBValue(char *name,COLORREF rgb){
235 sprintf(buffer+lstrlen(buffer),"%s=%x\r\n",name,rgb);
236}
237void CNonVolatile::SetStringValue(char *name,char *str){
238 sprintf(buffer+lstrlen(buffer),"%s=%s\r\n",name,str);
239}
240void CNonVolatile::SetRectValue(char *name,RECT *prc){
241 sprintf(buffer+lstrlen(buffer),"%s=%d,%d,%d,%d\r\n",name,prc->left,prc->top,prc->right,prc->bottom);
242}
243void CNonVolatile::SetPointValue(char *name,POINT *pos){
244 sprintf(buffer+lstrlen(buffer),"%s=%d,%d\r\n",name,pos->x,pos->y);
245}
246
247void CNonVolatile::save(){
248 int i;
249 char temporary[MAX_PATH];
250
251 const string userSettingDirPath = Jenga::Common::Environment::GetAppDir() + "\\UserSetting";
252
253 HANDLE hFind;
254 WIN32_FIND_DATA wfd;
255 hFind=FindFirstFile( userSettingDirPath.c_str() ,&wfd);
256 if(hFind==INVALID_HANDLE_VALUE){
257 //UserSettingディレクトリを作成
258 if(!CreateDirectory( userSettingDirPath.c_str() ,NULL)){
259 extern HWND hOwnerEditor;
260 MessageBox(hOwnerEditor,"UserSettingディレクトリの作成に失敗","ActiveBasic",MB_OK|MB_ICONEXCLAMATION);
261 return;
262 }
263 }
264
265 extern HANDLE hHeap;
266 buffer=(char *)HeapAlloc(hHeap,0,65536);
267 buffer[0]=0;
268
269
270 SetWholeValue("IsShow_DefaultSystem_Var",bShow_DefaultSystem_Var);
271 SetWholeValue("IsShow_Rad_Var",bShow_Rad_Var);
272 SetWholeValue("IsShow_GUID_Var",bShow_GUID_Var);
273
274 SetPointValue("MainDlgPos",&MainDlgPos);
275
276 SetRectValue("VarDlgRect",&VarDlgRect);
277
278 //ウォッチリスト
279 SetWholeValue("WatchNum",WatchNum);
280 for(i=0;i<WatchNum;i++){
281 sprintf(temporary,"Watch%03d",i);
282 SetStringValue(temporary,ppWatchStr[i]);
283 HeapDefaultFree(ppWatchStr[i]);
284 }
285 HeapDefaultFree(ppWatchStr);
286
287
288 //保存
289 WriteBuffer(
290 userSettingDirPath + "\\compiler.ini",
291 buffer,lstrlen(buffer));
292
293
294 HeapDefaultFree(buffer);
295}
Note: See TracBrowser for help on using the repository browser.