uint8_t CompactQik2s9v1::getError() { sendByte(ERRORPACKET); _errByte = readByte(); return _errByte; }
/** Read a single bit from an 8-bit device register. * @param devAddr I2C slave device address * @param regAddr Register regAddr to read from * @param bitNum Bit position to read (0-7) * @param data Container for single bit value * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) { uint8_t b; uint8_t count = readByte(devAddr, regAddr, &b, timeout); *data = b & (1 << bitNum); return count; }
unsigned short PacketBuffer::readShort(){ unsigned char shrtC[] = {readByte(), readByte()}; return (shrtC[0]<<8)|(shrtC[1]); }
unsigned int PacketBuffer::readInt(){ unsigned char intC[] = {readByte(), readByte(), readByte(), readByte()}; return (intC[0]<<24)|(intC[1]<<16)|(intC[2]<<8)|(intC[3]); }
uint16 readUint16BE(FILE *file) { uint16 x = readByte(file) << 8; return x | readByte(file); }
uint16 File::readUint16BE() { uint8 hi = readByte(); uint8 lo = readByte(); return (hi << 8) | lo; }
bool JpegFrameParser::parse(const unsigned char* src, const std::size_t src_size) { std::size_t scan_data_begin_offset = 0; std::size_t scan_data_end_offset = 0; const unsigned char* data = src; std::size_t offset = 0; while (offset < src_size) { unsigned char one_byte_data = '0'; readByte(data, offset, one_byte_data); if (one_byte_data == START_MARKER) { unsigned char marker = '0'; readByte(data, offset, marker); switch (marker) { case SOI_MARKER: { // start of image break; } case JFIF_MARKER: { parseApp0(data, offset); break; } case DQT_MARKER: { parseDqt(data, offset); break; } case SOF0_MARKER: m_start_of_frame.dct_mode = 0x00; case SOF1_MARKER: m_start_of_frame.dct_mode = 0x01; case SOF2_MARKER: m_start_of_frame.dct_mode = 0x02; case SOF3_MARKER: m_start_of_frame.dct_mode = 0x03; case SOF5_MARKER: m_start_of_frame.dct_mode = 0x05; case SOF6_MARKER: m_start_of_frame.dct_mode = 0x06; case SOF7_MARKER: m_start_of_frame.dct_mode = 0x07; case SOF9_MARKER: m_start_of_frame.dct_mode = 0x09; case SOFA_MARKER: m_start_of_frame.dct_mode = 0x0a; case SOFB_MARKER: m_start_of_frame.dct_mode = 0x0b; case SOFD_MARKER: m_start_of_frame.dct_mode = 0x0d; case SOFE_MARKER: m_start_of_frame.dct_mode = 0x0e; case SOFF_MARKER: m_start_of_frame.dct_mode = 0x0f; { parseSof(data, offset); break; } case DHT_MARKER: { // define huffman table break; } case SOS_MARKER: { parseSos(data, offset); scan_data_begin_offset = offset; break; } case DRI_MARKER: { parseDri(data, offset); break; } case RST0_MARKER: case RST1_MARKER: case RST2_MARKER: case RST3_MARKER: case RST4_MARKER: case RST5_MARKER: case RST6_MARKER: case RST7_MARKER: { break; } case DNL_MARKER: { break; } case EOI_MARKER: { // end of image size_t eoi_marker_size = 2; // 0xFFD9 scan_data_end_offset = (offset - eoi_marker_size); break; } case COM_MARKER: { // comment break; } } } } BOOST_ASSERT(scan_data_begin_offset != 0); BOOST_ASSERT(scan_data_end_offset != 0); m_scan_data_size = scan_data_end_offset - scan_data_begin_offset; m_scan_data = new unsigned char[m_scan_data_size]; memcpy(m_scan_data, src + scan_data_begin_offset, m_scan_data_size); return true; }
char *SeekableReadStream::readLine(char *buf, size_t bufSize) { assert(buf != 0 && bufSize > 1); char *p = buf; size_t len = 0; char c = 0; // If end-of-file occurs before any characters are read, return NULL // and the buffer contents remain unchanged. if (eos() || err()) { return 0; } // Loop as long as there is still free space in the buffer, // and the line has not ended while (len + 1 < bufSize && c != LF) { c = readByte(); if (eos()) { // If end-of-file occurs before any characters are read, return // NULL and the buffer contents remain unchanged. if (len == 0) return 0; break; } // If an error occurs, return NULL and the buffer contents // are indeterminate. if (err()) return 0; // Check for CR or CR/LF // * DOS and Windows use CRLF line breaks // * Unix and OS X use LF line breaks // * Macintosh before OS X used CR line breaks if (c == CR) { // Look at the next char -- is it LF? If not, seek back c = readByte(); if (err()) { return 0; // error: the buffer contents are indeterminate } if (eos()) { // The CR was the last character in the file. // Reset the eos() flag since we successfully finished a line clearErr(); } else if (c != LF) { seek(-1, SEEK_CUR); } // Treat CR & CR/LF as plain LF c = LF; } *p++ = c; len++; } // We always terminate the buffer if no error occurred *p = 0; return buf; }
void JpegFrameParser::parseSof(const unsigned char*& data, std::size_t& offset) { boost::uint16_t frame_header_size = 0; { readByte(data, offset, frame_header_size); boost::endian::big_to_native_inplace(frame_header_size); } boost::uint8_t frame_precision = 0; { readByte(data, offset, frame_precision); } boost::uint16_t height = 0; { readByte(data, offset, height); boost::endian::big_to_native_inplace(height); } boost::uint16_t width = 0; { readByte(data, offset, width); boost::endian::big_to_native_inplace(width); } boost::uint8_t component_count = 0; { readByte(data, offset, component_count); } std::vector<StartOfFrame::Component> component_vec; for (int i = 0; i < component_count; ++i) { boost::uint8_t component_number = 0; { readByte(data, offset, component_number); } boost::uint8_t horizontal_sample_factor = 0; boost::uint8_t vertical_sample_factor = 0; { boost::uint8_t chunk = 0; readByte(data, offset, chunk); horizontal_sample_factor = util::getMost4Bit(chunk); vertical_sample_factor = util::getLeast4Bit(chunk); } boost::uint8_t quantization_table_number = 0; { readByte(data, offset, quantization_table_number); } StartOfFrame::Component component; component.component_number = component_number; component.horizontal_sampling_factor = horizontal_sample_factor; component.vertical_sampling_factor = vertical_sample_factor; component.quantization_table_number = quantization_table_number; component_vec.push_back(component); } m_start_of_frame.header_length = frame_header_size; m_start_of_frame.precision = frame_precision; m_start_of_frame.height = height; m_start_of_frame.width = width; m_start_of_frame.component_count = component_count; m_start_of_frame.component_vec = component_vec; }
void JpegFrameParser::parseSos(const unsigned char*& data, std::size_t& offset) { boost::uint16_t header_size = 0; { readByte(data, offset, header_size); boost::endian::big_to_native_inplace(header_size); } boost::uint8_t component_count = 0; { readByte(data, offset, component_count); } std::vector<StartOfScan::Component> component_vec; for (int i = 0; i < component_count; ++i) { boost::uint8_t component_number = 0; { readByte(data, offset, component_number); } boost::uint8_t dc_huffman_table_number = 0; boost::uint8_t ac_huffman_table_number = 0; { boost::uint8_t chunk = 0; readByte(data, offset, chunk); dc_huffman_table_number = util::getMost4Bit(chunk); ac_huffman_table_number = util::getLeast4Bit(chunk); } StartOfScan::Component component; component.component_number = component_number; component.dc_huffman_table_number = dc_huffman_table_number; component.ac_huffman_table_number = ac_huffman_table_number; component_vec.push_back(component); } boost::uint8_t spectral_selection_start = 0; { readByte(data, offset, spectral_selection_start); } boost::uint8_t spectral_selection_end = 0; { readByte(data, offset, spectral_selection_end); } boost::uint8_t successive_approximation_high = 0; boost::uint8_t successive_approximation_low = 0; { boost::uint8_t chunk = 0; readByte(data, offset, chunk); successive_approximation_high = util::getMost4Bit(chunk); successive_approximation_low = util::getLeast4Bit(chunk); } m_start_of_scan.header_length = header_size; m_start_of_scan.component_count = component_count; m_start_of_scan.component_vec = component_vec; m_start_of_scan.spectral_selection_start = spectral_selection_start; m_start_of_scan.spectral_selection_end = spectral_selection_end; m_start_of_scan.successive_approximation_high = successive_approximation_high; m_start_of_scan.successive_approximation_low = successive_approximation_low; }
/** * Read a single byte * This function performs as readByte and is added to be similar to the EEPROM library */ uint8_t EEPROMClassEx::read(int address) { return readByte(address); }
int Path::read(std::ifstream &in, std::string &msg) { msg = ""; bool finished = false; while (!finished) { short record_size; Byte record_type, record_dt; if (!readShort(in, record_size) || !readByte(in, record_type) || !readByte(in, record_dt)) return FILE_ERROR; switch (record_type) { case ENDEL: finished = true; break; case EFLAGS: if (record_size != 6) { std::stringstream ss; ss << "Wrong record size of EFLAGS ("; ss << std::hex << record_size << record_type << record_dt; ss << ")."; msg = ss.str(); return FORMAT_ERROR; } if (!readShort(in, Eflags)) return FILE_ERROR; break; case LAYER: if (record_size != 6) { std::stringstream ss; ss << "Wrong record size of LAYER ("; ss << std::hex << record_size << record_type << record_dt; ss << ")."; msg = ss.str(); return FORMAT_ERROR; } if (!readShort(in, Layer)) return FILE_ERROR; break; case DATATYPE: if (record_size != 6) { std::stringstream ss; ss << "Wrong record size of DATATYPE ("; ss << std::hex << record_size << record_type << record_dt; ss << ")."; msg = ss.str(); return FORMAT_ERROR; } if (!readShort(in, Data_type)) return FILE_ERROR; break; case XY: { record_size -= 4; int num = record_size / 8; if (record_size % 8 != 0 || num < 2) { std::stringstream ss; ss << "wrong record size of XY for PATH ("; ss << std::hex << record_size << record_type << record_dt; ss << "). "; msg = ss.str(); return FORMAT_ERROR; } for (int i = 0; i < num; i++) { int x, y; if (!readInteger(in, x) || !readInteger(in, y)) return FILE_ERROR; Pts.push_back(Point(x, y)); } break; } case WIDTH: if (record_size != 8) { std::stringstream ss; ss << "Wrong record size of WIDTH ("; ss << std::hex << record_size << record_type << record_dt; ss << "). "; msg = ss.str(); return FORMAT_ERROR; } if (!readInteger(in, Width)) return FILE_ERROR; break; case PATHTYPE: if (record_size != 6) { std::stringstream ss; ss << "Wrong record size of PATHTYPE ("; ss << std::hex << record_size << record_type << record_dt; ss << "). "; msg = ss.str(); return FORMAT_ERROR; } if (!readShort(in, Path_type)) return FILE_ERROR; break; default: break; } } return 0; }
static int readPacket (BrailleDisplay *brl, InputPacket *packet) { typedef enum { IPG_PRODUCT, IPG_KEY, IPG_DEFAULT } InputPacketGroup; InputPacketGroup group = IPG_DEFAULT; int length = 1; int offset = 0; while (1) { unsigned char byte; { int started = offset > 0; if (!readByte(&byte, started)) { if (started) logPartialPacket(packet->bytes, offset); return 0; } } gotByte: if (!offset) { switch (byte) { case IPT_KEY_NAVIGATION: case IPT_KEY_SIMULATION: case IPT_KEY_ROUTING: group = IPG_KEY; length = 4; break; default: if (byte == productPrefix[0]) { group = IPG_PRODUCT; length = sizeof(packet->product) - 1; } else { logIgnoredByte(byte); continue; } break; } } else { int unexpected = 0; switch (group) { case IPG_PRODUCT: if (offset < productPrefixLength) { if (byte != productPrefix[offset]) unexpected = 1; } else if (byte == '@') { length = offset + 1; } break; case IPG_KEY: if (offset == 1) { if (byte != packet->bytes[0]) unexpected = 1; } else if (offset == 3) { if (byte != 0X19) unexpected = 1; } break; default: break; } if (unexpected) { logShortPacket(packet->bytes, offset); group = IPG_DEFAULT; offset = 0; length = 1; goto gotByte; } } packet->bytes[offset++] = byte; if (offset == length) { if (group == IPG_PRODUCT) { packet->bytes[length] = 0; } logInputPacket(packet->bytes, offset); return length; } } }
bool SaveGame::readBool() { return readByte() != 0; }
bool InputStream::readBool() { return readByte() != 0; }