2014-08-10 21:33:01 +02:00
|
|
|
#include <glib.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <libaudcore/plugin.h>
|
|
|
|
|
|
|
|
#include "../src/vgmstream.h"
|
2015-06-22 10:03:44 +02:00
|
|
|
#include "plugin.h"
|
2014-08-10 21:33:01 +02:00
|
|
|
#include "vfs.h"
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
typedef struct _VFSSTREAMFILE {
|
2014-08-10 21:33:01 +02:00
|
|
|
STREAMFILE sf;
|
|
|
|
VFSFile *vfsFile;
|
|
|
|
off_t offset;
|
2018-04-19 22:54:45 +02:00
|
|
|
char name[32768];
|
2014-08-10 21:33:01 +02:00
|
|
|
} VFSSTREAMFILE;
|
|
|
|
|
|
|
|
static STREAMFILE *open_vfs_by_VFSFILE(VFSFile *file, const char *path);
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
static size_t read_vfs(VFSSTREAMFILE *streamfile, uint8_t *dest, off_t offset,
|
|
|
|
size_t length) {
|
2014-08-10 21:33:01 +02:00
|
|
|
size_t sz;
|
|
|
|
// if the offsets don't match, then we need to perform a seek
|
2015-07-10 04:32:53 +02:00
|
|
|
if (streamfile->offset != offset) {
|
2015-06-22 10:03:44 +02:00
|
|
|
streamfile->vfsFile->fseek(offset, VFS_SEEK_SET);
|
2014-08-10 21:33:01 +02:00
|
|
|
streamfile->offset = offset;
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
sz = streamfile->vfsFile->fread(dest, 1, length);
|
2014-08-10 21:33:01 +02:00
|
|
|
// increment our current offset
|
|
|
|
streamfile->offset += sz;
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
static void close_vfs(VFSSTREAMFILE *streamfile) {
|
2014-08-10 21:33:01 +02:00
|
|
|
debugMessage("close_vfs");
|
2019-09-21 13:30:55 +02:00
|
|
|
delete streamfile->vfsFile; //fcloses the internal file too
|
2014-08-10 21:33:01 +02:00
|
|
|
free(streamfile);
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
static size_t get_size_vfs(VFSSTREAMFILE *streamfile) {
|
2015-06-22 10:03:44 +02:00
|
|
|
return streamfile->vfsFile->fsize();
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
static size_t get_offset_vfs(VFSSTREAMFILE *streamfile) {
|
2014-08-10 21:33:01 +02:00
|
|
|
return streamfile->offset;
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
static void get_name_vfs(VFSSTREAMFILE *streamfile, char *buffer,
|
|
|
|
size_t length) {
|
2014-08-10 21:33:01 +02:00
|
|
|
strncpy(buffer, streamfile->name, length);
|
2015-07-10 04:32:53 +02:00
|
|
|
buffer[length - 1] = '\0';
|
2014-08-10 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
static STREAMFILE *open_vfs_impl(VFSSTREAMFILE *streamfile,
|
|
|
|
const char *const filename,
|
|
|
|
size_t buffersize) {
|
2014-08-10 21:33:01 +02:00
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return open_vfs(filename);
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
STREAMFILE *open_vfs_by_VFSFILE(VFSFile *file, const char *path) {
|
|
|
|
VFSSTREAMFILE *streamfile = (VFSSTREAMFILE *)malloc(sizeof(VFSSTREAMFILE));
|
2014-08-10 21:33:01 +02:00
|
|
|
if (!streamfile)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// success, set our pointers
|
|
|
|
memset(streamfile, 0, sizeof(VFSSTREAMFILE));
|
|
|
|
|
2019-03-18 05:17:08 +01:00
|
|
|
streamfile->sf.read = (size_t (*)(STREAMFILE *, uint8_t *, off_t, size_t))read_vfs;
|
|
|
|
streamfile->sf.get_size = (size_t (*)(STREAMFILE *))get_size_vfs;
|
|
|
|
streamfile->sf.get_offset = (off_t (*)(STREAMFILE *))get_offset_vfs;
|
|
|
|
streamfile->sf.get_name = (void (*)(STREAMFILE *, char *, size_t))get_name_vfs;
|
|
|
|
streamfile->sf.open = (STREAMFILE *(*)(STREAMFILE *, const char *, size_t))open_vfs_impl;
|
|
|
|
streamfile->sf.close = (void (*)(STREAMFILE *))close_vfs;
|
2014-08-10 21:33:01 +02:00
|
|
|
|
|
|
|
streamfile->vfsFile = file;
|
2015-07-10 04:32:53 +02:00
|
|
|
streamfile->offset = 0;
|
2014-08-10 21:33:01 +02:00
|
|
|
strncpy(streamfile->name, path, sizeof(streamfile->name));
|
2015-07-10 04:32:53 +02:00
|
|
|
streamfile->name[sizeof(streamfile->name) - 1] = '\0';
|
2018-04-19 22:54:45 +02:00
|
|
|
|
|
|
|
// for reference, actual file path ("name" has protocol path, file://...).
|
|
|
|
// name should work for all situations but in case it's needed again maybe
|
|
|
|
// get_name should always return realname, as it's used to open companion VFSFiles
|
|
|
|
//{
|
|
|
|
// gchar *realname = g_filename_from_uri(path, NULL, NULL);
|
|
|
|
// strncpy(streamfile->realname, realname, sizeof(streamfile->realname));
|
|
|
|
// streamfile->realname[sizeof(streamfile->realname) - 1] = '\0';
|
|
|
|
// g_free(realname);
|
|
|
|
//}
|
2014-08-10 21:33:01 +02:00
|
|
|
|
|
|
|
return &streamfile->sf;
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:32:53 +02:00
|
|
|
STREAMFILE *open_vfs(const char *path) {
|
|
|
|
VFSFile *vfsFile = new VFSFile(path, "rb");
|
2017-12-03 05:29:22 +01:00
|
|
|
if (!vfsFile || !*vfsFile) {
|
|
|
|
delete vfsFile;
|
2014-08-10 21:33:01 +02:00
|
|
|
return NULL;
|
2017-12-03 05:29:22 +01:00
|
|
|
}
|
2014-08-10 21:33:01 +02:00
|
|
|
|
|
|
|
return open_vfs_by_VFSFILE(vfsFile, path);
|
|
|
|
}
|