void SnapshotBuilder::writeToFile(const std::string& filename) { m_file = fopen(filename.c_str(), "w+"); if (!m_file) { Logger::Error("Failed to open snapshot file %s", filename.c_str()); exit(1); } // Invalid header for now, to avoid truncated output due to errors looking // valid (and to allow computing diskOffset while serializing). writeRaw(SnapshotHeader::makeEmpty()); // Index write(m_ints); write(m_chars); write(m_stringMem); write32(m_stringDisk.size()); for (const auto& p : m_stringDisk) { write(p.first); // KeyValuePair uses sign of sSize for type info; preserve it here... write32(p.second.sSize); } // Disk auto diskOffset = ftello64(m_file); for (const auto& p : m_stringDisk) { // ...but remember to ignore it when we need the actual size. writeRaw(p.second.sAddr, abs(p.second.sSize) + 1); // \0 } // Write real header, as we are confident the output is complete. auto totalSize = ftello64(m_file); rewind(m_file); writeRaw(SnapshotHeader::makeValid(diskOffset, totalSize)); fclose(m_file); m_file = nullptr; }
bool QueryJob::write(const String &out, Flags<WriteFlag> flags) { if ((mJobFlags & WriteUnfiltered) || (flags & Unfiltered) || filter(out)) { if ((mJobFlags & QuoteOutput) && !(flags & DontQuote)) { String o((out.size() * 2) + 2, '"'); char *ch = o.data() + 1; int l = 2; for (size_t i=0; i<out.size(); ++i) { const char c = out.at(i); switch (c) { case '"': case '\\': *(ch + 1) = c; *ch = '\\'; ch += 2; l += 2; break; default: ++l; *ch++ = c; break; } } o.truncate(l); return writeRaw(o, flags); } else { return writeRaw(out, flags); } } return true; }
int QuotedPrintableEncoderBuf::writeToDevice(char c) { if (_pending != -1) { if (_pending == '\r' && c == '\n') writeRaw((char) _pending); else if (c == '\r' || c == '\n') writeEncoded((char) _pending); else writeRaw((char) _pending); _pending = -1; } if (c == '\t' || c == ' ') { _pending = charToInt(c); return _pending; } else if (c == '\r' || c == '\n' || c > 32 && c < 127 && c != '=') { writeRaw(c); } else { writeEncoded(c); } return charToInt(c); }
bool QueryJob::write(const String &out, Flags<WriteFlag> flags) { if ((mJobFlags & WriteUnfiltered) || (flags & Unfiltered) || filter(out)) { if ((mJobFlags & QuoteOutput) && !(flags & DontQuote)) { String o((out.size() * 2) + 2, '"'); char *ch = o.data() + 1; int l = 2; for (int i=0; i<out.size(); ++i) { const char c = out.at(i); if (c == '"') { *ch = '\\'; ch += 2; l += 2; } else { ++l; *ch++ = c; } } o.truncate(l); return writeRaw(o, flags); } else { return writeRaw(out, flags); } } return true; }
void putData(ivc_connection_t *con, void *buffer, size_t size) { void *buf_cur = buffer, *buf_end = (void*)((uintptr_t)buffer + size); unsigned long item_size = size; uint32_t avail, wrt_amount; assert( con->outbuf ); /* wait until we have enough data to write the size, then do so */ do { avail = con->outsize - getAvail(con->outmod, con->output); } while(avail < sizeof(unsigned long)); writeRaw(con, &item_size, sizeof(unsigned long)); /* write out the data */ while(buf_cur < buf_end) { avail = con->outsize - getAvail(con->outmod, con->output); wrt_amount = (avail > item_size) ? item_size : avail; if(wrt_amount > 0) { writeRaw(con, buf_cur, wrt_amount); buf_cur = (void*)((uintptr_t)buf_cur + wrt_amount); item_size -= wrt_amount; } else usleep(0); } assert(item_size == 0); }
void writeString(const char *value) { if (!value) { writeRaw("null"); } else { writeRaw('\"'); while (*value) writeChar(*value++); writeRaw('\"'); } }
void writeChar(char c) { char specialChar = Encoding::escapeChar(c); if (specialChar) { writeRaw('\\'); writeRaw(specialChar); } else { writeRaw(c); } }
void DigitDisplay::clear() { for (uint8_t i = 0; i < 4; i++) { _content[i] = DIGIT_NULL; } _colon = false; writeRaw(0, DIGIT_NULL); writeRaw(1, DIGIT_NULL); writeRaw(2, DIGIT_NULL); writeRaw(3, DIGIT_NULL); }
void DigitDisplay::write(int16_t n) { uint8_t negative = 0; if (n < 0) { negative = 1; n = (-n) % 1000; } else { n = n % 10000; } int8_t i = 3; do { uint8_t r = n % 10; _content[i] = conv(r); i--; n = n / 10; } while (n != 0); if (negative) { _content[i] = DIGIT_MINUS; i--; } for (int8_t j = 0; j <= i; j++) { _content[j] = DIGIT_NULL; } if (_colon) { _content[POSITION_COLON] |= 0x80; } writeRaw(_content); }
AMFWriter& RTMFPWriter::write(AMF::ContentType type,UInt32 time,PacketReader* pPacket) { if(pPacket && !reliable) { UInt8 headerRequired(type==AMF::DATA ? 6 : 5); if(pPacket->position()>=headerRequired) { pPacket->reset(pPacket->position()-headerRequired); BinaryWriter writer((UInt8*)pPacket->current(),headerRequired); writer.write8(type); writer.write32(time); if(type==AMF::DATA) writer.write8(0); writeRaw(pPacket->current(),pPacket->available()); return AMFWriter::Null; } DEBUG("Written unbuffered impossible, it requires 6 head bytes available on MemoryReader given"); } AMFWriter& amf = createBufferedMessage().writer(); BinaryWriter& packet = amf.packet; packet.write8(type); packet.write32(time); if(type==AMF::DATA) packet.write8(0); if(pPacket) packet.writeRaw(pPacket->current(),pPacket->available()); return amf; }
void OMXMLWriterSimple::writeProlog(void) { TRACE("OMXMLWriterSimple::writeProlog"); if (_encoding == UTF16) { OMUInt8 bom[2]; if (_byteOrder == BE) { bom[0] = 0xFE; bom[1] = 0xFF; } else { bom[0] = 0xFF; bom[1] = 0xFE; } writeRaw(bom, 2); } write(L"<?xml version=\"1.0\" encoding=\"", 30); if (_encoding == UTF8) { write(L"UTF-8\"?>\n", 9); } else { write(L"UTF-16\"?>\n", 10); } }
void RemotePluginClient::setVSTChunk(std::vector<char> chunk) { std::cerr << "RemotePluginClient::setChunk: writing vst chunk.." << std::endl; std::cerr << "RemotePluginClient::setChunk: read vst chunk, size=" << chunk.size() << std::endl; writeOpcode(m_controlRequestFd, RemotePluginSetVSTChunk); writeRaw(m_controlRequestFd, chunk); }
byte Dynamixel::txPacket(byte bID, byte bInstruction, int bParameterLength) { word bCount,bCheckSum,bPacketLength; byte offsetParamIndex; if(mPacketType == DXL_PACKET_TYPE1) { //dxl protocol 1.0 mTxBuffer[0] = 0xff; mTxBuffer[1] = 0xff; mTxBuffer[2] = bID; mTxBuffer[3] = bParameterLength+2; //2(byte) <- instruction(1byte) + checksum(1byte) mTxBuffer[4] = bInstruction; offsetParamIndex = 5; bPacketLength = bParameterLength+2+4; } else { //dxl protocol 2.0 mTxBuffer[0] = 0xff; mTxBuffer[1] = 0xff; mTxBuffer[2] = 0xfd; mTxBuffer[3] = 0x00; mTxBuffer[4] = bID; //get parameter length mTxBuffer[5] = DXL_LOBYTE(bParameterLength+3);// 3(byte) <- instruction(1byte) + checksum(2byte) mTxBuffer[6] = DXL_HIBYTE(bParameterLength+3); mTxBuffer[7] = bInstruction; offsetParamIndex = 8; bPacketLength = bParameterLength+3+7; //parameter length 3bytes, 7bytes = packet header 4bytes, ID 1byte, length 2bytes } //copy parameters from mParamBuffer to mTxBuffer for(bCount = 0; bCount < bParameterLength; bCount++) { mTxBuffer[bCount+offsetParamIndex] = mParamBuffer[bCount]; } if(mPacketType == DXL_PACKET_TYPE1) { bCheckSum = 0; for(bCount = 2; bCount < bPacketLength-1; bCount++) { //except 0xff,checksum bCheckSum += mTxBuffer[bCount]; } mTxBuffer[bCount] = ~bCheckSum; //Writing Checksum with Bit Inversion } else { bCheckSum = update_crc(0, mTxBuffer, bPacketLength-2); // -2 : except CRC16 mTxBuffer[bPacketLength-2] = DXL_LOBYTE(bCheckSum); // last - 2 mTxBuffer[bPacketLength-1] = DXL_HIBYTE(bCheckSum); // last - 1 } //TxDStringC("bPacketLength = ");TxDHex8C(bPacketLength);TxDStringC("\r\n"); this->dxlTxEnable(); // this define is declared in dxl.h for(bCount = 0; bCount < bPacketLength; bCount++) { writeRaw(mTxBuffer[bCount]); } this->dxlTxDisable();// this define is declared in dxl.h return(bPacketLength); // return packet length }
void ofxSickGrabber::stopRecording(string filename) { if(recording) { ofLogVerbose("ofxSickGrabber") << "Stopped recording data, saving " << recordedData.size() << " frames to " << filename; recording = false; ofFile out(filename, ofFile::WriteOnly, true); for(int i = 0; i < recordedData.size(); i++) { ScanData& cur = recordedData[i]; writeRaw(out, cur.first.distance); writeRaw(out, cur.first.brightness); writeRaw(out, cur.second.distance); writeRaw(out, cur.second.brightness); } ofLogVerbose("ofxSickGrabber") << "Done saving data."; } else { ofLogVerbose("ofxSickGrabber") << "Not recording data, not saving."; } }
void JSONWriter::writeKey(std::string key){ if (_options == SER_OPT_FORMATTED){ writeIndented("\"" + key + "\""); } else{ writeRaw("\"" + key + "\""); } }
void JSONWriter::writeValue(std::string value){ if (_options == SER_OPT_FORMATTED && typeStack.top() == JSON_ARRAY){ writeNewlyIndented(value); } else{ writeRaw(value); } }
void JSONWriter::writeEndArray(){ if (_options == SER_OPT_FORMATTED){ indent--; writeNewlyIndented("]"); } else{ writeRaw("]"); } assert(typeStack.top() == JSON_ARRAY); typeStack.pop(); }
void JSONWriter::writeEndObject(){ if (_options == SER_OPT_FORMATTED){ indent--; writeNewlyIndented("}"); } else{ writeRaw("}"); } assert(typeStack.top() == JSON_OBJECT); typeStack.pop(); }
void JSONWriter::writeStartArray(){ if (_options == SER_OPT_FORMATTED && typeStack.top() == JSON_ARRAY){ writeIndented("["); } else{ writeRaw("["); } if (_options == SER_OPT_FORMATTED){ indent++; } typeStack.push(JSON_ARRAY); }
void writeInteger(JsonUInt value) { char buffer[22]; char *ptr = buffer + sizeof(buffer) - 1; *ptr = 0; do { *--ptr = static_cast<char>(value % 10 + '0'); value /= 10; } while (value); writeRaw(ptr); }
void writeInteger(JsonUInt value) { char buffer[22]; uint8_t i = 0; do { buffer[i++] = static_cast<char>(value % 10 + '0'); value /= 10; } while (value); while (i > 0) { writeRaw(buffer[--i]); } }
inline void writeUTF8String(std::ostream& output, const UChar *str, size_t len = 0) { if (len == 0) { len = u_strlen(str); } std::vector<char> buffer(len * 4); int32_t olen = 0; UErrorCode status = U_ZERO_ERROR; u_strToUTF8(&buffer[0], len * 4 - 1, &olen, str, len, &status); uint16_t cs = static_cast<uint16_t>(olen); writeRaw(output, cs); output.write(&buffer[0], cs); }
void MulticastPipe::broadcastRaw(void* data,size_t size) { /* Write or read data depending on whether this is the master node or a slave node: */ if(master) { /* Write the data: */ writeRaw(data,size); } else { /* Read the data: */ readRaw(data,size); } }
void DigitDisplay::setColon(bool enable) { if (_colon != enable) { _colon = enable; if (enable) { _content[POSITION_COLON] |= 0x80; } else { _content[POSITION_COLON] &= 0x7F; } writeRaw(POSITION_COLON, _content[POSITION_COLON]); } }
void writeFloat(JsonFloat value, uint8_t digits = 2) { if (Polyfills::isNaN(value)) return writeRaw("NaN"); if (value < 0.0) { writeRaw('-'); value = -value; } if (Polyfills::isInfinity(value)) return writeRaw("Infinity"); short powersOf10; if (value > 1000 || value < 0.001) { powersOf10 = Polyfills::normalize(value); } else { powersOf10 = 0; } // Round up last digit (so that print(1.999, 2) prints as "2.00") value += getRoundingBias(digits); // Extract the integer part of the value and print it JsonUInt int_part = static_cast<JsonUInt>(value); JsonFloat remainder = value - static_cast<JsonFloat>(int_part); writeInteger(int_part); // Print the decimal point, but only if there are digits beyond if (digits > 0) { writeRaw('.'); } // Extract digits from the remainder one at a time while (digits-- > 0) { // Extract digit remainder *= 10.0; char currentDigit = char(remainder); remainder -= static_cast<JsonFloat>(currentDigit); // Print writeRaw(char('0' + currentDigit)); } if (powersOf10 < 0) { writeRaw("e-"); writeInteger(-powersOf10); } if (powersOf10 > 0) { writeRaw('e'); writeInteger(powersOf10); } }
void OStream::writeVersionStamp() { if (mRuntimeVersion >= 6) { const std::size_t BufferLen = 1+5+5; char buffer[BufferLen] = {0}; buffer[0] = BeginVersionStamp; RCF::ByteBuffer byteBuffer(&buffer[0], BufferLen); std::size_t pos = 1; encodeInt(mRuntimeVersion, byteBuffer, pos); encodeInt(mArchiveVersion, byteBuffer, pos); writeRaw(&buffer[0], static_cast<UInt32>(pos)); } }
void writeFloat(JsonFloat value, int digits = 2) { if (Polyfills::isNaN(value)) return writeRaw("NaN"); if (value < 0.0) { writeRaw('-'); value = -value; } if (Polyfills::isInfinity(value)) return writeRaw("Infinity"); short powersOf10; if (value > 1000 || value < 0.001) { powersOf10 = Polyfills::normalize(value); } else { powersOf10 = 0; } // Round correctly so that print(1.999, 2) prints as "2.00" JsonFloat rounding = 0.5; for (uint8_t i = 0; i < digits; ++i) rounding /= 10.0; value += rounding; // Extract the integer part of the value and print it JsonUInt int_part = static_cast<JsonUInt>(value); JsonFloat remainder = value - static_cast<JsonFloat>(int_part); writeInteger(int_part); // Print the decimal point, but only if there are digits beyond if (digits > 0) { writeRaw('.'); } // Extract digits from the remainder one at a time while (digits-- > 0) { remainder *= 10.0; JsonUInt toPrint = JsonUInt(remainder); writeInteger(JsonUInt(remainder)); remainder -= static_cast<JsonFloat>(toPrint); } if (powersOf10 < 0) { writeRaw("e-"); writeInteger(-powersOf10); } if (powersOf10 > 0) { writeRaw('e'); writeInteger(powersOf10); } }
void xpcc::Hd44780Minimal<E, RW, RS, DATA>::write(char c) { if (c == '\n') { line++; if (line >= lineCount) { line = 0; } column = 0; setCursor(line, column); } else { if (column >= lineWidth) { write('\n'); } writeRaw(c); column++; } }
byte Dynamixel::txPacket(byte bID, byte bInstruction, byte bParameterLength){ byte bCount,bCheckSum,bPacketLength; mTxBuffer[0] = 0xff; mTxBuffer[1] = 0xff; mTxBuffer[2] = bID; mTxBuffer[3] = bParameterLength+2; //Length(Paramter,Instruction,Checksum) mTxBuffer[4] = bInstruction; for(bCount = 0; bCount < bParameterLength; bCount++) { mTxBuffer[bCount+5] = mParamBuffer[bCount]; } bCheckSum = 0; bPacketLength = bParameterLength+4+2; for(bCount = 2; bCount < bPacketLength-1; bCount++) //except 0xff,checksum { bCheckSum += mTxBuffer[bCount]; } mTxBuffer[bCount] = ~bCheckSum; //Writing Checksum with Bit Inversion dxlTxEnable(); // this define is declared in dxl.h for(bCount = 0; bCount < bPacketLength; bCount++) { writeRaw(mTxBuffer[bCount]); } dxlTxDisable();// this define is declared in dxl.h return(bPacketLength); }
void OStream::writeArchiveMetadata() { if (mRuntimeVersion >= 6) { // Max possible size. const std::size_t BufferLen = 1+5+5+1; char buffer[BufferLen] = {0}; buffer[0] = BeginArchiveMetadata; RCF::ByteBuffer byteBuffer(&buffer[0], BufferLen); std::size_t pos = 1; encodeInt(mRuntimeVersion, byteBuffer, pos); encodeInt(mArchiveVersion, byteBuffer, pos); if (mRuntimeVersion >= 10) { encodeBool(getTrackingContext().getEnabled(), byteBuffer, pos); } writeRaw(&buffer[0], static_cast<UInt32>(pos)); } }