Add an extension to handle binary data. MsdFile is now
8-bit clean.
This commit is contained in:
+161
-93
@@ -10,7 +10,34 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* The original MSD format is simply:
|
||||||
|
*
|
||||||
|
* #PARAM0:PARAM1:PARAM2:PARAM3;
|
||||||
|
* #NEXTPARAM0:PARAM1:PARAM2:PARAM3;
|
||||||
|
*
|
||||||
|
* (The first field is typically an identifier, but doesn't have to be.)
|
||||||
|
*
|
||||||
|
* The semicolon is not optional, though if we hit a # on a new line, eg:
|
||||||
|
* #VALUE:PARAM1
|
||||||
|
* #VALUE2:PARAM2
|
||||||
|
* we'll recover.
|
||||||
|
*
|
||||||
|
* Extension: If : is followed by ASCII 1 (^A), we read a binary value, in this
|
||||||
|
* form:
|
||||||
|
*
|
||||||
|
* #VALUE:^A4,DATA:more tags;
|
||||||
|
* "4" is the number of bytes of data to expect, not including itself, and not
|
||||||
|
* including the comma separating it from the data. The data is completely unparsed
|
||||||
|
* and may contain colons, #, or whitespace without fear of corruption. The ^A
|
||||||
|
* character is used to avoid it showing up in real data. We only use this internally
|
||||||
|
* for caching; this is ugly, so don't use it in distributed data!
|
||||||
|
*
|
||||||
|
* TODO: Normal text fields need some way of escaping. We need to be able to escape
|
||||||
|
* colons and "//". Also, we should escape #s, so if we really want to put a # at the
|
||||||
|
* beginning of a line, we can.
|
||||||
|
*/
|
||||||
#include "MsdFile.h"
|
#include "MsdFile.h"
|
||||||
|
#include "RageLog.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "fcntl.h"
|
#include "fcntl.h"
|
||||||
|
|
||||||
@@ -24,121 +51,162 @@ MsdFile::~MsdFile()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
//returns true if successful, false otherwise
|
void MsdFile::AddParam( char *buf, int len )
|
||||||
bool MsdFile::ReadFile( CString sNewPath )
|
|
||||||
{
|
{
|
||||||
int fh;
|
int valueno = m_iNumValues-1;
|
||||||
|
int paramno = m_iNumParams[valueno];
|
||||||
|
ASSERT( paramno < MAX_PARAMS_PER_VALUE );
|
||||||
|
m_iNumParams[valueno]++;
|
||||||
|
m_sParams[valueno][paramno].assign(buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
/* Open a file */
|
void MsdFile::AddValue() /* (no extra charge) */
|
||||||
if( (fh = _open(sNewPath, _O_RDONLY, 0)) == -1 )
|
{
|
||||||
return false;
|
m_iNumValues++;
|
||||||
|
ASSERT( m_iNumValues < MAX_VALUES );
|
||||||
int iBufferSize = _filelength( fh ) + 1000; // +1000 because sometimes the bytes read is > filelength. Why?
|
}
|
||||||
_close( fh );
|
|
||||||
|
|
||||||
|
|
||||||
FILE* fp = fopen(sNewPath, "r");
|
|
||||||
if( fp == NULL )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// allocate a string to hold the file
|
|
||||||
char* szFileString = new char[iBufferSize];
|
|
||||||
ASSERT( szFileString );
|
|
||||||
|
|
||||||
int iBytesRead = fread( szFileString, 1, iBufferSize, fp );
|
|
||||||
ASSERT( iBufferSize > iBytesRead );
|
|
||||||
szFileString[iBytesRead] = '\0';
|
|
||||||
|
|
||||||
char* szParams[MAX_VALUES][MAX_PARAMS_PER_VALUE];
|
|
||||||
|
|
||||||
|
void MsdFile::ReadBuf( char *buf, int len )
|
||||||
|
{
|
||||||
|
int value_start = -1;
|
||||||
|
memset(m_iNumParams, 0, sizeof(m_iNumParams));
|
||||||
m_iNumValues = 0;
|
m_iNumValues = 0;
|
||||||
|
|
||||||
bool ReadingValue=false;
|
bool ReadingValue=false;
|
||||||
int i;
|
int i = 0;
|
||||||
for( i=0; i<iBytesRead; i++ )
|
while(i < len)
|
||||||
{
|
{
|
||||||
switch( szFileString[i] )
|
if(!strncmp(buf+i, "//", 2))
|
||||||
{
|
{
|
||||||
case '#': // begins a new value
|
/* //; erase with spaces until newline */
|
||||||
{
|
do {
|
||||||
/* Unfortunately, many of these files are missing ;'s.
|
buf[i] = ' ';
|
||||||
* For now, if we get a # when we thought we were inside
|
i++;
|
||||||
* a value/parameter, assume we missed the ;. Back up,
|
} while(buf[i] != '\n');
|
||||||
* delete whitespace and end the value. */
|
|
||||||
if(ReadingValue) {
|
|
||||||
/* Make sure this # is the first non-whitespace character on the line. */
|
|
||||||
bool FirstChar = true;
|
|
||||||
int j;
|
|
||||||
for(j = i-1; j >= 0 && !strchr("\r\n", szFileString[j]); --j)
|
|
||||||
{
|
|
||||||
if(szFileString[j] == ' ' || szFileString[j] == '\t')
|
|
||||||
continue;
|
|
||||||
|
|
||||||
FirstChar = false;
|
continue;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
if(!FirstChar) {
|
|
||||||
/* Oops, we're not; handle this like a regular character. */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(j = i-1; j >= 0 && isspace(szFileString[j]); --j)
|
if(!ReadingValue) {
|
||||||
szFileString[j] = 0;
|
// fast forward until the next '#' (optimization)
|
||||||
}
|
while(i < len && buf[i] != '#') i++;
|
||||||
|
}
|
||||||
|
|
||||||
ReadingValue=true;
|
if(ReadingValue && buf[i] == '#') {
|
||||||
m_iNumValues++;
|
/* Unfortunately, many of these files are missing ;'s.
|
||||||
ASSERT( m_iNumValues < MAX_VALUES );
|
* If we get a # when we thought we were inside a value, assume we
|
||||||
int iCurValueIndex = m_iNumValues-1;
|
* missed the ;. Back up and end the value. */
|
||||||
m_iNumParams[iCurValueIndex] = 1;
|
/* Make sure this # is the first non-whitespace character on the line. */
|
||||||
szParams[iCurValueIndex][0] = &szFileString[i+1];
|
bool FirstChar = true;
|
||||||
}
|
int j;
|
||||||
break;
|
for(j = i-1; j >= 0 && !strchr("\r\n", buf[j]); --j)
|
||||||
case ':': // begins a new parameter
|
|
||||||
{
|
{
|
||||||
szFileString[i] = '\0';
|
if(buf[j] == ' ' || buf[j] == '\t')
|
||||||
int iCurValueIndex = m_iNumValues-1;
|
continue;
|
||||||
m_iNumParams[iCurValueIndex] ++;
|
|
||||||
int iCurParamIndex = m_iNumParams[iCurValueIndex]-1;
|
FirstChar = false;
|
||||||
szParams[iCurValueIndex][iCurParamIndex] = &szFileString[i+1];
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case ';': // ends a value
|
if(!FirstChar) {
|
||||||
{
|
/* Oops, we're not; handle this like a regular character. */
|
||||||
szFileString[i] = '\0';
|
i++;
|
||||||
// fast forward until just before the next '#'
|
continue;
|
||||||
while( szFileString[i+1] != '#' && i<iBytesRead )
|
|
||||||
i++;
|
|
||||||
ReadingValue=false;
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case '/':
|
AddParam(buf+value_start, j - value_start);
|
||||||
if( szFileString[i+1] == '/' )
|
ReadingValue=false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* # starts a new value. */
|
||||||
|
if(!ReadingValue && buf[i] == '#') {
|
||||||
|
AddValue();
|
||||||
|
ReadingValue=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!ReadingValue)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
continue; /* nothing else is meaningful outside of a value */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* : and ; end the current param, if any. */
|
||||||
|
if(value_start != -1 && (buf[i] == ':' || buf[i] == ';'))
|
||||||
|
AddParam(buf+value_start, i - value_start);
|
||||||
|
|
||||||
|
/* # and : begin new params. */
|
||||||
|
if(buf[i] == '#' || buf[i] == ':')
|
||||||
|
{
|
||||||
|
i++; /* skip */
|
||||||
|
value_start = i;
|
||||||
|
if(buf[value_start] == '\001')
|
||||||
{
|
{
|
||||||
// advance until new line
|
value_start++;
|
||||||
for( ; i<iBytesRead; i++ )
|
|
||||||
|
/* Binary param. Expect digits followed by a comma. */
|
||||||
|
if(!isdigit(buf[value_start]))
|
||||||
{
|
{
|
||||||
if( szFileString[i] == '\n' )
|
LOG->Trace("Expected digit at position %i", value_start);
|
||||||
break;
|
ReadingValue = false; /* so we skip to the next value */
|
||||||
else
|
continue;
|
||||||
szFileString[i] = ' ';
|
|
||||||
}
|
}
|
||||||
|
int bytes = atoi(buf+value_start);
|
||||||
|
if(bytes < 0) bytes = 0; /* sanity */
|
||||||
|
while(isdigit(buf[value_start]))
|
||||||
|
value_start++;
|
||||||
|
if(buf[value_start] != ',')
|
||||||
|
{
|
||||||
|
LOG->Trace("Expected comma at position %i", value_start);
|
||||||
|
ReadingValue = false; /* so we skip to the next value */
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
value_start++;
|
||||||
|
|
||||||
|
/* Make sure we have enough data left. */
|
||||||
|
if(len - value_start < bytes)
|
||||||
|
{
|
||||||
|
LOG->Trace("%i bytes not available at position %i", value_start);
|
||||||
|
ReadingValue = false; /* so we skip to the next value */
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
AddParam(buf+value_start, bytes);
|
||||||
|
i = value_start + bytes;
|
||||||
|
value_start = -1;
|
||||||
}
|
}
|
||||||
// fall through. If we didn't take the if above, then this '/' is part of a parameter!
|
continue;
|
||||||
default:
|
|
||||||
; // do nothing
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for( unsigned x=0; x<m_iNumValues; x++ )
|
/* ; ends the current value. */
|
||||||
{
|
if(buf[i] == ';')
|
||||||
for( unsigned y=0; y<m_iNumParams[x]; y++ )
|
ReadingValue=false;
|
||||||
{
|
|
||||||
m_sParams[x][y] = szParams[x][y];
|
i++;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns true if successful, false otherwise
|
||||||
|
bool MsdFile::ReadFile( CString sNewPath )
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
/* Open a file */
|
||||||
|
if( (fd = open(sNewPath, _O_RDONLY, 0)) == -1 )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int iBufferSize = _filelength( fd ) + 1000; // +1000 because sometimes the bytes read is > filelength. Why?
|
||||||
|
|
||||||
|
// allocate a string to hold the file
|
||||||
|
char* szFileString = new char[iBufferSize];
|
||||||
|
|
||||||
|
int iBytesRead = read( fd, szFileString, iBufferSize );
|
||||||
|
close( fd );
|
||||||
|
|
||||||
|
ASSERT( iBufferSize > iBytesRead );
|
||||||
|
szFileString[iBytesRead] = '\0';
|
||||||
|
|
||||||
|
ReadBuf(szFileString, iBytesRead);
|
||||||
|
|
||||||
fclose( fp );
|
|
||||||
delete [] szFileString;
|
delete [] szFileString;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ const unsigned MAX_PARAMS_PER_VALUE = 10;
|
|||||||
|
|
||||||
class MsdFile
|
class MsdFile
|
||||||
{
|
{
|
||||||
|
void ReadBuf( char *buf, int len );
|
||||||
|
void AddParam( char *buf, int len );
|
||||||
|
void AddValue();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MsdFile();
|
MsdFile();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user