태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

달력

6

« 2021/6 »

  •  
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  •  
  •  
  •  
2011. 10. 6. 14:11

MiniUtil Source Code C | C++ | VC++2011. 10. 6. 14:11

Free! 소스 창고 대방출 사업..

/* 
	miniutil.h
	author: krkim
	mailto://yeamaec@hanafos.com (=> durumul@gmail.com)
	http://yeamaec.com (=> http://krkim.net)
	If you get this source code,use freely but leave this notice.
	1990-2007,Yeamaec Communication.Co,.Ltd.All right reserved.
*/

#ifndef _miniutil__h_
#define _miniutil__h_

#pragma once
void TRACELOG(LPSTR szFile,LPCSTR pszFormat,...);
void __stdcall TRACELOGFILE(LPSTR szFile,LPCSTR pszFormat,...);

int GetFtpFile(char* szLocalFileName, char* szFtpFileName, char* url, int port, char* username, char* password);
BOOL PutFtpFile(char* szLocalFileName, char* szFtpFileName, char* url, int port, char* username, char* password);
CHAR * gettokenstr(CHAR *line,CHAR *delimiter,int pos,CHAR *token,int *tokenlen,int *boperator);
int wildcmp(char *wild, char *string);
CHAR skipcomments(FILE *f);
void removecomment(LPSTR buff);
void removeblank(LPSTR buff,int where = 0);
void GetHomeDirectory(char *szHomeDirectory);
void __stdcall TRACELOG(LPCTSTR pszFormat,...);	
LPTSTR __stdcall allocbuff(LPTSTR str);
void __stdcall freebuff(LPTSTR *str);
void __stdcall reallocbuff(LPTSTR *dest,LPTSTR src);
void __stdcall commastr(TCHAR *figure, TCHAR *buffer, bool no_comma = false);
void __stdcall commastr(LONGLONG figure, TCHAR *buffer, bool no_comma = false);
void __stdcall filesizestr(LONGLONG figure,TCHAR *buffer);
int __stdcall HexStrtoDec(LPTSTR hexastring);
__int64 __stdcall getfilepointer(HANDLE hFile);
__int64 __stdcall setfilepointer(HANDLE hFile,ULONGLONG Distance,DWORD dwMoveMethod = FILE_BEGIN);
HANDLE __stdcall openfile(LPTSTR szFile,bool bWrite = false,DWORD dwAttr = FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_NORMAL);
HANDLE __stdcall createfile(LPTSTR szFile,DWORD dwAttr = FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_NORMAL);
BOOL closefile(HANDLE hFile);
BOOL __stdcall writefile(HANDLE hFile, LPVOID lpBuffer, LONG lCount,DWORD *dwWritten = NULL);
BOOL __stdcall readfile(HANDLE hFile,LPVOID lpBuffer,LONG lCount,DWORD *dwReaded =  NULL);
bool __stdcall getlastwfiletime(HANDLE hFile,FILETIME *lastwtime);
bool __stdcall checkfilewrite(LPTSTR szDir);
bool __stdcall isfileexists(LPTSTR szFile,WIN32_FIND_DATA * finddata = NULL);
bool __stdcall createfolder(LPTSTR szFolder);
bool ismbslead(LPCTSTR string, int ncol);
bool ismbstrail(LPCTSTR string,int ncol);
bool __stdcall getprefixbypath(LPTSTR path,LPTSTR prefix);
void __stdcall getuniquefilename(LPTSTR path,LPTSTR prefix,LPTSTR ext,LPTSTR filename);
float __stdcall stopwatch(bool end,LARGE_INTEGER *swStart);
void __stdcall fileAttr2Str(DWORD attr,LPSTR szattr);

#endif

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/* 
	miniutil.h
	author: krkim
	mailto://durumul@gmail.com
	http://krkim.net
	If you get this source code,use freely but leave this notice.
	1990-2007,Yeamaec Communication.Co,.Ltd.All right reserved.
*/

