Linuxc/C ++ programming BASICS (36) poco: binaryreader implementation

Source: Internet
Author: User

I. binaryreader. H is defined as follows:

# Ifndef linyanwenbinaryreader_h _
# Define linyanwenbinaryreader_h _

# Include "poco/textconverter. H"
# Include "poco/Foundation. H"
# Include <vector>
# Include <istream>
Namespace linyanwen {
Class textencoding;
Class binaryreader
/// This class reads basic types (and STD: vectors thereof)
/// In binary form into an input stream.
/// It provides an extractor-based interface similar to istream.
/// The reader also supports automatic conversion from big-Endian
/// (Network byte order) to little-Endian and vice-versa.
/// Use a binarywriter to create a stream suitable for a binaryreader.
{
Public:
Enum streambyteorder
{
Native_byte_order = 1, // the host's native byte-order
Big_endian_byte_order = 2, // big-Endian (network) byte-order
Network_byte_order = 2, // big-Endian (network) byte-order
Little_endian_byte_order = 3, // little-Endian byte-order
Unspecified_byte_order = 4 // unknown, byte-order will be determined by reading the byte-order mark
};
Binaryreader (POCO: uint8 * _ buffer, Poco: uint32 _ size );
~ Binaryreader ();
Binaryreader & operator> (bool & value );
Binaryreader & operator> (char & value );
Binaryreader & operator> (unsigned char & value );
Binaryreader & operator> (signed Char & value );
Binaryreader & operator> (short & value );
Binaryreader & operator> (unsigned short & value );
Binaryreader & operator> (Int & value );
Binaryreader & operator> (unsigned Int & value );
Binaryreader & operator> (Long & value );
Binaryreader & operator> (unsigned long & value );
Binaryreader & operator> (float & value );
Binaryreader & operator> (double & value );
Binaryreader & operator> (STD: string & value );
/*************************************** */
Void read7bitencoded (POCO: uint32 & value );
Void readraw (STD: streamsize length, STD: string & value );
// Reads length bytes of raw data into value.
Void readraw (char * Buf, STD: streamsize length );
// Reads length bytes of raw data into Buf.
/*************************************** */
Bool good ();
PRIVATE:
Bool _ flipbytes;
Poco: textconverter * _ ptextconverter;
Poco: uint8 * buffer;
Poco: uint32 size;
Poco: int32 beg, end, cur;
};
Inline bool binaryreader: Good (){
Return cur <size; // note that cur starts from 0 and size starts from 1, so it is '<' rather than '<='
}
} // Namespace linyanwen

# Endif/* linyanwenbinaryreader_h _*/

 

Ii. binaryreader. cpp implementation:

