source: dev/trunk/abdev/BasicCompiler_Common/NonVolatile.cpp@ 153

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