vgmstream/winamp/in_streamfile.c

133 lines
3.6 KiB
C
Raw Normal View History

2021-04-18 13:04:00 +02:00
/**
* streamfile for Winamp
*/
#include <stdio.h>
#include <io.h>
#include "in_vgmstream.h"
/* ************************************* */
/* IN_UNICODE */
/* ************************************* */
/* opens a utf16 (unicode) path */
2021-09-05 17:53:47 +02:00
static FILE* wa_fopen(const in_char* wpath) {
2021-04-18 13:04:00 +02:00
#ifdef UNICODE_INPUT_PLUGIN
return _wfopen(wpath, L"rb");
2021-04-18 13:04:00 +02:00
#else
return fopen(wpath, "rb");
2021-04-18 13:04:00 +02:00
#endif
}
/* in theory fdopen and _wfdopen are the same, except flags is a wchar */
#if 0
2021-04-18 13:04:00 +02:00
/* dupes a utf16 (unicode) file */
static FILE* wa_fdopen(int fd) {
#ifdef UNICODE_INPUT_PLUGIN
return _wfdopen(fd,L"rb");
#else
return fdopen(fd,"rb");
#endif
}
#endif
2021-04-18 13:04:00 +02:00
/* ************************************* */
/* IN_STREAMFILE */
/* ************************************* */
/* a STREAMFILE that operates via STDIOSTREAMFILE but handles Winamp's unicode (in_char) paths */
typedef struct {
2021-09-05 17:53:47 +02:00
STREAMFILE vt;
STREAMFILE* stdiosf;
2021-04-18 13:04:00 +02:00
} WINAMP_STREAMFILE;
2021-09-05 17:53:47 +02:00
static STREAMFILE* open_winamp_streamfile_by_file(FILE* infile, const char* path);
//static STREAMFILE* open_winamp_streamfile_by_ipath(const in_char* wpath);
2021-04-18 13:04:00 +02:00
2021-09-05 17:53:47 +02:00
static size_t wasf_read(WINAMP_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
return sf->stdiosf->read(sf->stdiosf, dst, offset, length);
2021-04-18 13:04:00 +02:00
}
2021-09-04 21:57:23 +02:00
static size_t wasf_get_size(WINAMP_STREAMFILE* sf) {
2021-04-18 13:04:00 +02:00
return sf->stdiosf->get_size(sf->stdiosf);
}
2021-09-04 21:57:23 +02:00
static offv_t wasf_get_offset(WINAMP_STREAMFILE* sf) {
2021-04-18 13:04:00 +02:00
return sf->stdiosf->get_offset(sf->stdiosf);
}
static void wasf_get_name(WINAMP_STREAMFILE* sf, char* buffer, size_t length) {
sf->stdiosf->get_name(sf->stdiosf, buffer, length);
}
2021-09-05 17:53:47 +02:00
static STREAMFILE* wasf_open(WINAMP_STREAMFILE* sf, const char* const filename, size_t buffersize) {
2024-08-18 17:50:55 +02:00
in_char wpath[WINAMP_PATH_LIMIT];
2021-04-18 13:04:00 +02:00
if (!filename)
return NULL;
/* no need to wfdopen here, may use standard IO */
2021-04-18 13:04:00 +02:00
/* STREAMFILEs carry char/UTF8 names, convert to wchar for Winamp */
2024-08-18 17:50:55 +02:00
wa_char_to_ichar(wpath, WINAMP_PATH_LIMIT, filename);
2021-04-18 13:04:00 +02:00
return open_winamp_streamfile_by_ipath(wpath);
}
static void wasf_close(WINAMP_STREAMFILE* sf) {
/* closes infile_ref + frees in the internal STDIOSTREAMFILE (fclose for wchar is not needed) */
sf->stdiosf->close(sf->stdiosf);
free(sf); /* and the current struct */
}
2021-09-05 17:53:47 +02:00
static STREAMFILE* open_winamp_streamfile_by_file(FILE* file, const char* path) {
2021-04-18 13:04:00 +02:00
WINAMP_STREAMFILE* this_sf = NULL;
STREAMFILE* stdiosf = NULL;
this_sf = calloc(1,sizeof(WINAMP_STREAMFILE));
if (!this_sf) goto fail;
stdiosf = open_stdio_streamfile_by_file(file, path);
if (!stdiosf) goto fail;
2021-09-05 17:53:47 +02:00
this_sf->vt.read = (void*)wasf_read;
this_sf->vt.get_size = (void*)wasf_get_size;
this_sf->vt.get_offset = (void*)wasf_get_offset;
this_sf->vt.get_name = (void*)wasf_get_name;
this_sf->vt.open = (void*)wasf_open;
this_sf->vt.close = (void*)wasf_close;
2021-04-18 13:04:00 +02:00
this_sf->stdiosf = stdiosf;
2021-09-05 17:53:47 +02:00
return &this_sf->vt;
2021-04-18 13:04:00 +02:00
fail:
close_streamfile(stdiosf);
free(this_sf);
return NULL;
}
STREAMFILE* open_winamp_streamfile_by_ipath(const in_char* wpath) {
FILE* infile = NULL;
STREAMFILE* sf;
2024-08-18 17:50:55 +02:00
char path[WINAMP_PATH_LIMIT];
2021-04-18 13:04:00 +02:00
/* convert to UTF-8 if needed for internal use */
2024-08-18 17:50:55 +02:00
wa_ichar_to_char(path, WINAMP_PATH_LIMIT, wpath);
2021-04-18 13:04:00 +02:00
/* open a FILE from a Winamp (possibly UTF-16) path */
infile = wa_fopen(wpath);
if (!infile) {
/* allow non-existing files in some cases */
if (!vgmstream_is_virtual_filename(path))
return NULL;
}
sf = open_winamp_streamfile_by_file(infile, path);
2021-04-18 13:04:00 +02:00
if (!sf) {
if (infile) fclose(infile);
}
return sf;
}