# Include "linyanwenbinaryreader. H"
# Include "poco/binaryreader. H"
# Include "poco/byteorder. H"
# Include "poco/textencoding. H"
# Include <algorithm>
Namespace linyanwen {

Binaryreader: binaryreader (POCO: uint8 * _ buffer, Poco: uint32 _ SIZE): buffer (_ buffer), size (_ SIZE)
, Beg (0), cur (0), end (_ size), _ ptextconverter (0)
{
# If defined (poco_arch_big_endian)
_ Flipbytes = (binaryreader: network_byte_order = little_endian_byte_order );
# Else
_ Flipbytes = (binaryreader: network_byte_order = big_endian_byte_order );
# Endif
}
Binaryreader ::~ Binaryreader ()
{
Delete _ ptextconverter;
}

Binaryreader & binaryreader: Operator >>( bool & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
* (Char *) & Value) = * (buffer + cur );
++ Cur;
Return * this;
}
Binaryreader & binaryreader: Operator >>( char & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
* (Char *) & Value) = * (buffer + cur );
++ Cur;
Return * this;
}
Binaryreader & binaryreader: Operator> (unsigned char & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
* (Char *) & Value) = * (buffer + cur );
++ Cur;
Return * this;
}
Binaryreader & binaryreader: Operator> (signed Char & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
* (Char *) & Value) = * (buffer + cur );
++ Cur;
Return * this;
}
Binaryreader & binaryreader: Operator >>( short & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
If (_ flipbytes) value = poco: byteorder: flipbytes (value );
Return * this;
}
Binaryreader & binaryreader: Operator >>( unsigned short & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
If (_ flipbytes) value = poco: byteorder: flipbytes (value );
Return * this;
}
Binaryreader & binaryreader: Operator >>( Int & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
If (_ flipbytes) value = poco: byteorder: flipbytes (value );
Return * this;
}
Binaryreader & binaryreader: Operator> (unsigned Int & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
If (_ flipbytes) value = poco: byteorder: flipbytes (value );
Return * this;
}
Binaryreader & binaryreader: Operator >>( long & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
# If defined (poco_long_is_64_bit)
If (_ flipbytes) value = poco: byteorder: flipbytes (POCO: int64) value );
# Else
If (_ flipbytes) value = poco: byteorder: flipbytes (POCO: int32) value );
# Endif
Return * this;
}
Binaryreader & binaryreader: Operator >>( unsigned long & value)
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
# If defined (poco_long_is_64_bit)
If (_ flipbytes) value = poco: byteorder: flipbytes (POCO: uint64) value );
# Else
If (_ flipbytes) value = poco: byteorder: flipbytes (POCO: uint32) value );
# Endif
Return * this;
}
Binaryreader & binaryreader: Operator >>( float & value)
{
If (_ flipbytes)
{
Char * PTR = (char *) & value;
PTR + = sizeof (value );
For (unsigned I = 0; I <sizeof (value); ++ I)
// _ Istr. Read (-- PTR, 1 );
* (-- PTR) = * (buffer + cur + I );
Cur = cur + sizeof (value );
}
Else
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
}
Return * this;
}
Binaryreader & binaryreader: Operator >>( double & value)
{
If (_ flipbytes)
{
Char * PTR = (char *) & value;
PTR + = sizeof (value );
For (unsigned I = 0; I <sizeof (value); ++ I)
// _ Istr. Read (-- PTR, 1 );
* (-- PTR) = * (buffer + cur + I );
Cur = cur + sizeof (value );
}
Else
{
// _ Istr. Read (char *) & Value, sizeof (value ));
For (INT I = 0; I <sizeof (value); ++ I)
* (Char *) & Value + I) = * (buffer + cur + I );
Cur = cur + sizeof (value );
}
Return * this;
}

Binaryreader & binaryreader: Operator> (STD: string & value)
{
Poco: uint32 size = 0;
Read7bitencoded (size );
Value. Clear ();
If (! Good () return * this;
Value. Reserve (size );
While (size --)
{
Char C;
// If (! _ Istr. Read (& C, 1). Good () break;
If (! Good () break;
* (Char *) & C) = * (buffer + cur );
++ Cur;
Value + = C;
}
If (_ ptextconverter)
{
STD: String converted;
_ Ptextconverter-> convert (value, converted );
STD: swap (value, converted );
}
Return * this;
}
Void binaryreader: read7bitencoded (POCO: uint32 & value)
{
Char C;
Value = 0;
Int S = 0;
Do
{
C = 0;
// _ Istr. Read (& C, 1 );
* (Char *) & C) = * (buffer + cur );
++ Cur;
Poco: uint32 x = (C & 0x7f );
X <= s;
Value + = X;
S + = 7;
}
While (C & 0x80 );
}

Void binaryreader: readraw (STD: streamsize length, STD: string & value)
{
Value. Clear ();
Value. Reserve (static_cast <STD: String: size_type> (length ));
While (length --)
{
Char C;
// If (! _ Istr. Read (& C, 1). Good () break;
If (! Good () break;
* (Char *) & C) = * (buffer + cur );
++ Cur;
Value + = C;
}
}
Void binaryreader: readraw (char * Buf, STD: streamsize length)
{
// _ Istr. Read (buffer, length );
For (INT I = 0; I <length; ++ I)
* (BUF + I) = * (buffer + cur + I );
Cur = cur + length;
}
} // Namespace linyanwen

 

Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8599680

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.