📄 mstream.h
字号:
#ifndef __mstream_h
#define __mstream_h
/////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/mstream.h 4 2/08/03 6:40a Arm $
//---------------------------------------------------------------------------
// This file is part of "libAndrix" library - a collection of classes
// and functions developed by Andrei Remenchuk.
//---------------------------------------------------------------------------
// While you may own complete copyright on the project with which you have
// received this file, the author reserves the right to use code contained
// in this very file for any purposes, including publishing and usage in
// any free or commercial software.
//
// You may re-distribute this file or re-use it in your own free or
// commercial software provided that this text is included in the file.
// If you change this file you must include clear notice stating that
// you changed this file and the date of change.
//
// This statement doesn't apply to other files that are part of the same
// package unless otherwise noted.
//---------------------------------------------------------------------------
// (c) 1998-2002 Andrei Remenchuk <andrei@remenchuk.com>
//---------------------------------------------------------------------------
// mstream.h - memory stream
/////////////////////////////////////////////////////////////////////////////
#include <stdarg.h>
#include <stdio.h>
// constants for MemoryStream::seek()
#define MSEEK_START 0
#define MSEEK_CURRENT 1
#define MSEEK_END 2
// constants for byte_order
#define BO_LITTLE_ENDIAN 0
#define BO_BIG_ENDIAN 1
#undef tell
/**
* Memory-based object allowing file-type manipulations.
* Internal buffer always has one extra zero byte at the end, so that
* it is possible to use string functions on the stored data.
*/
class mstream
{
private:
char* m_buffer;
int m_size;
int m_capacity;
int m_position;
short m_byte_order;
int m_last_read_count;
public:
mstream(int initial_capacity = 0);
void set_byte_order(short new_byte_order);
int tell() const { return m_position; }
int seek(int offset, int from = MSEEK_START);
void rewind() { seek(0);}
bool eof() const { return m_position == m_size; }
void* provide_window(int size);
int load_from_file(const char *filename);
int load_from_stream(FILE* f);
int save_to_file(const char *filename) const;
int printf(const char* format, ...);
int vprintf(const char* format, va_list args);
int puts(const char* s);
int putq(const char* s);
int putsl(const char* s);
void put_b(int arg);
void put_w(int arg);
void put_l(int arg);
void put_f(double arg);
void put_z(const char* s);
void put_lz(const char* s);
int put_zeroes(int count);
int write(const void* b, int count);
int stdread(void* b, int count);
int read(void* b, int count);
int read_zeroes(int count);
char read_b();
short read_w();
int read_l();
double read_f();
const char* read_z();
const char* read_lz();
int size() const { return m_size; }
char* data() const { return m_buffer; }
void clear();
// reports whether or not there's more data after current position
bool have_more_data() const;
// returns a pointer to the location in buffer at current position
const char* remainder() const;
const char* next_line(void** position);
bool fail() const;
bool bad() const;
int gcount() const;
mstream& operator << (const char *s) { putsl(s); return *this; }
mstream& operator << (const char c) { put_b(c); return *this; }
mstream& operator << (const short c) { put_w(c); return *this; }
mstream& operator << (const int c) { put_l(c); return *this; }
~mstream();
};
#if defined( __hppa__ ) || defined( _HPUX_SOURCE )
#define HOST_BYTE_ORDER BO_BIG_ENDIAN
#else
#define HOST_BYTE_ORDER BO_LITTLE_ENDIAN
#endif
/*
class istream : public mstream
{
};
class ostream : public mstream
{
};
extern istream cin;
extern ostream cout;
extern ostream cerr;
*/
#endif // __mstream_h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -