bool UIImage::initWithContentsOfFile(const string &strPath, eImageFormat imageType) { bool bRet = false; FileData data; unsigned long nSize = 0; unsigned char* pBuffer = data.getFileData(strPath.c_str(), "rb", &nSize); if (pBuffer) { switch (imageType) { case kCCImageFormatPNG: // use libpng load image bRet = loadPngFromStream(pBuffer, nSize); break; case kCCImageFormatJPG: bRet = loadJpgFromStream(pBuffer, nSize); break; default: // unsupported image type bRet = false; break; } } return bRet; }
NSDictionary<std::string, NSObject*> *dictionaryWithContentsOfFile(const char *pFileName) { FileData data; unsigned long size = 0; char *pBuffer = (char *) data.getFileData(pFileName, "r", &size); if (! pBuffer) { return NULL; } /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION xmlSAXHandler saxHandler; memset( &saxHandler, 0, sizeof(saxHandler) ); // Using xmlSAXVersion( &saxHandler, 2 ) generate crash as it sets plenty of other pointers... saxHandler.initialized = XML_SAX2_MAGIC; // so we do this to force parsing as SAX2. saxHandler.startElement = &plist_startElement; saxHandler.endElement = &plist_endElement; saxHandler.characters = &plist_characters; int result = xmlSAXUserParseMemory( &saxHandler, this, pBuffer, size ); if ( result != 0 ) { return NULL; } /* * Cleanup function for the XML library. */ xmlCleanupParser(); /* * this is to debug memory for regression tests */ xmlMemoryDump(); return m_pRootDict; }
// this is the function to call when we want to load an image tImageTGA * tgaLoad(const char *pszFilename) { int mode,total; tImageTGA *info = NULL; FileData data; unsigned long nSize = 0; unsigned char* pBuffer = data.getFileData(pszFilename, "rb", &nSize); do { CCX_BREAK_IF(! pBuffer); info = (tImageTGA *)malloc(sizeof(tImageTGA)); // get the file header info if (! tgaLoadHeader(pBuffer, nSize, info)) { info->status = TGA_ERROR_MEMORY; break; } // check if the image is color indexed if (info->type == 1) { info->status = TGA_ERROR_INDEXED_COLOR; break; } // check for other types (compressed images) if ((info->type != 2) && (info->type !=3) && (info->type !=10) ) { info->status = TGA_ERROR_COMPRESSED_FILE; break; } // mode equals the number of image components mode = info->pixelDepth / 8; // total is the number of unsigned chars to read total = info->height * info->width * mode; // allocate memory for image pixels info->imageData = (unsigned char *)malloc(sizeof(unsigned char) * total); // check to make sure we have the memory required if (info->imageData == NULL) { info->status = TGA_ERROR_MEMORY; break; } bool bLoadImage = false; // finally load the image pixels if ( info->type == 10 ) { bLoadImage = tgaLoadRLEImageData(pBuffer, nSize, info); } else { bLoadImage = tgaLoadImageData(pBuffer, nSize, info); } // check for errors when reading the pixels if (! bLoadImage) { info->status = TGA_ERROR_READING_FILE; break; } info->status = TGA_OK; if ( info->flipped ) { tgaFlipImage( info ); if ( info->flipped ) { info->status = TGA_ERROR_MEMORY; } } } while(0); return info; }
void CCBMFontConfiguration::parseConfigFile(const char *controlFile) { std::string fullpath = CCFileUtils::fullPathFromRelativePath(controlFile); FileData data; unsigned long nBufSize = 0; char* pBuffer = (char*) data.getFileData(fullpath.c_str(), "r", &nBufSize); NSAssert(pBuffer, "CCBMFontConfiguration::parseConfigFile | Open file error."); if (!pBuffer) { return; } char LineMax[LINE_MAX_CHAR_NUM] = {0}; size_t step = 0; size_t leftSize = nBufSize - step; while (leftSize > 0) { // clean temp data memset(LineMax, 0, sizeof(char) * LINE_MAX_CHAR_NUM); // read some data into LineMax[LINE_MAX_CHAR_NUM] if (leftSize < LINE_MAX_CHAR_NUM) { memcpy(LineMax, pBuffer + step, sizeof(char) * leftSize); } else { // only read LINE_MAX_CHAR_NUM - 1 char from buffer,to make sure the LineMax is end with '\0' memcpy(LineMax, pBuffer + step, sizeof(char) * (LINE_MAX_CHAR_NUM - 1)); } // find the '\n' char* pos = strchr(LineMax, '\n'); size_t lineSize = strlen(LineMax) * sizeof(char); if (pos) { lineSize = (pos - LineMax + 1) * sizeof(char); memset(LineMax + lineSize, 0, sizeof(char) * LINE_MAX_CHAR_NUM - lineSize); } step += lineSize; leftSize = nBufSize - step; // parse spacing / padding std::string line = LineMax; if(line.substr(0,strlen("info face")) == "info face") { // XXX: info parsing is incomplete // Not needed for the Hiero editors, but needed for the AngelCode editor // [self parseInfoArguments:line]; } // Check to see if the start of the line is something we are interested in else if(line.substr(0,strlen("common lineHeight")) == "common lineHeight") { this->parseCommonArguments(line); } else if(line.substr(0,strlen("page id")) == "page id") { this->parseImageFileName(line, controlFile); } else if(line.substr(0,strlen("chars c")) == "chars c") { // Ignore this line } else if(line.substr(0,strlen("char")) == "char") { // Parse the current line and create a new CharDef ccBMFontDef characterDefinition; this->parseCharacterDefinition(line, &characterDefinition); // Add the CharDef returned to the charArray m_pBitmapFontArray[ characterDefinition.charID ] = characterDefinition; } else if(line.substr(0,strlen("kernings count")) == "kernings count") { this->parseKerningCapacity(line); } else if(line.substr(0,strlen("kerning first")) == "kerning first") { this->parseKerningEntry(line); } } }