收集的一些代碼

// 得到應用程序路徑 
void cutFileName(wchar_t *lpszPath)
{
	wchar_t *lpszEnd = lpszPath;
	int charset = 0  ;
	while(*lpszPath != '\0') 
	{
		if((*lpszPath == '\\') || (*lpszPath == '/') || (*lpszPath == ':')) 
		{
			lpszEnd=lpszPath+1;
		}
		lpszPath++;
	}
	if(lpszEnd == lpszPath) 
	{
		return;
	}

	*lpszEnd='\0';
	return;
}


int GetAppPath(wchar_t *App_Path)
{
	GetModuleFileNameW(NULL, App_Path, MAX_PATH);
	cutFileName(App_Path);
	return 1;
}
 //16進制字符串轉換成INT類型整形:"FFFF" -> 65535
int HexToInt(const char *str, unsigned int *ival )
{
	u_long u;
	const char *cp;
	cp = str;
	if (*cp == '\0')
		return 0;
	u = 0;
	while (*cp != '\0') 
	{
		if (u >= 0x10000000)
			return 0;   /* overflow */
		u <<= 4;
		if (*cp <= '9')         /* very ascii dependent */
			u += *cp++ - '0';
		else if (*cp >= 'a')
			u += *cp++ - 'a' + 10;
		else
			u += *cp++ - 'A' + 10;
	}
	*ival = u;
	return 1;
}
char *strstr(char *buf, char *sub)     
{    
	char *bp = NULL;    
	char *sp = NULL;    
	if (!*sub)      
		return buf;   
	while (*buf)   
	{        
		bp = buf;        
		sp = sub;    
		while(true)
		{
			if (!*sp)   
				return buf;        
			if (tolower(*bp) != tolower(*sp))
			{
				break;
			}
			bp++;
			sp++;
		}
		buf += 1;    
	}    
	return 0;
}
void Log(const char *format, ...)
{
        va_list parameters;
	TCHAR szFileName[MAX_PATH] = {0};
	char buf[1024];
	FILE *fp;
	if(*szFileName == '\0')
	{
		GetAppPath(szFileName);
		_tcscat(szFileName,_T("debug.log"));
	}

	fp = _tfopen(szFileName, _T("a+"));
	if (fp != NULL)
	{
		va_start(parameters, format);
		vsprintf(buf, format, parameters);	
		fputs(buf, fp);
		va_end(parameters);
		fclose(fp);
	}
}
//.h
#ifdef    UNICODE
    #pragma comment(linker,"/ENTRY:wWinMainCRTStartup")
#endif

-------------------------------------分割線-------------------------------------
static char HEXC[] = "0123456789ABCDEF";
#define ISCHINESE(c)  *(c)&0x80
//需要轉義的字符 (除 '0'-'9' 'A'-'Z' 'a'-'z' 外) 
#define IsEscapeChar(c) ((((c)<'0' ) || ( (c)>'9'&& *ptmp<'A') ||  ((c)>'z') || ((c)>'Z' && (c)<'a')))

void URLEnGBKCode(char *szsrc,char *szdst)
{
	int len=0;
	unsigned char *ptmp=(unsigned char *)szsrc;
	int index=0;
	if (szsrc==NULL)
	{
		return;
	}
	len=strlen(szsrc);
	while(len)
	{		
		if(IsEscapeChar(*ptmp))
		{
			szdst[index]='%';
			szdst[index+1]=HEXC[(*ptmp)>>4];
			szdst[index+2]=HEXC[(*ptmp)&0xF];
			index+=3;
			len--;
			ptmp++;
		}
		else
		{
			szdst[index]=*ptmp;
			index++;
			len--;
			ptmp++;
		}
	}
}

//GBK轉義解碼
void URLDeGBKCode(char *szsrc,char *szdst)
{
	int len=0;
	int nchar;
	char *ptmp=szsrc;
	int index=0;
	int lentmp=0;
	int ideHex;
	if (szsrc==NULL)
	{
		return;
	}
	len=strlen(szsrc);
	while(len)
	{	
		char c=*ptmp;
		if (c=='%' && len>2)
		{
			ideHex=(ptmp[1]-'0');
			nchar=(int)((ideHex)<9?(ideHex<<4):((ideHex-7)<<4));
			ideHex=(ptmp[2]-'0');
			nchar+=(int)((ideHex)<9?(ideHex):((ideHex-7)));
			szdst[index++]=(char)nchar;
			ptmp+=3;
			len-=3;
		}	
		else
		{
			szdst[index++]=*(ptmp++);
			len--;
		}
	}
}
time_t rep_getsystemtime(void)
{
struct tm stm = {0};
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_MAC)
	time_t ltime = 0;
	time(<ime);
	stm = *localtime(<ime);
	return rep_mktime(&stm);
