Squashed 'libretro-common/' changes from 999e69a9f1..c3c0facc97

c3c0facc97 whoops, cute bug
fc2866b999 no real point leaving these unimplemented, they're easy
bd1d5800db Add filestream_scanf

git-subtree-dir: libretro-common
git-subtree-split: c3c0facc97d3a4c065c72961b5ab165b99d3153c
This commit is contained in:
twinaphex 2018-06-22 15:00:02 +02:00
parent c68f2796b9
commit 48cb38619f
2 changed files with 98 additions and 0 deletions

View File

@ -78,6 +78,8 @@ char *filestream_gets(RFILE *stream, char *s, size_t len);
int filestream_getc(RFILE *stream);
int filestream_scanf(RFILE *stream, const char* format, ...);
int filestream_eof(RFILE *stream);
bool filestream_write_file(const char *path, const void *data, int64_t size);

View File

@ -191,6 +191,102 @@ int filestream_getc(RFILE *stream)
return EOF;
}
int filestream_scanf(RFILE *stream, const char* format, ...)
{
char buf[4096];
char subfmt[64];
const char * bufiter = buf;
int64_t startpos = filestream_tell(stream);
va_list args;
int ret = 0;
int maxlen = filestream_read(stream, buf, sizeof(buf)-1);
buf[maxlen] = '\0';
va_start(args, format);
while (*format)
{
if (*format == '%')
{
char* subfmtiter = subfmt;
int subret;
int sublen;
bool asterisk = false;
*subfmtiter++ = *format++; /* '%' */
/* %[*][width][length]specifier */
if (*format == '*')
{
asterisk = true;
*subfmtiter++ = *format++;
}
while (isdigit(*format)) *subfmtiter++ = *format++; /* width */
/* length */
if (*format == 'h' || *format == 'l')
{
if (format[1] == format[0]) *subfmtiter++ = *format++;
*subfmtiter++ = *format++;
}
else if (*format == 'j' || *format == 'z' || *format == 't' || *format == 'L')
{
*subfmtiter++ = *format++;
}
/* specifier - always a single character (except ]) */
if (*format == '[')
{
while (*format != ']') *subfmtiter++ = *format++;
*subfmtiter++ = *format++;
}
else *subfmtiter++ = *format++;
*subfmtiter++ = '%';
*subfmtiter++ = 'n';
*subfmtiter++ = '\0';
if (sizeof(void*) != sizeof(long*)) abort(); /* all pointers must have the same size */
if (asterisk)
{
if (sscanf(bufiter, subfmt, &sublen) != 0) break;
}
else
{
if (sscanf(bufiter, subfmt, va_arg(args, void*), &sublen) != 1) break;
}
ret++;
bufiter += sublen;
}
else if (isspace(*format))
{
while (isspace(*bufiter)) bufiter++;
format++;
}
else
{
if (*bufiter != *format) break;
bufiter++;
format++;
}
}
va_end(args);
filestream_seek(stream, startpos+(bufiter-buf), RETRO_VFS_SEEK_POSITION_START);
return ret;
}
int64_t filestream_seek(RFILE *stream, int64_t offset, int seek_position)
{
int64_t output;