Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 2 / lab.05.by mice / spice / src / csp_file

.h
Скачиваний:
10
Добавлен:
17.04.2013
Размер:
4.22 Кб
Скачать
/*******************************************************************************
* file:         csp_file.h                                                     *
* version:      0.0.1                                                          *
* author:       d-evil [tmd] (mailto:d-evil.tmd@mail.ru)                       *
* description:  not available                                                  *
* comments:     1. this object is single threaded. Avoid of using it in more   *
*               than  one threads.                                             *
*******************************************************************************/

#ifndef CSP_FILE_INCLUDED
#define CSP_FILE_INCLUDED


////////////////////////////////////////////////////////////////////////////////
// headers
#include <afx.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>


////////////////////////////////////////////////////////////////////////////////
// csp_file declaration
class csp_file {
public:
	////////////////////////////////////////////////////////////////////////////
	// csp_file public types
	#pragma pack(1)
	struct ssp_item {
		unsigned int pos;
		unsigned int length;
	};

	struct ssp_arch {
		unsigned int num;
		unsigned int next;
		unsigned int pos;
		ssp_item items[1];
	};
	#pragma pack()


	////////////////////////////////////////////////////
	// csp_file public constants
	static const int FLUSH_NONE		= 0x0000;
	static const int FLUSH_HEADER	= 0x0001;
	static const int FLUSH_ARCHES	= 0x0002;
	static const int FLUSH_ALL		= 0xFFFF;


	////////////////////////////////////////////////////////////////////////////
	// csp_file public functions
	csp_file();
	~csp_file();

	int set_items_in_arch(const int items_in_arch);
	int get_items_in_arch() const { return _items_in_arch; }
	int total_arches() const { return _header.total_arches; }
	int total_datagrams() const;

	int use_handle(const HANDLE hfile, const bool ro = false);
	HANDLE using_handle() const { return _hfile; }
	int flush(const int flush_mode = FLUSH_ALL);
	int finish_using();

	int arch_used(const ssp_arch *const arch) { return _arch_used(arch); }
	ssp_arch *load_arch(const int num) { return _load_arch(num); }
	void free_arch(ssp_arch *const arch) { _free_arch(arch); }
	ssp_arch *load_first_arch();
	ssp_arch *load_next_arch(ssp_arch *const arch, const bool reuse = true);

	int add_dtgm(const char *const dtgm, const int sz);
	int load_dtgm(const ssp_arch *const arch, const int num, char *const dtgm);	// fast
	int load_dtgm(const int num, char *const dtgm, int *const sz);	// slow

protected:
	////////////////////////////////////////////////////////////////////////////
	// protected types
	#pragma pack(1)
	struct _ssp_file_header {
		char type[4];	// must be ".SPF"
		int version_hi;	// 1
		int version_lo;	// 0
		unsigned int items_in_arch;
		unsigned int total_arches;
		unsigned int first_arch;	// position in file
		unsigned int last_arch;		// position in file
	};
	#pragma pack()


	////////////////////////////////////////////////////////////////////////////
	// protected constants
	static const int _VERSION_HI = 1;
	static const int _VERSION_LO = 0;
	static const char _SPF_TYPE_STR[];

	static const int _DEF_ITEMS_IN_ARCH = 0x40;
	static const int _DEF_ARCH_OFFSET	= sizeof(_ssp_file_header) + 0x2F;


	////////////////////////////////////////////////////////////////////////////
	// protected functions
	int _set_items_in_arch(const int items_in_arch);
	int _arch_used(const ssp_arch *const arch);

	int _read_header();
	int _write_header();
	void _make_header(const int total_arches = 0, const int last_arch = 0);

	ssp_arch *_load_arch_from(const int pos, ssp_arch *const garch = NULL);
	ssp_arch *_load_arch(const int num = 0, ssp_arch *const garch = NULL);

	int _save_arch(const ssp_arch *const arch);
	ssp_arch *_write_new_arch(const int num = 0);

	void _free_arch(ssp_arch *const arch);


private:
	HANDLE _hfile;
	bool _ro_mode;
	_ssp_file_header _header;
	int _items_in_arch;

	ssp_arch *_cur_arch;
	int _cur_arch_pos;


	// cashes
	int _arch_sz;
};



#endif
Соседние файлы в папке src