#else	
	SYSTEMTIME st = {0};
	GetLocalTime(&st);
	stm.tm_year = (Gint32)(st.wYear-1900u);
	stm.tm_mon = (Gint32)(st.wMonth-1u);
	stm.tm_mday = (Gint32)st.wDay;
	stm.tm_hour = (Gint32)st.wHour;
	stm.tm_min = (Gint32)st.wMinute;
	stm.tm_sec = (Gint32)st.wSecond;
	stm.tm_wday = (Gint32)st.wDayOfWeek;
	return rep_mktime(&stm);
#endif
}
struct tm rep_gmtime(Guint32 time)
{
		struct tm sttm = {0};
		Guint32 caltim = time;
		int islpyr = 0;  
		int tmptim;
		struct tm *ptb = &sttm;
		int *mdays;      
	 /*
         * Determine years since 1970. First, identify the four-year interval
         * since this makes handling leap-years easy (note that 2000 IS a
         * leap year and 2100 is out-of-range).
         */
        tmptim = (int)(caltim / FOUR_YEAR_SEC);
        caltim -= ((Guint32)tmptim * FOUR_YEAR_SEC);

        /*
         * Determine which year of the interval
         */
        tmptim = (tmptim * 4) + 70;         /* 1970, 1974, 1978,...,etc. */

        if ( caltim >= YEAR_SEC ) {

            tmptim++;                       /* 1971, 1975, 1979,...,etc. */
            caltim -= YEAR_SEC;

            if ( caltim >= YEAR_SEC ) {

                tmptim++;                   /* 1972, 1976, 1980,...,etc. */
                caltim -= YEAR_SEC;

                /*
                 * Note, it takes 366 days-worth of seconds to get past a leap
                 * year.
                 */
                if ( caltim >= (YEAR_SEC + DAY_SEC) ) {

                        tmptim++;           /* 1973, 1977, 1981,...,etc. */
                        caltim -= (YEAR_SEC + DAY_SEC);
                }
                else 
				{
                        /*
                         * In a leap year after all, set the flag.
                         */
                        islpyr++;
                }
            }
        }

        /*
         * tmptim now holds the value for tm_year. caltim now holds the
         * number of elapsed seconds since the beginning of that year.
         */
        ptb->tm_year = tmptim;

        /*
         * Determine days since January 1 (0 - 365). This is the tm_yday value.
         * Leave caltim with number of elapsed seconds in that day.
         */
        ptb->tm_yday = (int)(caltim / DAY_SEC);
        caltim -= (long)(ptb->tm_yday) * DAY_SEC;

        /*
         * Determine months since January (0 - 11) and day of month (1 - 31)
         */
        if ( islpyr )
            mdays = _lpdays;
        else
            mdays = _days;


        for ( tmptim = 1 ; mdays[tmptim] < ptb->tm_yday ; tmptim++ ) ;

        ptb->tm_mon = --tmptim;

        ptb->tm_mday = ptb->tm_yday - mdays[tmptim];

        /*
         * Determine days since Sunday (0 - 6)
         */
        ptb->tm_wday = ((int)( time / DAY_SEC) + BASE_DOW) % 7;

        /*
         *  Determine hours since midnight (0 - 23), minutes after the hour
         *  (0 - 59), and seconds after the minute (0 - 59).
         */
        ptb->tm_hour = (int)(caltim / 3600);
        caltim -= (Guint32)ptb->tm_hour * 3600L;

        ptb->tm_min = (int)(caltim / 60);
        ptb->tm_sec = (int)(caltim - (ptb->tm_min) * 60);

        ptb->tm_isdst = 0;
         return sttm;

}
struct tm rep_gmtime2(time_t t)
{
	struct tm stm = {0};
 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_MAC)
 	stm = *gmtime(&t);
#else
	FILETIME pft = {0};
	SYSTEMTIME st = {0};
	Gint64 ll = Int32x32To64(t, 10000000) + 116444736000000000;
	pft.dwLowDateTime = (DWORD) ll;
	pft.dwHighDateTime = (DWORD)(ll >>32);
	FileTimeToSystemTime(&pft,&st); 
	stm.tm_year = (Gint32)st.wYear-1900;
	stm.tm_mon = (Gint32)st.wMonth-1;
	stm.tm_mday = (Gint32)st.wDay;
	stm.tm_hour =(Gint32) st.wHour;
	stm.tm_min = (Gint32)st.wMinute;
	stm.tm_sec = (Gint32)st.wSecond;
	stm.tm_wday = (Gint32)st.wDayOfWeek;
#endif
	return stm;
}




 

 

發佈了17 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章