mirror of
https://github.com/vgmstream/vgmstream.git
synced 2025-02-17 19:19:16 +01:00
streamfile: minor performance tweaks
This commit is contained in:
parent
6405113c82
commit
43d89a46a1
@ -953,22 +953,12 @@ STREAMFILE* open_streamfile(STREAMFILE* sf, const char* pathname) {
|
||||
|
||||
STREAMFILE* open_streamfile_by_ext(STREAMFILE* sf, const char* ext) {
|
||||
char filename[PATH_LIMIT];
|
||||
int filename_len, fileext_len;
|
||||
|
||||
sf->get_name(sf, filename, sizeof(filename));
|
||||
get_streamfile_name(sf, filename, sizeof(filename));
|
||||
|
||||
filename_len = strlen(filename);
|
||||
fileext_len = strlen(filename_extension(filename));
|
||||
swap_extension(filename, sizeof(filename), ext);
|
||||
|
||||
if (fileext_len == 0) {/* extensionless */
|
||||
strcat(filename,".");
|
||||
strcat(filename,ext);
|
||||
}
|
||||
else {
|
||||
strcpy(filename + filename_len - fileext_len, ext);
|
||||
}
|
||||
|
||||
return sf->open(sf, filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
||||
return open_streamfile(sf, filename);
|
||||
}
|
||||
|
||||
STREAMFILE* open_streamfile_by_filename(STREAMFILE* sf, const char* filename) {
|
||||
@ -978,7 +968,7 @@ STREAMFILE* open_streamfile_by_filename(STREAMFILE* sf, const char* filename) {
|
||||
|
||||
if (!sf || !filename || !filename[0]) return NULL;
|
||||
|
||||
sf->get_name(sf, fullname, sizeof(fullname));
|
||||
get_streamfile_name(sf, fullname, sizeof(fullname));
|
||||
|
||||
//todo normalize separators in a better way, safeops, improve copying
|
||||
|
||||
@ -1027,7 +1017,7 @@ STREAMFILE* open_streamfile_by_filename(STREAMFILE* sf, const char* filename) {
|
||||
strcpy(fullname, filename);
|
||||
}
|
||||
|
||||
return sf->open(sf, fullname, STREAMFILE_DEFAULT_BUFFER_SIZE);
|
||||
return open_streamfile(sf, fullname);
|
||||
}
|
||||
|
||||
STREAMFILE* reopen_streamfile(STREAMFILE* sf, size_t buffer_size) {
|
||||
@ -1037,7 +1027,7 @@ STREAMFILE* reopen_streamfile(STREAMFILE* sf, size_t buffer_size) {
|
||||
|
||||
if (buffer_size == 0)
|
||||
buffer_size = STREAMFILE_DEFAULT_BUFFER_SIZE;
|
||||
sf->get_name(sf, pathname,sizeof(pathname));
|
||||
get_streamfile_name(sf, pathname, sizeof(pathname));
|
||||
return sf->open(sf, pathname, buffer_size);
|
||||
}
|
||||
|
||||
|
48
src/util.c
48
src/util.c
@ -2,34 +2,40 @@
|
||||
#include "util.h"
|
||||
#include "streamtypes.h"
|
||||
|
||||
const char * filename_extension(const char * pathname) {
|
||||
const char * filename;
|
||||
const char * extension;
|
||||
const char* filename_extension(const char* pathname) {
|
||||
const char* extension;
|
||||
|
||||
/* favor strrchr (optimized/aligned) rather than homemade loops */
|
||||
extension = strrchr(pathname,'.');
|
||||
|
||||
/* find possible separator first to avoid misdetecting folders with dots + extensionless files
|
||||
* (allow both slashes as plugin could pass normalized '/') */
|
||||
filename = strrchr(pathname, '/');
|
||||
if (filename != NULL)
|
||||
filename++; /* skip separator */
|
||||
else {
|
||||
filename = strrchr(pathname, '\\');
|
||||
if (filename != NULL)
|
||||
filename++; /* skip separator */
|
||||
else
|
||||
filename = pathname; /* pathname has no separators (single filename) */
|
||||
if (extension != NULL) {
|
||||
/* probably has extension */
|
||||
extension++; /* skip dot */
|
||||
|
||||
/* find possible separators to avoid misdetecting folders with dots + extensionless files
|
||||
* (after the above to reduce search space, allows both slashes in case of non-normalized names) */
|
||||
if (strchr(extension, '/') == NULL && strchr(extension, '\\') == NULL)
|
||||
return extension; /* no slashes = really has extension */
|
||||
}
|
||||
|
||||
extension = strrchr(filename,'.');
|
||||
if (extension != NULL)
|
||||
extension++; /* skip dot */
|
||||
else
|
||||
extension = filename + strlen(filename); /* point to null (empty "" string for extensionless files) */
|
||||
|
||||
return extension;
|
||||
/* extensionless: point to null after current name
|
||||
* (could return NULL but prev code expects with to return an actual c-string) */
|
||||
return pathname + strlen(pathname);
|
||||
}
|
||||
|
||||
void swap_extension(char* pathname, size_t pathname_len, const char* swap) {
|
||||
char* extension = (char*)filename_extension(pathname);
|
||||
//todo safeops
|
||||
if (extension[0] == '\0') {
|
||||
strcat(pathname, ".");
|
||||
strcat(pathname, swap);
|
||||
}
|
||||
else {
|
||||
strcpy(extension, swap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* unused */
|
||||
/*
|
||||
void interleave_channel(sample_t * outbuffer, sample_t * inbuffer, int32_t sample_count, int channel_count, int channel_number) {
|
||||
|
@ -175,7 +175,10 @@ int round10(int val);
|
||||
|
||||
/* return a file's extension (a pointer to the first character of the
|
||||
* extension in the original filename or the ending null byte if no extension */
|
||||
const char * filename_extension(const char * filename);
|
||||
const char* filename_extension(const char* pathname);
|
||||
|
||||
/* change pathname's extension to another (or add it if extensionless) */
|
||||
void swap_extension(char* pathname, size_t pathname_len, const char* swap);
|
||||
|
||||
/* swap samples in machine endianness to little endian (useful to write .wav) */
|
||||
void swap_samples_le(sample_t *buf, int count);
|
||||
|
Loading…
x
Reference in New Issue
Block a user