#include "stdafx.h"
#include "miniutil.h"
#include <stdio.h>
#include <process.h>
#include <sys/stat.h>
#include <math.h>
#include <vector>
#include <stdexcept>

#include <wininet.h>

//#include <atlconv.h>
//#include <atlbase.h>
//#include <atlcom.h>
#include <process.h>
#include <sys/stat.h>

#pragma warning (push)
#pragma warning(disable : 4996)

//using namespace ATL;
using namespace std;

#pragma comment(lib,"comctl32.lib")
void __stdcall TRACELOG(LPCSTR pszFormat,...)
{
	va_list arglist;
	va_start(arglist,pszFormat);
	int nMin = _vscprintf( pszFormat, arglist );
	static const int nCount = 1024;
	CHAR szBuf[nCount] = {'\0',};

	//vsprintf_s(szBuf,nNeed,pszFormat,arglist);
	_vsnprintf_s(szBuf, nCount, nCount - 1, pszFormat, arglist);
	va_end(arglist);
	OutputDebugString((LPCTSTR)szBuf);
}

void __stdcall TRACELOGFILE(LPSTR szFile,LPCSTR pszFormat,...)
{
#if 0
	va_list arglist;
	va_start(arglist,pszFormat);
	int nMin = _vscprintf( pszFormat, arglist );
	static const int nCount = 1024;
	CHAR szBuf[nCount] = {'\0',};

	//vsprintf_s(szBuf,nNeed,pszFormat,arglist);
	_vsnprintf_s(szBuf, nCount, nCount - 1, pszFormat, arglist);
	va_end(arglist);
	OutputDebugString((LPCTSTR)szBuf);
	if(szFile !=NULL && szFile[0]){
		FILE *fp = fopen(szFile,"a+");
		if(fp){
			fputs(szBuf,fp);
			fclose(fp);
		}
	}
#else
	int nwritten = 0;
	va_list arglist;
	char szBuf[2048]={0,};
	int towritelen = 0;

	va_start(arglist,pszFormat);
	vsprintf(szBuf,pszFormat,arglist);
	va_end(arglist);

	OutputDebugString((LPCTSTR)szBuf);
	if(szFile !=NULL && szFile[0]){
		FILE *fp = fopen(szFile,"a+");
		if(!fp)
			fp = fopen(szFile,"w");
		if(fp){
			fputs(szBuf,fp);
			fclose(fp);
		}
	}
#endif
}

LPTSTR __stdcall allocbuff(LPTSTR str)
{
	LPTSTR buff;
	if(!str) return NULL;
	int len = (int) _tcslen(str) + 1;
	buff = new TCHAR[len];
	memset(buff,0,len);
	lstrcpy(buff,str);
	return buff;
}

void __stdcall freebuff(LPTSTR *str)
{
	if(*str){
		delete [] *str;
		*str = NULL;
	}
}

void __stdcall reallocbuff(LPTSTR *dest,LPTSTR src)
{
	LPTSTR buff;
	if(*dest) delete [] *dest;
	*dest = NULL;

	int len = (int) _tcslen(src) + 1;
	buff = new TCHAR[len];
	memset(buff,0,len);
	lstrcpy(buff,src);
	
	*dest = buff;
}

void __stdcall commastr(TCHAR *figure, TCHAR *buffer, bool no_comma)
{
	TCHAR numb[128];
	_tcsncpy(numb,figure,sizeof(numb)-1);
	int i,j;
	TCHAR temp[128] ={0,};
	TCHAR *p = temp;
	for(i = (int)_tcslen(numb) - 1,j = 0; i >= 0 ; i--){ 
		if(no_comma == false && j % 3 ==0 && j >= 3 && numb[i] != '.'){
			*p++ = ',';
			*p++ = numb[i];
		}
		else
			*p++ = numb[i];
		j = (numb[i] == '.') ? 0 : j + 1;
    }
	for(i = (int)_tcslen(temp) - 1; i >= 0 ; i--)
		*buffer++ = temp[i];
	*buffer = 0;
}

