bool readFile(const io::Consumer &stream, uint8_t *buf, size_t bsize, const String &ipath, size_t off, size_t size) { auto f = openForReading(ipath); if (f) { size_t fsize = f.size(); if (fsize <= off) { f.close(); return false; } if (fsize - off < size) { size = fsize - off; } bool ret = true; f.seek(off, io::Seek::Set); while (size > 0) { auto read = min(size, bsize); if (f.read(buf, read) == read) { stream.write(buf, read); } else { ret = false; break; } size -= read; } f.close(); return ret; } return false; }
bool QuickDC::Hash::CacheStorage::open(enum Access access) { if (access == ReadAccess) return openForReading(); if (access == WriteAccess) return openForWriting(); opened = false; return false; }
void MergeTreePartition::load(const MergeTreeData & storage, const String & part_path) { if (!storage.partition_key_expr) return; ReadBufferFromFile file = openForReading(part_path + "partition.dat"); value.resize(storage.partition_key_sample.columns()); for (size_t i = 0; i < storage.partition_key_sample.columns(); ++i) storage.partition_key_sample.getByPosition(i).type->deserializeBinary(value[i], file); }
String readTextFile(const String &ipath) { auto f = openForReading(ipath); if (f) { auto fsize = f.size(); String ret; ret.resize(fsize); f.read((uint8_t *)ret.data(), fsize); f.close(); return ret; } return String(); }
bool FileDescriptor::openForReadingAndMaybeWriting(const std::string& path) { if (! openForReadingAndWriting(path)) { if (! openForReading(path)) { return false; } } return true; }
Bytes readFile(const String &ipath, size_t off, size_t size) { auto f = openForReading(ipath); if (f) { auto fsize = f.size(); if (fsize <= off) { f.close(); return Bytes(); } if (fsize - off < size) { size = fsize - off; } Bytes ret; ret.resize(size); f.seek(off, io::Seek::Set); f.read(ret.data(), size); f.close(); return ret; } return Bytes(); }
static inline bool performCopy(const String &source, const String &dest) { if (stappler::filesystem::exists(dest)) { stappler::filesystem::remove(dest); } #if SPDEFAULT if (!stappler::filesystem::exists(dest)) { std::ofstream destStream(dest, std::ios::binary); auto f = openForReading(source); if (f && destStream.is_open()) { if (io::read(f, destStream) > 0) { return true; } } destStream.close(); } return false; #else return apr_file_copy(source.c_str(), dest.c_str(), APR_FPROT_FILE_SOURCE_PERMS, apr::AllocStack::get().top()) == APR_SUCCESS; #endif }
// reads all the header information from a dat file and adds ito to the vector bool DatFile::readAndAddDatSpectrumStats(vector<DatSpectrumStats>& headerStats, unsigned int fileIdx) { if (! indOpen_) openForReading(); if (numSpectra_ == 0) return false; fseek(stream_, DAT_HEADER_SIZE, SEEK_SET); const size_t statStartIdx = headerStats.size(); headerStats.resize(headerStats.size()+numSpectra_); size_t numSpectraExamined = 0; longInt8_t bufferStartOffset = DAT_HEADER_SIZE; // the dat header long nextSpectrumPosition = bufferStartOffset; while (numSpectraExamined < numSpectra_) { // use large margin 131072 to avoid possibility that the spectrum // overreaces the buffer boundry, as this causes problems const size_t bufferTail = bufferEnd_ - bufferPosition_; if (bufferEnd_ == 0 || bufferEnd_ == DAT_BUFFER_SIZE && bufferTail < 131072) { // shunt end of buffer if (bufferPosition_>0) { memmove(buffer_, buffer_ + bufferPosition_, bufferEnd_ - bufferPosition_); bufferEnd_ = bufferEnd_ - bufferPosition_; bufferStartOffset += bufferPosition_; bufferPosition_ = 0; } bufferEnd_ += fread(buffer_ + bufferEnd_, 1, DAT_BUFFER_SIZE - bufferEnd_, stream_); } if (bufferEnd_ == 0) return (numSpectraExamined == numSpectra_); assert(bufferPosition_ < bufferEnd_); DatSpectrumStats& stats = headerStats[statStartIdx + numSpectraExamined]; stats.fileIdx = fileIdx; stats.filePosition = bufferStartOffset + bufferPosition_; char* specStart = buffer_ + bufferPosition_; char* p = specStart; const unsigned int spectrumSize = *(reinterpret_cast<unsigned int*>(p)); // add number of bytes to skip bufferPosition_ += spectrumSize; p+= sizeof(unsigned int); stats.numPeaks = *(reinterpret_cast<unsigned int*>(p)); p+= sizeof(unsigned int); stats.mOverZ = *(reinterpret_cast<mass_t*>(p)); if (spectrumSize>=131072) { cout << endl << "ERROR: Spectrum size too large, how did this happen?" << endl; cout << this->path_ << endl; cout << "SIZE : " << spectrumSize << endl; cout << "File idx : " << stats.fileIdx << endl; cout << "File pos : " << stats.filePosition << endl; cout << "m/z : " << stats.mOverZ << endl; cout << "num peaks: " << stats.numPeaks << endl; cout.flush(); } assert(spectrumSize < 131072); assert(stats.filePosition == nextSpectrumPosition); nextSpectrumPosition += spectrumSize; numSpectraExamined++; } closeAfterReading(); return (numSpectraExamined == numSpectra_); }