Files
itgmania212121/stepmania/src/RageUtil.h
T

432 lines
14 KiB
C++
Raw Normal View History

2004-06-08 05:55:00 +00:00
/* RageUtil - Miscellaneous helper macros and functions. */
2004-05-06 02:40:33 +00:00
2004-12-05 21:09:31 +00:00
#ifndef RAGE_UTIL_H
#define RAGE_UTIL_H
2002-08-23 00:52:12 +00:00
2004-12-05 21:09:31 +00:00
#define SAFE_DELETE(p) { delete (p); (p)=NULL; }
#define SAFE_DELETE_ARRAY(p) { delete[] (p); (p)=NULL; }
2001-11-03 10:52:42 +00:00
2003-07-22 07:47:27 +00:00
#define ZERO(x) memset(&x, 0, sizeof(x))
2003-08-02 20:05:46 +00:00
#define COPY(a,b) { ASSERT(sizeof(a)==sizeof(b)); memcpy(&(a), &(b), sizeof(a)); }
2003-07-22 07:47:27 +00:00
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
2001-11-03 10:52:42 +00:00
/* Common harmless mismatches. All min(T,T) and max(T,T) cases are handled
* by the generic template we get from <algorithm>. */
inline float min(float a, int b) { return a < b? a:b; }
inline float min(int a, float b) { return a < b? a:b; }
inline float max(float a, int b) { return a > b? a:b; }
inline float max(int a, float b) { return a > b? a:b; }
2003-04-26 00:58:01 +00:00
inline unsigned long min(unsigned int a, unsigned long b) { return a < b? a:b; }
inline unsigned long min(unsigned long a, unsigned int b) { return a < b? a:b; }
inline unsigned long max(unsigned int a, unsigned long b) { return a > b? a:b; }
inline unsigned long max(unsigned long a, unsigned int b) { return a > b? a:b; }
/* Traditional defines. Only use this if you absolutely need
* a constant value. */
2002-07-28 20:28:37 +00:00
#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
2002-01-16 10:01:32 +00:00
#define clamp(val,low,high) ( max( (low), min((val),(high)) ) )
2002-07-23 01:41:40 +00:00
// Scales x so that l1 corresponds to l2 and h1 corresponds to h2. Does not modify x, MUST assign the result to something!
2004-01-12 00:32:18 +00:00
// Do the multiply before the divide to that integer scales have more precision.
#define SCALE(x, l1, h1, l2, h2) (((x) - (l1)) * ((h2) - (l2)) / ((h1) - (l1)) + (l2))
2003-08-31 22:37:32 +00:00
2004-06-03 08:22:02 +00:00
inline bool CLAMP(int &x, int l, int h)
{
if (x > h) { x = h; return true; }
else if (x < l) { x = l; return true; }
return false;
}
2005-02-14 01:30:05 +00:00
inline bool CLAMP(unsigned &x, unsigned l, unsigned h)
{
if (x > h) { x = h; return true; }
else if (x < l) { x = l; return true; }
return false;
}
2004-06-03 08:22:02 +00:00
inline bool CLAMP(float &x, float l, float h)
{
if (x > h) { x = h; return true; }
else if (x < l) { x = l; return true; }
return false;
}
2002-07-23 01:41:40 +00:00
2005-02-14 01:30:05 +00:00
inline void wrap(int &x, int n)
2003-09-01 02:04:16 +00:00
{
if (x<0)
x += ((-x/n)+1)*n;
x %= n;
}
2005-02-14 01:30:05 +00:00
inline void wrap(unsigned &x, unsigned n)
{
x %= n;
}
inline void wrap(float &x, float n)
2003-12-22 10:30:10 +00:00
{
if (x<0)
x += truncf(((-x/n)+1))*n;
2003-12-22 10:30:10 +00:00
x = fmodf(x,n);
}
2004-06-12 08:22:32 +00:00
/*
* We only have unsigned swaps; byte swapping a signed value doesn't make sense.
*
* Platform-specific, optimized versions are defined in arch_setup, with the names
* ArchSwap32, ArchSwap24, and ArchSwap16; we define them to their real names here,
* to force inclusion of this file when swaps are in use (to prevent different dependencies
* on different systems).
*/
#ifdef HAVE_BYTE_SWAPS
#define Swap32 ArchSwap32
#define Swap24 ArchSwap24
#define Swap16 ArchSwap16
#else
inline uint32_t Swap32( uint32_t n )
{
return (n >> 24) |
((n >> 8) & 0x0000FF00) |
((n << 8) & 0x00FF0000) |
(n << 24);
}
inline uint32_t Swap24( uint32_t n )
{
return Swap32(n) >> 8; // xx223344 -> 443322xx -> 00443322
}
inline uint16_t Swap16( uint16_t n )
{
return (n >> 8) | (n << 8);
}
#endif
#if defined(ENDIAN_LITTLE)
inline uint32_t Swap32LE( uint32_t n ) { return n; }
inline uint32_t Swap24LE( uint32_t n ) { return n; }
2004-06-13 07:14:24 +00:00
inline uint16_t Swap16LE( uint16_t n ) { return n; }
2004-06-12 08:22:32 +00:00
inline uint32_t Swap32BE( uint32_t n ) { return Swap32( n ); }
inline uint32_t Swap24BE( uint32_t n ) { return Swap24( n ); }
inline uint16_t Swap16BE( uint16_t n ) { return Swap16( n ); }
#else
inline uint32_t Swap32BE( uint32_t n ) { return n; }
inline uint32_t Swap24BE( uint32_t n ) { return n; }
2004-06-13 07:14:24 +00:00
inline uint16_t Swap16BE( uint16_t n ) { return n; }
2004-06-12 08:22:32 +00:00
inline uint32_t Swap32LE( uint32_t n ) { return Swap32( n ); }
inline uint32_t Swap24LE( uint32_t n ) { return Swap24( n ); }
2004-06-12 08:27:46 +00:00
inline uint16_t Swap16LE( uint16_t n ) { return Swap16( n ); }
2004-06-12 08:22:32 +00:00
#endif
2003-08-31 22:37:32 +00:00
2004-10-07 04:10:45 +00:00
extern int randseed;
2002-07-23 01:41:40 +00:00
2004-10-07 04:10:45 +00:00
float RandomFloat( int &seed );
2002-07-23 01:41:40 +00:00
inline float RandomFloat()
{
2004-10-07 04:10:45 +00:00
return RandomFloat( randseed );
2002-07-23 01:41:40 +00:00
}
// Returns a float between dLow and dHigh inclusive
inline float RandomFloat(float fLow, float fHigh)
{
2004-10-07 04:10:45 +00:00
return SCALE( RandomFloat(), 0.0f, 1.0f, fLow, fHigh );
2002-07-23 01:41:40 +00:00
}
// Returns an integer between nLow and nHigh inclusive
inline int RandomInt(int nLow, int nHigh)
{
2004-10-07 04:10:45 +00:00
return int( RandomFloat() * (nHigh - nLow + 1) + nLow );
2002-07-23 01:41:40 +00:00
}
/* Alternative: */
class RandomGen
{
2004-10-07 04:10:45 +00:00
int seed;
public:
RandomGen( unsigned long seed = 0 );
int operator() ( int maximum = INT_MAX-1 );
};
2002-07-23 01:41:40 +00:00
2001-11-03 10:52:42 +00:00
// Simple function for generating random numbers
inline float randomf( const float low=-1.0f, const float high=1.0f )
2002-02-28 19:40:40 +00:00
{
2004-10-07 04:10:45 +00:00
return RandomFloat( low, high );
2002-02-28 19:40:40 +00:00
}
/* return f rounded to the nearest multiple of fRoundInterval */
2004-10-24 17:44:51 +00:00
inline float Quantize( const float f, const float fRoundInterval )
2002-04-16 17:31:00 +00:00
{
return int( (f + fRoundInterval/2)/fRoundInterval ) * fRoundInterval;
}
2003-02-05 20:18:02 +00:00
2004-11-01 11:02:54 +00:00
inline int Quantize( const int i, const int iRoundInterval )
{
return int( (i + iRoundInterval/2)/iRoundInterval ) * iRoundInterval;
}
/* return f truncated to the nearest multiple of fTruncInterval */
inline float ftruncf( const float f, const float fTruncInterval )
{
return int( (f)/fTruncInterval ) * fTruncInterval;
}
2004-12-22 04:07:42 +00:00
/* Return i rounded up to the nearest multiple of iInterval. */
inline int QuantizeUp( int i, int iInterval )
{
return int( (i+iInterval-1)/iInterval ) * iInterval;
}
// Move val toward other_val by to_move.
void fapproach( float& val, float other_val, float to_move );
2003-02-05 20:18:02 +00:00
/* Return a positive x mod y. */
float fmodfp(float x, float y);
2001-11-03 10:52:42 +00:00
2002-12-20 06:28:22 +00:00
int power_of_two(int input);
2003-01-03 05:26:15 +00:00
bool IsAnInt( const CString &s );
2003-01-03 22:51:01 +00:00
bool IsHexVal( const CString &s );
2004-02-22 19:51:46 +00:00
float HHMMSSToSeconds( const CString &sHMS );
CString SecondsToHHMMSS( float fSecs );
CString SecondsToMMSSMsMs( float fSecs );
CString SecondsToMMSSMsMsMs( float fSecs );
2004-02-22 19:51:46 +00:00
CString PrettyPercent( float fNumerator, float fDenominator );
2004-02-22 21:32:36 +00:00
inline CString PrettyPercent( int fNumerator, int fDenominator ) { return PrettyPercent( float(fNumerator), float(fDenominator) ); }
2004-02-22 19:51:46 +00:00
CString Commify( int iNum );
2002-02-28 19:40:40 +00:00
2004-02-14 11:19:18 +00:00
struct tm GetLocalTime();
2004-02-14 08:27:09 +00:00
2003-12-17 04:17:28 +00:00
CString ssprintf( const char *fmt, ...) PRINTF(1,2);
2002-08-29 21:02:04 +00:00
CString vssprintf( const char *fmt, va_list argList );
2001-11-03 10:52:42 +00:00
#ifdef WIN32
CString hr_ssprintf( int hr, const char *fmt, ...);
2002-12-21 08:40:40 +00:00
CString werr_ssprintf( int err, const char *fmt, ...);
CString ConvertWstringToACP( wstring s );
CString ConvertUTF8ToACP( CString s );
#endif
2001-11-03 10:52:42 +00:00
2002-03-06 08:25:09 +00:00
// Splits a Path into 4 parts (Directory, Drive, Filename, Extention). Supports UNC path names.
2003-01-16 22:49:22 +00:00
/* If Path is a directory (eg. c:\games\stepmania"), append a slash so the last
* element will end up in Dir, not FName: "c:\games\stepmania\". */
2003-10-29 21:22:28 +00:00
void splitpath( const CString &Path, CString &Dir, CString &Filename, CString &Ext );
2001-12-28 10:15:59 +00:00
2003-09-04 19:28:39 +00:00
CString SetExtension( const CString &path, const CString &ext );
2003-12-07 22:54:50 +00:00
CString GetExtension( const CString &sPath );
2003-09-03 04:30:29 +00:00
typedef int longchar;
2003-01-28 00:45:01 +00:00
extern const wchar_t INVALID_CHAR;
2004-05-17 21:28:26 +00:00
int utf8_get_char_len( char p );
bool utf8_to_wchar( const CString &s, unsigned &start, wchar_t &ch );
bool utf8_to_wchar_ec( const CString &s, unsigned &start, wchar_t &ch );
void wchar_to_utf8( wchar_t ch, CString &out );
wchar_t utf8_get_char( const CString &s );
bool utf8_is_valid( const CString &s );
2004-12-03 01:02:45 +00:00
void utf8_remove_bom( CString &s );
2004-05-17 21:28:26 +00:00
2003-01-28 00:45:01 +00:00
CString WStringToCString(const wstring &str);
CString WcharToUTF8( wchar_t c );
wstring CStringToWstring(const CString &str);
2003-12-15 05:34:24 +00:00
// Splits a CString into an CStringArray according the Delimitor.
2003-12-07 22:54:50 +00:00
void split( const CString &Source, const CString &Delimitor, CStringArray& AddIt, const bool bIgnoreEmpty = true );
2003-12-15 05:34:24 +00:00
void split( const wstring &Source, const wstring &Delimitor, vector<wstring> &AddIt, const bool bIgnoreEmpty = true );
/* In-place split. */
void split( const CString &Source, const CString &Delimitor, int &begin, int &size, const bool bIgnoreEmpty = true );
void split( const wstring &Source, const wstring &Delimitor, int &begin, int &size, const bool bIgnoreEmpty = true );
/* In-place split of partial string. */
void split( const CString &Source, const CString &Delimitor, int &begin, int &size, int len, const bool bIgnoreEmpty ); /* no default to avoid ambiguity */
void split( const wstring &Source, const wstring &Delimitor, int &begin, int &size, int len, const bool bIgnoreEmpty );
2001-11-03 10:52:42 +00:00
2002-03-06 08:25:09 +00:00
// Joins a CStringArray to create a CString according the Deliminator.
2003-12-07 22:54:50 +00:00
CString join( const CString &Delimitor, const CStringArray& Source );
CString join( const CString &Delimitor, CStringArray::const_iterator begin, CStringArray::const_iterator end );
2001-11-03 10:52:42 +00:00
2003-09-05 06:20:55 +00:00
CString GetCwd();
2002-09-01 00:37:10 +00:00
2004-12-12 00:54:44 +00:00
void CRC32( unsigned int &iCRC, const void *pBuffer, size_t iSize );
2003-12-07 22:54:50 +00:00
unsigned int GetHashForString( const CString &s );
unsigned int GetHashForFile( const CString &sPath );
unsigned int GetHashForDirectory( const CString &sDir ); // a hash value that remains the same as long as nothing in the directory has changed
bool DirectoryIsEmpty( const CString &dir );
2001-11-03 10:52:42 +00:00
2002-10-24 08:17:09 +00:00
bool CompareCStringsAsc(const CString &str1, const CString &str2);
bool CompareCStringsDesc(const CString &str1, const CString &str2);
2003-12-07 22:54:50 +00:00
void SortCStringArray( CStringArray &AddTo, const bool bSortAscending = true );
2001-11-03 10:52:42 +00:00
/* Find the mean and standard deviation of all numbers in [start,end). */
float calc_mean(const float *start, const float *end);
float calc_stddev(const float *start, const float *end);
2003-11-17 03:38:14 +00:00
template<class T1, class T2>
2003-11-17 03:47:09 +00:00
int FindIndex( T1 begin, T1 end, const T2 *p )
2003-11-17 03:38:14 +00:00
{
2003-11-17 03:47:09 +00:00
T1 iter = find( begin, end, p );
if( iter == end )
2003-11-17 03:38:14 +00:00
return -1;
return iter - begin;
2003-11-17 03:38:14 +00:00
}
2005-01-25 00:21:21 +00:00
/* Useful for objects with no operator-, eg. map::iterator (more convenient than advance). */
template<class T>
inline T Increment( T a ) { ++a; return a; }
2005-01-25 00:21:21 +00:00
template<class T>
inline T Decrement( T a ) { --a; return a; }
2005-01-25 00:21:21 +00:00
void TrimLeft(CString &str, const char *s = "\r\n\t ");
void TrimRight(CString &str, const char *s = "\r\n\t ");
2003-02-14 22:28:29 +00:00
void StripCrnl(CString &s);
2003-01-01 22:02:41 +00:00
CString DerefRedir(const CString &path);
CString GetRedirContents(const CString &path);
2003-01-01 22:02:41 +00:00
class Regex {
void *reg;
unsigned backrefs;
2003-01-23 21:03:57 +00:00
CString pattern;
void Compile();
void Release();
public:
2003-03-27 20:16:17 +00:00
Regex(const CString &pat = "");
2003-01-23 21:03:57 +00:00
Regex(const Regex &rhs);
Regex &operator=(const Regex &rhs);
~Regex();
2003-03-27 20:16:17 +00:00
void Set(const CString &str);
bool Compare(const CString &str);
bool Compare(const CString &str, vector<CString> &matches);
};
2003-01-05 02:48:15 +00:00
2003-01-06 02:48:14 +00:00
void Replace_Unicode_Markers( CString &Text );
2003-01-09 23:53:14 +00:00
CString WcharDisplayText(wchar_t c);
2003-01-06 02:48:14 +00:00
2003-10-29 20:18:04 +00:00
CString Basename( const CString &dir );
CString Dirname( const CString &dir );
2003-12-07 22:54:50 +00:00
CString Capitalize( const CString &s );
2002-12-17 08:24:11 +00:00
#ifndef WIN32
2002-12-16 02:11:16 +00:00
#include <unistd.h> /* correct place with correct definitions */
2002-08-29 21:02:04 +00:00
#endif
2003-07-03 02:41:27 +00:00
/* ASCII-only case insensitivity. */
struct char_traits_char_nocase: public char_traits<char>
{
2003-10-20 03:57:02 +00:00
static char uptab[256];
2003-07-03 02:41:27 +00:00
2003-10-20 03:57:02 +00:00
static inline bool eq( char c1, char c2 )
{ return uptab[(unsigned char)c1] == uptab[(unsigned char)c2]; }
2003-07-03 02:41:27 +00:00
2003-10-20 03:57:02 +00:00
static inline bool ne( char c1, char c2 )
{ return uptab[(unsigned char)c1] != uptab[(unsigned char)c2]; }
2003-10-20 03:57:02 +00:00
static inline bool lt( char c1, char c2 )
{ return uptab[(unsigned char)c1] < uptab[(unsigned char)c2]; }
2003-07-03 02:41:27 +00:00
2003-07-03 19:42:48 +00:00
static int compare( const char* s1, const char* s2, size_t n )
{
2003-10-20 03:57:02 +00:00
int ret = 0;
while( n-- )
2003-07-03 19:42:48 +00:00
{
2003-10-20 03:57:02 +00:00
ret = fasttoupper(*s1++) - fasttoupper(*s2++);
if( ret != 0 )
break;
2003-07-03 19:42:48 +00:00
}
2003-10-20 03:57:02 +00:00
return ret;
2003-07-03 02:41:27 +00:00
}
static inline char fasttoupper(char a)
{
return uptab[(unsigned char)a];
2003-07-03 02:41:27 +00:00
}
2003-07-03 19:42:48 +00:00
static const char *find( const char* s, int n, char a )
{
a = fasttoupper(a);
while( n-- > 0 && fasttoupper(*s) != a )
++s;
if(fasttoupper(*s) == a)
return s;
return NULL;
2003-07-03 02:41:27 +00:00
}
};
typedef basic_string<char,char_traits_char_nocase> istring;
2003-12-07 04:20:07 +00:00
/* Compatibility/convenience shortcuts. These are actually defined in RageFileManager.h, but
* declared here since they're used in many places. */
2003-12-07 22:54:50 +00:00
void GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs=false, bool bReturnPathToo=false );
2003-12-07 04:20:07 +00:00
bool DoesFileExist( const CString &sPath );
bool IsAFile( const CString &sPath );
bool IsADirectory( const CString &sPath );
unsigned GetFileSizeInBytes( const CString &sFilePath );
void FlushDirCache();
2003-12-21 02:20:53 +00:00
// call FixSlashes on any path that came from the user
void FixSlashesInPlace( CString &sPath );
CString FixSlashes( CString sPath );
void CollapsePath( CString &sPath, bool bRemoveLeadingDot=false );
2003-12-21 02:20:53 +00:00
2004-12-07 01:43:06 +00:00
bool FromString( const CString &sValue, int &out );
bool FromString( const CString &sValue, unsigned &out );
bool FromString( const CString &sValue, float &out );
bool FromString( const CString &sValue, bool &out );
2005-02-20 03:43:32 +00:00
inline bool FromString( const CString &sValue, CString &out ) { out = sValue; return true; }
2004-12-07 01:43:06 +00:00
CString ToString( int value );
CString ToString( unsigned value );
CString ToString( float value );
CString ToString( bool value );
2005-02-20 03:09:14 +00:00
inline CString ToString( const CString &value ) { return value; }
2004-12-07 01:43:06 +00:00
// helper file functions used by Bookkeeper and ProfileManager
//
// Helper function for reading/writing scores
//
2003-12-21 02:20:53 +00:00
class RageFile;
bool FileRead(RageFile& f, CString& sOut);
bool FileRead(RageFile& f, int& iOut);
bool FileRead(RageFile& f, unsigned& uOut);
bool FileRead(RageFile& f, float& fOut);
void FileWrite(RageFile& f, const CString& sWrite);
void FileWrite(RageFile& f, int iWrite);
void FileWrite(RageFile& f, size_t uWrite);
void FileWrite(RageFile& f, float fWrite);
2003-12-07 04:20:07 +00:00
bool FileCopy( CString sSrcFile, CString sDstFile );
2003-12-08 10:27:45 +00:00
2002-11-16 06:56:25 +00:00
#endif
2004-05-06 02:40:33 +00:00
/*
* Copyright (c) 2001-2004 Chris Danford, Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/