bool CFileHelper::createDirs(const char* szDir) { if (szDir == NULL) { ScutLog("createDirs Error %s %d", szDir, __LINE__); return false; } const char *pBegin = szDir; const char *pend = szDir; do { char szBuf[MAX_PATH]; pend = strchr(pend,FILE_SEP); if (NULL != pend) { ++pend; int nSize = pend-pBegin; memcpy(szBuf,pBegin,nSize); szBuf[nSize] = '\0'; std::string dir; dir = szBuf; if (!isDirExists(dir.c_str())) { if(!createDir(dir.c_str())) { ScutLog("createDirs Error %s %d", szDir, __LINE__); break; } } } } while (NULL != pend); return true; }
int ZipUtils::ccInflateMemory(unsigned char *in, unsigned int inLength, unsigned char **out) { unsigned int outLength = 0; int err = inflateMemory_(in, inLength, out, &outLength); if (err != Z_OK || *out == NULL) { if (err == Z_MEM_ERROR) { ScutLog("ScutDataLogic: ZipUtils: Out of memory while decompressing map data!"); } else if (err == Z_VERSION_ERROR) { ScutLog("ScutDataLogic: ZipUtils: Incompatible zlib version!\r\n"); } else if (err == Z_DATA_ERROR) { ScutLog("ScutDataLogic: ZipUtils: Incorrect zlib compressed data!\r\n"); } else { ScutLog("ScutDataLogic: ZipUtils: Unknown error while decompressing map data!\r\n"); } delete[] *out; *out = NULL; outLength = 0; } return outLength; }
const char* CScutLuaLan::Get(const char *group, const char *key) { if (m_curLan.empty()) { ScutLog("current language is empty"); return ""; } LANINFO_MAP::iterator it = m_mapLanInfo.find(m_curLan); if (it == m_mapLanInfo.end()) { ScutLog("cann't find language %s", m_curLan.c_str()); return ""; } assert(it->second != NULL); if (NULL == it->second) { return ""; } if (NULL == it->second->pIni) { load_ini(m_curLan.c_str()); } return it->second->pIni->Get(group, key, ""); }
//如果NETWORK 传入的包大小与包前两个字节的值不一样,返回FALSE。 bool CNetStreamExport::pushNetStream(char* pdataStream,int wSize) { m_pDataStream = pdataStream; m_nSize = wSize; m_nStreamPos = 0; ////特殊编译给陈波 //return true; ////end //m_nRecoredSize = 0; //m_nRecoredReadSize = 0; assert(m_RecordStack.empty()); while(!m_RecordStack.empty()) { RECORDINFO* pInfo = m_RecordStack.top(); delete pInfo; m_RecordStack.pop(); } int nStreamSize = getInt(); if(nStreamSize != wSize) { ScutLog(" Failed: CNetStreamExport: pushNetStream , wSize error "); return false; } //跳过包大小 m_pDataStream = pdataStream + 4; m_nStreamPos = 0; //m_nRecoredReadSize = 0; return true; }
//在LUA中分配字符内存,传到C中设置内容 bool CNetStreamExport::getString(char* psz, int nLength) { if(!psz) return 0; if(!m_pDataStream) return 0; if(m_nStreamPos + nLength > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getString"); return 0; } char ch; int i; for ( i= 0; i < nLength; i = i+1) { ch = *( m_pDataStream + m_nStreamPos + i ); *(psz + i) = ch; } *(psz + i) = '\0'; m_nStreamPos = m_nStreamPos + nLength; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += nLength; } //m_nRecoredReadSize += nLength; return 1; }
//备用接口,在C中分配内存 const char* CNetStreamExport::getString(/*const char* pData,*/int nLength) { if(m_nStreamPos + nLength > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getString"); return NULL; } m_szRet = (char*)malloc(nLength+1); if (m_szRet == NULL) { return NULL; } memcpy(m_szRet, m_pDataStream + m_nStreamPos, nLength); m_szRet[nLength] = '\0'; m_nStreamPos = m_nStreamPos + nLength; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += nLength; } return m_szRet; }
bool CScutLuaLan::Add(const char* pszLan, const char* pszPath) { std::string path = LANPATH_DIR; path += FILE_SEP; path += pszPath; path = ScutDataLogic::CFileHelper::getWritablePath(path.c_str()); if (!ScutSystem::CPathUtility::IsFileExists(path.c_str())) { std::string dir = CPlatformProcedure::ProcessDir(LANPATH_DIR, pszPath); if (!dir.empty()) { if (ScutSystem::CPathUtility::IsFileExists(dir.c_str())) { path = dir; }else { ScutLog("can not open file: %s", dir.c_str()); return false; } }else { ScutLog("can not open file: %s", path.c_str()); return false; } } LANINFO_MAP::iterator itfind = m_mapLanInfo.find(std::string(pszLan)); bool bSame = false; if (itfind != m_mapLanInfo.end()) { bSame = (itfind->second->strName == path); if (!bSame) { Remove(pszLan); } } if (!bSame) { add_node(pszLan, path.c_str()); } return true; }
int CIniFile::Load(const char *filename) { // open the model file FILE *fp = fopen(filename, "rb"); //FILE *fp = fopen(filename, "r"); if (fp == NULL) { ScutLog("error cannot load ini file %s\n", filename); return 1; } // if we already have a file loaded, free it first if (Valid()) { FreeAllTheStuff(); } //------------------->lrb //char str[256], section[256]; char str[300], section[256]; //<-------------------lrb section[0] = '\0'; while (fgets(str, 300, fp)) {//while (fgets(str, 256, fp)) { trim(str); // trim all the blanks or linefeeds // skip all comment lines or empty lines if (!str[0] || str[0] == ';' || str[0] == '/' || str[0] == '#') continue; int length = strlen(str); char *p; //-------------->lrb //<--------------lrb // check if this is a session line (e.g., [SECTION]) if (str[0] == '[' && str[length - 1] == ']') { strcpy(section, &str[1]); section[length - 2] = 0; // remove the ] trim(section); // trim section name after removing [] } else if ((p = strchr(str, '=')) != NULL) { *(p++) = '\0'; trim(str); // /<--------------------lrb // while((rt = strchr(p,'\\')) != NULL ) // { // *rt='\n'; // } // -------------------->lrb trim(p); Set(section, str, p); } } fclose(fp); // load completed; close the file return 0; }
bool CLuaIni::Load(const char *filename) { if (filename == NULL) { return false; } if (m_pIni->Load(initSavePath(filename).c_str()) != 0) { ScutLog("Load Ini file Error %s\r\n", initSavePath(filename).c_str()); return false; } return true; }
bool CLuaIni::APLoad(const char *filename) { if (filename == NULL) { return false; } if (m_pIni->Load(filename) != 0) { ScutLog("Load Ini file Error %s\r\n", filename); return false; } return true; }
bool CLuaIni::Save(const char *filename) { if (filename == NULL) { return false; } if (m_pIni->Save(initSavePath(filename).c_str()) != 0) { ScutLog("Save Ini file Error %s", initSavePath(filename).c_str()); return false; } return true; }
unsigned long long CNetStreamExport::getInt64() { if(m_nStreamPos + 8 > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getInt64"); return 0; } unsigned long long value; memcpy(&value, m_pDataStream + m_nStreamPos, sizeof(unsigned long long)); m_nStreamPos = m_nStreamPos + 8; //m_nRecoredReadSize += 8; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += 8; } return value; }
int CNetStreamExport::getInt(void) { if(m_nStreamPos + 4 > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getInt"); return 0; } //int dw = *( (int*)(m_pDataStream + m_nStreamPos) ); int dw; memcpy(&dw, m_pDataStream + m_nStreamPos, sizeof(int)); m_nStreamPos=m_nStreamPos+4; //m_nRecoredReadSize +=4; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += 4; } return dw; }
char CNetStreamExport::getCHAR(void) { if(m_nStreamPos + 1> m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getCHAR "); return 0; } //char by = *( (char*)(m_pDataStream + m_nStreamPos) ); char by; memcpy(&by, m_pDataStream + m_nStreamPos, sizeof(char)); m_nStreamPos=m_nStreamPos+1; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += 1; } return by; }
short CNetStreamExport::getSHORT(void) { if(m_nStreamPos + 2 > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getSHORT "); return 0; } //short w = *( (short*)(m_pDataStream + m_nStreamPos) ); short w; memcpy(&w, m_pDataStream + m_nStreamPos, sizeof(short)); m_nStreamPos=m_nStreamPos+2; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += 2; } return w; }
unsigned char* CFileHelper::getFileData(const char* pszFileName, const char* pszMode, unsigned long *pSize) { unsigned char * pBuffer = NULL; *pSize = 0; #ifdef SCUT_ANDROID do { // read the file from hardware FILE *fp = fopen(pszFileName, pszMode); if(!fp)break; fseek(fp,0,SEEK_END); *pSize = ftell(fp); fseek(fp,0,SEEK_SET); pBuffer = new unsigned char[*pSize]; *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); if (pBuffer == NULL) { ScutLog("getFileData Error fileName=%s", pszFileName); } #else do { // read the file from hardware FILE *fp = fopen(pszFileName, pszMode); if(!fp)break; fseek(fp,0,SEEK_END); *pSize = ftell(fp); fseek(fp,0,SEEK_SET); pBuffer = new unsigned char[*pSize]; *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); #endif return pBuffer; }
double CNetStreamExport::getDouble(void) { if(m_nStreamPos + 8 > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getDouble"); return 0; } //double dw = *( (double*)(m_pDataStream + m_nStreamPos) ); double dw; memcpy(&dw, m_pDataStream + m_nStreamPos, sizeof(double)); m_nStreamPos = m_nStreamPos+8; //m_nRecoredReadSize +=8; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += 8; } return dw; }
float CNetStreamExport::getFloat(void) { if(m_nStreamPos + 4 > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: getFloat"); return 0; } //float f = *( (float*)(m_pDataStream + m_nStreamPos) ); float f; memcpy(&f, m_pDataStream + m_nStreamPos, sizeof(float)); m_nStreamPos=m_nStreamPos+4; //m_nRecoredReadSize +=4; if (m_RecordStack.size()) { RECORDINFO* pInfo = m_RecordStack.top(); pInfo->nRecordReadSize += 4; } return f; }
bool CNetStreamExport::recordBegin() { if(m_nStreamPos + 4 > m_nSize ) { ScutLog(" Failed: 长度越界 CNetStreamExport: recordBegin "); return false; } //m_nRecoredReadSize = 0; int nRecoredSize = getInt(); RECORDINFO *pinfo = new RECORDINFO(nRecoredSize, 4); m_RecordStack.push(pinfo); if(nRecoredSize > 4) return true; else return false; }
int ZipUtils::inflateMemory_(unsigned char *in, unsigned int inLength, unsigned char **out, unsigned int *outLength) { /* ret value */ int err = Z_OK; /* 256k initial decompress buffer */ int bufferSize = 256 * 1024; if (inLength * 8 > 256 * 1024) { bufferSize = 8 * inLength; } *out = new unsigned char[bufferSize]; z_stream d_stream; /* decompression stream */ d_stream.zalloc = (alloc_func)0; d_stream.zfree = (free_func)0; d_stream.opaque = (voidpf)0; d_stream.next_in = in; d_stream.avail_in = inLength; d_stream.next_out = *out; d_stream.avail_out = bufferSize; /* window size to hold 256k */ if( (err = inflateInit2(&d_stream, 15 + 32)) != Z_OK ) { return err; } for (;;) { err = inflate(&d_stream, Z_NO_FLUSH); if (err == Z_STREAM_END) { break; } switch (err) { case Z_NEED_DICT: err = Z_DATA_ERROR; case Z_DATA_ERROR: case Z_MEM_ERROR: inflateEnd(&d_stream); return err; } // not enough memory ? if (err != Z_STREAM_END) { delete [] *out; *out = new unsigned char[bufferSize * BUFFER_INC_FACTOR]; /* not enough memory, ouch */ if (! *out ) { ScutLog("ScutDataLogic: ZipUtils: realloc failed"); inflateEnd(&d_stream); return Z_MEM_ERROR; } d_stream.next_out = *out + bufferSize; d_stream.avail_out = bufferSize; bufferSize *= BUFFER_INC_FACTOR; } } *outLength = bufferSize - d_stream.avail_out; err = inflateEnd(&d_stream); return err; }
int ZipUtils::ccInflateGZipFile(const char *path, unsigned char **out) { int len; unsigned int offset = 0; assert( out ); assert( &*out ); gzFile inFile = gzopen(path, "rb"); if( inFile == NULL ) { ScutLog("ScutDataLogic: ZipUtils: error open gzip file: %s", path); return -1; } /* 512k initial decompress buffer */ unsigned int bufferSize = 512 * 1024; unsigned int totalBufferSize = bufferSize; *out = (unsigned char*)malloc( bufferSize ); if( ! out ) { ScutLog("ScutDataLogic: ZipUtils: out of memory"); return -1; } for (;;) { len = gzread(inFile, *out + offset, bufferSize); if (len < 0) { ScutLog("ScutDataLogic: ZipUtils: error in gzread"); free( *out ); *out = NULL; return -1; } if (len == 0) { break; } offset += len; // finish reading the file if( (unsigned int)len < bufferSize ) { break; } bufferSize *= BUFFER_INC_FACTOR; totalBufferSize += bufferSize; unsigned char *tmp = (unsigned char*)realloc(*out, totalBufferSize ); if( ! tmp ) { ScutLog("ScutDataLogic: ZipUtils: out of memory"); free( *out ); *out = NULL; return -1; } *out = tmp; } if (gzclose(inFile) != Z_OK) { ScutLog("ScutDataLogic: ZipUtils: gzclose failed"); } return offset; }
std::string CFileHelper::getPath(const char* szPath, bool bOnly2X) { if (szPath == NULL) { ScutLog("getPath Input parameter == Null"); return std::string(); } if (szPath[0] == '/') { return std::string(szPath); } #ifdef SCUT_ANDROID if (strstr(szPath, "resource/")) { return std::string(szPath); } #endif std::string strDirPath = ""; strDirPath += getResourceDir(szPath); // prevent getPath(getPath(imageName)); "icon/head.png" will become "resource/resource/head.png" std::string strTempPath(szPath); if (strDirPath.length() > 0 && strTempPath.find(strDirPath) == 0) { strDirPath = ""; } if (strDirPath.size()) { strDirPath += FILE_SEP; float fScale = cocos2d::Director::getInstance()->getContentScaleFactor(); if (fScale > 1 || !(s_width == 480 && s_height == 320 || s_height == 320&& s_width == 480 || s_width < 480 && s_height <480)) { bool bTmx = false; const char* pPos = strstr(szPath, ".jpg"); if (pPos == NULL) //yubo image { pPos = strstr(szPath, ".ndj"); } if (pPos == NULL) { pPos = strstr(szPath, ".png"); } /* if (pPos == NULL) { pPos = strstr(szPath, ".pnx"); } */ if (pPos == NULL) //yubo image { pPos = strstr(szPath, ".ndp"); } if (pPos == NULL) { pPos = strstr(szPath, ".tmx"); bTmx = true; } if (pPos == NULL) { pPos = strstr(szPath, ".pvr"); bTmx = true; } if (pPos == NULL) { pPos = strstr(szPath, ".ccz"); bTmx = true; } if (pPos == NULL) { pPos = strstr(szPath, ".plist"); bTmx = true; } if(pPos == NULL) { pPos = strstr(szPath, ".dat"); } if (pPos != NULL) { char szTmp[256] = {0}; char szFileName[256] = {0}; memcpy(szFileName, szPath, pPos - szPath); if (bTmx && ((s_width == 1024 && s_height == 768) || (s_height == 1024&& s_width == 768))) { if (bOnly2X) { sprintf(szTmp, "%s@2x%s", szFileName, pPos); } else sprintf(szTmp, "%s@2x-ipad%s", szFileName, pPos); } #if (defined SCUT_MAC || defined SCUT_WIN32) else if (bTmx && ((s_width == 960 && s_height == 640) || (s_height == 960 && s_width == 640))) { if (bOnly2X) { sprintf(szTmp, "%s@2x%s", szFileName, pPos); } else sprintf(szTmp, "%s@2x-ipad%s", szFileName, pPos); } #endif else { sprintf(szTmp, "%s@2x%s", szFileName, pPos); } strDirPath += szTmp; } else { strDirPath += szPath; } } else { strDirPath += szPath; } } else { strDirPath = szPath; } #if defined(SCUT_IPHONE) || defined(SCUT_MAC) if (s_strIPhoneBundleID.size() == 0) { s_strIPhoneBundleID = ScutDataLogic::getBundleID(); } std::string strRelativePath = s_strIPhoneBundleID; strRelativePath += FILE_SEP; strRelativePath += strDirPath; std::string strFilePath = ScutDataLogic::getDocumentFilePathByFileName(strRelativePath.c_str()); struct stat st; if (true && stat(strFilePath.c_str(), &st) != 0)//cocos2d::CCImage::getIsScaleEnabled() { if (strFilePath.rfind("@2x") == std::string::npos) { strTempPath = strFilePath; int t = strTempPath.rfind("."); if (t != std::string::npos) { strTempPath.insert(t, "@2x"); if (stat(strTempPath.c_str(), &st) == 0) { return strFilePath; } } } // if (strDirPath.rfind("@2x") == std::string::npos) // { // int t = strDirPath.rfind("."); // if (t != std::string::npos) // { // strDirPath.insert(t, "@2x"); // } // } } if (stat(strFilePath.c_str(), &st) == 0) { return strFilePath; } else { if (strFilePath.rfind(".png") != std::string::npos) { strTempPath = strFilePath; string_replace(strTempPath, ".png", ".pnx"); if (stat(strTempPath.c_str(), &st) == 0) { return strTempPath; } } std::string retPath = std::string(appFullPathFromRelativePath(strDirPath.c_str())); if (retPath == strDirPath) { strTempPath = strDirPath; string_replace(strTempPath, ".png", ".pnx"); retPath = std::string(appFullPathFromRelativePath(strTempPath.c_str())); if (retPath == strTempPath) { return strDirPath; } else return retPath; } return retPath; } #endif #ifdef SCUT_ANDROID if (s_strAndroidSDPath.size()) { std::string strFilePath = s_strAndroidSDPath + strDirPath; struct stat st; if (true && stat(strFilePath.c_str(), &st) != 0) { if (strFilePath.rfind("@2x") == std::string::npos) { strTempPath = strFilePath; int t = strTempPath.rfind("."); if (t != std::string::npos) { strTempPath.insert(t, "@2x"); if (stat(strTempPath.c_str(), &st) == 0) { return strFilePath; } } } } if (stat(strFilePath.c_str(), &st) == 0) { return strFilePath; } else { if (strFilePath.rfind(".png") != std::string::npos) { strTempPath = strFilePath; string_replace(strTempPath, ".png", ".pnx"); if (stat(strTempPath.c_str(), &st) == 0) { return strTempPath; } } } } return std::string(strDirPath); #endif #ifdef SCUT_WIN32 std::string ret= ScutExt::getResRootDir(); ret += strDirPath; struct stat st; if (ret.rfind(".png") != std::string::npos && stat(ret.c_str(), &st) != 0) { strTempPath = ret; string_replace(strTempPath, ".png", ".pnx"); if (stat(strTempPath.c_str(), &st) == 0) { return strTempPath; } } return ret; #endif }