long FileReader::FileSize(int Mode_) { long StartPos_, CurPos_, Diff_; if ((Mode_ & ios::out) || (Mode_ & ios::app)) { CurPos_ = tellp(); seekp(0); StartPos_ = tellp(); seekp(0, ios::end); Diff_ = tellp() - StartPos_; seekp(CurPos_); } else if (Mode_ & ios::in) { CurPos_ = tellg(); seekg(0); StartPos_ = tellg(); seekg(0, ios::end); Diff_ = tellg() - StartPos_; seekg(CurPos_); } return Diff_; }
/* Member function obitstream::writeBit * ---------------------------------- * If bits remain to be written in curByte, add bit into byte and increment pos * Else if end of curByte (or some other write happened), then start a fresh * byte at position 0. * We write the byte out for each bit (backing up to overwrite as needed), rather * than waiting for 8 bits. This is because the client might make * 3 writeBit calls and then start using << so we can't wait til full-byte * boundary to flush any partial-byte bits. */ void obitstream::writeBit(int bit) { if (bit != 0 && bit != 1) { error(string("writeBit must be passed an integer argument of 0 or 1. You passed the integer ") + toPrintable(bit) + " (" + integerToString(bit) + ")."); } if (!is_open()) { error("Cannot writeBit to stream which is not open."); } // if just filled curByte or if data written to stream after last writeBit() if (lastTell != tellp() || pos == NUM_BITS_IN_BYTE) { curByte = 0; // zero out byte for next writes pos = 0; // start writing to first bit of new byte } if (bit) { // only need to change if bit needs to be 1 (byte starts already zeroed) SetNthBit(pos, curByte); } if (pos == 0 || bit) { // only write if first bit in byte or changing 0 to 1 if (pos != 0) { seekp(-1, ios::cur); // back up to overwite if pos > 0 } put(curByte); } pos++; // advance to next bit position for next write lastTell = tellp(); }
size_t c_disk_file::write(const char *buf, size_t bytes) { assert(m_mode == e_file_mode::write || m_mode == e_file_mode::read_write); auto stream = dynamic_cast<std::ofstream *>(m_stream); auto before = stream->tellp(); stream->write(buf, bytes); return stream->tellp() - before; }
/* Member function obitstream::size * ------------------------------ * Seek to file end and use tell to retrieve position. * In order to not disrupt writing, we also record cur streampos and * re-seek to there before returning. */ long obitstream::size() { if (!is_open()) { error("Cannot get size of stream which is not open."); } clear(); // clear any error state streampos cur = tellp(); // save current streampos seekp(0, ios::end); // seek to end streampos end = tellp(); // get offset seekp(cur); // seek back to original pos return long(end); }
void TCPClient::DownloadFile(string fileName) { auto file = new fstream(); OpenFile(file, GetLocalFileName(fileName)); fpos_t currentProgress = file->tellp(); fpos_t fileSize = 0; auto done = false; cout << "Download started." << endl; while (!done) { try { SendMessage(this->_tcp_socket, CreateFileInfo(fileName, currentProgress)); fileSize = ReceiveFileSize(); ReceiveFile(file, currentProgress, fileSize); done = true; } catch (ConnectionInterrupted e) { currentProgress = e.GetProgress(); Reconnect(); } catch (ServerError) { file->close(); throw; } catch (runtime_error e) { cout << e.what() << endl; Reconnect(); } } file->close(); cout << "Done." << endl; }
std::streampos BOStream::maxp() const { std::streampos cur = tellp(); std::streampos max = rdbuf()->pubseekoff(0, std::ios_base::end, std::ios_base::out); rdbuf()->pubseekpos(cur, std::ios_base::out); return max; }
// Insert a file at the given location. Shared by --insert and --add. bool insertFile(std::shared_ptr<ga::Archive> pArchive, const std::string& strLocalFile, const std::string& strArchFile, const ga::Archive::FileHandle& idBeforeThis, const std::string& type, ga::Archive::File::Attribute attr, stream::len lenReal) { // Open the file auto fsIn = std::make_unique<stream::file>(strLocalFile, false); stream::len lenSource = fsIn->size(); fsIn->seekg(0, stream::start); // Make sure either filters are active, or we've got a nonzero prefilter // length (but it's ok to have a zero prefilter length if the file is empty) assert(bUseFilters || (lenSource == 0) || (lenReal != 0)); // Create a new entry in the archive large enough to hold the file ga::Archive::FileHandle id = pArchive->insert(idBeforeThis, strArchFile, lenSource, type, attr); // Open the new (empty) file in the archive auto psNew = pArchive->open(id, bUseFilters); // Copy all the data from the file on disk into the archive file. try { stream::copy(*psNew, *fsIn); psNew->flush(); } catch (const stream::error& e) { std::cout << " [failed; " << e.what() << "]"; return false; } if (!bUseFilters) { // Since filters were skipped we will pretend we applied the filter and // we got more source data than we really did, so the next check works. lenSource = lenReal; } // If the data that went in was a different length to what we expected it // must have been compressed so update the file size (keeping the original // size as the 'uncompressed length' field.) stream::len lenActual = psNew->tellp(); if (lenActual != lenSource) { pArchive->resize(id, lenActual, lenSource); } return true; }
void CConn_MemoryStream::ToVector(vector<char>* vec) { if (!vec) { NCBI_THROW(CIO_Exception, eInvalidArg, "CConn_MemoryStream::ToVector(NULL) is not allowed"); } CConn_Streambuf* sb = dynamic_cast<CConn_Streambuf*>(rdbuf()); size_t size = sb && good() ? (size_t)(tellp() - tellg()) : 0; vec->resize(size); if (sb) { size_t s = (size_t) sb->sgetn(&(*vec)[0], size); #ifdef NCBI_COMPILER_WORKSHOP if (s < 0) { s = 0; // WS6 weirdness to sometimes return -1 from sgetn() :-/ } else #endif //NCBI_COMPILER_WORKSHOP _ASSERT(s == size); vec->resize(s); // NB: just in case, essentially NOOP when s == size } }
Boolean FileReader::ChangeFileSize(int Mode_, long Size_) { char Blank_ = 0; char EofMarker_ = EOF; if (!(Mode_ & ios::out) && !(Mode_ & ios::app)) return FALSE; long CurPos_ = tellp(); seekp(0, ios::end); write(&Blank_, 1); clear(); seekp(Size_ + 1); write(&EofMarker_, 1); seekp(CurPos_); clear(); return TRUE; }