www.pudn.com > MINSortDict.zip > MinidxGUICom.cpp


//************************************************************************* 
// You are free to use/modify this code but leave this header intact. 
// This class is public domain so you are free to use it any of 
// your applications (Freeware,Shareware,Commercial). All I ask is 
// that you let me know so that if you have a real winner I can 
// brag to my buddies that some of my code is in your app. I also 
// wouldn't mind if you sent me a copy of your application since I 
// like to play with new stuff. 
// Author	: DingZhiGang 
// Email	: minidxer@gmail.com 
// HomePage : http://minidx.com 
// Help     : http://minidx.com/forum/ 
//************************************************************************* 
 
#include "StdAfx.h" 
#include "MinidxGUICom.h" 
//#include "stdint.h" 
 
#define DEF_EXT_CODE_DOC  4 
#define DEF_EXT_CODE_XLS  5 
#define DEF_EXT_CODE_PPT  6 
#define DEF_EXT_CODE_PDF  7 
#define DEF_EXT_CODE_RTF  8 
#define DEF_EXT_CODE_ZIP  9 
#define DEF_EXT_CODE_RAR  10 
#define DEF_EXT_CODE_CPP  11 
#define DEF_EXT_CODE_H    12 
#define DEF_EXT_CODE_HPP  13 
#define DEF_EXT_CODE_CXX  14 
#define DEF_EXT_CODE_C	  15  // ---- 
#define DEF_EXT_CODE_CC	  16  // ---- 
#define DEF_EXT_CODE_CS	  17  // ---- 
#define DEF_EXT_CODE_HH	  18  // ---- 
#define DEF_EXT_CODE_ID	  19  // ---- 
#define DEF_EXT_CODE_JS	  20  // ---- 
#define DEF_EXT_CODE_OD	  21  // ---- 
#define DEF_EXT_CODE_PY	  22  // ---- 
#define DEF_EXT_CODE_RC	  23  // ---- 
 
#define DEF_EXT_CODE_DEF  24 
#define DEF_EXT_CODE_DSW  25 
#define DEF_EXT_CODE_DSP  26 
#define DEF_EXT_CODE_CSS  27 
#define DEF_EXT_CODE_INI  28 
#define DEF_EXT_CODE_JAVA 29 
#define DEF_EXT_CODE_PL   30 
#define DEF_EXT_CODE_REG  31 
#define DEF_EXT_CODE_BAS  32 
#define DEF_EXT_CODE_FOR  33 
#define DEF_EXT_CODE_F77  34 
#define DEF_EXT_CODE_F90  35 
#define DEF_EXT_CODE_PAS  36 
#define DEF_EXT_CODE_ASM  37 
#define DEF_EXT_CODE_MAK  38 
#define DEF_EXT_CODE_AWK  39 
#define DEF_EXT_CODE_SED  40 
#define DEF_EXT_CODE_PHP  41 
 
CMinidxGUICom::CMinidxGUICom(void) 
{ 
} 
 
CMinidxGUICom::~CMinidxGUICom(void) 
{ 
} 
 
 
// 「'」→「''」 
char* CMinidxGUICom::st_replace(char *str) 
{ 
	if(str == NULL) return NULL; 
	int len = (int)strlen(str); 
	char *tmp = new char[2*len + 1]; 
 
	int num = 0; 
 
	for( int i=0; i 0; --i) 
		{ 
			utf[i] = (char)((c & 0x3f) | 0x80); 
			c >>= 6; 
		} 
		utf[0] = c | first; 
	} 
 
	return len; 
} 
 
size_t CMinidxGUICom::wcstoutf8(char * utf,size_t ulen, const wchar_t * wcs, size_t wlen) 
{ 
	char *p = utf; 
	size_t i = 0; 
	if(p) 
	{				 
		while( (p < utf + ulen) && (i < wlen) ) 
			p += wctoutf8(p,wcs[i++]);				 
		return p - utf; 
	} 
	else  
	{ 
		size_t len = 0; 
		while( i < wlen) 
			len += wctoutf8(NULL,wcs[i++]); 
		return len; 
	}		 
} 
 
