How to simulate a memory into a C ++ stream,

xiaoxiao2021-03-06  96

How to simulate a memory into a C stream, set up a memory New, build a C ISTREAM class above this memory. All the operations can be done in this ISTREAM class. Do you have a lot of benefits, such as you Resource Analyzer can accept an ISTREAM pointer, this istream may be a fstream. It is also possible that after you solve the memory from the zip file, a component from the memory constructed is to derive a class from Basic_StreamBuf, here Just introduce a simple support output streambuf. If you improve it, or have a feature that supports various features, input and output, block buffer, etc., the trouble also tells me.

#ifndef __MEM_STREAM_BUFFER_H__

#define __MEM_STREAM_BUFFER_H____

/ *

Provides an extension of the C standard library. Support to create an ISTREAM class from a period of memory.

Author: Pan Liang 2004--10--25

XheartBlue 2004-10-25

This code is free code, which can only be used and modified.

But you have to keep this statement and author name.

* /

#include

Namespace STD

{

Template >

Class Mem_Streambuf: Public Basic_StreamBuf <_chart, _Chartrait>

{

Public: // Definition of type

Typedef _chart streambuf_type;

TYPEDEF _CHART CHAR_TYPE;

TYPEDEF INT_TYPE;

TypeDef TypeName Std :: streampos pos_type;

Typedef type s s ;: streamsize off_type;

protected:

SIZE_T M_BUFLEN

CHAR_TYPE * M_BUFFER;

BOOL M_BISALLOCINNER;

PUBLIC:

//Constructor

MEM_STREAMBUF (Char_Type * BUF, SIZE_T LEN)

{

Set_buf (buf, len);

}

MEM_STREAMBUF ()

{

m_buffer = null;

m_bufflen = 0;

m_bisallocinner = false;

}

MEM_STREAMBUF (SIZE_T LEN)

{

m_buffer = null;

m_bufflen = 0;

m_bisallocinner = false;

Alloc_buf (len);

_init ();

}

~ MEM_STREAMBUF ()

{

DEALLOC_BUF ();

}

Bool Alloc_buf (int LEN)

{

DEALLOC_BUF ();

m_buffer = new char_type [len];

m_bisallocinner = true;

m_buflen = len;

Return True;

}

BOOL set_buf (char_type * buf, size_t len)

{

M_Buffer = BUF;

m_buflen = len;

m_bisallocinner = false;

_init ();

Return True;}

BOOL DEAALLOC_BUF ()

{

IF (m_bisallocinner)

{

delete [] m_buffer;

}

m_buffer = null;

m_bufflen = 0;

m_bisallocinner = false;

Return True;

}

Char_type * get_buf ()

{

Return M_Buffer;

}

protected:

void _init ()

{

Setg (m_buffer, m_buffer, m_buffer m_bufflen);

Setp (m_buffer, m_buffer, m_buffer m_buffle;

}

}; // endl class mem_streambuf;

Template >

Class Basic_Imemstream: Public Basic_istream <_chart, _Chartrait>

{

PUBLIC:

Typedef Mem_StreamBuf <_chart, _Chartrait> stream_type;

TYPEDEF _CHART CHAR_TYPE;

protected:

stream_type m_buffer;

PUBLIC:

Basic_imemstream (stream_type * buf): Basic_istream <_chart, _Chartrait> (BUF)

{

}

Basic_imemstream (char_type * buf, size_t len)

: M_Buff, Len, Basic_istream <_chart, _Chartrait> (& M_Buffer)

{

}

Basic_imemstream (): m_buffer (), Basic_istream <_chart, _Chartrait> (& M_Buffer)

{

}

Basic_imemstream (int LEN)

: m_buffer (len), Basic_istream <_chart, _Chartrait> (& M_Buffer)

{

}

Void set_buf (char_type * buf, int LEN)

{

m_buffer.set_buf (buf, len);

}

Char_type * get_buf ()

{

Return m_buffer.get_buf ();

}

Basic_streambuf <_chart, _Chartrait> * RDBUF ()

{

Return & M_Buffer;

}

Void Rdbuf (Basic_StreamBuf <_Chart, _Chartrait> * BUF)

{

M_Buffer = BUF;

}

}

Template >

Class Basic_OmemStream: Public Basic_OStream <_chart, _Chartrait>

{

PUBLIC:

Typedef Mem_StreamBuf <_chart, _Chartrait> stream_type;

TYPEDEF _CHART CHAR_TYPE;

protected:

stream_type m_buffer;

Public: Basic_OmemStream (stream_type * buf): Basic_OStream <_chart, _Chartrait> (BUF)

{

}

Basic_omstream (char_type * buf, size_t len)

: M_Buff, Len, Basic_OStream <_Chart, _Chartrait> (& M_Buffer)

{

}

Basic_omemstream (): m_buffer (), Basic_OStream <_chart, _Chartrait> (& M_Buffer)

{

}

Basic_omstream (int LEN)

: m_buffer (len), Basic_OStream <_Chart, _Chartrait> (& M_Buffer)

{

}

Void set_buf (char_type * buf, int LEN)

{

m_buffer.set_buf (buf, len);

}

Char_type * get_buf ()

{

Return m_buffer.get_buf ();

}

Basic_streambuf <_chart, _Chartrait> * RDBUF ()

{

Return & M_Buffer;

}

Void Rdbuf (Basic_StreamBuf <_Chart, _Chartrait> * BUF)

{

M_Buffer = BUF;

}

}

Template >

Class Basic_MemStream: Public Basic_OStream <_chart, _Chartrait>, Public Basic_istream <_chart, _Chartrait>

{

PUBLIC:

Typedef Mem_StreamBuf <_chart, _Chartrait> stream_type;

TYPEDEF _CHART CHAR_TYPE;

protected:

stream_type m_buffer;

PUBLIC:

Basic_memstream (stream_type * buf)

: Basic_OStream <_chart, _Chartrait> (BUF),

Basic_istream <_chart, _Chartrait> (buf)

{

}

Basic_MemStream (char_type * buf, size_t len)

: M_Buffer (BUF, LEN),

Basic_ostream <_chart, _chartrait> (& M_Buffer),

Basic_istream <_chart, _chartrait> (& m_buffer)

{

}

Basic_memstream ()

: m_buffer (),

Basic_ostream <_chart, _chartrait> (& M_Buffer),

Basic_istream <_chart, _chartrait> (& m_buffer)

{

}

Basic_MemStream (int LEN)

: m_buffer (len),

Basic_istream <_chart, _chartrait> (& M_Buffer),

Basic_ostream <_chart, _chartrait> (& m_buffer)

{

}

Void set_buf (char_type * buf, int LEN) {

m_buffer.set_buf (buf, len);

}

Char_type * get_buf ()

{

Return m_buffer.get_buf ();

}

Basic_streambuf <_chart, _Chartrait> * RDBUF ()

{

Return & M_Buffer;

}

Void Rdbuf (Basic_StreamBuf <_Chart, _Chartrait> * BUF)

{

M_Buffer = BUF;

}

}

Typedef Basic_imemstream IMEMSTREAM;

Typedef Basic_imstream Wimemstream;

Typedef Basic_OmemStream omemstream;

Typedef Basic_OmemStream Womemstream;

Typedef Basic_Memstream memstream;

Typedef Basic_Memstream wmemstream;

}

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-101430.html

New Post(0)