boost::shared_ptr<IMGArchive> File::getIMGArchive() const { boost::shared_ptr<IMGArchive>* sharedPtr = archivePtr.get(); if (sharedPtr->get() == NULL) { if (path->isIMGPath()) { File* parent = getParent(); parent->getIMGArchive(); delete parent; } else { FilePath* absPath = path->absolute(); map<CString, boost::shared_ptr<IMGArchive> >::iterator it = IMGArchiveMap.find(absPath->toString()); delete absPath; if (it != IMGArchiveMap.end()) { (*sharedPtr) = it->second; } else { (*sharedPtr) = boost::shared_ptr<IMGArchive>(new IMGArchive(*this)); FilePath* absPath = path->absolute(); IMGArchiveMap.insert(pair<CString, boost::shared_ptr<IMGArchive> >( absPath->toString(), *sharedPtr)); delete absPath; } } } return *sharedPtr; }
void storeImage(const FilePath& filename, DynamicArray<unsigned char> data, unsigned int width, unsigned int height) { constexpr int CHANNEL_COUNT = 3; OutFileStream file(filename.c_str(), OutFileStream::binary); if (!file.is_open()) throw InvalidArgumentException("Cannot open output file: " + filename.toString()); // Image is vertically flipped DynamicArray<unsigned char> data_flip(data.size()); const unsigned int line_size = width * CHANNEL_COUNT; // Vertically flip lines for (unsigned int i = 0; i < height; ++i) { std::memcpy( data_flip.data() + i * line_size, data.data() + (height - i - 1) * line_size, line_size ); } // Write function stbi_write_func* func = [] (void* context, void* data, int size) { reinterpret_cast<OutFileStream*>(context)->write(reinterpret_cast<const char*>(data), size); }; // Copy data if (!stbi_write_png_to_func(func, &file, width, height, CHANNEL_COUNT, data_flip.data(), 0)) throw RuntimeException("Unable to write a picture"); }
//! The < operator is provided so that CFileList can sort and quickly search the list. bool operator <(const FileListItem& other) const { if (isDirectory != other.isDirectory) return isDirectory; return StringHelper::isEquale( fullName.toString(), other.fullName.toString(), StringHelper::equaleIgnoreCase ); }
//! Searches for a file or folder within the list, returns the index int FileList::findFile(const FilePath& filename, bool isDirectory) const { FileListItem entry; // we only need fullName to be set for the search entry.fullName = StringHelper::replace( filename.toString(), "\\", "/" ); entry.isDirectory = isDirectory; // remove trailing slash if( *entry.fullName.toString().rbegin() == '/' ) { entry.isDirectory = true; } entry.fullName = entry.fullName.removeEndSlash(); if( _d->ignoreCase ) { entry.fullName = StringHelper::localeLower( entry.fullName.toString() ); } if( _d->ignorePaths ) { entry.fullName = entry.fullName.getBasename(); } for( Items::iterator it=_d->files.begin(); it != _d->files.end(); it++ ) { if( (*it).fullName == entry.fullName ) { return std::distance( _d->files.begin(), it ); } } return -1; }
void LoadProtoPack(proto::Entity& entity, const FilePath& fname) { if (fname.isValidPath() && fname.FileExists()) { std::fstream input(fname.GetNativePath(), std::ios::in | std::ios::binary); entity.ParseFromIstream(&input); } else { std::cout << "error loading protopack " << fname.toString() << std::endl; } }
FileList::FileList( const FilePath& path, bool ignoreCase, bool ignorePaths ) : _d( new Impl ) { #ifdef _DEBUG setDebugName( "FileList" ); #endif _d->ignorePaths = ignorePaths; _d->path = StringHelper::replace( path.toString(), "\\", "/" ); _d->ignoreCase = ignoreCase; }
FilePath FileDir::find( const FilePath& fileName ) const { _OC3_DEBUG_BREAK_IF( !isExist() ); if( !fileName.toString().size() ) { return ""; } if( fileName.isExist() ) { return fileName; } FilePath finalPath( addEndSlash().toString() + fileName.toString() ); if( finalPath.isExist() ) { return finalPath; } return fileName; }
std::shared_ptr<MD5Mesh> MD5Mesh::Create(const FilePath& fname) { auto mesh = std::make_shared<MD5Mesh>(); mesh->SetFileName(fname); mesh->SetName(fname.SubpathFrom("assets").toGenericString()); if (mesh->Parse()) { mesh->CalculateVertexPositions(); mesh->CalculateVertexNormals(); mesh->UpdateIndexList(); MeshMap::Set(mesh->GetName(), mesh); return mesh; } spdlog::get("console_log")->warn("[MD5Mesh] Error parsing file {}", fname.toString()); return nullptr; }
//! flatten a path and file name for example: "/you/me/../." becomes "/you" FilePath FilePath::flattenFilename( const FilePath& root ) const { std::string directory = addEndSlash().toString(); directory = StringHelper::replace( directory, "\\", "/" ); FilePath dir; FilePath subdir; int lastpos = 0; std::string::size_type pos = 0; bool lastWasRealDir=false; while( ( pos = directory.find( '/', lastpos) ) != std::string::npos ) { subdir = FilePath( directory.substr(lastpos, pos - lastpos + 1) ); if( subdir.toString() == "../" ) { if (lastWasRealDir) { dir = dir.getUpDir(); dir = dir.getUpDir(); lastWasRealDir=( dir.toString().size()!=0); } else { dir = FilePath( dir.toString() + subdir.toString() ); lastWasRealDir=false; } } else if( subdir.toString() == "/") { dir = root; } else if( subdir.toString() != "./" ) { dir = FilePath( dir.toString() + subdir.toString() ); lastWasRealDir=true; } lastpos = pos + 1; } return dir; }
//! adds a file or folder unsigned int FileList::addItem( const FilePath& fullPath, unsigned int offset, unsigned int size, bool isDirectory, unsigned int id ) { FileListItem entry; entry.iD = id ? id : _d->files.size(); entry.Offset = offset; entry.size = size; entry.name = StringHelper::replace( fullPath.toString(), "\\", "/" ); entry.isDirectory = isDirectory; // remove trailing slash if( *(entry.name.toString().rbegin()) == '/') { entry.isDirectory = true; entry.name = entry.name.removeEndSlash(); //entry.name.validate(); } if( _d->ignoreCase) { entry.name = StringHelper::localeLower( entry.name.toString() ); } entry.fullName = entry.name; entry.name = entry.name.getBasename(); if(_d->ignorePaths ) { entry.fullName = entry.name; } //os::Printer::log(Path.c_str(), entry.fullName); _d->files.push_back(entry); return _d->files.size() - 1; }
bool FilePath::operator==( const FilePath& other ) const { return toString() == other.toString(); }
//! Get the relative filename, relative to the given directory FilePath FilePath::getRelativePathTo( const FilePath& directory ) const { if ( toString().empty() || directory.toString().empty() ) return *this; FilePath path1, file, ext; getAbsolutePath().splitToDirPathExt( &path1, &file, &ext ); FilePath path2(directory.getAbsolutePath()); StringArray list1, list2; list1 = StringHelper::split( path1.toString(), "/\\", 2); list2 = StringHelper::split( path2.toString(), "/\\", 2); unsigned int i=0; unsigned int it1=0; unsigned int it2=0; #if defined (_WIN32) char partition1 = 0, partition2 = 0; FilePath prefix1, prefix2; if ( it1 > 0 ) prefix1 = list1[ it1 ]; if ( it2 > 0 ) prefix2 = list2[ it2 ]; if ( prefix1.toString().size() > 1 && prefix1.toString()[1] == ':' ) { partition1 = StringHelper::localeLower( prefix1.toString()[0] ); } if ( prefix2.toString().size() > 1 && prefix2.toString()[1] == ':' ) { partition2 = StringHelper::localeLower( prefix2.toString()[0] ); } // must have the same prefix or we can't resolve it to a relative filename if ( partition1 != partition2 ) { return *this; } #endif for (; i<list1.size() && i<list2.size() #if defined (_WIN32) && ( StringHelper::isEquale( list1[ it1 ], list2[ it2 ], StringHelper::equaleIgnoreCase ) ) #else && ( list1[ it1 ]== list2[ it2 ] ) #endif ; ++i) { ++it1; ++it2; } path1=""; for (; i<list2.size(); ++i) { path1 = path1.toString() + "../"; } while( it1 != list1.size() ) { path1 = path1.toString() + list1[ it1 ] + "/"; it1++; } path1 = path1.toString() + file.toString(); if( ext.toString().size() ) { path1 = path1.toString() + "." + ext.toString(); } return path1; }
void FilePath::_OsRename( const FilePath& newName ) { ::rename( toString().c_str(), newName.toString().c_str() ); }