#define UTF8_LENGTH(Char)						\ 
	((Char) < 0x80 ? 1 :						\ 
	((Char) < 0x800 ? 2 :						\ 
	((Char) < 0x10000 ? 3 :						\ 
	((Char) < 0x200000 ? 4 :					\ 
	((Char) < 0x4000000 ? 5 : 6))))) 
 
#define UTF8_GET(Result, Chars, Count, Mask, Len)	\ 
	(Result) = (Chars)[0] & (Mask);					\ 
	for ((Count) = 1; (Count) < (Len); ++(Count))	\ 
		{											\ 
			if (((Chars)[(Count)] & 0xc0) != 0x80)	\ 
			{										\ 
				(Result) = -1;						\ 
				break;								\ 
			}										\ 
			(Result) <<= 6;							\ 
			(Result) |= ((Chars)[(Count)] & 0x3f);	\ 
		} 
 
size_t CMinidxGUICom::utf8towc(wchar_t *wc, const char *utf, size_t ulen) 
{ 
	int i, mask = 0; 
	int result; 
	unsigned char c = (unsigned char) *utf; 
	size_t len = 0; 
 
	if (c < 128) 
	{ 
		len = 1; 
		mask = 0x7f; 
	}				 
	else if ((c & 0xe0) == 0xc0) 
	{ 
		len = 2; 
		mask = 0x1f; 
	} 
	else if ((c & 0xf0) == 0xe0) 
	{	 
		len = 3; 
		mask = 0x0f; 
	} 
	else if ((c & 0xf8) == 0xf0)	 
	{	 
		len = 4; 
		mask = 0x07; 
	} 
	else if ((c & 0xfc) == 0xf8) 
	{ 
		len = 5; 
		mask = 0x03; 
	} 
	else if ((c & 0xfe) == 0xfc) 
	{ 
		len = 6; 
		mask = 0x01; 
	}				 
	else 
		return 0; 
	if(ulen < len) 
		return 0; 
 
	UTF8_GET (result, utf,i, mask, (int)len); 
 
	if(wc) 
		*wc = (wchar_t)result; 
	return len; 
} 
 
size_t CMinidxGUICom::utf8towcs(wchar_t * wcs,size_t wlen,const char * utf, size_t ulen) 
{ 
	char *sp = (char*)utf; 
	wchar_t *rp = wcs; 
	size_t i = 0; 
	size_t len = 0; 
 
	while ( ( (!rp) || (rp < wcs + wlen) ) && (sp < utf + ulen) ) 
	{ 
		size_t r = utf8towc(rp,sp,6); 
		if ( r == -1 ) 
			return 0; 
		sp += r; 
		if(rp) 
			rp++; 
		len ++; 
	}			 
	return len; 
} 
 
// 2007/06/07 ADD 
size_t CMinidxGUICom::utf8towcs_tooltips(wchar_t * wcs,size_t wlen,const char * utf, size_t ulen, size_t ret_len) 
{ 
	// get size check 
	if( ret_len == 0 ) return 0; 
 
	char *sp = (char*)utf; 
	wchar_t *rp = wcs; 
	size_t i = 0; 
	size_t len = 0; 
 
	int isBreak = 0; 
	while ( ( (!rp) || (rp < wcs + wlen) ) && (sp < utf + ulen) ) 
	{ 
		size_t r = utf8towc(rp,sp,6); 
		if ( r == -1 ) 
			return 0; 
 
		sp += r; 
		// skip space 
		if( (*rp == ' ' || *rp == ' ') && !isBreak ) continue; 
		 
		if(rp) 
			rp++; 
		len ++; 
		isBreak = 1; 
		if( len >= ret_len ) break; 
	}			 
	return len; 
} 
 
int CMinidxGUICom::get_int_length(int n) { 
	int i; 
 
	for (i = 0; n; n /= 10, ++i); 
 
	return i; 
} 
 