void __stdcall commastr(LONGLONG figure, TCHAR *buffer, bool no_comma)
{
	TCHAR numb[128];
	sprintf(numb,"%.f",(float)figure);//don't use wsprintf for floating point
	commastr((TCHAR *)numb,buffer,no_comma);
}

void __stdcall filesizestr(LONGLONG figure,TCHAR *buffer)
{
	TCHAR tempsize[128];
	TCHAR unitsize[128];
	float dwtempsize = (float)figure;
	int unit = 0;
	LONGLONG kb = (LONGLONG)(double)pow((double)2,(double)10);
	LONGLONG mb = (LONGLONG)(double)pow((double)2,(double)20);
	LONGLONG gb = (LONGLONG)(double)pow((double)2,(double)30);
	LONGLONG tb = (LONGLONG)(double)pow((double)2,(double)40);
	LONGLONG pb = (LONGLONG)(double)pow((double)2,(double)40);

	if(dwtempsize < kb){
		unit = 0;
		if(dwtempsize == 0.)
			sprintf(tempsize,"0");//BYTES => 1KB
		else
			sprintf(tempsize,"1");//BYTES => 1KB
	}
	else if(dwtempsize < mb * 1){// ~ 1MB
		unit = 0;
		sprintf(tempsize,"%.f",dwtempsize/kb);//BYTES => KB
		if(dwtempsize/kb >= 1000){ //1KB ~ 999KB
			goto nextmb;
		}
	}
	else if(dwtempsize < gb * 10){// ~ 10000MB (1mb - 9999mb)
nextmb:
		unit = 1;
		sprintf(tempsize,"%.f",dwtempsize/mb);//BYTES => MB
		if(dwtempsize/mb >= 10000){// over 10000MB
			goto nextgb;
		}
	}
	else if(dwtempsize < tb * 10){// ~ 10000MB (1mb - 9999mb)
nextgb:
		unit = 2;
		sprintf(tempsize,"%.f",dwtempsize/gb);//BYTES => GB
	}
	else if(dwtempsize < pb){
		unit = 3;
		sprintf(tempsize,"%.f",dwtempsize/tb);//BYTES => TB
	}

	TCHAR *p = _tcsrchr(tempsize,'.');
	if (p && *(p + 1) == '0') *p = '\0';
	if (p && *(p + 1) && *(p + 2) != '\0') *(p + 2) = '\0';

	commastr(tempsize,unitsize);
	TCHAR *units[ ] = {
		"KB","MB","GB","TB","PB","EB","ZB","YB"};

	if(unit >= 0 && unit <  sizeof(units) / sizeof(units[0]) )
		_tcscat(unitsize,units[unit]);
	_tcscpy(buffer,unitsize);
}

int __stdcall HexStrtoDec(LPTSTR hexastring)
{
	int ret=0, i =0;
	int p = 1;
	TCHAR hexstr[80];
	int len;
	LPTSTR buff = &hexstr[0];
	lstrcpy(buff,hexastring);
	len = (int)_tcslen(hexstr);

	while(1){
		if(hexstr[i] >= 'A' && hexstr[i] <= 'F')
			hexstr[i] = hexstr[i] -'A' + 0x0A;

		else if(hexstr[i] >= 'a' && hexstr[i] <= 'f')
			hexstr[i] = hexstr[i] -'a' + 0x0A;

		else
			hexstr[i] = hexstr[i] - '0';

		i++;
		if(i>=len) break;
	}
	for(i=len-1;i>=0;i--)
	{
		ret += hexstr[i]*p;
		p = p*16;
	}
	return ret;
}

