view src/adplug/core/binio_virtual.h @ 1404:ab7d89bb8fcb

Switched to new threading model
author Christian Birchinger <joker@netswarm.net>
date Sat, 04 Aug 2007 21:37:11 +0200
parents cae46214b8bf
children fa9f85cebade
line wrap: on
line source

/*
 * Copyright (c) 2006 William Pitcock <nenolod -at- atheme.org>
 * This code is in the public domain.
 */

#ifndef __BINIO_VIRTUAL__
#define __BINIO_VIRTUAL__

#include <binio.h>
#include <binstr.h>

#include <string>

#include <glib.h>
#include <audacious/vfs.h>

class vfsistream : public binistream, virtual public binio {
private:
	VFSFile *fd;

public:
	vfsistream() {};
	~vfsistream() {};

	vfsistream(VFSFile *fd) {
		this->fd = fd;
	};

	vfsistream(const char *file) {
		this->fd = vfs_fopen(file, "rb");
	};

	vfsistream(std::string &file) {
		this->fd = vfs_fopen(file.c_str(), "rb");
	};

	void open(const char *file) {
		this->fd = vfs_fopen(file, "rb");
	};

	void open(std::string &file) {
		this->fd = vfs_fopen(file.c_str(), "rb");
	};

	// XXX: this sucks because binio won't let us do sanity checking
	Byte getByte(void) {
		int c = vfs_getc(this->fd);

		if (c == EOF) err |= Eof;

		return (Byte) c;
	};

	void seek(long pos, Offset offs = Set) {
		switch (offs)
		{
			case Add:
				vfs_fseek(this->fd, pos, SEEK_CUR);
				break;
			case End:
				vfs_fseek(this->fd, pos, SEEK_END);
				break;
			case Set:
			default:
				vfs_fseek(this->fd, pos, SEEK_SET);
				break;
		}

	}

	long pos(void) {
		return vfs_ftell(this->fd);
	}
};

// XXX: binio sucks and doesn't let us just combine the two.
class vfsostream : public binostream, virtual public binio {
private:
	VFSFile *fd;

public:
	vfsostream() {};
	~vfsostream() {};

	vfsostream(VFSFile *fd) {
		this->fd = fd;
	};

	vfsostream(const char *file) {
		this->fd = vfs_fopen(file, "wb");
	};

	vfsostream(std::string &file) {
		this->fd = vfs_fopen(file.c_str(), "wb");
	};

	void open(const char *file) {
		this->fd = vfs_fopen(file, "wb");
	};

	void open(std::string &file) {
		this->fd = vfs_fopen(file.c_str(), "wb");
	};

	// XXX: this sucks because binio won't let us do sanity checking
	void putByte(Byte b) {
		vfs_fwrite(&b, 1, 1, this->fd);
	};

	void seek(long pos, Offset offs = Set) {
		switch (offs)
		{
			case Add:
				vfs_fseek(this->fd, pos, SEEK_CUR);
				break;
			case End:
				vfs_fseek(this->fd, pos, SEEK_END);
				break;
			case Set:
			default:
				vfs_fseek(this->fd, pos, SEEK_SET);
				break;
		}

	}

	long pos(void) {
		return vfs_ftell(this->fd);
	}
};

#endif