view src/adplug/core/binio_virtual.h @ 1978:fa9f85cebade

s/vfs_/aud_vfs_/g
author William Pitcock <nenolod@atheme.org>
date Sun, 07 Oct 2007 00:25:33 -0500
parents cae46214b8bf
children b0ff8640f338
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 = aud_vfs_fopen(file, "rb");
	};

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

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

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

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

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

		return (Byte) c;
	};

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

	}

	long pos(void) {
		return aud_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 = aud_vfs_fopen(file, "wb");
	};

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

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

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

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

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

	}

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

#endif