/// Writes the contents of \p buffer of \p size into the stream. ostringstream& ostringstream::write (const void* buffer, size_type sz) { const char* buf = (const char*) buffer; for (size_type bw = 0; (bw = minV(sz, remaining() ? remaining() : overflow(sz))); buf += bw, sz -= bw) ostream::write (buf, bw); return (*this); }
void buffer::append_from(network::input_stream* istream) { CPPA_REQUIRE(remaining() > 0); auto num_bytes = istream->read_some(wr_ptr(), remaining()); if (num_bytes > 0) { inc_size(num_bytes); } }
/* find_http_req DATA LEN FOUND FOUNDLEN * Look for an HTTP request and response in buffer DATA of length LEN. The * return value is a pointer into DATA suitable for a subsequent call to this * function; *FOUND is either NULL, or a pointer to the start of an HTTP * request; in the latter case, *FOUNDLEN is the length of the match * containing enough information to obtain the URL. */ unsigned char *find_http_req(const unsigned char *data, const size_t len, unsigned char **http, size_t *httplen) { unsigned char *req, *le, *blankline, *hosthdr; #define remaining(x) (len - (data - (x))) #define MAX_REQ 16384 /* HTTP requests look like: * * GET {path} HTTP/1.(0|1)\r\n * header: value\r\n * ... * \r\n * * We may care about the Host: header in the request. */ if (len < 40) return (unsigned char*)data; if (!(req = memstr(data, len, (unsigned char*) "GET ", 4))) return (unsigned char*)(data + len - 4); /* Find the end of the request line. */ if (!(le = memstr(req + 4, remaining(req + 4), (unsigned char*) "\r\n", 2))) { if (remaining(req + 4) > MAX_REQ) return (unsigned char*)(req + 4); else return (unsigned char*)req; } /* Not enough space for a path. */ if (le < req + 5) return le + 2; /* Not an HTTP request, just a line starting GET.... */ if (memcmp(le - 9, " HTTP/1.", 8) || !strchr("01", (int)*(le - 1))) return le + 2; /* Find the end of the request headers. */ if (!(blankline = memstr(le + 2, remaining(le + 2), (unsigned char*) "\r\n\r\n", 4))) { if (remaining(le + 2) > MAX_REQ) return (unsigned char*)(data + len - 4); else return req; } if (memcmp(req + 4, "http://", 7) == 0) /* Probably a cache request; in any case, don't need to look for a Host:. */ goto found; /* Is there a Host: header? */ if (!(hosthdr = memstr(le, blankline - le + 2, (unsigned char*) "\r\nHost: ", 8))) { return blankline + 4; } found: *http = req; *httplen = blankline - req; return blankline + 4; }
void read(void * data, const std::size_t & len) { if (file_) { if (file_->read(reinterpret_cast<char *>(data), len)) { file_offset_ += len; file_->seek_set(file_offset_); } else { assert(0); } } else { if (remaining() < len) { throw std::runtime_error( "buffer underrun, len = " + std::to_string(len) + ", remaining = " + std::to_string(remaining()) ); } if (m_read_ptr == 0) { m_read_ptr = &m_data[0]; } std::memcpy(data, m_read_ptr, len); m_read_ptr += len; } }
size_t UpdaterClass::write(uint8_t *data, size_t len) { size_t left = len; if(hasError() || !isRunning()) return 0; if(len > remaining()) len = remaining(); while((_bufferLen + left) > FLASH_SECTOR_SIZE) { size_t toBuff = FLASH_SECTOR_SIZE - _bufferLen; memcpy(_buffer + _bufferLen, data + (len - left), toBuff); _bufferLen += toBuff; if(!_writeBuffer()){ return len - left; } left -= toBuff; yield(); } //lets see whats left memcpy(_buffer + _bufferLen, data + (len - left), left); _bufferLen += left; if(_bufferLen == remaining()){ //we are at the end of the update, so should write what's left to flash if(!_writeBuffer()){ return len - left; } } return len; }
size_t UpdaterClass::writeStream(Stream &data) { size_t written = 0; size_t toRead = 0; if(hasError() || !isRunning()) return 0; while(remaining()) { toRead = FLASH_SECTOR_SIZE - _bufferLen; toRead = data.readBytes(_buffer + _bufferLen, toRead); if(toRead == 0){ //Timeout _error = UPDATE_ERROR_STREAM; _currentAddress = (_startAddress + _size); #ifdef DEBUG_UPDATER printError(DEBUG_UPDATER); #endif return written; } _bufferLen += toRead; if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer()) return written; written += toRead; yield(); } return written; }
unsigned read( uint8_t* out, unsigned amount ){ if( amount > remaining() ) amount = remaining(); std::memcpy( out, data+pos, amount ); pos += amount; return amount; }
virtual unsigned getn(void *dst, unsigned n) { if (!compressed) return getBytes(dst, size*n)/size; byte *d = (byte *)dst; byte *e = d+(size*n); byte *p = (byte *)prev; unsigned ret = 0; while (d!=e) { if (first) { if (getBytes(d, size)!=size) break; first = false; } else { if (remaining()<maxcompsize) refill(); if (remaining()==0) break; ptr += DiffExpand(ptr,d,p,size); } p = d; d += size; ret++; } if (ret) // we got at least 1 so copy to prev memcpy(prev,e-size,size); return ret; }
int DumpPacketCommand::Execute() { if (remaining().empty()) { cout << GetUsage() << GetHelp() << endl; return 2; } const string filename(remaining().front()); return dump_file(filename); }
/// Called when more buffer space (\p n bytes) is needed. ofstream::size_type ofstream::overflow (size_type n) { if (eof() || (n > remaining() && n < capacity() - pos())) return (ostringstream::overflow (n)); size_type bw = m_File.write (cdata(), pos()); clear (m_File.rdstate()); erase (begin(), bw); if (remaining() < n) ostringstream::overflow (n); return (remaining()); }
/// Attempts to create more output space. Returns remaining(). ostringstream::size_type ostringstream::overflow (size_type n) { if (n > remaining()) { const uoff_t oldPos (pos()); m_Buffer.reserve (oldPos + n, false); m_Buffer.resize (oldPos + n); ostream::link (m_Buffer); SetPos (oldPos); } verify_remaining ("write", "text", n); return (remaining()); }
void StringBuffer::append(const TCHAR *pStr, int len) { if(!pStr || !len)return; int rem = remaining(); while(len >= rem) { expandBuffer(len + 16 - rem); rem = remaining(); } TCHAR *pTail = m_pStr + m_strLen; CopyMemory(pTail, pStr, len * sizeof(TCHAR)); m_strLen += len; }
long CharStringBuffer::append(const char* s, unsigned long n) { if ( n <= remaining() ) { memcpy(&contents[fill_point_marker], s, n); fill_point_marker += n; return n; } else { unsigned int num_to_copy = remaining(); memcpy(&contents[fill_point_marker], s, num_to_copy); fill_point_marker += num_to_copy; return num_to_copy; } }
bool buffer_writer::write(const char * ptr, uint32 n) { bool allWritten = true; if (n > remaining()) { allWritten = false; n = remaining(); } std::memcpy(_ptr+_iter, ptr, n*sizeof(char)); _iter += n; return allWritten; }
cc7::ByteRange TestFile::readMemory(size_t size) { size = std::min(size, remaining()); ByteRange range = _resource->range().subRange(_offset, size); _offset += size; return range; }
void ThriftServer::stopListening() { auto sockets = getSockets(); std::atomic<size_t> remaining(1 + sockets.size()); folly::Baton<> done; auto defer_wait = folly::makeGuard([&] { done.wait(); }); auto maybe_post = [&] { --remaining ? void() : done.post(); }; maybe_post(); for (auto& socket : sockets) { // Stop accepting new connections auto eb = socket->getEventBase(); eb->runInEventBaseThread( [socket = std::move(socket), g = folly::makeGuard(maybe_post)] { socket->pauseAccepting(); // Close the listening socket // This will also cause the workers to stop socket->stopAccepting(); }); } if (stopWorkersOnStopListening_) { // Wait for any tasks currently running on the task queue workers to // finish, then stop the task queue workers. Have to do this now, so // there aren't tasks completing and trying to write to i/o thread // workers after we've stopped the i/o workers. threadManager_->join(); } }
cc7::U16 TestFile::readByte() { if (remaining() > 0) { return _resource->data()[_offset++]; } return TestFile::EndOfFile; }
uint8 * ArrayReader::upTo(uint8 aCharacter, bool inclusive, bool& timeout) { int16 arraySize = remaining(); uint8 * result = new uint8[arraySize]; int16 i = 0; bool found = false; while (i < arraySize && !found) { uint8 nextChar = next(timeout); found = (nextChar == aCharacter) || timeout; if (!found || inclusive) { result[i] = nextChar; i++; } } if (i < arraySize) { uint8 * temp = new uint8[i]; memcpy(temp, result, i); delete[] result; result = temp; } return result; }
/// Reads at least \p n more bytes and returns available bytes. ifstream::size_type ifstream::underflow (size_type n) { if (eof()) return (istringstream::underflow (n)); const ssize_t freeSpace = m_Buffer.size() - pos(); const ssize_t neededFreeSpace = max (n, m_Buffer.size() / 2); const size_t oughtToErase = Align (max (0, neededFreeSpace - freeSpace)); const size_t nToErase = min (pos(), oughtToErase); m_Buffer.memlink::erase (m_Buffer.begin(), nToErase); const uoff_t oldPos (pos() - nToErase); size_type br = oldPos; if (m_Buffer.size() - br < n) { m_Buffer.resize (br + neededFreeSpace); link (m_Buffer.data(), 0U); } cout.flush(); while (br - oldPos < n && m_File.good()) br += m_File.readsome (m_Buffer.begin() + br, m_Buffer.size() - br); clear (m_File.rdstate()); m_Buffer[br] = string::c_Terminator; link (m_Buffer.data(), br); seek (oldPos); return (remaining()); }
void write(const T& obj) { CHECKED_LENGTH(sizeof(T), remaining()); // Write directly into stream Stream.write(reinterpret_cast<const byte*>(&obj), sizeof(T)); }
Errc to_view(View& v, const Stats_request& m) { if (remaining(v) < bytes(m)) return Errc::STATS_REQUEST_OVERFLOW; to_view(v, m.header); return to_view(v, m.payload, m.header.type); }
void write(const element_t (&arr)[LENGTH]) { CHECKED_LENGTH(LENGTH * sizeof(element_t), remaining()); // Write elements to stream Stream.write(arr); }
bool IsLeft(word32 sz) { if (remaining() >= sz) return true; else { SetError(CONTENT_E); return false; } }
/// <summary>Draw multiple lines of rich text</summary> /// <param name="dc">dc.</param> /// <param name="rect">drawing rectangle</param> /// <param name="str">string.</param> /// <param name="flags">drawing flags.</param> /// <exception cref="Logic::Win32Exception">Drawing error</exception> /// <returns>Width of widest line</returns> int RichTextRenderer::DrawLines(CDC* dc, CRect& rect, const RichString& str, RenderFlags flags) { LOGFONT fontData; CFont* oldFont; LineRect line(rect, dc->GetTextExtent(L"ABC").cy); long rightMargin = rect.left; // Get original font properties oldFont = dc->GetCurrentFont(); if (!oldFont->GetLogFont(&fontData)) throw Win32Exception(HERE, L"Unable to get logical font"); // Draw paragraphs for (auto para = str.Paragraphs.begin(); para != str.Paragraphs.end(); ) { auto words(GetWords(*para)); // Draw words for (auto w = words.begin(); w != words.end(); ) { CRect remaining(line); // Measure words on line auto first = w; int line_remaining = MeasureLine(dc, w, words.end(), line, flags); // Alignment: Offset all word rectangles for (auto word = first; word != w; ++word) { switch (para->Align) { case Alignment::Right: word->Offset(line_remaining, 0); break; case Alignment::Centre: case Alignment::Justify: word->Offset(line_remaining/2, 0); break; } // Set rightmost margin rightMargin = max(rightMargin, word->Rect.right); } // Render words if (flags != RenderFlags::Calculate) RenderLine(dc, first, w, flags); // NewLine if (w != words.end()) line.Advance(); } // Start each paragraph on a separate line if (++para != str.Paragraphs.end()) line.Advance(); } // Set drawing extent rect.bottom = (!str.FirstParagraph.empty() ? line.bottom : line.top); // Return width of widest line of text return rightMargin - rect.left; }
ZCountdown::ZCountdown(const ZConfig &el, QObject *parent) : QObject(parent), ZConfigurable(el,this) { _tracker = NULL; _startMsec = ZDateTime::nowMsec_t(); parse(_config); if(_tracker){ connect(_tracker, SIGNAL(timeout()), this, SLOT(_tick())); connect(_tracker, SIGNAL(started()), this, SIGNAL(started())); connect(_tracker, SIGNAL(stopped()), this, SIGNAL(stopped())); } zEvent->registerSignal(this, SIGNAL(started())); zEvent->registerSignal(this, SIGNAL(stopped())); zEvent->registerSignal(this, SIGNAL(tick())); zEvent->registerSignal(this, SIGNAL(timeout())); zEvent->registerSignal(this, SIGNAL(elapsed(qint64))); zEvent->registerSignal(this, SIGNAL(remaining(qint64))); zEvent->registerSlot(this, SLOT(start())); zEvent->registerSlot(this, SLOT(stop())); zEvent->registerSlot(this, SLOT(reset())); }
Errc from_view(View& v, Queue_config_request& m) { if (remaining(v) < bytes(m)) return Errc::QUEUE_CONFIG_REQUEST_OVERFLOW; from_view(v, m.port); return {}; }
/** Writes the current content line to the stream after conversion to UTF-8. Performs any necessary folding. @internalTechnology */ void CICalContentLineWriter::WriteContentLineL() { TRACE_ENTRY_POINT; // Convert to UTF-8 for writing HBufC8* tmpLine = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*iCurrentLine); CleanupStack::PushL(tmpLine); TInt pos(0); TInt remaining(tmpLine->Length()); // Fold the line if longer than 75 octets TInt lineLength(KICalMaxLineLength); while (remaining > lineLength) { iWriteStream.WriteL(tmpLine->Mid(pos), lineLength); iWriteStream.WriteL(KICalFoldLine); pos += lineLength; remaining -= lineLength; lineLength = KICalMaxFoldedLineLength; } // Complete the line iWriteStream.WriteL(tmpLine->Mid(pos)); iWriteStream.WriteL(KICalCRLF); CleanupStack::PopAndDestroy(tmpLine); iCurrentLine->Des().SetLength(0); TRACE_EXIT_POINT; }
////////////////////////////////////////////////////////////////////////////////////////// // BinaryWriter::write //! Writes a boolean to the output stream as a single element containing 0 or 1 //! //! \param[in] const &b - Boolean flag //! //! \throw wtl::length_error - [Debug Only] Insufficient buffer space remaining //! \throw wtl::logic_error - [Debug only] Stream has been closed //! \throw wtl::out_of_range - [Debug only] Stream position out of bounds ////////////////////////////////////////////////////////////////////////////////////////// void write(bool b) { CHECKED_LENGTH(1UL, remaining()); // Write as single element Stream.put(b ? 1 : 0); }
Errc from_view(View& v, Stats_reply& m) { if (remaining(v) < bytes(m)) return Errc::STATS_REPLY_OVERFLOW; from_view(v, m.header); return from_view(v, m.payload, m.header.type); }
Errc to_view(View& v, const Echo_reply& m) { if (remaining(v) < bytes(m)) return Errc::HELLO_OVERFLOW; to_view(v, m.data); return {}; }