////////////////////////////////////////////////////////////////////////////////////////////
// Raw File I/O Function
HANDLE __stdcall openfile(LPTSTR szFile,bool bWrite,DWORD dwAttr)
{
	HANDLE hFile;
	WIN32_FIND_DATA find32;
	//DWORD dwAttrDefault = FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_NORMAL;

	if(bWrite && isfileexists(szFile,&find32)){
		DWORD dwfattr = find32.dwFileAttributes;
		if((dwfattr & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_READONLY))){
			dwfattr &= ~(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_READONLY);
			SetFileAttributes(szFile,dwfattr);
			DeleteFile(szFile);
		}
	}
	hFile = (bWrite) ?	CreateFile(szFile,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,dwAttr,NULL) :
						CreateFile(szFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,dwAttr,NULL);
	return hFile;
}

HANDLE __stdcall createfile(LPTSTR szFile,DWORD dwAttr)
{
	return openfile(szFile,true,dwAttr);
}

BOOL closefile(HANDLE hFile)
{
	return CloseHandle(hFile);
}

BOOL __stdcall writefile(HANDLE hFile, LPVOID lpBuffer, LONG lCount,DWORD *dwWritten)
{
	static const int maxsize = 32768 -1;
	ULONG  lOffset;
	DWORD wsize,written,twrite = 0L;
	LPTSTR lpPos;
	if(dwWritten) *dwWritten = 0L;

	for(lOffset = 0L; lCount > 0L; lOffset += wsize){
		wsize = (lCount <= maxsize) ? lCount : maxsize;
		lCount -= wsize;
		lpPos = (LPTSTR) lpBuffer + lOffset;
		::WriteFile(hFile,lpPos,wsize,&written,NULL);
		twrite += written;
		if(wsize != written)
			return twrite;
	}
	if(dwWritten) *dwWritten = twrite;
	return (twrite > 0);
}

BOOL __stdcall readfile(HANDLE hFile,LPVOID lpBuffer,LONG lCount,DWORD *dwReaded)
{
	static const int maxsize = 32768 -1;
	ULONG  lOffset;
	DWORD rsize,readed,tread = 0L;
	LPTSTR lpPos;
	if(dwReaded) *dwReaded = 0L;

	for(lOffset = 0L; lCount > 0L; lOffset += rsize){
		rsize = (lCount <= maxsize) ? lCount : maxsize;
		lCount -= rsize;
		lpPos = (LPTSTR) lpBuffer + lOffset;
		::ReadFile(hFile,lpPos,rsize,&readed,NULL);
		tread += readed;
		if(rsize != readed)
			return tread;
	}
	if(dwReaded) *dwReaded = tread;
	return (tread > 0);
}

__int64 __stdcall getfilepointer(HANDLE hFile)
{
	//get file pointer
	//return SetFilePointer(hFile,0,lpDistanceToMoveHigh,FILE_CURRENT);
	ULONGLONG dwStart = 0;
	LARGE_INTEGER liPos;
	liPos.QuadPart = 0;
	liPos.LowPart = ::SetFilePointer(hFile, liPos.LowPart, &liPos.HighPart ,FILE_CURRENT);
	return liPos.QuadPart;
}

__int64 __stdcall setfilepointer(HANDLE hFile,ULONGLONG Distance,DWORD dwMoveMethod)
{
	//set pointer to beginning of Catalog Data
	LARGE_INTEGER liOff;
	liOff.QuadPart = Distance;
	liOff.LowPart = ::SetFilePointer(hFile, liOff.LowPart, &liOff.HighPart,dwMoveMethod);
	return liOff.QuadPart;
}

bool __stdcall getlastwfiletime(HANDLE hFile,FILETIME *lastwtime)
{
	BOOL b;
	FILETIME CreationTime,LastAccessTime;
	b = ::GetFileTime((HANDLE)hFile, &CreationTime, &LastAccessTime, lastwtime);
	return ((b)? true : false);
}

