| 1 |
#include "defextern.h" |
| 2 |
|
| 3 |
#ifndef STRICT |
| 4 |
#define STRICT |
| 5 |
#endif |
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <iostream.h> |
| 9 |
#include <fstream.h> |
| 10 |
#include <windows.h> |
| 11 |
#include "edit.h" |
| 12 |
#include "mes.h" |
| 13 |
#include "initial.h" |
| 14 |
#include "file.h" |
| 15 |
#include "status.h" |
| 16 |
#include "mode.h" |
| 17 |
|
| 18 |
#define MAX_EDIT 1024*30 |
| 19 |
#define editsize MAX_EDIT |
| 20 |
char buff[editsize]; |
| 21 |
char backupbuff[editsize]; |
| 22 |
char editfile[MAX_PATH]; |
| 23 |
|
| 24 |
struct buffers |
| 25 |
{ |
| 26 |
BOOL first; |
| 27 |
char buff[editsize]; |
| 28 |
char backupbuff[editsize]; |
| 29 |
char updatebuff[editsize]; |
| 30 |
buffers(){ |
| 31 |
first =TRUE; |
| 32 |
reset(buff); |
| 33 |
reset(backupbuff); |
| 34 |
reset(updatebuff); |
| 35 |
} |
| 36 |
void reset(char *buff) {memset(buff,0,editsize);} |
| 37 |
BOOL update(ofstream &klog) { |
| 38 |
|
| 39 |
BOOL ans = strcmp(buff,backupbuff)==0; |
| 40 |
if(!ans && getnew()>0) |
| 41 |
{ |
| 42 |
if(!first) klog<<updatebuff; |
| 43 |
wsprintf(backupbuff,"%s",buff); |
| 44 |
first =FALSE; |
| 45 |
} |
| 46 |
//Mes(buff); |
| 47 |
return ans; |
| 48 |
} |
| 49 |
int getnew() { |
| 50 |
char *p =buff; |
| 51 |
int i; |
| 52 |
for(i=0;i<strlen(buff);i++){ |
| 53 |
if(*p == backupbuff[i]) p++; |
| 54 |
else break; |
| 55 |
} |
| 56 |
reset(updatebuff); |
| 57 |
if(i<strlen(buff)-1){ |
| 58 |
strcpy(updatebuff,p); |
| 59 |
for(int j=0;j<strlen(updatebuff);j++){ |
| 60 |
if(*p != backupbuff[i]) p++; |
| 61 |
else if(strcmp(p,(char*)(&(backupbuff[i])))==0){ |
| 62 |
updatebuff[j]=0; |
| 63 |
break; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
//Mes(updatebuff); |
| 68 |
return strlen(updatebuff); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
buffers klogbuffer; |
| 73 |
|
| 74 |
|
| 75 |
//������ ���������t�@�C�����`�F�b�N |
| 76 |
|
| 77 |
BOOL checkoverwriteOK(char *file) |
| 78 |
{ |
| 79 |
setstatusonly(1,"-"); |
| 80 |
if(checkExt(file,"exe")) return FALSE; |
| 81 |
if(checkExt(file,"EXE")) return FALSE; |
| 82 |
setstatusonly(1,"+"); |
| 83 |
return TRUE; |
| 84 |
} |
| 85 |
|
| 86 |
//������ ���������t�@�C�����`�F�b�N |
| 87 |
|
| 88 |
void overwriteNO() |
| 89 |
{ |
| 90 |
setstatusonly(1,"big"); |
| 91 |
} |
| 92 |
|
| 93 |
void cnvtoa(char *buf, int delim,long len,long denum) |
| 94 |
{ |
| 95 |
strcpy(backupbuff,buf); |
| 96 |
char *pfrom,*pto; |
| 97 |
pfrom =backupbuff; |
| 98 |
pto =buf; |
| 99 |
long count=0; |
| 100 |
|
| 101 |
for(long i=0; i+count<len; i++,pfrom++,pto++) |
| 102 |
{ |
| 103 |
if(*pfrom!=delim) *pto=*pfrom; |
| 104 |
else { |
| 105 |
*pto=13; |
| 106 |
pto++; |
| 107 |
*pto=10; |
| 108 |
count++; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
char *cnvbuff(char *buf, long len) |
| 115 |
{ |
| 116 |
int a=0; |
| 117 |
int d=0; |
| 118 |
int da=0; |
| 119 |
for(int i=0; i<1000; i++) |
| 120 |
{ |
| 121 |
if(buf[i]==13){ |
| 122 |
if( buf[i+1]==10) da++; |
| 123 |
else d++; |
| 124 |
} |
| 125 |
else if(buf[i]==10) a++; |
| 126 |
} |
| 127 |
if(da==1 && a+d>0) |
| 128 |
{ |
| 129 |
if(a>3){ |
| 130 |
cnvtoa(buf,10,len,a); |
| 131 |
} |
| 132 |
else if(d>3){ |
| 133 |
cnvtoa(buf,13,len,d); |
| 134 |
} |
| 135 |
} |
| 136 |
return buf; |
| 137 |
} |
| 138 |
|
| 139 |
//edit window �\���� |
| 140 |
//�t�@�C���������A���e�\���A���e���o�b�N�A�b�v�o�b�t�@���R�s�[�i�������������j |
| 141 |
//utf-8�ifirefox bookmark.htm�����j�I���������������������������H |
| 142 |
|
| 143 |
void setfile(HWND hEdit,char *file,char *dir) |
| 144 |
{ |
| 145 |
SetWindowText(hEdit,"loading.."); |
| 146 |
SetCurrentDirectory(dir); |
| 147 |
_fullpath(editfile,file,MAX_PATH); |
| 148 |
editflag=FALSE; |
| 149 |
memset(buff,0,sizeof(buff)); |
| 150 |
memset(backupbuff,0,sizeof(backupbuff)); |
| 151 |
ifstream in(editfile,ios::binary | ios::nocreate); |
| 152 |
sprintf(buff,"$.%s\r\n",file); |
| 153 |
int headlen=strlen(buff); |
| 154 |
long rsize =editsize-headlen-2; |
| 155 |
if(in) { |
| 156 |
in.read(buff+headlen,rsize); |
| 157 |
cnvbuff(buff,rsize); |
| 158 |
strcpy(backupbuff,buff); |
| 159 |
SetWindowText(hEdit,buff); |
| 160 |
checkoverwriteOK(editfile); |
| 161 |
} |
| 162 |
if(getfilesize(file)<editsize-1000) |
| 163 |
{ |
| 164 |
editflag=TRUE; |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
editflag=FALSE; |
| 169 |
overwriteNO(); |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
//������ |
| 175 |
//�o�b�N�A�b�v�o�b�t�@�����r�A�X�V�������������������B |
| 176 |
|
| 177 |
void overwrite(HWND hEdit) |
| 178 |
{ |
| 179 |
if(editflag) |
| 180 |
{ |
| 181 |
memset(buff,0,sizeof(buff)); |
| 182 |
GetWindowText(hEdit,buff,editsize-2); |
| 183 |
if(strcmp(backupbuff,buff)!=0 && checkoverwriteOK(editfile)) |
| 184 |
{ |
| 185 |
ofstream ou(editfile,ios::binary); |
| 186 |
ou.write(buff,strlen(buff)); |
| 187 |
} |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
MessageBox(NULL, "�T�C�Y�����������������o�����t�@�C����������������", "Error", MB_OK); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
//�����������A�t�@�C�����������O�������o�������A���������������������� |
| 196 |
void autooverwrite(HWND hEdit) |
| 197 |
{ |
| 198 |
if(editflag) |
| 199 |
{ |
| 200 |
overwrite(hEdit); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
BOOL geteditflag() |
| 206 |
{ |
| 207 |
return editflag; |
| 208 |
} |
| 209 |
|
| 210 |
void seteditflag(BOOL f) |
| 211 |
{ |
| 212 |
editflag=f; |
| 213 |
} |
| 214 |
|
| 215 |
int readline(char *to,char *from,int len=0) |
| 216 |
{ |
| 217 |
int i; |
| 218 |
if(len==0) len=strlen(from); |
| 219 |
strncpy(to,from,len); |
| 220 |
for (i=0; i<strlen(to);i++) |
| 221 |
{ |
| 222 |
if(to[i]==0x0d ||to[i]==0x0a){ |
| 223 |
to[i]=0; |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
return i; |
| 228 |
} |
| 229 |
|
| 230 |
void cnvfname(char *to,char *from=NULL) |
| 231 |
{ |
| 232 |
if(from==NULL) from=to; |
| 233 |
char buff[MAX_PATH]; |
| 234 |
strcpy(buff,from); |
| 235 |
char bad[]="\\ /:*?\"<>|"; |
| 236 |
for(int i=0;i<strlen(from);i++) |
| 237 |
{ |
| 238 |
if(strchr(bad,buff[i])!=NULL) buff[i]='_'; |
| 239 |
} |
| 240 |
strcpy(to,buff); |
| 241 |
// Mes(buff); |
| 242 |
} |
| 243 |
|
| 244 |
int gettmpname(char *tmpfile, char *buff ,char *top=NULL) |
| 245 |
{ |
| 246 |
int i=0; |
| 247 |
int hlen; |
| 248 |
if(top!=NULL) { |
| 249 |
hlen=strlen(top); |
| 250 |
if(strncmp(buff,top,hlen)==0) { |
| 251 |
i=readline(tmpfile,buff+hlen,MAX_PATH-6); |
| 252 |
if( strchr(tmpfile,'.') ==NULL ) strcat(tmpfile,".txt"); |
| 253 |
return i; |
| 254 |
} |
| 255 |
} |
| 256 |
else { |
| 257 |
i=readline(tmpfile,buff+2,MAX_PATH-2); |
| 258 |
cnvfname(tmpfile); |
| 259 |
if( strchr(tmpfile,'.') ==NULL ) strcat(tmpfile,".txt"); |
| 260 |
} |
| 261 |
return i; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
// �����s�����O���w���������������� |
| 266 |
// $.filename => current folder |
| 267 |
// $>filename => memo folder |
| 268 |
|
| 269 |
void saveTempo(HWND hEdit) |
| 270 |
{ |
| 271 |
char mfile[MAX_PATH]; |
| 272 |
char tmpfile[MAX_PATH]; |
| 273 |
|
| 274 |
memset(buff,0,sizeof(buff)); |
| 275 |
memset(tmpfile,0,sizeof(tmpfile)); |
| 276 |
GetWindowText(hEdit,buff,editsize-2); |
| 277 |
int pos = gettmpname( tmpfile, buff, "$>" ); |
| 278 |
if( pos!=0 ) { |
| 279 |
GetFullName(mfile, tmpfile, memopath, MAX_PATH); |
| 280 |
} |
| 281 |
else { |
| 282 |
pos = gettmpname( tmpfile, buff, "$." ); |
| 283 |
if( pos!=0 ) { |
| 284 |
strcpy(mfile, tmpfile); |
| 285 |
} |
| 286 |
else { |
| 287 |
wsprintf(mfile,"%s.txt", gettimestr()); |
| 288 |
// Mes(mfile); |
| 289 |
ofstream ou(mfile,ios::binary); |
| 290 |
if(ou) { |
| 291 |
ou.write(buff,strlen(buff)); |
| 292 |
//Mes(mfile); |
| 293 |
} |
| 294 |
return; |
| 295 |
} |
| 296 |
} |
| 297 |
ofstream ou(mfile,ios::binary); |
| 298 |
if(ou && strlen(buff)-pos-4>0) { |
| 299 |
ou.write(buff+pos+4,strlen(buff)-pos-4); |
| 300 |
} |
| 301 |
else Mes("can't save"); |
| 302 |
} |
| 303 |
|
| 304 |
void saveMemo(HWND hEdit) |
| 305 |
{ |
| 306 |
//Mes("save memo"); |
| 307 |
char mfile[MAX_PATH]; |
| 308 |
char buff[editsize]; |
| 309 |
GetFullName(mfile, memofile, memopath, MAX_PATH); |
| 310 |
|
| 311 |
memset(buff,0,sizeof(buff)); |
| 312 |
if( MemoMode ==_OFF || hEdit == NULL ) return; |
| 313 |
GetWindowText(hEdit,buff,editsize-2); |
| 314 |
if(strcmp(backupbuff,buff)!=0 ) |
| 315 |
{ |
| 316 |
ofstream ou(mfile,ios::binary); |
| 317 |
if(ou) { |
| 318 |
ou.write(buff,strlen(buff)); |
| 319 |
SetWindowText(hEdit,""); |
| 320 |
} |
| 321 |
else Mes("can't save"); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
int renameMemo() |
| 326 |
{ |
| 327 |
char newfile[MAX_PATH]; |
| 328 |
char ToFile[MAX_PATH], FromFile[MAX_PATH]; |
| 329 |
|
| 330 |
makenewfilenameEx(memofile,newfile); |
| 331 |
//wsprintf(memofile,"%s",newfile); |
| 332 |
GetFullName(ToFile, newfile, memopath, MAX_PATH); |
| 333 |
GetFullName(FromFile, memofile, memopath, MAX_PATH); |
| 334 |
return rename( FromFile, ToFile ); |
| 335 |
} |
| 336 |
|
| 337 |
int changeMemo(HWND Edit) |
| 338 |
{ |
| 339 |
if (Edit!=NULL) saveMemo(Edit); |
| 340 |
int res=(renameMemo()); |
| 341 |
if(res) SetWindowText(Edit,""); |
| 342 |
return res; |
| 343 |
} |
| 344 |
|
| 345 |
void askmemoname(char *path) |
| 346 |
{ |
| 347 |
// |
| 348 |
} |
| 349 |
|
| 350 |
void loadMemo(HWND hEdit) |
| 351 |
{ |
| 352 |
if(strlen(memopath)==0) |
| 353 |
{ |
| 354 |
askmemoname(memopath); |
| 355 |
} |
| 356 |
if(strlen(memopath)==0) |
| 357 |
{ |
| 358 |
strcpy(memopath,getCdir()); |
| 359 |
} |
| 360 |
|
| 361 |
char mfile[MAX_PATH]; |
| 362 |
GetFullName(mfile, memofile, memopath, MAX_PATH); |
| 363 |
|
| 364 |
memset(buff,0,sizeof(buff)); |
| 365 |
ifstream in(mfile,ios::binary | ios::nocreate); |
| 366 |
if(in) in.read(buff,editsize-2); |
| 367 |
cnvbuff(buff,editsize-2); |
| 368 |
strcpy(backupbuff,buff); |
| 369 |
SetWindowText(hEdit,buff); |
| 370 |
//Mes(mfile); |
| 371 |
} |
| 372 |
|
| 373 |
void keylog( HWND hEdit, WORD key) |
| 374 |
{ |
| 375 |
if(MemoMode == _OFF) return; |
| 376 |
GetStream("key.log.txt", memopath, klog, ios::ate); |
| 377 |
// Mes(klog? 1:-1); |
| 378 |
GetWindowText(hEdit,klogbuffer.buff,editsize-2); |
| 379 |
klogbuffer.update(klog); |
| 380 |
klog.close(); |
| 381 |
} |