Files
itgmania212121/stepmania/src/RageUtil.cpp
T

489 lines
12 KiB
C++
Raw Normal View History

2001-11-03 10:52:42 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
File: RageUtil.cpp
Desc: Helper and error-controlling function used throughout the program.
Copyright (c) 2001 Chris Danford. All rights reserved.
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
#include "RageUtil.h"
//-----------------------------------------------------------------------------
// UTIL globals
//-----------------------------------------------------------------------------
const CString g_sLogFileName = "debug.log";
//-----------------------------------------------------------------------------
// Name: randomf()
// Desc:
//-----------------------------------------------------------------------------
2001-11-30 09:38:35 +00:00
float randomf( float low, float high )
2001-11-03 10:52:42 +00:00
{
return low + ( high - low ) * ( (FLOAT)rand() ) / RAND_MAX;
}
//-----------------------------------------------------------------------------
// Name: roundf()
// Desc:
//-----------------------------------------------------------------------------
2001-11-30 09:38:35 +00:00
int roundf( float f )
2001-11-03 10:52:42 +00:00
{
return (int)((f)+0.5f);
}
int roundf( double f )
{
2001-11-30 09:38:35 +00:00
return (int)((f)+0.5);
2001-11-03 10:52:42 +00:00
}
2001-12-28 10:15:59 +00:00
bool IsAnInt( CString s )
{
if( s.GetLength() == 0 )
return false;
for( int i=0; i<s.GetLength(); i++ )
{
if( s[i] < '0' || s[i] > '9' )
return false;
}
return true;
}
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// Name: RageLogStart()
// Desc:
//-----------------------------------------------------------------------------
void RageLogStart()
{
2001-12-28 10:15:59 +00:00
#if defined(DEBUG) | defined(_DEBUG)
2001-11-03 10:52:42 +00:00
// delete the old log and create a new one
DeleteFile( g_sLogFileName );
FILE *fp = NULL;
fp = fopen( g_sLogFileName, "w" );
fclose( fp );
SYSTEMTIME st;
GetLocalTime( &st );
RageLog( "%s: last compiled on %s.", g_sLogFileName, __TIMESTAMP__ );
RageLog( "Log starting %.4d-%.2d-%.2d %.2d:%.2d:%.2d",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond );
RageLog( "\n" );
2001-12-28 10:15:59 +00:00
#endif
2001-11-03 10:52:42 +00:00
}
//-----------------------------------------------------------------------------
// Name: RageLog()
// Desc:
//-----------------------------------------------------------------------------
void RageLog( LPCTSTR fmt, ...)
{
va_list va;
va_start(va, fmt);
CString sBuff = vssprintf( fmt, va );
sBuff += "\n";
FILE *fp = NULL;
// open our logging file
fp = fopen( g_sLogFileName, "a" );
if( fp==NULL )
RageLog( ssprintf("Couldn't write to log %s", g_sLogFileName) );
fprintf(fp, sBuff);
fclose(fp);
2002-01-20 07:51:09 +00:00
}
2001-11-03 10:52:42 +00:00
2002-01-20 07:51:09 +00:00
void RageLogHr( HRESULT hr, LPCTSTR fmt, ...)
{
va_list va;
va_start(va, fmt);
CString s = vssprintf( fmt, va );
s += ssprintf( "(%s)", DXGetErrorString8(hr) );
RageLog( s );
2001-11-03 10:52:42 +00:00
}
//-----------------------------------------------------------------------------
// Name: ssprintf()
// Desc:
//-----------------------------------------------------------------------------
CString ssprintf( LPCTSTR fmt, ...)
{
va_list va;
va_start(va, fmt);
return vssprintf(fmt, va);
}
//-----------------------------------------------------------------------------
// Name: vssprintf()
// Desc:
//-----------------------------------------------------------------------------
CString vssprintf( LPCTSTR fmt, va_list argList)
{
CString str;
str.FormatV(fmt, argList);
return str;
}
//-----------------------------------------------------------------------------
// Name: join()
// Desc:
//-----------------------------------------------------------------------------
CString join(CString Deliminator, CStringArray& Source)
{
CString csReturn;
CString csTmp;
// Loop through the Array and Append the Deliminator
for( int iNum = 0; iNum < Source.GetSize(); iNum++ ) {
csTmp += Source.GetAt(iNum);
csTmp += Deliminator;
}
csReturn = csTmp.Left(csTmp.GetLength() - 1);
return csReturn;
}
//-----------------------------------------------------------------------------
// Name: split()
// Desc:
//-----------------------------------------------------------------------------
2001-12-28 10:15:59 +00:00
void split( CString Source, CString Deliminator, CStringArray& AddIt, bool bIgnoreEmpty )
2001-11-03 10:52:42 +00:00
{
CString newCString;
CString tmpCString;
CString AddCString;
int pos1 = 0;
int pos = 0;
newCString = Source;
do {
pos1 = 0;
pos = newCString.Find(Deliminator, pos1);
if ( pos != -1 ) {
CString AddCString = newCString.Left(pos);
2001-12-28 10:15:59 +00:00
if( newCString.IsEmpty() && bIgnoreEmpty )
; // do nothing
else
AddIt.Add(AddCString);
2001-11-03 10:52:42 +00:00
tmpCString = newCString.Mid(pos + Deliminator.GetLength());
newCString = tmpCString;
}
} while ( pos != -1 );
2001-12-28 10:15:59 +00:00
if( newCString.IsEmpty() && bIgnoreEmpty )
; // do nothing
else
2001-11-03 10:52:42 +00:00
AddIt.Add(newCString);
}
//-----------------------------------------------------------------------------
// Name: splitpath()
// Desc:
//-----------------------------------------------------------------------------
2001-12-28 10:15:59 +00:00
void splitpath( BOOL UsingDirsOnly, CString Path, CString& Drive, CString& Dir, CString& FName, CString& Ext )
2001-11-03 10:52:42 +00:00
{
int nSecond;
// Look for a UNC Name!
2001-12-28 10:15:59 +00:00
if (Path.Left(2) == "\\\\")
{
2001-11-03 10:52:42 +00:00
int nFirst = Path.Find("\\",3);
nSecond = Path.Find("\\",nFirst + 1);
if (nSecond == -1) {
Drive = Path;
Dir = "";
FName = "";
Ext = "";
}
else if (nSecond > nFirst)
Drive = Path.Left(nSecond);
}
2001-12-28 10:15:59 +00:00
else if (Path.Mid(1,1) == ":" ) // normal Drive Structure
{
2001-11-03 10:52:42 +00:00
nSecond = 2;
Drive = Path.Left(2);
}
2001-12-28 10:15:59 +00:00
else // no UNC or drive letter
{
nSecond = -1;
}
2001-11-03 10:52:42 +00:00
if (UsingDirsOnly) {
Dir = Path.Right((Path.GetLength() - nSecond) - 1);
FName = "";
Ext = "";
}
else {
int nDirEnd = Path.ReverseFind('\\');
if (nDirEnd == Path.GetLength()) {
Dir = "";
FName = "";
Ext = "";
}
else {
Dir = Path.Mid(nSecond + 1, (nDirEnd - nSecond) - 1);
int nFileEnd = Path.ReverseFind('.');
if (nFileEnd != -1) {
if (nDirEnd > nFileEnd) {
FName = Path.Right(Path.GetLength() - nDirEnd);
Ext = "";
}
else {
FName = Path.Mid(nDirEnd + 1, (nFileEnd - nDirEnd) - 1);
Ext = Path.Right((Path.GetLength() - nFileEnd) - 1);
}
}
else {
FName = Path.Right((Path.GetLength() - nDirEnd) - 1);
Ext = "";
}
}
}
}
2001-12-28 10:15:59 +00:00
//-----------------------------------------------------------------------------
// Name: splitpath()
// Desc:
//-----------------------------------------------------------------------------
void splitrelpath( CString Path, CString& Dir, CString& FName, CString& Ext )
{
// need to split on both forward slashes and back slashes
CStringArray sPathBits;
CStringArray sBackShashPathBits;
split( Path, "\\", sBackShashPathBits, true );
for( int i=0; i<sBackShashPathBits.GetSize(); i++ ) // foreach backslash bit
{
CStringArray sForwardShashPathBits;
split( sBackShashPathBits[i], "/", sForwardShashPathBits, true );
sPathBits.Append( sForwardShashPathBits );
}
if( sPathBits.GetSize() == 0 )
{
Dir = FName = Ext = "";
return;
}
CString sFNameAndExt = sPathBits[ sPathBits.GetSize()-1 ];
// subtract the FNameAndExt from Path
Dir = Path.Left( Path.GetLength()-sFNameAndExt.GetLength() ); // don't subtract out the trailing slash
CStringArray sFNameAndExtBits;
split( sFNameAndExt, ".", sFNameAndExtBits, false );
if( sFNameAndExtBits.GetSize() == 0 ) // no file at the end of this path
{
FName = "";
Ext = "";
}
else if( sFNameAndExtBits.GetSize() == 1 ) // file doesn't have extension
{
FName = sFNameAndExtBits[0];
Ext = "";
}
else if( sFNameAndExtBits.GetSize() > 1 ) // file has extension and possibly multiple periods
{
Ext = sFNameAndExtBits[ sFNameAndExtBits.GetSize()-1 ];
// subtract the Ext and last period from FNameAndExt
FName = sFNameAndExt.Left( sFNameAndExt.GetLength()-Ext.GetLength()-1 );
}
}
2001-11-03 10:52:42 +00:00
void GetDirListing( CString sPath, CStringArray &AddTo, BOOL bOnlyDirs )
{
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile( sPath, &fd );
if( INVALID_HANDLE_VALUE != hFind )
{
do
{
2001-12-28 10:15:59 +00:00
if( bOnlyDirs && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
2001-11-03 10:52:42 +00:00
{
2001-12-28 10:15:59 +00:00
; // do nothing
}
else
{
// add it
2001-11-03 10:52:42 +00:00
CString sDirName( fd.cFileName );
if( sDirName != "." && sDirName != ".." )
AddTo.Add( sDirName );
}
} while( ::FindNextFile( hFind, &fd ) );
::FindClose( hFind );
}
}
2001-12-20 12:37:38 +00:00
DWORD GetFileSizeInBytes( CString sFilePath )
{
HANDLE hFile = CreateFile(
sFilePath, // pointer to name of the file
GENERIC_READ, // access (read-write) mode
FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode
NULL, // pointer to security attributes
OPEN_EXISTING, // how to create
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL // handle to file with attributes to
);
return GetFileSize( hFile, NULL );
}
2001-11-25 04:31:44 +00:00
bool DoesFileExist( CString sPath )
2001-11-03 10:52:42 +00:00
{
2002-01-20 07:51:09 +00:00
//RageLog( "DoesFileExist(%s)", sPath );
2001-11-03 10:52:42 +00:00
DWORD dwAttr = GetFileAttributes( sPath );
if( dwAttr == (DWORD)-1 )
return FALSE;
else
return TRUE;
}
int CompareCStrings(const void *arg1, const void *arg2)
{
CString str1 = *(CString *)arg1;
CString str2 = *(CString *)arg2;
return str1.CompareNoCase( str2 );
}
2001-11-30 09:38:35 +00:00
void SortCStringArray( CStringArray &arrayCStrings, BOOL bSortAcsending )
2001-11-03 10:52:42 +00:00
{
2001-11-30 09:38:35 +00:00
qsort( arrayCStrings.GetData(), arrayCStrings.GetSize(), sizeof(CString), CompareCStrings );
2001-11-03 10:52:42 +00:00
}
//-----------------------------------------------------------------------------
// Name: DisplayErrorAndDie()
// Desc:
//-----------------------------------------------------------------------------
VOID DisplayErrorAndDie( CString sError )
{
2002-01-16 10:01:32 +00:00
// Something very bad happened. Display an error dialog, then exit right away.
/*
////////////////////////
// get a stack trace
////////////////////////
AfxDumpStack( AFX_STACK_DUMP_TARGET_CLIPBOARD );
//open the clipboard
CString fromClipboard;
if( OpenClipboard(NULL) )
{
HANDLE hData = GetClipboardData( CF_TEXT );
char *buffer = (char*)GlobalLock( hData );
fromClipboard = buffer;
GlobalUnlock( hData );
CloseClipboard();
}
sError += "\n\n" + fromClipboard;
*/
2001-11-03 10:52:42 +00:00
RageLog( "" );
RageLog( "// Fatal Error /////////////////////////////" );
RageLog( sError );
RageLog( "////////////////////////////////////////////" );
2002-01-16 10:01:32 +00:00
2001-11-03 10:52:42 +00:00
MessageBox(
NULL, // handle of owner window
sError, // text in message box
"Fatal Error", // address of title of message box
MB_OK | MB_ICONERROR // style of message box
);
exit(1);
}
LONG GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
{
HKEY hkey;
LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
if (retval == ERROR_SUCCESS) {
long datasize = MAX_PATH;
TCHAR data[MAX_PATH];
RegQueryValue(hkey, NULL, data, &datasize);
lstrcpy(retdata,data);
RegCloseKey(hkey);
}
return retval;
}
HINSTANCE GotoURL(LPCTSTR url)
{
TCHAR key[MAX_PATH + MAX_PATH];
// First try ShellExecute()
HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, SW_SHOWDEFAULT);
// If it failed, get the .htm regkey and lookup the program
if ((UINT)result <= HINSTANCE_ERROR) {
if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS) {
lstrcat(key, _T("\\shell\\open\\command"));
if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS) {
TCHAR *pos;
pos = _tcsstr(key, _T("\"%1\""));
if (pos == NULL) { // No quotes found
pos = strstr(key, _T("%1")); // Check for %1, without quotes
if (pos == NULL) // No parameter at all...
pos = key+lstrlen(key)-1;
else
*pos = '\0'; // Remove the parameter
}
else
*pos = '\0'; // Remove the parameter
lstrcat(pos, _T(" "));
lstrcat(pos, url);
result = (HINSTANCE) WinExec(key,SW_SHOWDEFAULT);
}
}
}
return result;
}