bool __stdcall checkfilewrite(LPTSTR szDir)
{
	HANDLE hFile;
	TCHAR szTestFile[MAX_PATH];

    GetTempFileName(szDir,(LPCTSTR)"_tc",0,szTestFile);//thumbnail cache(TNC)

	if((hFile = createfile(szTestFile)) == INVALID_HANDLE_VALUE){
		DWORD dwerr = GetLastError();
		if(dwerr == ERROR_ACCESS_DENIED){
			//TRACE("err = %d(%x)\n",dwerr,dwerr);
		}
		return false;
	}
	else{
		closefile(hFile);
		DeleteFile(szTestFile);
	}
	return true;
}

bool __stdcall isfileexists(LPTSTR szFile,WIN32_FIND_DATA * finddata)
{
	BOOL bFind = FALSE;
	HANDLE flag32;
	WIN32_FIND_DATA read32={0,};//32bit porting routine..
	TCHAR szPath[MAX_PATH];
	_tcscpy(szPath,szFile);
	int len = (int)_tcslen(szPath);
	if(len > 0 && szPath[ len - 1] == _T('\\')) //bugfix : last '\\' exist, folder is unreconized even if it exists.
		szPath[len - 1] = NULL;                   // 2005 11 yeamaec

	if((flag32 = FindFirstFile(szPath,&read32)) == INVALID_HANDLE_VALUE)
		return false;
	if(finddata) *finddata = read32;
	FindClose(flag32);
	return true;
}

//2more sub-path included full path create directory
bool __stdcall createfolder(LPTSTR szFolder)
{
	TCHAR szSub[MAX_PATH+2]={0,};
	int flag,len,i;
	flag = CreateDirectory(szFolder,NULL);
	if(flag) return true;
	//c:\111\222\333
	len = (int)_tcslen(szFolder);
	if(len < 2) return false;

	for(i = 0;i < len; i++){
		szSub[i] = szFolder[i];
    if(szSub[i] == _T('\\')){ // c:\ or \\(windows network path)
			if( i >= 2){
				szSub[i+1] = NULL;
				CreateDirectory(szSub,NULL);
			}
		}
	}
	flag = CreateDirectory(szSub,NULL);
	return isfileexists(szSub);
}

bool ismbslead(LPCTSTR string, int ncol)//copy from my another project 'miedit'
{
#ifdef _UNICODE
	LPCTSTR current = string + ncol;
	return false;
#else // _UNICODE
	const unsigned char *current = (const unsigned char *)string + ncol;
	if(_ismbslead ((const unsigned char *)string, current) < 0)
		return true;
	return false;
#endif // _UNICODE
}

bool ismbstrail (LPCTSTR string, int ncol)
{
#ifdef _UNICODE
	LPCTSTR current = string + ncol;
	return false;
#else // _UNICODE
	const unsigned char *current = (const unsigned char *)string + ncol;
	if(_ismbstrail ((const unsigned char *)string, current) < 0)
		return true;
	return false;
#endif // _UNICODE
}

//디렉토리 경로에서 \\ 구분자다음의 첫문자만을 구해온다.
bool __stdcall getprefixbypath(LPTSTR path,LPTSTR prefix)
{
	TCHAR *p = path,*q = prefix,ch;
	while ((ch = *p) != NULL){
		if(p == path)
			*prefix++ = *p++;
		else if(ch == '\\'){
			ch = *++p;
			*prefix++ = ch;
			if(ch == NULL) break;
			if(ismbslead(path,(int)(p - path))){
				ch = *++p;
				*prefix++ = ch;
			}
			p++;
		}
		else
			p++;
	}
	*prefix = NULL;
	return (q != prefix);
}

void __stdcall getuniquefilename(LPTSTR path,LPTSTR prefix,LPTSTR ext,LPTSTR filename)//ext = with .
{
	SYSTEMTIME systime;
	int len = (int) _tcslen(path);
	if(len > 0 && path[len - 1] == _T('\\'))
		path[len - 1] = NULL;
	do{
		GetLocalTime(&systime);
		int x = systime.wHour+systime.wMinute+systime.wMilliseconds*systime.wSecond;
		wsprintf(filename,_T("%s\\%s%04x%s"),path,prefix,x,ext);
	}while(isfileexists(filename));
}

