Files
itgmania212121/stepmania/src/RageUtil.h
T

124 lines
4.0 KiB
C
Raw Normal View History

2002-05-19 01:59:48 +00:00
#pragma once
/*
-----------------------------------------------------------------------------
File: RageUtil
Desc: Miscellaneous helper functions.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
2002-01-20 07:51:09 +00:00
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// SAFE_ Macros
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
//-----------------------------------------------------------------------------
// Other Macros
//-----------------------------------------------------------------------------
#define RECTWIDTH(rect) ((rect).right - (rect).left)
#define RECTHEIGHT(rect) ((rect).bottom - (rect).top)
2002-05-19 01:59:48 +00:00
inline int RECTCENTERX(RECT rect) { return rect.left + (rect.right-rect.left)/2; }
inline int RECTCENTERY(RECT rect) { return rect.top + (rect.bottom-rect.top)/2; }
2001-11-03 10:52:42 +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)) ) )
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// Misc helper functions
//-----------------------------------------------------------------------------
// 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
{
return low + ( high - low ) * ( (FLOAT)rand() ) / RAND_MAX;
}
inline int roundf( const float f ) { return (int)((f)+0.5f); };
inline int roundf( const double f ) { return (int)((f)+0.5); };
2002-04-16 17:31:00 +00:00
inline float froundf( const float f, const float fRoundInterval )
{
return int( (f + fRoundInterval/2)/fRoundInterval ) * fRoundInterval;
}
2001-11-03 10:52:42 +00:00
bool IsAnInt( LPCTSTR s );
2002-04-28 20:42:32 +00:00
float TimeToSeconds( CString sHMS );
2001-12-28 10:15:59 +00:00
2002-02-28 19:40:40 +00:00
2001-11-03 10:52:42 +00:00
CString ssprintf( LPCTSTR fmt, ...);
CString vssprintf( LPCTSTR fmt, va_list argList );
2002-03-06 08:25:09 +00:00
// Splits a Path into 4 parts (Directory, Drive, Filename, Extention). Supports UNC path names.
// param1: Whether the Supplied Path (PARAM2) contains a directory name only
// or a file name (Reason: some directories will end with "xxx.xxx"
// which is like a file name).
void splitpath(
const bool UsingDirsOnly,
const CString &Path,
CString &Drive,
CString &Dir,
CString &FName,
CString &Ext
);
2001-11-03 10:52:42 +00:00
void splitrelpath(
const CString &Path,
CString& Dir,
CString& FName,
CString& Ext
);
2001-12-28 10:15:59 +00:00
2002-04-16 17:31:00 +00:00
// Splits a CString into an CStringArray according the Deliminator.
void split(
const CString &Source,
const CString &Deliminator,
CStringArray& AddIt,
const bool bIgnoreEmpty = true
);
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.
CString join(
const CString &Deliminator,
const CStringArray& Source
);
2001-11-03 10:52:42 +00:00
2002-05-19 06:13:16 +00:00
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs=false, bool bReturnPathToo=false );
2002-05-19 01:59:48 +00:00
ULONG GetHashForString( CString s );
ULONG GetHashForFile( CString sPath );
ULONG GetHashForDirectory( CString sDir ); // a hash value that remains the same as long as nothing in the directory has changed
2001-11-03 10:52:42 +00:00
bool DoesFileExist( const CString &sPath );
DWORD GetFileSizeInBytes( const CString &sFilePath );
2001-11-03 10:52:42 +00:00
int CompareCStringsAsc(const void *arg1, const void *arg2);
int CompareCStringsDesc(const void *arg1, const void *arg2);
void SortCStringArray( CStringArray &AddTo, const bool bSortAcsending = true );
2001-11-03 10:52:42 +00:00
2002-03-06 08:25:09 +00:00
LONG GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata);
HINSTANCE GotoURL(LPCTSTR url);
2001-11-03 10:52:42 +00:00
2002-05-19 01:59:48 +00:00
void WriteStringToFile( FILE* file, CString s );
void ReadStringFromFile( FILE* file, CString& s );
2002-05-27 08:23:27 +00:00
void WriteIntToFile( FILE* file, int i );
void ReadIntFromFile( FILE* file, int& i );
void WriteFloatToFile( FILE* file, float f );
void ReadFloatFromFile( FILE* file, float& f );
void WriteUlongToFile( FILE* file, ULONG u );
void ReadUlongFromFile( FILE* file, ULONG& u );
2001-11-03 10:52:42 +00:00