//----------------------------------------------------------------------------// void OgreResourceProvider::unloadRawDataContainer(RawDataContainer& data) { if (data.getDataPtr()) { delete[] data.getDataPtr(); data.setData(0); data.setSize(0); } }
//----------------------------------------------------------------------------// void LuaScriptModule::executeScriptFile_impl(const String& filename, const String& resourceGroup, const int err_idx, const int top) { // load file RawDataContainer raw; System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, raw, resourceGroup.empty() ? d_defaultResourceGroup : resourceGroup); // load code into lua int loaderr = luaL_loadbuffer(d_state, reinterpret_cast<char*>(raw.getDataPtr()), raw.getSize(), filename.c_str()); System::getSingleton().getResourceProvider()->unloadRawDataContainer( raw ); if (loaderr) { String errMsg = lua_tostring(d_state,-1); lua_settop(d_state,top); CEGUI_THROW(ScriptException("Unable to execute Lua script file: '" + filename + "'\n\n" + errMsg + "\n")); } // call it if (lua_pcall(d_state, 0, 0, err_idx)) { String errMsg = lua_tostring(d_state,-1); lua_settop(d_state,top); CEGUI_THROW(ScriptException("Unable to execute Lua script file: '" + filename + "'\n\n" + errMsg + "\n")); } lua_settop(d_state,top); // just in case :P }
//----------------------------------------------------------------------------// void DefaultResourceProvider::unloadRawDataContainer(RawDataContainer& data) { uint8* const ptr = data.getDataPtr(); delete[] ptr; data.setData(0); data.setSize(0); }
void ExpatParser::parseXML(XMLHandler& handler, const RawDataContainer& source, const String& /*schemaName*/, bool /*allowXmlValidation*/) { // All stuff goes here XML_Parser parser = XML_ParserCreate(0); // Create a parser if (!parser) { CEGUI_THROW(GenericException("Unable to create a new Expat Parser")); } XML_SetUserData(parser, (void*)&handler); // Initialise user data XML_SetElementHandler(parser, startElement, endElement); // Register callback for elements XML_SetCharacterDataHandler(parser, characterData); // Register callback for character data // Parse the data (note that the last true parameter tels Expat that this is the last chunk of the document if (!XML_Parse(parser, reinterpret_cast<const char*>(source.getDataPtr()), source.getSize(), true)) { String exception (String((const encoded_char*)"XML Parsing error '") + String((const encoded_char*)XML_ErrorString(XML_GetErrorCode(parser))) + String((const encoded_char*)"' at line ") + PropertyHelper<uint>::toString(XML_GetCurrentLineNumber(parser))); // (We know it is a valid pointer, otherwise an exception would have been thrown above.) XML_ParserFree(parser); CEGUI_THROW(GenericException(exception)); } // (We know it is a valid pointer, otherwise an exception would have been thrown above.) XML_ParserFree(parser); }
void LibxmlParser::parseXML(XMLHandler& handler, const RawDataContainer& source, const String& /*schemaName*/, bool /*allowXmlValidation*/) { xmlDocPtr doc = xmlParseMemory( reinterpret_cast<const char*>(source.getDataPtr()), source.getSize()); if (!doc) { xmlError* err = xmlGetLastError(); CEGUI_THROW(GenericException( String("xmlParseMemory failed in file: '") + err->file + "' at line number" + PropertyHelper<uint>::toString(err->line) + ". Error is:" + err->message)); } // get root element xmlNode* root = xmlDocGetRootElement(doc); // process all elements from root to end of doc processXMLElement(handler, root); // release the xmlDoc xmlFreeDoc(doc); }
void XercesParser::doParse(XERCES_CPP_NAMESPACE::SAX2XMLReader* parser, const String& xmlFilename, const String& resourceGroup) { XERCES_CPP_NAMESPACE_USE; // use resource provider to load file data RawDataContainer rawXMLData; System::getSingleton().getResourceProvider()->loadRawDataContainer(xmlFilename, rawXMLData, resourceGroup); MemBufInputSource fileData( rawXMLData.getDataPtr(), static_cast<const unsigned int>(rawXMLData.getSize()), xmlFilename.c_str(), false); // perform parse try { parser->parse(fileData); } catch(...) { // use resource provider to release loaded XML source (if it supports this) System::getSingleton().getResourceProvider()->unloadRawDataContainer(rawXMLData); throw; } // use resource provider to release loaded XML source (if it supports this) System::getSingleton().getResourceProvider()->unloadRawDataContainer(rawXMLData); }
TinyXMLDocument::TinyXMLDocument(XMLHandler& handler, const String& filename, const String& schemaName, const String& resourceGroup) { d_handler = &handler; // use resource provider to load file data // Fixed using patch from tracker item #000057 // cegui_mk2-0.4.1-fixtinyxml.patch RawDataContainer rawXMLData; System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, rawXMLData, resourceGroup); // Create a buffer with extra bytes for a newline and a terminating null size_t size = rawXMLData.getSize(); char* buf = new char[size + 2]; memcpy(buf, rawXMLData.getDataPtr(), size); // PDT: The addition of the newline is a kludge to resolve an issue // whereby parse returns 0 if the xml file has no newline at the end but // is otherwise well formed. buf[size] = '\n'; buf[size+1] = 0; // Parse the document CEGUI_TINYXML_NAMESPACE::TiXmlDocument doc; if (!doc.Parse((const char*)buf)) { // error detected, cleanup out buffers delete[] buf; System::getSingleton().getResourceProvider()-> unloadRawDataContainer(rawXMLData); // throw exception throw FileIOException("TinyXMLParser: an error occurred while " "parsing the XML document '" + filename + "' - check it for potential errors!."); } const CEGUI_TINYXML_NAMESPACE::TiXmlElement* currElement = doc.RootElement(); if (currElement) { try { // function called recursively to parse xml data processElement(currElement); } catch(...) { delete [] buf; System::getSingleton().getResourceProvider()->unloadRawDataContainer(rawXMLData); throw; } } // if (currElement) // Free memory delete [] buf; System::getSingleton().getResourceProvider()->unloadRawDataContainer(rawXMLData); }
void XercesParser::initialiseSchema(XERCES_CPP_NAMESPACE::SAX2XMLReader* reader, const String& schemaName, const String& xmlFilename, const String& resourceGroup) { XERCES_CPP_NAMESPACE_USE; // enable schema use and set validation options reader->setFeature(XMLUni::fgXercesSchema, true); reader->setFeature(XMLUni::fgSAX2CoreValidation, true); reader->setFeature(XMLUni::fgXercesValidationErrorAsFatal, true); // load in the raw schema data RawDataContainer rawSchemaData; // try base filename first, from default resource group try { Logger::getSingleton().logEvent("XercesParser::initialiseSchema - Attempting to load schema from file '" + schemaName + "'."); System::getSingleton().getResourceProvider()->loadRawDataContainer(schemaName, rawSchemaData, d_defaultSchemaResourceGroup); } // oops, no file. Try an alternative instead, using base path and // resource group from the XML file we're going to be processing. catch(InvalidRequestException) { // get path from filename String schemaFilename; size_t pos = xmlFilename.rfind("/"); if (pos == String::npos) pos = xmlFilename.rfind("\\"); if (pos != String::npos) schemaFilename.assign(xmlFilename, 0, pos + 1); // append schema filename schemaFilename += schemaName; // re-try the load operation. Logger::getSingleton().logEvent("XercesParser::initialiseSchema - Attempting to load schema from file '" + schemaFilename + "'."); System::getSingleton().getResourceProvider()->loadRawDataContainer(schemaFilename, rawSchemaData, resourceGroup); } // wrap schema data in a xerces MemBufInputSource object MemBufInputSource schemaData( rawSchemaData.getDataPtr(), static_cast<const unsigned int>(rawSchemaData.getSize()), schemaName.c_str(), false); reader->loadGrammar(schemaData, Grammar::SchemaGrammarType, true); // enable grammar reuse reader->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true); // set schema for usage XMLCh* pval = XMLString::transcode(schemaName.c_str()); reader->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, pval); XMLString::release(&pval); Logger::getSingleton().logEvent("XercesParser::initialiseSchema - XML schema file '" + schemaName + "' has been initialised."); // use resource provider to release loaded schema data (if it supports this) System::getSingleton().getResourceProvider()->unloadRawDataContainer(rawSchemaData); }
void IrrlichtTexture::loadFromFile(const String& filename, const String& resourceGroup) { freeTexture(); RawDataContainer texFile; System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, texFile, resourceGroup); IrrlichtMemoryFile imf(filename, texFile.getDataPtr(), texFile.getSize()); driver->setTextureCreationFlag(irr::video::ETCF_CREATE_MIP_MAPS,true); tex=driver->getTexture(&imf); tex->grab(); // unload file data buffer System::getSingleton().getResourceProvider()->unloadRawDataContainer(texFile); updateCachedScaleValues(); }
TinyXMLDocument::TinyXMLDocument(XMLHandler& handler, const RawDataContainer& source, const String& /*schemaName*/) { d_handler = &handler; // Create a buffer with extra bytes for a newline and a terminating null size_t size = source.getSize(); char* buf = new char[size + 2]; memcpy(buf, source.getDataPtr(), size); // PDT: The addition of the newline is a kludge to resolve an issue // whereby parse returns 0 if the xml file has no newline at the end but // is otherwise well formed. buf[size] = '\n'; buf[size+1] = 0; // Parse the document TiXmlDocument doc; if (!doc.Parse((const char*)buf)) { // error detected, cleanup out buffers delete[] buf; // throw exception throw FileIOException("an error occurred while " "parsing the XML document - check it for potential errors!."); } const TiXmlElement* currElement = doc.RootElement(); if (currElement) { try { // function called recursively to parse xml data processElement(currElement); } catch (...) { delete [] buf; throw; } } // if (currElement) // Free memory delete [] buf; }
TinyXMLDocument::TinyXMLDocument(XMLHandler& handler, const String& filename, const String& schemaName, const String& resourceGroup) { d_handler = &handler; // use resource provider to load file data // Fixed using patch from tracker item #000057 // cegui_mk2-0.4.1-fixtinyxml.patch RawDataContainer rawXMLData; System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, rawXMLData, resourceGroup); // Create a buffer with the missing extra byte size_t size = rawXMLData.getSize(); char* buf = new char[size + 1]; memcpy(buf, rawXMLData.getDataPtr(), size); buf[size] = 0; try { // Parse the document CEGUITinyXML::TiXmlDocument doc; doc.Parse((const char*)buf); const CEGUITinyXML::TiXmlElement* currElement = doc.RootElement(); if (currElement) { // function called recursively to parse xml data processElement(currElement); } // if (currElement) } catch(...) { delete [] buf; System::getSingleton().getResourceProvider()->unloadRawDataContainer(rawXMLData); throw; } // Free memory delete [] buf; System::getSingleton().getResourceProvider()->unloadRawDataContainer(rawXMLData); }
/************************************************************************* Load texture from file. Texture is made to be same size as image in file. *************************************************************************/ void DirectX9Texture::loadFromFile(const String& filename, const String& resourceGroup) { freeD3DTexture(); // load the file via the resource provider RawDataContainer texFile; System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, texFile, resourceGroup); D3DXIMAGE_INFO texInfo; HRESULT hr = D3DXCreateTextureFromFileInMemoryEx(((DirectX9Renderer*)getRenderer())->getDevice(), texFile.getDataPtr(), static_cast<UINT>(texFile.getSize()), D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &texInfo, NULL, &d_d3dtexture); System::getSingleton().getResourceProvider()->unloadRawDataContainer(texFile); if (SUCCEEDED(hr)) { d_width = (ushort)texInfo.Width; d_height = (ushort)texInfo.Height; d_filename = filename; d_resourceGroup = resourceGroup; d_isMemoryTexture = true; d_isRenderTarget = false; } else { throw RendererException((utf8*)"Failed to create Texture object from file '" + filename ); } }
//----------------------------------------------------------------------------// RapidXMLDocument::RapidXMLDocument(XMLHandler& handler, const String& filename, const String& /*schemaName*/, const String& resourceGroup) { d_handler = &handler; // use resource provider to load file data RawDataContainer rawXMLData; System::getSingleton().getResourceProvider()-> loadRawDataContainer(filename, rawXMLData, resourceGroup); // Create a buffer with extra bytes for a newline and a terminating null size_t size = rawXMLData.getSize(); char* buf = new char[size + 2]; memcpy(buf, rawXMLData.getDataPtr(), size); // PDT: The addition of the newline is a kludge to resolve an issue // whereby parse returns 0 if the xml file has no newline at the end but // is otherwise well formed. buf[size] = '\n'; buf[size + 1] = 0; // Parse the document rapidxml::xml_document<> doc; // character type defaults to char try { doc.parse<0>(buf); // 0 means default parse flags } catch (...) { // error detected, cleanup out buffers delete[] buf; System::getSingleton().getResourceProvider()-> unloadRawDataContainer(rawXMLData); // throw exception throw FileIOException("RapidXMLParser: an error occurred while " "parsing the XML document '" + filename + "' - check it for potential errors!."); } rapidxml::xml_node<>* currElement = doc.first_node(); if (currElement) { try { // function called recursively to parse xml data processElement(currElement); } catch (...) { delete[] buf; System::getSingleton().getResourceProvider()-> unloadRawDataContainer(rawXMLData); throw; } } // Free memory delete[] buf; System::getSingleton().getResourceProvider()-> unloadRawDataContainer(rawXMLData); }