float __stdcall stopwatch(bool end,LARGE_INTEGER *swStart)
{
	static LARGE_INTEGER swFreq;
	if (swFreq.LowPart==0 && swFreq.HighPart==0) 	
		QueryPerformanceFrequency(&swFreq);

	if (end == false){
		QueryPerformanceCounter(swStart);
		return 0.;
	}
	else {
		float etime; //elapsed time
		LARGE_INTEGER swStop;
		QueryPerformanceCounter(&swStop);
		if (swFreq.LowPart==0 && swFreq.HighPart==0) etime = -1;
		else {
			etime = (float)(swStop.LowPart - swStart->LowPart);
			if (etime < 0) etime += 2^32;
			etime /= (swFreq.LowPart+swFreq.HighPart * 2^32);
		}
		return etime;
	}
}

void __stdcall fileAttr2Str(DWORD attr,LPSTR szattr)
{
  if(attr & FILE_ATTRIBUTE_ARCHIVE)
    *szattr++ = _T('A');
  if(attr & FILE_ATTRIBUTE_DIRECTORY)
    *szattr++ = _T('D');
  if(attr & FILE_ATTRIBUTE_HIDDEN)
    *szattr++ = _T('H');
  if(attr & FILE_ATTRIBUTE_READONLY)
    *szattr++ = _T('R');
  if(attr & FILE_ATTRIBUTE_SYSTEM)
    *szattr++ = _T('S');
  *szattr = NULL;
}

//주의 : VB와 같은 EXE 모듈에서 호출시 클래스 생성 이전또는 호출자의 FORM 로드전 또는
//       여러번 호출시에 HMODULE 의 값이 호출자인 EXE 가 되거나 DLL이 되거나 변경된다.
//       따라서 EXE 측의 폼이나 다이얼로그 가 최초 생성된 완료 이후에서 한번 호출하면
//       DLL의 Home 디렉토리가 얻어진다.(이후에 계속 호출하면 순간 exe의 home 경로로 
//       가져오게 되기도 한다.VB IDE 에서는 VB6.EXE 경로로 가져오므로 INI등의 경로가 
//       달라지므로 주의 하라.

void GetHomeDirectory(char *szHomeDirectory)
{
	char szHome[_MAX_PATH]={0,};
	char sFilename0[_MAX_PATH]={0,};
	char sFilename[_MAX_PATH]={0,};
	char *lp =NULL;
	int len=0;
	HMODULE hModule;

	hModule = AfxGetInstanceHandle();//GetModuleHandle(NULL); => EXE's PATH
	GetModuleFileName(hModule, sFilename, _MAX_PATH);//>> DLL FILE PATH
	//CString msg,a;
	//msg = sFilename0;
	//msg += "\r\n";
	//msg += sFilename;
	//a.Format("\r\nhModule = %x hModule2 = %x",hModule,hModule2);
	//msg += a;
	//AfxMessageBox(msg);
	len= strlen(sFilename);
	memcpy(szHome,sFilename,len);
	
	lp=strrchr(szHome,'\\');
	if(lp!=NULL)
		*lp = NULL;

	len=strlen(szHome);
	if(szHome[len-1]!='\\')
	{
		szHome[len] ='\\';
		szHome[len+1] = NULL;
	}
	len=strlen(szHome);
	memcpy(szHomeDirectory,szHome,len);
	szHomeDirectory[len] = NULL;
}

// 0 for all side, -1 for beginning only, 1 for end only
void removeblank(LPSTR buff,int where)
{
	int len;
	CHAR *p,*q;
	CHAR *tmpbuff;
	len = (int)_tcslen(buff);
	tmpbuff = (CHAR *)LocalAlloc (LPTR,len+1);
	_tcscpy(tmpbuff,buff);

	p = tmpbuff;
	if(where == 0 || where == -1){
		while(*p && (*p == _T(' ')||*p == _T('\t')||*p == _T('\r')||*p == _T('\n')||*p == _T('\b')))
			++p;
	}

	if(where == 0 || where == 1){
		len = (int)_tcslen(p);
		q = p + max(0,len-1);
		while(q >= p && (*q == _T(' ')||*q == _T('\t')||*q == _T('\r')||*q == _T('\n')||*q == _T('\b')))
			*q-- = _T('\0');
	}
	_tcscpy(buff,p);
	LocalFree(tmpbuff);
}

