2003-02-16 04:01:45 +00:00
|
|
|
#include "global.h"
|
2001-11-04 19:34:28 +00:00
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
2002-03-30 20:00:13 +00:00
|
|
|
File: RageUtil
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
Desc: See header.
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
2002-03-30 20:00:13 +00:00
|
|
|
Chris Danford
|
2001-11-04 19:34:28 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
2003-07-04 18:03:20 +00:00
|
|
|
#include "RageMath.h"
|
2001-11-03 10:52:42 +00:00
|
|
|
#include "RageUtil.h"
|
2003-07-03 02:41:27 +00:00
|
|
|
#include "RageLog.h"
|
2003-12-07 23:43:58 +00:00
|
|
|
#include "RageFile.h"
|
2003-07-22 07:47:27 +00:00
|
|
|
#include "arch/arch.h"
|
2002-07-23 01:41:40 +00:00
|
|
|
|
2002-10-24 07:09:38 +00:00
|
|
|
#include <numeric>
|
2003-01-03 05:26:15 +00:00
|
|
|
#include <time.h>
|
2002-10-28 05:30:45 +00:00
|
|
|
#include <math.h>
|
2003-01-06 02:48:14 +00:00
|
|
|
#include <map>
|
2003-07-03 09:22:03 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
2003-02-15 00:32:32 +00:00
|
|
|
|
2003-01-03 05:26:15 +00:00
|
|
|
unsigned long randseed = time(NULL);
|
|
|
|
|
|
2003-07-30 20:31:50 +00:00
|
|
|
RandomGen::RandomGen( unsigned long seed_ )
|
|
|
|
|
{
|
|
|
|
|
seed = seed_;
|
|
|
|
|
if(seed == 0)
|
|
|
|
|
seed = time(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RandomGen::operator() ( int maximum )
|
|
|
|
|
{
|
|
|
|
|
seed = 1664525L * seed + 1013904223L;
|
2003-08-03 04:47:53 +00:00
|
|
|
return (seed >> 2) % maximum;
|
2003-07-30 20:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-16 22:49:40 +00:00
|
|
|
void fapproach( float& val, float other_val, float to_move )
|
|
|
|
|
{
|
2003-10-21 21:33:51 +00:00
|
|
|
RAGE_ASSERT_M( to_move >= 0, ssprintf("to_move: %f", to_move) );
|
2003-04-16 22:49:40 +00:00
|
|
|
if( val == other_val )
|
|
|
|
|
return;
|
|
|
|
|
float fDelta = other_val - val;
|
|
|
|
|
float fSign = fDelta / fabsf( fDelta );
|
|
|
|
|
float fToMove = fSign*to_move;
|
|
|
|
|
if( fabsf(fToMove) > fabsf(fDelta) )
|
|
|
|
|
fToMove = fDelta; // snap
|
|
|
|
|
val += fToMove;
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-05 20:18:02 +00:00
|
|
|
/* Return a positive x mod y. */
|
|
|
|
|
float fmodfp(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
x = fmodf(x, y); /* x is [-y,y] */
|
|
|
|
|
x += y; /* x is [0,y*2] */
|
|
|
|
|
x = fmodf(x, y); /* x is [0,y] */
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-20 06:28:22 +00:00
|
|
|
int power_of_two(int input)
|
|
|
|
|
{
|
|
|
|
|
int value = 1;
|
|
|
|
|
|
|
|
|
|
while ( value < input ) value <<= 1;
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-03 05:26:15 +00:00
|
|
|
bool IsAnInt( const CString &s )
|
2001-12-28 10:15:59 +00:00
|
|
|
{
|
2003-05-22 21:13:02 +00:00
|
|
|
if( !s.size() )
|
2001-12-28 10:15:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
2002-12-17 06:47:19 +00:00
|
|
|
for( int i=0; s[i]; i++ )
|
2001-12-28 10:15:59 +00:00
|
|
|
if( s[i] < '0' || s[i] > '9' )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-03 22:51:01 +00:00
|
|
|
bool IsHexVal( const CString &s )
|
|
|
|
|
{
|
2003-05-22 21:13:29 +00:00
|
|
|
if( !s.size() )
|
2003-01-03 22:51:01 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for( int i=0; s[i]; i++ )
|
|
|
|
|
if( !(s[i] >= '0' && s[i] <= '9') &&
|
|
|
|
|
!(toupper(s[i]) >= 'A' && toupper(s[i]) <= 'F'))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-07 22:54:50 +00:00
|
|
|
float TimeToSeconds( const CString &sHMS )
|
2002-04-28 20:42:32 +00:00
|
|
|
{
|
|
|
|
|
CStringArray arrayBits;
|
|
|
|
|
split( sHMS, ":", arrayBits, false );
|
|
|
|
|
|
2002-10-31 02:11:52 +00:00
|
|
|
while( arrayBits.size() < 3 )
|
2002-10-24 19:55:09 +00:00
|
|
|
arrayBits.insert(arrayBits.begin(), "0" ); // pad missing bits
|
2002-04-28 20:42:32 +00:00
|
|
|
|
|
|
|
|
float fSeconds = 0;
|
|
|
|
|
fSeconds += atoi( arrayBits[0] ) * 60 * 60;
|
|
|
|
|
fSeconds += atoi( arrayBits[1] ) * 60;
|
|
|
|
|
fSeconds += (float)atof( arrayBits[2] );
|
|
|
|
|
|
|
|
|
|
return fSeconds;
|
|
|
|
|
}
|
2001-12-28 10:15:59 +00:00
|
|
|
|
2002-08-01 13:42:56 +00:00
|
|
|
CString SecondsToTime( float fSecs )
|
|
|
|
|
{
|
2003-06-24 20:21:09 +00:00
|
|
|
const int iMinsDisplay = (int)fSecs/60;
|
|
|
|
|
const int iSecsDisplay = (int)fSecs - iMinsDisplay*60;
|
|
|
|
|
const float fLeftoverDisplay = (fSecs - iMinsDisplay*60 - iSecsDisplay) * 100;
|
2002-10-02 05:42:59 +00:00
|
|
|
CString sReturn = ssprintf( "%02d:%02d:%02.0f", iMinsDisplay, iSecsDisplay, min(99.0f,fLeftoverDisplay) );
|
2003-06-24 20:21:09 +00:00
|
|
|
if( iMinsDisplay >= 60 )
|
|
|
|
|
{
|
2002-10-16 18:25:14 +00:00
|
|
|
/* Oops. Probably a really long endless course. Do "hh:mm.ss"; use a period
|
|
|
|
|
* to differentiate between "mm:ss:ms". I'd much prefer reversing those, since
|
|
|
|
|
* it makes much more sense to do "mm:ss.ms", but people would probably complain
|
2003-06-24 20:21:09 +00:00
|
|
|
* about "arcade accuracy" ...
|
|
|
|
|
*/
|
|
|
|
|
sReturn = ssprintf( "%02d:%02d.%02d", iMinsDisplay/60, iMinsDisplay%60, iSecsDisplay );
|
2002-10-16 18:25:14 +00:00
|
|
|
}
|
2003-06-24 20:21:09 +00:00
|
|
|
|
2002-10-02 05:42:59 +00:00
|
|
|
ASSERT( sReturn.GetLength() <= 8 );
|
|
|
|
|
return sReturn;
|
2002-08-01 13:42:56 +00:00
|
|
|
}
|
2001-12-28 10:15:59 +00:00
|
|
|
|
2002-08-29 21:02:04 +00:00
|
|
|
CString ssprintf( const char *fmt, ...)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
|
|
|
|
va_list va;
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
|
return vssprintf(fmt, va);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-29 21:02:04 +00:00
|
|
|
CString vssprintf( const char *fmt, va_list argList)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
|
|
|
|
CString str;
|
|
|
|
|
str.FormatV(fmt, argList);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-09 03:09:59 +00:00
|
|
|
#ifdef WIN32
|
2003-11-14 17:17:36 +00:00
|
|
|
#include "windows.h"
|
2003-07-14 22:23:04 +00:00
|
|
|
#ifdef _XBOX
|
2003-07-11 03:15:28 +00:00
|
|
|
#include "D3DX8Core.h"
|
2003-07-14 22:23:04 +00:00
|
|
|
#else
|
|
|
|
|
#include "Dxerr8.h"
|
|
|
|
|
#pragma comment(lib, "Dxerr8.lib")
|
|
|
|
|
#endif
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-12-09 03:09:59 +00:00
|
|
|
CString hr_ssprintf( int hr, const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list va;
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
|
CString s = vssprintf( fmt, va );
|
|
|
|
|
va_end(va);
|
|
|
|
|
|
2003-07-14 22:23:04 +00:00
|
|
|
#ifdef _XBOX
|
2003-07-11 03:15:28 +00:00
|
|
|
char szError[1024] = "";
|
|
|
|
|
D3DXGetErrorString( hr, szError, sizeof(szError) );
|
2003-07-14 22:23:04 +00:00
|
|
|
#else
|
|
|
|
|
const char *szError = DXGetErrorString8( hr );
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-07-11 03:15:28 +00:00
|
|
|
return s + ssprintf( " (%s)", szError );
|
2002-12-09 03:09:59 +00:00
|
|
|
}
|
2002-12-21 08:40:40 +00:00
|
|
|
|
|
|
|
|
CString werr_ssprintf( int err, const char *fmt, ...)
|
|
|
|
|
{
|
2003-05-22 05:28:37 +00:00
|
|
|
char buf[1024] = "";
|
|
|
|
|
#ifndef _XBOX
|
2002-12-21 08:40:40 +00:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
|
0, err, 0, buf, sizeof(buf), NULL);
|
2003-05-22 05:28:37 +00:00
|
|
|
#endif
|
2002-12-21 08:40:40 +00:00
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
|
CString s = vssprintf( fmt, va );
|
|
|
|
|
va_end(va);
|
|
|
|
|
|
2002-12-21 08:44:26 +00:00
|
|
|
return s += ssprintf( " (%s)", buf );
|
2002-12-21 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2002-12-09 03:09:59 +00:00
|
|
|
#endif
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-06-24 22:04:31 +00:00
|
|
|
CString join( const CString &Deliminator, const CStringArray& Source)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-31 02:11:52 +00:00
|
|
|
if( Source.empty() )
|
2002-06-27 17:49:10 +00:00
|
|
|
return "";
|
|
|
|
|
|
2001-11-03 10:52:42 +00:00
|
|
|
CString csTmp;
|
|
|
|
|
|
|
|
|
|
// Loop through the Array and Append the Deliminator
|
2002-10-31 02:11:52 +00:00
|
|
|
for( unsigned iNum = 0; iNum < Source.size()-1; iNum++ ) {
|
2002-10-24 19:55:09 +00:00
|
|
|
csTmp += Source[iNum];
|
2001-11-03 10:52:42 +00:00
|
|
|
csTmp += Deliminator;
|
|
|
|
|
}
|
2002-10-24 19:55:09 +00:00
|
|
|
csTmp += Source.back();
|
2002-06-24 22:04:31 +00:00
|
|
|
return csTmp;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-07 22:54:50 +00:00
|
|
|
CString join( const CString &Delimitor, CStringArray::const_iterator begin, CStringArray::const_iterator end )
|
|
|
|
|
{
|
|
|
|
|
if( begin == end )
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
CString ret;
|
|
|
|
|
while( begin != end )
|
|
|
|
|
{
|
|
|
|
|
ret += *begin;
|
|
|
|
|
++begin;
|
|
|
|
|
if( begin != end )
|
|
|
|
|
ret += Delimitor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-01-05 03:59:07 +00:00
|
|
|
template <class S>
|
2003-10-28 22:37:20 +00:00
|
|
|
void do_split( const S &Source, const S &Delimitor, vector<S> &AddIt, const bool bIgnoreEmpty )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2003-01-05 03:59:07 +00:00
|
|
|
unsigned startpos = 0;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
do {
|
2003-10-28 22:37:20 +00:00
|
|
|
unsigned pos;
|
|
|
|
|
if( Delimitor.size() == 1 )
|
|
|
|
|
pos = Source.find( Delimitor[0], startpos );
|
2002-08-30 00:28:20 +00:00
|
|
|
else
|
2003-10-28 22:37:20 +00:00
|
|
|
pos = Source.find( Delimitor, startpos );
|
|
|
|
|
if( pos == Source.npos )
|
|
|
|
|
pos = Source.size();
|
|
|
|
|
|
|
|
|
|
if( pos-startpos > 0 || !bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
const S AddCString = Source.substr(startpos, pos-startpos);
|
2002-10-31 04:23:39 +00:00
|
|
|
AddIt.push_back(AddCString);
|
2003-10-28 22:37:20 +00:00
|
|
|
}
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-10-28 22:37:20 +00:00
|
|
|
startpos = pos+Delimitor.size();
|
2003-01-05 03:59:07 +00:00
|
|
|
} while ( startpos <= Source.size() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void split( const CString &Source, const CString &Deliminator, CStringArray &AddIt, const bool bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
do_split(Source, Deliminator, AddIt, bIgnoreEmpty );
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
void split( const wstring &Source, const wstring &Deliminator, vector<wstring> &AddIt, const bool bIgnoreEmpty )
|
2003-01-05 03:59:07 +00:00
|
|
|
{
|
|
|
|
|
do_split(Source, Deliminator, AddIt, bIgnoreEmpty );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-15 05:34:24 +00:00
|
|
|
/* Use:
|
|
|
|
|
|
|
|
|
|
CString str="a,b,c";
|
|
|
|
|
int start = 0, size = -1;
|
|
|
|
|
while( 1 )
|
|
|
|
|
{
|
|
|
|
|
do_split( str, ",", begin, size );
|
|
|
|
|
if( begin == str.end() )
|
|
|
|
|
break;
|
|
|
|
|
str[begin] = 'Q';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template <class S>
|
|
|
|
|
void do_split( const S &Source, const S &Delimitor, int &begin, int &size, int len, const bool bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
if( size != -1 )
|
|
|
|
|
{
|
|
|
|
|
/* Start points to the beginning of the last delimiter. Move it up. */
|
|
|
|
|
begin += size+Delimitor.size();
|
|
|
|
|
begin = min( begin, len );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
|
|
if( bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
/* Skip delims. */
|
|
|
|
|
while( begin + Delimitor.size() < Source.size() &&
|
|
|
|
|
!Source.compare( begin, Delimitor.size(), Delimitor ) )
|
|
|
|
|
++begin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Where's the string function to find within a substring? C++ strings apparently
|
|
|
|
|
* are missing that ... */
|
|
|
|
|
unsigned pos;
|
|
|
|
|
if( Delimitor.size() == 1 )
|
|
|
|
|
pos = Source.find( Delimitor[0], begin );
|
|
|
|
|
else
|
|
|
|
|
pos = Source.find( Delimitor, begin );
|
|
|
|
|
if( pos == Source.npos || (int) pos > len )
|
|
|
|
|
pos = len;
|
|
|
|
|
size = pos - begin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void split( const CString &Source, const CString &Delimitor, int &begin, int &size, int len, const bool bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
do_split( Source, Delimitor, begin, size, len, bIgnoreEmpty );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void split( const wstring &Source, const wstring &Delimitor, int &begin, int &size, int len, const bool bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
do_split( Source, Delimitor, begin, size, len, bIgnoreEmpty );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void split( const CString &Source, const CString &Delimitor, int &begin, int &size, const bool bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
do_split( Source, Delimitor, begin, size, Source.size(), bIgnoreEmpty );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void split( const wstring &Source, const wstring &Delimitor, int &begin, int &size, const bool bIgnoreEmpty )
|
|
|
|
|
{
|
|
|
|
|
do_split( Source, Delimitor, begin, size, Source.size(), bIgnoreEmpty );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-01-16 22:49:22 +00:00
|
|
|
/*
|
|
|
|
|
* foo\fum\ -> "foo\fum\", "", ""
|
|
|
|
|
* c:\foo\bar.txt -> "c:\foo\", "bar", ".txt"
|
|
|
|
|
* \\foo\fum -> "\\foo\", "fum", ""
|
|
|
|
|
*/
|
2003-10-29 21:22:28 +00:00
|
|
|
void splitpath( const CString &Path, CString& Dir, CString& Filename, CString& Ext )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2003-01-16 22:49:22 +00:00
|
|
|
Dir = Filename = Ext = "";
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-01-16 22:49:22 +00:00
|
|
|
CStringArray mat;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-01-23 04:22:07 +00:00
|
|
|
/* One level of escapes for the regex, one for C. Ew.
|
|
|
|
|
* This is really:
|
|
|
|
|
* ^(.*[\\/])?(.*)$ */
|
|
|
|
|
static Regex sep("^(.*[\\\\/])?(.*)$");
|
2003-07-07 23:14:50 +00:00
|
|
|
bool check = sep.Compare(Path, mat);
|
|
|
|
|
ASSERT(check);
|
2003-01-16 22:49:22 +00:00
|
|
|
|
|
|
|
|
Dir = mat[0];
|
2003-10-29 21:22:28 +00:00
|
|
|
const CString Base = mat[1];
|
2003-01-16 22:49:22 +00:00
|
|
|
|
2003-01-23 04:22:07 +00:00
|
|
|
/* ^(.*)(\.[^\.]+)$ */
|
|
|
|
|
static Regex SplitExt("^(.*)(\\.[^\\.]+)$");
|
2003-10-29 21:22:28 +00:00
|
|
|
if( SplitExt.Compare(Base, mat) )
|
2003-01-16 22:49:22 +00:00
|
|
|
{
|
|
|
|
|
Filename = mat[0];
|
|
|
|
|
Ext = mat[1];
|
|
|
|
|
} else
|
2003-10-29 21:22:28 +00:00
|
|
|
Filename = Base;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2001-12-28 10:15:59 +00:00
|
|
|
|
2003-10-29 20:59:40 +00:00
|
|
|
/* "foo.bar", "baz" -> "foo.baz"
|
|
|
|
|
* "foo", "baz" -> "foo.baz"
|
|
|
|
|
* "foo.bar", "" -> "foo" */
|
2003-09-04 19:28:39 +00:00
|
|
|
CString SetExtension( const CString &path, const CString &ext )
|
|
|
|
|
{
|
|
|
|
|
CString Dir, FName, OldExt;
|
|
|
|
|
splitpath( path, Dir, FName, OldExt );
|
2003-10-29 20:59:40 +00:00
|
|
|
return Dir + FName + (ext.size()? ".":"") + ext;
|
2003-09-04 19:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-07 22:54:50 +00:00
|
|
|
CString GetExtension( const CString &sPath )
|
2003-09-03 18:22:55 +00:00
|
|
|
{
|
2003-10-22 05:49:26 +00:00
|
|
|
unsigned pos = sPath.rfind( '.' );
|
|
|
|
|
if( pos == sPath.npos )
|
|
|
|
|
return "";
|
|
|
|
|
|
2003-10-22 07:23:50 +00:00
|
|
|
unsigned slash = sPath.find( '/', pos );
|
2003-10-22 05:49:26 +00:00
|
|
|
if( slash != sPath.npos )
|
|
|
|
|
return ""; /* rare: path/dir.ext/fn */
|
|
|
|
|
|
|
|
|
|
return sPath.substr( pos+1, sPath.size()-pos+1 );
|
2003-09-03 18:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
2003-09-05 06:20:55 +00:00
|
|
|
CString GetCwd()
|
|
|
|
|
{
|
2003-11-13 00:39:36 +00:00
|
|
|
#ifdef _XBOX
|
2003-12-07 04:20:07 +00:00
|
|
|
return SYS_BASE_PATH;
|
2003-11-13 00:39:36 +00:00
|
|
|
#else
|
2003-09-05 06:20:55 +00:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
|
bool ret = getcwd(buf, PATH_MAX) != NULL;
|
|
|
|
|
ASSERT(ret);
|
|
|
|
|
return buf;
|
2003-11-13 00:39:36 +00:00
|
|
|
#endif
|
2003-09-05 06:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
2003-05-22 20:32:08 +00:00
|
|
|
/* Reference: http://www.theorem.com/java/CRC32.java, rewritten by Glenn Maynard.
|
|
|
|
|
* Public domain. */
|
2003-12-07 22:54:50 +00:00
|
|
|
unsigned int GetHashForString ( const CString &s )
|
2002-05-19 01:59:48 +00:00
|
|
|
{
|
2003-05-22 20:32:08 +00:00
|
|
|
static unsigned tab[256];
|
|
|
|
|
static bool initted = false;
|
|
|
|
|
if(!initted)
|
|
|
|
|
{
|
|
|
|
|
initted = true;
|
|
|
|
|
const unsigned POLY = 0xEDB88320;
|
2002-07-23 01:41:40 +00:00
|
|
|
|
2003-05-22 20:32:08 +00:00
|
|
|
for(int i = 0; i < 256; ++i)
|
|
|
|
|
{
|
|
|
|
|
tab[i] = i;
|
|
|
|
|
for(int j = 0; j < 8; ++j)
|
|
|
|
|
{
|
|
|
|
|
if(tab[i] & 1) tab[i] = (tab[i] >> 1) ^ POLY;
|
|
|
|
|
else tab[i] >>= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-07-23 01:41:40 +00:00
|
|
|
|
2003-05-22 20:32:08 +00:00
|
|
|
unsigned crc = 0;
|
|
|
|
|
for(unsigned i = 0; i < s.size(); ++i)
|
|
|
|
|
crc = (crc >> 8) ^ tab[(crc ^ s[i]) & 0xFF];
|
|
|
|
|
return crc;
|
2002-05-19 01:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-07 22:54:50 +00:00
|
|
|
/* Return true if "dir" is empty or does not exist. */
|
|
|
|
|
bool DirectoryIsEmpty( const CString &dir )
|
|
|
|
|
{
|
|
|
|
|
if(dir == "")
|
|
|
|
|
return true;
|
|
|
|
|
if(!DoesFileExist(dir))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
CStringArray asFileNames;
|
|
|
|
|
GetDirListing( dir, asFileNames );
|
|
|
|
|
return asFileNames.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-24 08:17:09 +00:00
|
|
|
bool CompareCStringsAsc(const CString &str1, const CString &str2)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-11-08 20:12:19 +00:00
|
|
|
return str1.CompareNoCase( str2 ) < 0;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-24 08:17:09 +00:00
|
|
|
bool CompareCStringsDesc(const CString &str1, const CString &str2)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-11-08 20:12:19 +00:00
|
|
|
return str1.CompareNoCase( str2 ) > 0;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-24 08:17:09 +00:00
|
|
|
void SortCStringArray( CStringArray &arrayCStrings, const bool bSortAscending )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-24 08:40:27 +00:00
|
|
|
sort( arrayCStrings.begin(), arrayCStrings.end(),
|
2002-10-24 08:17:09 +00:00
|
|
|
bSortAscending?CompareCStringsAsc:CompareCStringsDesc);
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
2001-11-26 08:28:48 +00:00
|
|
|
|
2002-10-24 07:09:38 +00:00
|
|
|
float calc_mean(const float *start, const float *end)
|
|
|
|
|
{
|
|
|
|
|
return accumulate(start, end, 0.f) / distance(start, end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float calc_stddev(const float *start, const float *end)
|
|
|
|
|
{
|
|
|
|
|
/* Calculate the mean. */
|
|
|
|
|
float mean = calc_mean(start, end);
|
|
|
|
|
|
|
|
|
|
/* Calculate stddev. */
|
|
|
|
|
float dev = 0.0f;
|
|
|
|
|
for( const float *i=start; i != end; ++i )
|
|
|
|
|
dev += (*i - mean) * (*i - mean);
|
|
|
|
|
dev /= distance(start, end) - 1;
|
|
|
|
|
dev = sqrtf(dev);
|
|
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
|
}
|
2002-05-27 08:23:27 +00:00
|
|
|
|
2002-12-11 11:17:12 +00:00
|
|
|
void TrimLeft(CString &str, const char *s)
|
2002-10-31 08:04:34 +00:00
|
|
|
{
|
|
|
|
|
int n = 0;
|
|
|
|
|
while(n < int(str.size()) && strchr(s, str[n]))
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
|
|
str.erase(str.begin(), str.begin()+n);
|
|
|
|
|
}
|
2002-05-27 08:23:27 +00:00
|
|
|
|
2002-12-11 11:17:12 +00:00
|
|
|
void TrimRight(CString &str, const char *s)
|
2002-10-31 08:04:34 +00:00
|
|
|
{
|
2002-11-01 20:48:36 +00:00
|
|
|
int n = str.size();
|
|
|
|
|
while(n > 0 && strchr(s, str[n-1]))
|
2002-10-31 08:04:34 +00:00
|
|
|
n--;
|
|
|
|
|
|
2002-11-01 20:48:36 +00:00
|
|
|
/* Delete from n to the end. If n == str.size(), nothing is deleted;
|
|
|
|
|
* if n == 0, the whole string is erased. */
|
2002-10-31 08:04:34 +00:00
|
|
|
str.erase(str.begin()+n, str.end());
|
|
|
|
|
}
|
2003-01-01 22:02:41 +00:00
|
|
|
|
2003-02-14 22:28:29 +00:00
|
|
|
void StripCrnl(CString &s)
|
|
|
|
|
{
|
2003-07-10 01:55:37 +00:00
|
|
|
while( s.size() && (s[s.size()-1] == '\r' || s[s.size()-1] == '\n') )
|
2003-02-14 22:28:29 +00:00
|
|
|
s.erase(s.size()-1);
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-01 22:02:41 +00:00
|
|
|
/* path is a .redir pathname. Read it and return the real one. */
|
|
|
|
|
CString DerefRedir(const CString &path)
|
|
|
|
|
{
|
2003-10-29 20:46:37 +00:00
|
|
|
if( GetExtension(path) != "redir" )
|
2003-07-10 11:47:28 +00:00
|
|
|
{
|
|
|
|
|
CString path2 = path;
|
|
|
|
|
ResolvePath( path2 );
|
|
|
|
|
return path2;
|
|
|
|
|
}
|
2003-01-01 22:02:41 +00:00
|
|
|
|
2003-10-29 20:46:37 +00:00
|
|
|
CString sNewFileName = GetRedirContents( path );
|
2003-02-16 05:12:27 +00:00
|
|
|
|
2003-01-01 22:02:41 +00:00
|
|
|
/* Empty is invalid. */
|
|
|
|
|
if(sNewFileName == "")
|
|
|
|
|
return "";
|
|
|
|
|
|
2003-08-02 19:28:31 +00:00
|
|
|
FixSlashesInPlace( sNewFileName );
|
|
|
|
|
|
2003-10-29 20:46:37 +00:00
|
|
|
CString path2 = Dirname(path) + sNewFileName;
|
2003-08-02 19:28:31 +00:00
|
|
|
|
|
|
|
|
CollapsePath( path2 );
|
|
|
|
|
|
2003-07-10 11:47:28 +00:00
|
|
|
ResolvePath( path2 );
|
2003-09-23 05:39:47 +00:00
|
|
|
|
|
|
|
|
if( !DoesFileExist(path2) )
|
|
|
|
|
RageException::Throw( "The redirect '%s' references a file '%s' which doesn't exist.", path.c_str(), path2.c_str() );
|
|
|
|
|
|
2003-07-10 11:47:28 +00:00
|
|
|
return path2;
|
2003-01-01 22:02:41 +00:00
|
|
|
}
|
2003-01-05 02:48:15 +00:00
|
|
|
|
2003-12-02 21:52:03 +00:00
|
|
|
/* XXX: This can be used to read one line from any file; rename it. */
|
2003-03-15 00:16:45 +00:00
|
|
|
CString GetRedirContents(const CString &path)
|
|
|
|
|
{
|
2003-11-30 06:20:25 +00:00
|
|
|
RageFile file(path);
|
|
|
|
|
CString sNewFileName = file.GetLine();
|
|
|
|
|
|
2003-03-15 00:16:45 +00:00
|
|
|
StripCrnl(sNewFileName);
|
|
|
|
|
return sNewFileName;
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-27 20:09:29 +00:00
|
|
|
#if 1
|
|
|
|
|
/* PCRE */
|
|
|
|
|
#include "pcre/pcre.h"
|
|
|
|
|
void Regex::Compile()
|
|
|
|
|
{
|
|
|
|
|
const char *error;
|
|
|
|
|
int offset;
|
|
|
|
|
reg = pcre_compile(pattern.c_str(), PCRE_CASELESS, &error, &offset, NULL);
|
|
|
|
|
|
|
|
|
|
if(reg == NULL)
|
|
|
|
|
RageException::Throw("Invalid regex: '%s' (%s)", pattern.c_str(), error);
|
|
|
|
|
|
|
|
|
|
int ret = pcre_fullinfo( (pcre *) reg, NULL, PCRE_INFO_CAPTURECOUNT, &backrefs);
|
|
|
|
|
ASSERT(ret >= 0);
|
|
|
|
|
|
|
|
|
|
backrefs++;
|
|
|
|
|
ASSERT(backrefs < 128);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Regex::Set(const CString &str)
|
|
|
|
|
{
|
|
|
|
|
Release();
|
|
|
|
|
pattern=str;
|
|
|
|
|
Compile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Regex::Release()
|
|
|
|
|
{
|
|
|
|
|
pcre_free(reg);
|
|
|
|
|
reg = NULL;
|
|
|
|
|
pattern = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Regex::Regex(const CString &str)
|
|
|
|
|
{
|
|
|
|
|
reg = NULL;
|
|
|
|
|
Set(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Regex::Regex(const Regex &rhs)
|
|
|
|
|
{
|
|
|
|
|
reg = NULL;
|
|
|
|
|
Set(rhs.pattern);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Regex &Regex::operator=(const Regex &rhs)
|
|
|
|
|
{
|
|
|
|
|
if(this != &rhs) Set(rhs.pattern);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Regex::~Regex()
|
|
|
|
|
{
|
|
|
|
|
Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Regex::Compare(const CString &str)
|
|
|
|
|
{
|
|
|
|
|
int mat[128*3];
|
|
|
|
|
int ret = pcre_exec( (pcre *) reg, NULL,
|
|
|
|
|
str.data(), str.size(), 0, 0, mat, 128*3);
|
|
|
|
|
|
|
|
|
|
if( ret < -1 )
|
|
|
|
|
RageException::Throw("Unexpected return from pcre_exec('%s'): %i",
|
|
|
|
|
pattern.c_str(), ret);
|
|
|
|
|
|
|
|
|
|
return ret >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Regex::Compare(const CString &str, vector<CString> &matches)
|
|
|
|
|
{
|
|
|
|
|
matches.clear();
|
|
|
|
|
|
|
|
|
|
int mat[128*3];
|
|
|
|
|
int ret = pcre_exec( (pcre *) reg, NULL,
|
|
|
|
|
str.data(), str.size(), 0, 0, mat, 128*3);
|
|
|
|
|
|
|
|
|
|
if( ret < -1 )
|
|
|
|
|
RageException::Throw("Unexpected return from pcre_exec('%s'): %i",
|
|
|
|
|
pattern.c_str(), ret);
|
|
|
|
|
|
|
|
|
|
if(ret == -1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for(unsigned i = 1; i < backrefs; ++i)
|
|
|
|
|
{
|
|
|
|
|
const int start = mat[i*2], end = mat[i*2+1];
|
|
|
|
|
if(start == -1)
|
|
|
|
|
matches.push_back(""); /* no match */
|
|
|
|
|
else
|
|
|
|
|
matches.push_back(str.substr(start, end - start));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* GNU regex */
|
|
|
|
|
#include "regex.h"
|
2003-01-23 21:03:57 +00:00
|
|
|
void Regex::Compile()
|
2003-01-05 02:48:15 +00:00
|
|
|
{
|
2003-01-23 21:03:57 +00:00
|
|
|
reg = new regex_t;
|
2003-01-23 04:22:07 +00:00
|
|
|
|
|
|
|
|
int ret = regcomp((regex_t *) reg, pattern.c_str(), REG_EXTENDED|REG_ICASE);
|
|
|
|
|
if(ret != 0)
|
|
|
|
|
RageException::Throw("Invalid regex: '%s'", pattern.c_str());
|
|
|
|
|
|
|
|
|
|
/* Count the number of backreferences. */
|
|
|
|
|
backrefs = 0;
|
|
|
|
|
for(int i = 0; i < int(pattern.size()); ++i)
|
|
|
|
|
if(pattern[i] == '(') backrefs++;
|
|
|
|
|
ASSERT(backrefs+1 < 128);
|
2003-01-05 02:48:15 +00:00
|
|
|
}
|
2003-01-24 00:26:54 +00:00
|
|
|
|
2003-03-27 20:16:17 +00:00
|
|
|
void Regex::Set(const CString &str)
|
|
|
|
|
{
|
|
|
|
|
Release();
|
|
|
|
|
pattern=str;
|
|
|
|
|
Compile();
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-23 21:03:57 +00:00
|
|
|
void Regex::Release()
|
|
|
|
|
{
|
|
|
|
|
delete (regex_t *)reg;
|
|
|
|
|
reg = NULL;
|
2003-03-28 05:23:35 +00:00
|
|
|
pattern = "";
|
2003-01-23 21:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
2003-03-27 20:16:17 +00:00
|
|
|
Regex::Regex(const CString &str)
|
2003-01-23 21:03:57 +00:00
|
|
|
{
|
|
|
|
|
reg = NULL;
|
2003-03-27 20:16:17 +00:00
|
|
|
Set(str);
|
2003-01-23 21:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Regex::Regex(const Regex &rhs)
|
|
|
|
|
{
|
|
|
|
|
reg = NULL;
|
2003-03-28 05:23:35 +00:00
|
|
|
Set(rhs.pattern);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Regex &Regex::operator=(const Regex &rhs)
|
|
|
|
|
{
|
|
|
|
|
if(this != &rhs) Set(rhs.pattern);
|
|
|
|
|
return *this;
|
2003-01-23 21:03:57 +00:00
|
|
|
}
|
2003-01-05 02:48:15 +00:00
|
|
|
|
2003-01-23 04:22:07 +00:00
|
|
|
Regex::~Regex()
|
2003-01-05 02:48:15 +00:00
|
|
|
{
|
2003-01-23 21:03:57 +00:00
|
|
|
Release();
|
2003-01-05 02:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
2003-01-23 04:22:07 +00:00
|
|
|
bool Regex::Compare(const CString &str)
|
2003-01-05 02:48:15 +00:00
|
|
|
{
|
2003-01-23 04:22:07 +00:00
|
|
|
return regexec((regex_t *) reg, str.c_str(), 0, NULL, 0) != REG_NOMATCH;
|
|
|
|
|
}
|
2003-01-05 02:48:15 +00:00
|
|
|
|
2003-01-23 04:22:07 +00:00
|
|
|
bool Regex::Compare(const CString &str, vector<CString> &matches)
|
|
|
|
|
{
|
|
|
|
|
matches.clear();
|
2003-01-05 02:48:15 +00:00
|
|
|
|
|
|
|
|
regmatch_t mat[128];
|
2003-01-23 04:22:07 +00:00
|
|
|
int ret = regexec((regex_t *) reg, str.c_str(), 128, mat, 0);
|
2003-01-05 02:48:15 +00:00
|
|
|
|
2003-01-23 04:22:07 +00:00
|
|
|
if(ret == REG_NOMATCH)
|
2003-01-05 02:48:15 +00:00
|
|
|
return false;
|
|
|
|
|
|
2003-01-23 04:22:07 +00:00
|
|
|
for(unsigned i = 1; i < backrefs+1; ++i)
|
2003-01-05 02:48:15 +00:00
|
|
|
{
|
|
|
|
|
if(mat[i].rm_so == -1)
|
|
|
|
|
matches.push_back(""); /* no match */
|
|
|
|
|
else
|
|
|
|
|
matches.push_back(str.substr(mat[i].rm_so, mat[i].rm_eo - mat[i].rm_so));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2003-06-27 20:09:29 +00:00
|
|
|
#endif
|
2003-01-05 02:48:15 +00:00
|
|
|
|
2003-01-05 05:31:59 +00:00
|
|
|
/* UTF-8 decoding code from glib. */
|
|
|
|
|
char *utf8_find_next_char (const char *p, const char *end)
|
|
|
|
|
{
|
2003-01-06 01:19:22 +00:00
|
|
|
if (end)
|
|
|
|
|
for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
|
|
|
|
|
;
|
|
|
|
|
else if (*p) {
|
|
|
|
|
for (++p; (*p & 0xc0) == 0x80; ++p)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
return (p == end) ? NULL : (char *)p;
|
2003-01-05 05:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
2003-01-06 01:19:22 +00:00
|
|
|
int masks[7] = { 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
|
|
|
|
|
|
|
|
|
|
int utf8_get_char_len (const char *pp)
|
|
|
|
|
{
|
2003-05-18 04:36:03 +00:00
|
|
|
const unsigned char *p = (const unsigned char *) pp;
|
|
|
|
|
if (*p < 128) return 1;
|
|
|
|
|
else if ((*p & 0xe0) == 0xc0) return 2;
|
|
|
|
|
else if ((*p & 0xf0) == 0xe0) return 3;
|
|
|
|
|
else if ((*p & 0xf8) == 0xf0) return 4;
|
|
|
|
|
else if ((*p & 0xfc) == 0xf8) return 5;
|
|
|
|
|
else if ((*p & 0xfe) == 0xfc) return 6;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool utf8_is_valid(const CString &str)
|
|
|
|
|
{
|
|
|
|
|
unsigned pos = 0;
|
|
|
|
|
while(pos < str.size())
|
|
|
|
|
{
|
|
|
|
|
int len = utf8_get_char_len(str.c_str() + pos);
|
|
|
|
|
if(len == -1)
|
|
|
|
|
return false;
|
2003-06-30 08:38:24 +00:00
|
|
|
if( utf8_get_char( str.c_str() + pos ) == INVALID_CHAR )
|
|
|
|
|
return false;
|
2003-05-18 04:36:03 +00:00
|
|
|
|
|
|
|
|
pos += len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2003-01-06 01:19:22 +00:00
|
|
|
}
|
2003-01-05 05:31:59 +00:00
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
wchar_t utf8_get_char (const char *p)
|
2003-01-05 05:31:59 +00:00
|
|
|
{
|
2003-01-06 01:19:22 +00:00
|
|
|
int len = utf8_get_char_len(p);
|
|
|
|
|
if(len == -1)
|
2003-01-28 00:45:01 +00:00
|
|
|
return INVALID_CHAR;
|
2003-01-06 01:19:22 +00:00
|
|
|
|
|
|
|
|
int mask = masks[len];
|
2003-01-28 00:45:01 +00:00
|
|
|
wchar_t result = wchar_t(p[0] & mask);
|
2003-01-06 01:19:22 +00:00
|
|
|
for (int i = 1; i < len; ++i) {
|
2003-01-05 05:31:59 +00:00
|
|
|
if ((p[i] & 0xc0) != 0x80)
|
2003-01-28 00:45:01 +00:00
|
|
|
return INVALID_CHAR;
|
2003-01-05 05:31:59 +00:00
|
|
|
|
|
|
|
|
result <<= 6;
|
|
|
|
|
result |= p[i] & 0x3f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
const wchar_t INVALID_CHAR = 0xFFFF;
|
2003-01-06 04:23:41 +00:00
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
wstring CStringToWstring(const CString &str)
|
2003-01-05 05:31:59 +00:00
|
|
|
{
|
|
|
|
|
const char *ptr = str.c_str(), *end = str.c_str()+str.size();
|
|
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
wstring ret;
|
2003-01-05 05:31:59 +00:00
|
|
|
|
2003-01-06 01:19:22 +00:00
|
|
|
while(ptr && ptr != end)
|
2003-01-05 05:31:59 +00:00
|
|
|
{
|
2003-01-28 00:45:01 +00:00
|
|
|
wchar_t c = utf8_get_char (ptr);
|
2003-01-06 04:23:41 +00:00
|
|
|
if(c == -1)
|
2003-01-07 08:16:55 +00:00
|
|
|
ret += INVALID_CHAR;
|
2003-01-06 04:23:41 +00:00
|
|
|
else
|
2003-01-07 08:16:55 +00:00
|
|
|
ret += c;
|
2003-01-05 05:31:59 +00:00
|
|
|
ptr = utf8_find_next_char (ptr, end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2003-01-06 01:35:05 +00:00
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
CString WStringToCString(const wstring &str)
|
2003-01-06 07:43:14 +00:00
|
|
|
{
|
|
|
|
|
CString ret;
|
|
|
|
|
|
|
|
|
|
for(unsigned i = 0; i < str.size(); ++i)
|
2003-01-28 00:45:01 +00:00
|
|
|
ret.append(WcharToUTF8(str[i]));
|
2003-01-06 07:43:14 +00:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
static int unichar_to_utf8 (wchar_t c, char *outbuf)
|
2003-01-06 01:35:05 +00:00
|
|
|
{
|
|
|
|
|
unsigned int len = 0;
|
|
|
|
|
int first;
|
|
|
|
|
|
|
|
|
|
if (c < 0x80) {
|
|
|
|
|
first = 0;
|
|
|
|
|
len = 1;
|
|
|
|
|
} else if (c < 0x800) {
|
|
|
|
|
first = 0xc0;
|
|
|
|
|
len = 2;
|
|
|
|
|
} else if (c < 0x10000) {
|
|
|
|
|
first = 0xe0;
|
|
|
|
|
len = 3;
|
|
|
|
|
} else if (c < 0x200000) {
|
|
|
|
|
first = 0xf0;
|
|
|
|
|
len = 4;
|
|
|
|
|
} else if (c < 0x4000000) {
|
|
|
|
|
first = 0xf8;
|
|
|
|
|
len = 5;
|
|
|
|
|
} else {
|
|
|
|
|
first = 0xfc;
|
|
|
|
|
len = 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (outbuf)
|
|
|
|
|
{
|
|
|
|
|
for (int i = len - 1; i > 0; --i)
|
|
|
|
|
{
|
|
|
|
|
outbuf[i] = char((c & 0x3f) | 0x80);
|
|
|
|
|
c >>= 6;
|
|
|
|
|
}
|
|
|
|
|
outbuf[0] = char(c | first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-28 00:45:01 +00:00
|
|
|
CString WcharToUTF8( wchar_t c )
|
2003-01-06 01:35:05 +00:00
|
|
|
{
|
|
|
|
|
char buf[6];
|
|
|
|
|
int cnt = unichar_to_utf8(c, buf);
|
|
|
|
|
return CString(buf, cnt);
|
|
|
|
|
}
|
2003-01-06 02:48:14 +00:00
|
|
|
|
2003-02-15 00:21:40 +00:00
|
|
|
|
2003-01-06 02:48:14 +00:00
|
|
|
/* Replace &#nnnn; (decimal) &xnnnn; (hex) with corresponding UTF-8 characters. */
|
|
|
|
|
void Replace_Unicode_Markers( CString &Text )
|
|
|
|
|
{
|
|
|
|
|
unsigned start = 0;
|
|
|
|
|
while(start < Text.size())
|
|
|
|
|
{
|
|
|
|
|
/* Look for &#digits; */
|
|
|
|
|
bool hex = false;
|
|
|
|
|
unsigned pos = Text.find("&#", start);
|
|
|
|
|
if(pos == Text.npos) {
|
|
|
|
|
hex = true;
|
|
|
|
|
pos = Text.find("&x", start);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(pos == Text.npos) break;
|
|
|
|
|
start = pos+1;
|
|
|
|
|
|
|
|
|
|
unsigned p = pos;
|
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
|
|
/* Found &# or &x. Is it followed by digits and a semicolon? */
|
|
|
|
|
if(p >= Text.size()) continue;
|
|
|
|
|
|
|
|
|
|
int numdigits = 0;
|
|
|
|
|
while(p < Text.size() &&
|
|
|
|
|
(hex && isxdigit(Text[p])) || (!hex && isdigit(Text[p])))
|
|
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
numdigits++;
|
|
|
|
|
}
|
|
|
|
|
if(!numdigits) continue; /* must have at least one digit */
|
|
|
|
|
if(p >= Text.size() || Text[p] != ';') continue;
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
int num;
|
|
|
|
|
if(hex) sscanf(Text.c_str()+pos, "&x%x;", &num);
|
|
|
|
|
else sscanf(Text.c_str()+pos, "&#%i;", &num);
|
2003-01-28 00:45:01 +00:00
|
|
|
if(num > 0xFFFF)
|
|
|
|
|
num = INVALID_CHAR;
|
|
|
|
|
|
|
|
|
|
Text.replace(pos, p-pos, WcharToUTF8(wchar_t(num)));
|
2003-01-06 02:48:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReplaceText( CString &Text, const map<CString,CString> &m )
|
|
|
|
|
{
|
2003-02-14 23:06:01 +00:00
|
|
|
basic_string<char,char_traits_char_nocase> txt(Text);
|
2003-01-18 07:26:31 +00:00
|
|
|
|
2003-01-06 02:48:14 +00:00
|
|
|
for(map<CString,CString>::const_iterator it = m.begin(); it != m.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
unsigned start = 0;
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
unsigned pos = txt.find(it->first, start);
|
|
|
|
|
if(pos == txt.npos)
|
|
|
|
|
break;
|
2003-01-18 07:26:31 +00:00
|
|
|
|
|
|
|
|
txt.replace(pos, it->first.size(), it->second);
|
2003-01-06 02:48:14 +00:00
|
|
|
start = pos+it->second.size();
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-01-18 07:26:31 +00:00
|
|
|
|
|
|
|
|
Text = txt.c_str();
|
2003-01-06 02:48:14 +00:00
|
|
|
}
|
2003-01-09 23:53:14 +00:00
|
|
|
|
|
|
|
|
/* Form a string to identify a wchar_t with ASCII. */
|
|
|
|
|
CString WcharDisplayText(wchar_t c)
|
|
|
|
|
{
|
|
|
|
|
CString chr;
|
|
|
|
|
chr = ssprintf("U+%4.4x", c);
|
|
|
|
|
if(c < 128) chr += ssprintf(" ('%c')", char(c));
|
|
|
|
|
return chr;
|
|
|
|
|
}
|
2003-01-23 04:22:07 +00:00
|
|
|
|
|
|
|
|
/* Return the last named component of dir:
|
|
|
|
|
* a/b/c -> c
|
|
|
|
|
* a/b/c/ -> c
|
|
|
|
|
*/
|
2003-10-29 20:18:04 +00:00
|
|
|
CString Basename( const CString &dir )
|
2003-01-23 04:22:07 +00:00
|
|
|
{
|
2003-10-29 20:18:04 +00:00
|
|
|
unsigned end = dir.find_last_not_of( "/\\" );
|
|
|
|
|
if( end == dir.npos )
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
unsigned start = dir.find_last_of( "/\\", end );
|
|
|
|
|
if( start == dir.npos )
|
|
|
|
|
start = 0;
|
|
|
|
|
else
|
|
|
|
|
++start;
|
|
|
|
|
|
|
|
|
|
return dir.substr( start, end-start+1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return all but the last named component of dir:
|
|
|
|
|
* a/b/c -> a/b/
|
|
|
|
|
* a/b/c/ -> a/b/
|
|
|
|
|
* c/ -> ./
|
|
|
|
|
* /foo -> /
|
|
|
|
|
* / -> /
|
|
|
|
|
*/
|
|
|
|
|
CString Dirname( const CString &dir )
|
|
|
|
|
{
|
|
|
|
|
/* Special case: "/" -> "/". */
|
2003-12-10 09:47:36 +00:00
|
|
|
if( dir.size() == 1 && dir[0] == '/' )
|
|
|
|
|
return "/";
|
2003-10-29 20:18:04 +00:00
|
|
|
|
|
|
|
|
int pos = dir.size()-1;
|
|
|
|
|
/* Skip trailing slashes. */
|
2003-12-10 09:47:36 +00:00
|
|
|
while( pos >= 0 && dir[pos] == '/' )
|
2003-10-29 20:18:04 +00:00
|
|
|
--pos;
|
|
|
|
|
|
|
|
|
|
/* Skip the last component. */
|
2003-12-10 09:47:36 +00:00
|
|
|
while( pos >= 0 && dir[pos] != '/' )
|
2003-10-29 20:18:04 +00:00
|
|
|
--pos;
|
2003-01-23 04:22:07 +00:00
|
|
|
|
2003-10-29 20:18:04 +00:00
|
|
|
if( pos < 0 )
|
2003-12-10 09:47:36 +00:00
|
|
|
return "./";
|
2003-01-23 04:22:07 +00:00
|
|
|
|
2003-10-29 20:18:04 +00:00
|
|
|
return dir.substr(0, pos+1);
|
2003-01-23 04:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-07 22:54:50 +00:00
|
|
|
CString Capitalize( const CString &s )
|
2003-04-13 04:50:08 +00:00
|
|
|
{
|
|
|
|
|
if( s.GetLength()==0 )
|
|
|
|
|
return "";
|
|
|
|
|
CString s1 = s.Left(1);
|
|
|
|
|
s1.MakeUpper();
|
|
|
|
|
CString s2 = s.Right( s.GetLength()-1 );
|
|
|
|
|
return s1+s2;
|
|
|
|
|
}
|
2003-10-20 03:57:02 +00:00
|
|
|
|
|
|
|
|
char char_traits_char_nocase::uptab[256] =
|
|
|
|
|
{
|
|
|
|
|
'\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0A','\x0B','\x0C','\x0D','\x0E','\x0F',
|
|
|
|
|
'\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1A','\x1B','\x1C','\x1D','\x1E','\x1F',
|
|
|
|
|
'\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28','\x29','\x2A','\x2B','\x2C','\x2D','\x2E','\x2F',
|
|
|
|
|
'\x30','\x31','\x32','\x33','\x34','\x35','\x36','\x37','\x38','\x39','\x3A','\x3B','\x3C','\x3D','\x3E','\x3F',
|
|
|
|
|
'\x40','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4A','\x4B','\x4C','\x4D','\x4E','\x4F',
|
|
|
|
|
'\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5A','\x5B','\x5C','\x5D','\x5E','\x5F',
|
|
|
|
|
'\x60','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4A','\x4B','\x4C','\x4D','\x4E','\x4F',
|
|
|
|
|
'\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5A','\x7B','\x7C','\x7D','\x7E','\x7F',
|
|
|
|
|
'\x80','\x81','\x82','\x83','\x84','\x85','\x86','\x87','\x88','\x89','\x8A','\x8B','\x8C','\x8D','\x8E','\x8F',
|
|
|
|
|
'\x90','\x91','\x92','\x93','\x94','\x95','\x96','\x97','\x98','\x99','\x9A','\x9B','\x9C','\x9D','\x9E','\x9F',
|
|
|
|
|
'\xA0','\xA1','\xA2','\xA3','\xA4','\xA5','\xA6','\xA7','\xA8','\xA9','\xAA','\xAB','\xAC','\xAD','\xAE','\xAF',
|
|
|
|
|
'\xB0','\xB1','\xB2','\xB3','\xB4','\xB5','\xB6','\xB7','\xB8','\xB9','\xBA','\xBB','\xBC','\xBD','\xBE','\xBF',
|
|
|
|
|
'\xC0','\xC1','\xC2','\xC3','\xC4','\xC5','\xC6','\xC7','\xC8','\xC9','\xCA','\xCB','\xCC','\xCD','\xCE','\xCF',
|
|
|
|
|
'\xD0','\xD1','\xD2','\xD3','\xD4','\xD5','\xD6','\xD7','\xD8','\xD9','\xDA','\xDB','\xDC','\xDD','\xDE','\xDF',
|
|
|
|
|
'\xE0','\xE1','\xE2','\xE3','\xE4','\xE5','\xE6','\xE7','\xE8','\xE9','\xEA','\xEB','\xEC','\xED','\xEE','\xEF',
|
|
|
|
|
'\xF0','\xF1','\xF2','\xF3','\xF4','\xF5','\xF6','\xF7','\xF8','\xF9','\xFA','\xFB','\xFC','\xFD','\xFE','\xFF',
|
|
|
|
|
};
|
2003-12-08 04:02:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Helper function for reading/writing scores
|
|
|
|
|
//
|
|
|
|
|
bool FileRead(RageFile& f, CString& sOut)
|
|
|
|
|
{
|
|
|
|
|
if (f.AtEOF())
|
|
|
|
|
return false;
|
|
|
|
|
sOut = f.GetLine();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileRead(RageFile& f, int& iOut)
|
|
|
|
|
{
|
|
|
|
|
CString s;
|
|
|
|
|
if (!FileRead(f, s))
|
|
|
|
|
return false;
|
|
|
|
|
iOut = atoi(s);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileRead(RageFile& f, unsigned& uOut)
|
|
|
|
|
{
|
|
|
|
|
CString s;
|
|
|
|
|
if (!FileRead(f, s))
|
|
|
|
|
return false;
|
|
|
|
|
uOut = atoi(s);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileRead(RageFile& f, float& fOut)
|
|
|
|
|
{
|
|
|
|
|
CString s;
|
|
|
|
|
if (!FileRead(f, s))
|
|
|
|
|
return false;
|
|
|
|
|
fOut = (float)atof(s);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileWrite(RageFile& f, const CString& sWrite)
|
|
|
|
|
{
|
|
|
|
|
f.PutLine( sWrite );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileWrite(RageFile& f, int iWrite)
|
|
|
|
|
{
|
|
|
|
|
f.PutLine( ssprintf("%d", iWrite) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileWrite(RageFile& f, size_t uWrite)
|
|
|
|
|
{
|
|
|
|
|
f.PutLine( ssprintf("%lu", uWrite) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileWrite(RageFile& f, float fWrite)
|
|
|
|
|
{
|
|
|
|
|
f.PutLine( ssprintf("%f", fWrite) );
|
|
|
|
|
}
|
2003-12-08 10:27:45 +00:00
|
|
|
|
|
|
|
|
bool CopyFile2( CString sSrcFile, CString sDstFile )
|
|
|
|
|
{
|
|
|
|
|
RageFile in;
|
|
|
|
|
if( !in.Open(sSrcFile, RageFile::READ) )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
RageFile out;
|
|
|
|
|
if( !out.Open(sDstFile, RageFile::WRITE) )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
#define CLEANUP_AND_RETURN(b) { delete data; return b; }
|
|
|
|
|
|
|
|
|
|
int size = in.GetFileSize();
|
|
|
|
|
char *data = new char[size];
|
|
|
|
|
if( !data )
|
|
|
|
|
CLEANUP_AND_RETURN( false );
|
|
|
|
|
if( !in.Read(data,size) )
|
|
|
|
|
CLEANUP_AND_RETURN( false );
|
|
|
|
|
if( !out.Write(data,size) )
|
|
|
|
|
CLEANUP_AND_RETURN( false );
|
|
|
|
|
CLEANUP_AND_RETURN( true );
|
|
|
|
|
}
|