#include "Common.h" #ifdef WIN32 #include <Windows.h> #else #include <stdlib.h> #include <locale.h> #endif #include "iconvstring/IconvString.h" #include "iconvstring/IconvStringEx.h" USING_NS_CC; namespace { int MonthWeekDay [10][12][1] = { // 2013 { {0}, {0}, {0}, {0}, {0}, {0}, {1}, {4}, {7}, {2}, {5}, {7} }, // 2014 { {3}, {6}, {6}, {2}, {4}, {7}, {2}, {5}, {1}, {3}, {6}, {2} }, // 2015 { {4}, {7}, {7}, {3}, {5}, {1}, {3}, {6}, {2}, {4}, {7}, {2} }, // 2016 { {5}, {1}, {2}, {5}, {7}, {3}, {5}, {1}, {4}, {6}, {2}, {4} }, // 2017 { {7}, {3}, {3}, {6}, {1}, {4}, {6}, {2}, {5}, {7}, {3}, {5} }, // 2018 { {1}, {4}, {4}, {7}, {2}, {5}, {7}, {3}, {6}, {1}, {4}, {6} }, // 2019 { {2}, {6}, {5}, {1}, {3}, {6}, {1}, {4}, {7}, {2}, {5}, {1} }, // 2020 { {1}, {6}, {7}, {3}, {5}, {1}, {3}, {6}, {2}, {4}, {7}, {2} }, // 2021 { {5}, {1}, {1}, {1}, {6}, {2}, {4}, {7}, {3}, {5}, {1}, {3} }, // 2022 { {6}, {2}, {2}, {5}, {7}, {3}, {5}, {1}, {4}, {6}, {2}, {4} } }; } ////////////////////////////////////////////////////////////////////////// // std::wstring s2ws(const std::string& s) { setlocale(LC_ALL, "chs"); const char* _Source = s.c_str(); size_t _Dsize = s.size() + 1; wchar_t *_Dest = new wchar_t[_Dsize]; wmemset(_Dest, 0, _Dsize); mbstowcs(_Dest,_Source,_Dsize); std::wstring result = _Dest; delete []_Dest; setlocale(LC_ALL, NULL); return result; } ////////////////////////////////////////////////////////////////////////// // bool SafeCopy(char * pTarget, const char * pSource, int nBufLen/* = 0 */) { try { if(pTarget) { pTarget[0] = 0; if(pSource) { if(nBufLen && (int)strlen(pSource) >= nBufLen) { strncpy(pTarget, pSource, nBufLen - 1); pTarget[nBufLen - 1] = ‘\0‘; return false; } strcpy(pTarget, pSource); } return true; } } catch(...) { CCLOG("SafeCopy Failed..."); } assert(!"SafeCopy()"); return false; } ////////////////////////////////////////////////////////////////////////// // static float nLine = 0.1; void OpenDebugDiglog(const char* pError) { return; if(!pError || strlen(pError) == 0) return; CCDirector* pDirector = CCDirector::sharedDirector(); if (!pDirector) return; CCScene* pScene = pDirector->getRunningScene(); if(!pScene) return; CCLayer* pDeBugLayer = CCLayer::create(); if (!pDeBugLayer) return; pScene->addChild(pDeBugLayer, 999999999); const CCSize& size = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF* pDeBugWord = CCLabelTTF::create(StrToUTF8(pError).c_str(), "微软雅黑", 24); pDeBugWord->setAnchorPoint(ccp(0.5, 0.5)); pDeBugWord->setColor(ccRED); pDeBugWord->setPosition(ccp(size.width * 0.5, size.height * nLine)); nLine = nLine + 0.05; pDeBugLayer->addChild(pDeBugWord); } ////////////////////////////////////////////////////////////////////////// // static int MultiByteToWideCharAlex(wchar_t* Dest, const char* Source, unsigned int nSize) { int n = 0; #ifdef WIN32 #else setlocale(LC_ALL, "chs");//设置代码页 n = mbstowcs(Dest, Source, nSize); setlocale(LC_ALL, NULL); #endif return n; } static int WideCharToMultiByteAlex(char* Dest, const wchar_t* Source, unsigned int nSize) { setlocale(LC_ALL, "chs");//设置代码页 int n = wcstombs(Dest, Source, nSize); setlocale(LC_ALL, NULL); return n; } ////////////////////////////////////////////////////////////////////////// // std::string StrToUTF8(const char* pczBuffer) { std::string strText(pczBuffer); #ifdef WIN32 if(!strText.empty()) { std::wstring pwszText = s2ws(strText); strText = WStrToUTF8(pwszText); } #endif return strText; } ////////////////////////////////////////////////////////////////////////// // std::string StrToUTF8Ex(const char* pczBuffer) { #ifdef WIN32 std::string strText(pczBuffer); if(!strText.empty()) { std::wstring pwszText = s2ws(strText); strText = WStrToUTF8(pwszText); } return strText; #else int nInLen = strlen(pczBuffer); size_t inlen = strlen(pczBuffer); size_t outlen = inlen << 1; char* outbuf = (char*)malloc(outlen); char* inbuf = new char[inlen + 1]; SafeCopy(inbuf, pczBuffer, inlen + 1); inbuf[inlen] = ‘\0‘; GBKToUTF8(inbuf, inlen, outbuf, outlen); std::string strText(outbuf); free(outbuf); delete[] pszBuffer; return strText; #endif } ////////////////////////////////////////////////////////////////////////// // std::string UTF8ToGBK(const char* pszBuffer) { if(!pszBuffer || strlen(pszBuffer) == 0) return NULL; size_t inlen = strlen(pszBuffer); size_t outlen = inlen << 1; char* outbuf = new char[outlen]; char* inbuf = new char[inlen + 1]; SafeCopy(inbuf, pszBuffer, inlen + 1); UTF8ToGBK(inbuf, inlen, outbuf, outlen); std::string strText(outbuf); delete[] outbuf; delete[] inbuf; return strText; } ////////////////////////////////////////////////////////////////////////// // int GetMonthDays(int nYear, int nMonth) { int nFlag = 0; if(nYear %4 ==0 && nYear % 100 !=0 || nYear % 400 == 0) nFlag = 1; //是闰年 switch(nMonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; break; case 2: { } if(nFlag == 1) return 29; else return 28; break; case 4: case 6: case 9: case 11: return 30; default: return -1; } } int GetMonthWeekDay(int nYear, int nMonth) { int nFirstParam = nYear - 13; int nSecondParam = nMonth - 1; return MonthWeekDay[nFirstParam][nSecondParam][0]; }
原文:http://www.cnblogs.com/newlist/p/3568601.html