//----------------------------------------------------------------------- DataStreamPtr FileSystemArchive::open(const String& filename, bool readOnly) const { String full_path = concatenate_path(mName, filename); // Use filesystem to determine size // (quicker than streaming to the end and back) struct stat tagStat; int ret = stat(full_path.c_str(), &tagStat); assert(ret == 0 && "Problem getting file size" ); (void)ret; // Silence warning // Always open in binary mode // Also, always include reading std::ios::openmode mode = std::ios::in | std::ios::binary; std::istream* baseStream = 0; std::ifstream* roStream = 0; std::fstream* rwStream = 0; if (!readOnly && isReadOnly()) { mode |= std::ios::out; rwStream = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); rwStream->open(full_path.c_str(), mode); baseStream = rwStream; } else { roStream = OGRE_NEW_T(std::ifstream, MEMCATEGORY_GENERAL)(); roStream->open(full_path.c_str(), mode); baseStream = roStream; } // Should check ensure open succeeded, in case fail for some reason. if (baseStream->fail()) { OGRE_DELETE_T(roStream, basic_ifstream, MEMCATEGORY_GENERAL); OGRE_DELETE_T(rwStream, basic_fstream, MEMCATEGORY_GENERAL); OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, "Cannot open file: " + filename, "FileSystemArchive::open"); } /// Construct return stream, tell it to delete on destroy FileStreamDataStream* stream = 0; if (rwStream) { // use the writeable stream stream = OGRE_NEW FileStreamDataStream(filename, rwStream, (size_t)tagStat.st_size, true); } else { // read-only stream stream = OGRE_NEW FileStreamDataStream(filename, roStream, (size_t)tagStat.st_size, true); } return DataStreamPtr(stream); }
//--------------------------------------------------------------------- Ogre::DataStreamPtr UnicodeFileSystemArchive::create(const String& _filename) const { if (isReadOnly()) { GOTHOGRE_EXCEPT(_filename << " - Cannot create a file in" << " read-only archive " << getName() << "."); } WString wfullpath = getFullPath(_filename); // Always open in binary mode // Also, always include reading std::ios::openmode mode = std::ios::out | std::ios::binary; std::fstream* rwStream = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); rwStream->open(wfullpath.c_str(), mode); // Should check ensure open succeeded, in case fail for some reason. if (rwStream->fail()) { OGRE_DELETE_T(rwStream, basic_fstream, MEMCATEGORY_GENERAL); GOTHOGRE_EXCEPT(_filename << " - Cannot create file in" << " archive " << getName() << "."); } GOTHOGRE_INFO(_filename << " - " << "Saving to" << " archive " << getName() << "."); /// Construct return stream, tell it to delete on destroy FileStreamDataStream* stream = OGRE_NEW FileStreamDataStream(_filename, rwStream, 0, true); return DataStreamPtr(stream); }
//----------------------------------------------------------------------- void ConfigFile::loadDirect(const String& filename, const String& separators, bool trimWhitespace) { #if OGRE_PLATFORM == OGRE_PLATFORM_NACL OGRE_EXCEPT(Exception::ERR_CANNOT_WRITE_TO_FILE, "loadDirect is not supported on NaCl - tried to open: " + filename, "ConfigFile::loadDirect"); #endif /* Open the configuration file */ std::ifstream fp; // Always open in binary mode fp.open(filename.c_str(), std::ios::in | std::ios::binary); if(!fp) OGRE_EXCEPT( Exception::ERR_FILE_NOT_FOUND, "'" + filename + "' file not found!", "ConfigFile::load" ); // Wrap as a stream DataStreamPtr stream(OGRE_NEW FileStreamDataStream(filename, &fp, false)); #if OGRE_PLATFORM == OGRE_PLATFORM_SYMBIAN // seems readLine doesn't work correctly in SYMBIAN with files DataStreamPtr memoryStream(OGRE_NEW MemoryDataStream(stream)); stream = memoryStream; #endif load(stream, separators, trimWhitespace); }
//--------------------------------------------------------------------- DataStreamPtr FileSystemArchive::create(const String& filename) const { if (isReadOnly()) { OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot create a file in a read-only archive", "FileSystemArchive::remove"); } String full_path = concatenate_path(mName, filename); // Always open in binary mode // Also, always include reading std::ios::openmode mode = std::ios::out | std::ios::binary; std::fstream* rwStream = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); rwStream->open(full_path.c_str(), mode); // Should check ensure open succeeded, in case fail for some reason. if (rwStream->fail()) { OGRE_DELETE_T(rwStream, basic_fstream, MEMCATEGORY_GENERAL); OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, "Cannot open file: " + filename, "FileSystemArchive::create"); } /// Construct return stream, tell it to delete on destroy FileStreamDataStream* stream = OGRE_NEW FileStreamDataStream(filename, rwStream, 0, true); return DataStreamPtr(stream); }
//----------------------------------------------------------------------- DataStreamPtr FileSystemArchive::open(const String& filename) const { #if (_MSC_VER >= 1400) // std::locale langLocale(""); // std::locale::global(langLocale); setlocale( LC_CTYPE, "" ); #endif String full_path = concatenate_path(mName, filename); // Use filesystem to determine size // (quicker than streaming to the end and back) struct stat tagStat; int ret = stat(full_path.c_str(), &tagStat); assert(ret == 0 && "Problem getting file size" ); // Always open in binary mode std::ifstream *origStream = OGRE_NEW_T(std::ifstream, MEMCATEGORY_GENERAL)(); origStream->open(full_path.c_str(), std::ios::in | std::ios::binary); // Should check ensure open succeeded, in case fail for some reason. if (origStream->fail()) { OGRE_DELETE_T(origStream, basic_ifstream, MEMCATEGORY_GENERAL); OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, "Cannot open file: " + filename, "FileSystemArchive::open"); } /// Construct return stream, tell it to delete on destroy FileStreamDataStream* stream = OGRE_NEW FileStreamDataStream(filename, origStream, tagStat.st_size, true); return DataStreamPtr(stream); }
//----------------------------------------------------------------------- DataStreamPtr FileSystemArchive::open(const String& filename, bool readOnly) const { String full_path = concatenate_path(mName, filename); // Use filesystem to determine size // (quicker than streaming to the end and back) struct stat tagStat; stat(full_path.c_str(), &tagStat); // Always open in binary mode std::ifstream *origStream = OGRE_NEW_T(std::ifstream, MEMCATEGORY_GENERAL)(); origStream->open(full_path.c_str(), std::ios::in | std::ios::binary); // Should check ensure open succeeded, in case fail for some reason. if (origStream->fail()) { OGRE_DELETE_T(origStream, basic_ifstream, MEMCATEGORY_GENERAL); OGRE_EXCEPT(Ogre::Exception::ERR_FILE_NOT_FOUND, "Cannot open file: " + filename, "FileSystemArchive::open"); } // Construct return stream, tell it to delete on destroy FileStreamDataStream* stream = OGRE_NEW FileStreamDataStream(filename, origStream, tagStat.st_size, true); return DataStreamPtr(stream); }
//--------------------------------------------------------------------- void DeflateStream::init() { mpZStream = OGRE_ALLOC_T(z_stream, 1, MEMCATEGORY_GENERAL); mpZStream->zalloc = OgreZalloc; mpZStream->zfree = OgreZfree; if (getAccessMode() == READ) { mpTmp = (unsigned char*)OGRE_MALLOC(OGRE_DEFLATE_TMP_SIZE, MEMCATEGORY_GENERAL); size_t restorePoint = mCompressedStream->tell(); // read early chunk mpZStream->next_in = mpTmp; mpZStream->avail_in = mCompressedStream->read(mpTmp, OGRE_DEFLATE_TMP_SIZE); if (inflateInit(mpZStream) != Z_OK) { mIsCompressedValid = false; } else mIsCompressedValid = true; if (mIsCompressedValid) { // in fact, inflateInit on some implementations doesn't try to read // anything. We need to at least read something to test Bytef testOut[4]; size_t savedIn = mpZStream->avail_in; mpZStream->avail_out = 4; mpZStream->next_out = testOut; if (inflate(mpZStream, Z_SYNC_FLUSH) != Z_OK) mIsCompressedValid = false; // restore for reading mpZStream->avail_in = savedIn; mpZStream->next_in = mpTmp; inflateReset(mpZStream); } if (!mIsCompressedValid) { // Not compressed data! // Fail gracefully, fall back on reading the underlying stream direct destroy(); mCompressedStream->seek(restorePoint); } } else { // Write to temp file char tmpname[L_tmpnam]; tmpnam(tmpname); mTempFileName = tmpname; std::fstream *f = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); f->open(tmpname, std::ios::binary | std::ios::out); mTmpWriteStream = DataStreamPtr(OGRE_NEW FileStreamDataStream(f)); } }
//-------------------------------------------------------------------------------// void ConfigFile::load(const String& name, const String& separators) { std::ifstream fp; fp.open(name.c_str(), std::ios::in | std::ios::binary ); if(fp == 0) { TITAN_EXCEPT_ITEMLOST( " Can not find config file called: "+ name); return ; } DataStreamPtr stream(TITAN_NEW FileStreamDataStream(name, &fp, false)); String line, optName, optVal; PropertyMap* currentMap = 0; while(!stream->eof()) { line = stream->getLine(); if(line.length() > 0 && line.at(0) != '#') { if(line.at(0) == '[' && line.at(line.length() - 1) == ']') { String sectionName = line.substr(1, line.length() - 2); SectionMap::iterator it = mSectionMap.find(sectionName); if(it == mSectionMap.end()) { currentMap = TITAN_NEW_T(PropertyMap, MEMCATEGORY_GENERAL)(); mSectionMap.insert(SectionMap::value_type(sectionName, currentMap)); } else currentMap = it->second; } else { String::size_type separator_pos = line.find_first_of(separators, 0); if (separator_pos != String::npos) { optName = line.substr(0, separator_pos); /* Find the first non-seperator character following the name */ String::size_type nonseparator_pos = line.find_first_not_of(separators, separator_pos); /* ... and extract the value */ /* Make sure we don't crash on an empty setting (it might be a valid value) */ optVal = (nonseparator_pos == String::npos) ? "" : line.substr(nonseparator_pos); StringUtil::trim(optVal); StringUtil::trim(optName); currentMap->insert(PropertyMap::value_type(optName, optVal)); } } } } fp.close(); }
//----------------------------------------------------------------------- void ConfigFileEx::save(const String& _filename, const String& _separator, const String& _comments) { std::fstream s; s.open( _filename.c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc ); if(s.fail()) GOTHOGRE_EXCEPT("ConfigFile: '" << _filename << "' file cannot be written!"); DataStreamPtr stream(OGRE_NEW FileStreamDataStream(_filename, &s, false)); save(stream, _separator, _comments); }
//--------------------------------------------------------------------- void MeshSerializer::exportMesh(const Mesh* pMesh, const String& filename, Endian endianMode) { std::fstream *f = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); f->open(filename.c_str(), std::ios::binary | std::ios::out); DataStreamPtr stream(OGRE_NEW FileStreamDataStream(f)); exportMesh(pMesh, stream, endianMode); stream->close(); }
//--------------------------------------------------------------------- void SkeletonSerializer::exportSkeleton(const Skeleton* pSkeleton, const String& filename, SkeletonVersion ver, Endian endianMode) { std::fstream *f = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); f->open(filename.c_str(), std::ios::binary | std::ios::out); DataStreamPtr stream(OGRE_NEW FileStreamDataStream(f)); exportSkeleton(pSkeleton, stream, ver, endianMode); stream->close(); }
//----------------------------------------------------------------------- void ConfigFileEx::loadDirect(const String& _filename, const String& _separators, const String& _comments, ConfigFileExFlags _flags) { /* Open the configuration file */ std::ifstream s; // Always open in binary mode s.open(_filename.c_str(), std::ios::in | std::ios::binary); if(s.fail()) GOTHOGRE_EXCEPT("ConfigFile: '" << _filename << "' file not found!"); // Wrap as a stream DataStreamPtr stream(OGRE_NEW FileStreamDataStream(_filename, &s, false)); load(stream, _separators, _comments, _flags); }
//--------------------------------------------------------------------- void MeshSerializer::exportMesh(const Mesh* pMesh, const String& filename, MeshVersion version, Endian endianMode) { if( version == MESH_VERSION_LEGACY ) { OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "You may not supply a legacy version number for writing meshes.", "MeshSerializer::exportMesh" ); } std::fstream *f = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); f->open(filename.c_str(), std::ios::binary | std::ios::out); DataStreamPtr stream(OGRE_NEW FileStreamDataStream(f)); exportMesh(pMesh, stream, version, endianMode); stream->close(); }
//------------------------------------------------------------------------- void TerrainSceneManager::setWorldGeometry( const String& filename ) { // try to open in the current folder first std::ifstream fs; fs.open(filename.c_str(), std::ios::in | std::ios::binary); if (fs) { // Wrap as a stream DataStreamPtr stream( OGRE_NEW FileStreamDataStream(filename, &fs, false)); setWorldGeometry(stream); } else { // otherwise try resource system DataStreamPtr stream = ResourceGroupManager::getSingleton().openResource(filename, ResourceGroupManager::getSingleton().getWorldResourceGroupName()); setWorldGeometry(stream); } }
//--------------------------------------------------------------------- void DeflateStream::init() { mZStream = OGRE_ALLOC_T(z_stream, 1, MEMCATEGORY_GENERAL); mZStream->zalloc = OgreZalloc; mZStream->zfree = OgreZfree; if (getAccessMode() == READ) { mTmp = (unsigned char*)OGRE_MALLOC(OGRE_DEFLATE_TMP_SIZE, MEMCATEGORY_GENERAL); size_t restorePoint = mCompressedStream->tell(); // read early chunk mZStream->next_in = mTmp; mZStream->avail_in = static_cast<uint>(mCompressedStream->read(mTmp, getAvailInForSinglePass())); if (inflateInit(mZStream) != Z_OK) { mIsCompressedValid = false; } else mIsCompressedValid = true; if (mIsCompressedValid) { // in fact, inflateInit on some implementations doesn't try to read // anything. We need to at least read something to test Bytef testOut[4]; size_t savedIn = mZStream->avail_in; mZStream->avail_out = 4; mZStream->next_out = testOut; if (inflate(mZStream, Z_SYNC_FLUSH) != Z_OK) mIsCompressedValid = false; // restore for reading mZStream->avail_in = static_cast<uint>(savedIn); mZStream->next_in = mTmp; inflateReset(mZStream); } if (!mIsCompressedValid) { // Not compressed data! // Fail gracefully, fall back on reading the underlying stream direct destroy(); mCompressedStream->seek(restorePoint); } } else { if(mTempFileName.empty()) { // Write to temp file #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT char* tmpname = _tempnam(".", "ogre"); if (!tmpname) { // Having no file name here will cause various problems later. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Temporary file name generation failed.", "DeflateStream::init"); } else { mTempFileName = tmpname; free(tmpname); } #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS || OGRE_PLATFORM == OGRE_PLATFORM_APPLE mTempFileName = macTempFileName(); #else char tmpname[] = "/tmp/ogreXXXXXX"; if (mkstemp(tmpname) == -1) OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Temporary file name generation failed.", "DeflateStream::init"); mTempFileName = tmpname; #endif } std::fstream *f = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); f->open(mTempFileName.c_str(), std::ios::binary | std::ios::out); mTmpWriteStream = DataStreamPtr(OGRE_NEW FileStreamDataStream(f)); } }
//----------------------------------------------------------------------- Ogre::DataStreamPtr UnicodeFileSystemArchive::open(const String& _filename, bool _readOnly) const { WString wfullpath = getFullPath(_filename); // Use filesystem to determine size // (quicker than streaming to the end and back) struct _stat tagStat; int ret = _wstat(wfullpath.c_str(), &tagStat); if(ret != 0) { GOTHOGRE_EXCEPT(_filename << " - Problem getting file size" << " (archive " << getName() << ")."); } // Always open in binary mode // Also, always include reading std::ios::openmode mode = std::ios::in | std::ios::binary; std::istream* baseStream = 0; std::ifstream* roStream = 0; std::fstream* rwStream = 0; if (_readOnly) { roStream = OGRE_NEW_T(std::ifstream, MEMCATEGORY_GENERAL)(); roStream->open(wfullpath.c_str(), mode); baseStream = roStream; } else { if (isReadOnly()) { GOTHOGRE_EXCEPT(_filename << " - Cannot open a file for writing in" << " read-only archive " << getName() << "."); } mode |= std::ios::out; rwStream = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)(); rwStream->open(wfullpath.c_str(), mode); baseStream = rwStream; } // Should check ensure open succeeded, in case fail for some reason. if (baseStream->fail()) { OGRE_DELETE_T(roStream, basic_ifstream, MEMCATEGORY_GENERAL); OGRE_DELETE_T(rwStream, basic_fstream, MEMCATEGORY_GENERAL); GOTHOGRE_EXCEPT(_filename << " - Cannot open file in" << " archive " << getName() << "."); } GOTHOGRE_INFO(_filename << " - " << (_readOnly ? "Loading from" : "Saving to") << " archive " << getName() << "."); /// Construct return stream, tell it to delete on destroy FileStreamDataStream* stream = 0; if (rwStream) { // use the writeable stream stream = OGRE_NEW FileStreamDataStream(_filename, rwStream, tagStat.st_size, true); } else { // read-only stream stream = OGRE_NEW FileStreamDataStream(_filename, roStream, tagStat.st_size, true); } return DataStreamPtr(stream); }