CHAR skipcomments(FILE *f)
{
	CHAR c;
	while (c = fgetc(f)) {
		while (c == '\n' || c == '\r') {
			c = fgetc(f); // Skip empty lines
		}
		if (c == '#' || c == ';') {
			while (c = fgetc(f)) {
				if (c == '\n') break;
			}
		}
		else break;
	}
	return c;
}

void removecomment(LPSTR buff)
{
	int len;
	CHAR *pbgnstr,*nextch;
	len = (int)_tcslen(buff);
	if(len > 0)
	{
		buff[len] = NULL;
		pbgnstr = _tcsrchr(buff,_T('#')); /*뒤에 # 주석이 붙은경우 주석제외*/
		if(pbgnstr != NULL){
			if(pbgnstr == buff){
				*pbgnstr = NULL;
			}
			else{
				nextch = pbgnstr - 1;
				while(nextch > buff && (*nextch == ' ' || *nextch == '\t')) nextch --;
				if(*nextch == '\'' || *nextch == '\"')//ignore char wrapped inside string data
					return;
				*pbgnstr = NULL;
			}
		}
	}
}


int wildcmp(char *wild, char *string)
{
	char *cp, *mp;


	while ((*string) && (*wild != '*')) {


		if ((*wild != *string) && (*wild != '?')) {
			return 0;
		}
		wild++;
		string++;
	}


	while (*string) {


		if (*wild == '*') {


			if (!*++wild) {
				return 1;
			}
			mp = wild;
			cp = string+1;


		} else if ((*wild == *string) || (*wild == '?')) {
			wild++;
			string++;


		} else {
			wild = mp;
			string = cp++;
		}
	}


	while (*wild == '*') {
		wild++;
	}
	return !*wild;
}

/*
바로 이전 또는 이후의 토큰을 구한다.
리턴값은 line 버퍼내의 토큰시작 포인터이다.토큰이름은 tokenname에 반환된다.
line = 수식 pos = line내 기준위치 forward = 0 (왼쪽으로),1(오른쪽으로) 나머지는 반환값
*/
CHAR * gettokenstr(CHAR *line,CHAR *delimiter,int pos,CHAR *token,int *tokenlen,int *boperator)
{
	char *start,*last;	
	CHAR *p = line + pos;
	int bop,bop2;	
	*boperator = 1;
	*tokenlen = 0;

	if(!p || p < line  || *line == NULL) return NULL;

	while(*p && (*p == ' ' || *p == '\t')) p++;
	start = last = p;	
	*boperator = bop = (strchr(delimiter,*p) != NULL);

//	if(*p =='\"'){
//		start = p++;
//
//		while(*p && (*p != '\"')) p++;
//		last = p;
//		*tokenlen = (int)(last - start) + 1 - 2;
//		_tcsncpy(token,start+1,*tokenlen);
//		*(token + *tokenlen) = NULL;
//		return last + 1;
//	} 

	if(bop)
		p++;
	else
		while(*p /*&& (*p != ' ' && *p != '\t')*/ && (bop == (bop2 = (strchr(delimiter,*p) != NULL)))) p++;

	last = p - 1;
	*tokenlen = (int)(last - start) + 1;
	_tcsncpy(token,start,*tokenlen);
	*(token + *tokenlen) = NULL;
	return last + 1;
}

