Click here to Skip to main content
15,893,622 members
Articles / Desktop Programming / MFC

What's New in ACF 0.3

Rate me:
Please Sign up or sign in to vote.
3.39/5 (7 votes)
23 Jun 20043 min read 40.5K   193   8  
This article introduces what's new in ACF (Another C++ Framework) version 0.3.
//---------------------------------------------------------------------
//
// Copyright (C) 2004 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is 
// granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied warranty, 
// and with no claim as to its suitability for any purpose.
//
// AcfStream.h - defines simple streams
//

#ifndef __Acf_Stream__
#define __Acf_Stream__

namespace Acf {
	namespace IO {

//
// Classes defined in this file:
//
class Stream;
    class FileStream;
    class MemoryStream;

typedef RefPtr<Stream>    StreamPtr;
typedef RefPtr<FileStream>    FileStreamPtr;
typedef RefPtr<MemoryStream>    MemoryStreamPtr;

//---------------------------------------------------------------------
// enum SeekOrigin

ACF_DECLARE_ENUM(SeekOrigin)
    Begin = 0,
    Current = 1,
    End = 2,
ACF_END_ENUM()

//---------------------------------------------------------------------
// class Stream: abstract base class for all streams

class Stream : public Object
{
// Constructor & Destructor
protected:
    Stream() { }
    virtual ~Stream() { Close(); }

// Properties
public:
    // CanRead
    __declspec(property(get=get_CanRead)) bool CanRead;
    virtual bool get_CanRead() = 0;
    // CanWrite
    __declspec(property(get=get_CanWrite)) bool CanWrite;
    virtual bool get_CanWrite() = 0;
    // CanSeek
    __declspec(property(get=get_CanSeek)) bool CanSeek;
    virtual bool get_CanSeek() = 0;
    // Length
    __declspec(property(get=get_Length)) int64 Length;
    virtual int64 get_Length() = 0;
    // Position
    __declspec(property(get=get_Position, put=set_Position)) int64 Position;
    virtual int64 get_Position() = 0;
    virtual void set_Position(int64 value) = 0;

// Methods
public:
    virtual void SetLength(int64 value) = 0;
    virtual int64 Seek(int64 offset, SeekOrigin origin) = 0;
    virtual int Read(Array<byte>* buffer, int offset, int count) = 0;
    virtual void Write(Array<byte>* buffer, int offset, int count) = 0;
    virtual void Flush() = 0;
    virtual void Close() { }
};

//---------------------------------------------------------------------
// class FileStream

// enum FileMode

ACF_DECLARE_ENUM(FileMode)
	CreateNew = 1,
	Create = 2,
	Open = 3,
	OpenOrCreate = 4,
	Truncate = 5,
	Append = 6,
ACF_END_ENUM()

// enum FileAccess

ACF_DECLARE_ENUM(FileAccess)
	Read = 0x1,
	Write = 0x2,
	ReadWrite = Read | Write,
ACF_END_ENUM()

// enum FileShare

ACF_DECLARE_ENUM(FileShare)
	None = 0x0,
	Read = 0x1,
	Write = 0x2,
	ReadWrite = Read | Write,
ACF_END_ENUM()

class FileStream : public Stream
{
// Constants
public:
	static const int DefaultBufferSize = 4096;

// Fields
private:
	intptr_t _hFile;
	FileAccess _access;
    int64 _appendStart; // start position in append mode

	RefPtr<Array<byte> > _buffer; // shared read/write buffer
	int64 _bufferStart; // location of buffer in file
	int _bufferLength; // number of valid bytes in buffer
	int _bufferOffset; // position of next byte
	bool _bufferDirty; // true if buffer has been written to

// Constructors
public:
	FileStream(String* name, FileMode mode, FileAccess access = FileAccess::ReadWrite, 
		FileShare share = FileShare::ReadWrite, int bufferSize = DefaultBufferSize);

// Destructor
protected:
    virtual ~FileStream();

// Properties
public:
	override bool get_CanRead();
    override bool get_CanWrite();
    override bool get_CanSeek();
    override int64 get_Length();
    override int64 get_Position();
	override void set_Position(int64 value);

// Methods
public:
	override void SetLength(int64 value);
	override int64 Seek(int64 offset, SeekOrigin origin);
	override int Read(Array<byte>* buffer, int offset, int count);
	override void Write(Array<byte>* buffer, int offset, int count);
	override void Flush();
	override void Close();

private:
    int ReadFromBuffer(Array<byte>* buffer, int offset, int count);
    int ReadFromFile(Array<byte>* buffer, int offset, int count);
    int WriteToBuffer(Array<byte>* buffer, int offset, int count);
    int WriteToFile(Array<byte>* buffer, int offset, int count);
};

//---------------------------------------------------------------------
// class MemoryStream

class MemoryStream : public Stream
{
// Fields
private:
    RefPtr<Array<byte> > _buffer; // buffer allocated internally or externally
    int _origin; // start posistion of user-provided buffer
    int _position; // read/write position
    int _length; // number of bytes within the memory stream
    int _capacity; // length of usable portion of buffer for stream
    bool _expandable;
    bool _writable;
    bool _exposable; // true if the buffer can be returned to the user.
    bool _isOpen;

// Constructors
public:
    MemoryStream(int capacity = 0);
    MemoryStream(Array<byte>* buffer, bool writable = true);
    MemoryStream(Array<byte>* buffer, int index, int count, bool writable = true, 
        bool publiclyVisible = false);

// Destructor
protected:
    virtual ~MemoryStream();

// Properties
public:
    override bool get_CanRead();
    override bool get_CanWrite();
    override bool get_CanSeek();
    override int64 get_Length();
    override int64 get_Position();
    override void set_Position(int64 value);

    // Capacity
    __declspec(property(get=get_Capacity, put=set_Capacity)) int Capacity;
    virtual int get_Capacity();
    virtual void set_Capacity(int value);

// Methods
public:
    override void SetLength(int64 value) = 0;
    override int64 Seek(int64 offset, SeekOrigin origin);
    override int Read(Array<byte>* buffer, int offset, int count);
    override void Write(Array<byte>* buffer, int offset, int count);
    override void Flush();
    override void Close();

    virtual void WriteTo(Stream* stream);
    virtual RefPtr<Array<byte> > GetBuffer();
    virtual RefPtr<Array<byte> > ToArray();

private:
    bool EnsureCapacity(int value);
};

	} // namespace IO
} // namespace Acf

#endif // #ifndef __Acf_Stream__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Yingle Jia is a software engineer located in Beijing, China. He currently works at IBM CSDL (China Software Development Lab). His interests include C++/COM/C#/.NET/XML, etc. He likes coding and writing.

He is the creator of ACF (Another C++ Framework) project. See http://acfproj.sourceforge.net/.

He also has a blog at http://blogs.wwwcoder.com/yljia/

Comments and Discussions