// CString → TCHAR * 変換 
TCHAR* CMinidxGUICom::CString2TCHAR(CString &str) 
{ 
	int iLen = str.GetLength(); 
	TCHAR* szRs = new TCHAR[iLen + 1]; 
	lstrcpy(szRs, str.GetBuffer(iLen)); 
	szRs[iLen] = 0; 
	str.ReleaseBuffer(); 
	return szRs; 
}  
 
#define _LEN(x)                ((::_tstrlen (x) + 1) * sizeof (TCHAR)) 
////// 
//////// remove directory and files 
//////bool CMinidxGUICom::DeleteDirectoryEx(const TCHAR* p) 
//////{ 
//////    if(p != NULL && _LEN(p) < 4)return false; 
//////    int len=_LEN(p); 
//////	TCHAR *path = new TCHAR[len + _LEN(_T("\\*.*")) + 1); 
//////	path[0] = 0; 
//////    //if(p[len-1]!='\\') Dir=Dir+'\\'; 
//////    //AnsiString Files=Dir+_T("*.*"); 
//////	::_tstrcat (path, p); 
//////	if(p[len-1]!='\\')  
//////	{ 
//////		::_tstrcat (path, _T("\\")); 
//////	} 
//////	::_tstrcat (path, _T("*.*")); 
////// 
//////    WIN32_FIND_DATA wfd; 
//////    HANDLE hFind=FindFirstFile(path,&wfd); 
//////    bool Ret=true; 
//////    TCHAR *Tmp; 
//////    if(hFind!=INVALID_HANDLE_VALUE) 
//////    { 
//////        bool bFind=true; 
//////        while(bFind) 
//////        { 
//////            if(wfd.cFileName[0]!='.')  
//////            { 
//////                Tmp=Dir+wfd.cFileName; 
//////                if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
//////                { // delete all sub directory 
//////                    Ret=Ret&&DeleteDirectoryEx(Tmp.c_str(),false); 
//////                }else 
//////                { // remove file 
//////                    SetFileAttributes(Tmp.c_str(),FILE_ATTRIBUTE_NORMAL); 
//////                    Ret=Ret&&DeleteFile(Tmp.c_str()); 
//////                } 
//////            } 
//////            bFind=FindNextFile(hFind,&wfd); 
//////        } 
//////        FindClose(hFind); 
//////    } 
//////    if(Ret)return RemoveDirectory(Path); 
//////    return false; 
//////}  
 
 
// ディレクトリ削除 
// MD = ONLEFILE, ALLDIRFELE 
bool CMinidxGUICom::removeDirFile(CString sPathfile, modes MD ) 
{     
	CString   s1,s2;     
	HANDLE   h   =   NULL; 
	WIN32_FIND_DATA   data;     
 
	(sPathfile[sPathfile.GetLength()]=='\\') ? s1=sPathfile:s1=sPathfile+_T("\\");     
	s2 = s1 + _T("*.*");     
	if((h=FindFirstFile(s2.GetBuffer(), &data))!=0)     
	do     
	{     
		if( (CString(data.cFileName)==_T(".")) || (CString(data.cFileName)==_T("..")) )     
			continue;     
		if ((data.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY) && (MD!=ONLEFILE))   // delete dir 
			removeDirFile(s1+CString(data.cFileName),   MD);     
		s2 = s1+CString(data.cFileName);   // get file name     
		DeleteFile(s2.GetBuffer());   // delete file     
	}while (FindNextFile(h,&data)); 
 
	FindClose(h);     
	if (MD!=ONLEFILE)     
		RemoveDirectory(s1.GetBuffer());   // delete dir   
	return   TRUE;     
}   
 
//source:  
//dest: 
//division:split char 
void CMinidxGUICom::decodeCString(CString source, CStringArray& dest, char division) 
{ 
	// 初期化 
	dest.RemoveAll(); 
	for(int i=0;i