BOOL PutFtpFile(char* szLocalFileName, char* szFtpFileName, char* url, int port, char* username, char* password)
{
	HINTERNET hINet = NULL;
	HINTERNET hConnection = NULL;
    BOOL res = FALSE;
	CHAR szAgent[80] = "MyFtpFile";
	int i;
	for (i=0; i<3 && !hINet; i++)
		hINet = InternetOpen(szAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
	
	if (!hINet) 
	{
		return FALSE;
	}

	for (i=0; i<3 && !hConnection; i++)
		hConnection = InternetConnect(hINet, url, port, username, password, INTERNET_SERVICE_FTP, 0, 0);
	
	if (!hConnection) 
	{
		InternetCloseHandle(hINet);	
		return FALSE;
	}
	//char buff[300];
	//wsprintf(buff,"file : %s,%s",szLocalFileName,szFtpFileName);
	//MessageBox(NULL,buff,"ftp",MB_OK);

	for (i=0; i<3 && !res; i++)
		res = ::FtpPutFile( hConnection, szLocalFileName, szFtpFileName, FTP_TRANSFER_TYPE_BINARY,0 );

	if(!res){
		CHAR szBuf[380]; 
		DWORD dw = GetLastError(); 
		wsprintf(szBuf, "failed: GetLastError returned %u,locfile = %s\r\n", dw,szLocalFileName); 
		//WriteLog(szBuf); 
	}
	InternetCloseHandle(hConnection);
	InternetCloseHandle(hINet);

	return res;/* TRUE = success */
}

int GetFtpFile(char* szLocalFileName, char* szFtpFileName, char* url, int port, char* username, char* password)
{
	HINTERNET hINet = NULL;
	HINTERNET hConnection = NULL;
    BOOL res = FALSE;
	CHAR szAgent[80] = "MyFtpFile";
	int i;
	int rc = 0;
	for (i=0; i<3 && !hINet; i++)
		hINet = InternetOpen(szAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
	
	if (!hINet) 
	{
		return 1;
	}

	for (i=0; i<3 && !hConnection; i++)
		hConnection = InternetConnect(hINet, url, port, username, password, INTERNET_SERVICE_FTP, 0, 0);
	
	if (!hConnection) 
	{
		InternetCloseHandle(hINet);	
		return 2;
	}
	//char buff[300];
	//wsprintf(buff,"file : %s,%s",szLocalFileName,szFtpFileName);
	//MessageBox(NULL,buff,"ftp",MB_OK);
	BOOL failExists = FALSE;
	DWORD dwAttrs = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_NORMAL;
	//DWORD_PTR dwContext = 0L;
	DWORD dwContext = 0L;
	CHAR szFile[MAX_PATH]={0,};
	strcpy(szFile,szLocalFileName);
	for (i=0; i<3 && !res; i++){
		res = ::FtpGetFile( hConnection,szFtpFileName, szFile,failExists,dwAttrs, FTP_TRANSFER_TYPE_BINARY,dwContext);
		if(res == FALSE){
			Sleep(500);
			DWORD dw = ::GetLastError();
			if(dw == ERROR_SHARING_VIOLATION){//The process cannot access the file because it is being used by another process.
				strcpy(szFile,szLocalFileName);
				wsprintf(szFile,"%s.%03d",szLocalFileName,i);
			}
		}
	}
	strcpy(szLocalFileName,szFile);
	if(!res){
		CHAR szBuf[380]; 
		DWORD dw = ::GetLastError(); 
		DWORD dwError;
		wsprintf(szBuf, "failed: GetLastError returned %u,locfile = %s\r\n", dw,szLocalFileName); 
		TRACE(szBuf);
		CHAR szBuffer[1024];
		DWORD BuffLen = sizeof(szBuffer)- 1;
		InternetGetLastResponseInfo(&dwError,szBuffer,&BuffLen);
		TRACE("%s\n",szBuffer);
		rc = dw;
		//WriteLog(szBuf); 
	}
	InternetCloseHandle(hConnection);
	InternetCloseHandle(hINet);

	return rc;/* TRUE = success */
}

#pragma warning (pop)
TAG
Posted by 3LegsCrow::하늘날다 두루물

댓글을 달아 주세요