⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 itapi.cpp

📁 完整的MP3播放器源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:

		OFSTRUCT ofn;
		memset(&ofn, 0, sizeof(ofn));

		if (::OpenFile(strFile, &ofn, OF_EXIST) != HFILE_ERROR)
			strFullPath = ofn.szPathName;
	}

	if (strFullPath.IsEmpty())
	{
		CString strError;

		// TODO_LATER: Make these strings resources
		if (strFile.IsEmpty())
		{
			strError.Format(
				"The following application profile setting is missing a file specification:\n"
				"\n"
				"Section:\t%s\n"
				"Entry:\t%s\n",
				"\n"
				"Please check the registry or the profile file.",
				lpszSection, lpszFileEntry);
		}
		else
		{
			strError.Format(
				"The following file cannot be found:\n"
				"%hs\n"
				"\n"
				"Please check the application profile setting '%s' in the '%s' section "
				"and make sure it contains a valid path.",
				(LPCTSTR)strFile, lpszFileEntry, lpszSection);
		}

		AfxMessageBox(strError);
		AfxThrowUserException();	
	}

	return strFullPath;
}

// @func Inserts a string into the specified string.
//
// @parm The index of the insertion.
// @parm The string to insert into the destination.
// @parm The string to insert into.
// @parm The size of the <p lpszDest> buffer.
//
// @rdesc A pointer to <p lpszDest>.
//
// @comm This function is in the ITCLIB namespace.
LPCTSTR ITCLIB::InsertString(int nIndex, LPCTSTR lpszInsert,
	LPTSTR lpszDest, size_t nDestLen)
{
	UNUSED(nDestLen);

	ASSERT((size_t)lstrlen(lpszInsert)+(size_t)lstrlen(lpszDest) < nDestLen);

	memmove(lpszDest+nIndex+lstrlen(lpszInsert), lpszDest, lstrlen(lpszDest));
	strncpy(lpszDest+nIndex, lpszInsert, strlen(lpszInsert));

	return lpszDest;
}

// @func This function trims leading and trailing whitespace from the
// specified string.
//
// @syntax LPCTSTR TrimString(CString& rString);
// @syntax LPCTSTR TrimString(LPTSTR lpszSrc);
//
// @parm CString& | rString | A reference to a <c CString>.
// @parm LPTSTR | lpszSrc | A null-terminated string.
//
// @rdesc A pointer the the string.
//
// @comm This function is in the ITCLIB namespace.
LPCTSTR ITCLIB::TrimString(CString& rString)
{
	rString.TrimLeft();
	rString.TrimRight();
	return rString;
}

LPCTSTR ITCLIB::TrimString(LPTSTR lpszSrc)
{
	TrimLeft(lpszSrc);
	TrimRight(lpszSrc);
	return lpszSrc;
}

// @func This function compares the rightmost characters in
// the string with <p pszTail>. 
//
// @parm A null-terminated string.
// @parm The string to compare to.
//
// @rdesc The results of <f stricmp> or -1 if <p pszTail> has more
// characters than <p lpszSrc>.
//
// @comm This function is in the ITCLIB namespace.
int ITCLIB::CompareRight(LPCTSTR lpszSrc, LPCTSTR pszTail)
{
	int n = lstrlen(lpszSrc) - lstrlen(pszTail);
	if (n < 0)
		return -1;

	return stricmp(lpszSrc+n, pszTail);
}

// @func This function removes the newline characters \r and \n
// from the specified string.
//
// @syntax LPCTSTR StripNewline(CString& rString);
// @syntax LPCTSTR StripNewline(LPTSTR lpszSrc);
//
// @parm CString& | rString | A reference to a <c CString>.
// @parm LPTSTR | lpszSrc | A null-terminated string.
//
// @rdesc A pointer the the string.
//
// @comm This function is in the ITCLIB namespace.
LPCTSTR ITCLIB::StripNewline(LPTSTR lpszSrc)
{
	while ((lstrlen(lpszSrc) > 0) && 
	       ((lpszSrc[lstrlen(lpszSrc)-1] == '\r') ||
		(lpszSrc[lstrlen(lpszSrc)-1] == '\n')))
	{
		lpszSrc[lstrlen(lpszSrc)-1] = '\0';
	}

	return lpszSrc;
}

LPCTSTR ITCLIB::StripNewline(CString& str)
{
	StripNewline(str.GetBuffer(0));
	str.ReleaseBuffer();
	return str;
}

// @func This function splits a string into an array of substrings.
//
// @parm The string to split.
// @parm The string that separates each substring.
// @parm A reference to a <c CStringArray> to add the substrings to.
//
// @rdesc The number of strings in the array.
//
// @comm This function is in the ITCLIB namespace.
// 
// @xref <f StringArrayToString>
int ITCLIB::StringToStringArray(CString strText,
	CString strSeperator, CStringArray& array)
{
	// Start with an empty array
	array.RemoveAll();

	ASSERT(array.GetSize() == 0);

	// This is the beginning of the separator
	int nSep = strText.Find(strSeperator);
	int nSepLen = strSeperator.GetLength();

	while (nSep != -1)
	{
		CString strLine = strText.Left(nSep);
		array.Add(strLine);

		// The next search starts after the separator
		strText = strText.Mid(nSep + nSepLen);

		nSep = strText.Find(strSeperator);
	}

	if (!strText.IsEmpty())
	{
		array.Add(strText);
	}

	return array.GetSize();
}

// @func This function concatenates the substrings in the specified
// <f CStringArray> to form a single string.
//
// @parm A reference to a <c CStringArray> containing substrings.
// @parm The separator to insert between each substring.
// @parm A reference to a <c CString> to fill.
//
// @rdesc The string <p strText>.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <f StringToStringArray>
CString& ITCLIB::StringArrayToString(CStringArray& src,
	CString strSeperator, CString& strText)
{
	strText.Empty();

	// Create a local copy that we can manipulate
	CStringArray array;
	array.Copy(src);
	ASSERT(array.GetSize() == src.GetSize());

	// Starting from the end, remove any empty strings
	// so we don't end up with a text buffer full of
	// separators.
	int ub = array.GetUpperBound();
	while (ub >= 0)
	{
		CString strLine = array.GetAt(ub);
		if (strLine.IsEmpty())
		{
			array.RemoveAt(ub);
			ub = array.GetUpperBound();
		}
		else
		{
			ub = -1; // Exit the loop
		}
	}

	// Build the text buffer
	for (int i=0; i < array.GetSize(); i++)
	{
		// Each line should be seperated
		if ((i > 0) && !strSeperator.IsEmpty())
			strText += strSeperator;

		strText += array.GetAt(i);
	}

	return strText;
}

// @func Converts a number representing bytes into a string description.
//
// @parm The number of bytes.
//
// @rdesc The string description.
//
// @comm This function is in the ITCLIB namespace.
//
// @ex |
// ASSERT(BytesToString(10) == "10");
// ASSERT(BytesToString(1024) == "1KB");
// ASSERT(BytesToString(1024*1024) == "1.0MB");
// ASSERT(BytesToString(1024*1024*1024) == "1.00GB");

CString ITCLIB::BytesToString(long bytes)
{
	CString str;
	const double OneKb = 1024;
	const double OneMb = OneKb * OneKb;
	const double OneGb = OneMb * OneKb;

	if (bytes < OneKb)
	{
		str.Format("%d", bytes);
	}
	else if ((bytes >= OneKb) && (bytes < OneMb))
	{
		double kbytes = (double)bytes / OneKb;
		str.Format("%.0fKB", kbytes);
	}
	else if ((bytes >= OneMb) && (bytes < OneGb))
	{
		double mbytes = (double)bytes / OneMb;
		str.Format("%.1fMB", mbytes);
	}
	else
	{
		ASSERT(bytes >= OneGb);
		double gbytes = (double)bytes / OneGb;
		str.Format("%.2fGB", gbytes);
	}

	return str;
}

// @func This function converts a value into a string using
// <mf CString::Format>.
//
// @parm The value to convert.
//
// @rdesc The string representation of the value.
//
// @comm This function is in the ITCLIB namespace.
CString ITCLIB::ToString(long value)
{
	CString str;
	str.Format("%ld", value);
	return str;
}

// @func Returns a <c CSize> object containing the HORZRES and VERTRES
// of the specified device context in device coordinates.
//
// @parm A pointer to a device context.
//
// @rdesc The device resolution in device coordinates.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <f DeviceResolutionLP>
CSize ITCLIB::DeviceResolutionDP(CDC* pDC)
{
	ASSERT_VALID(pDC);

	return CSize(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
}

// @func Returns a <c CSize> object containing the HORZRES and VERTRES
// of the specified device context in logical coordinates.
//
// @parm A pointer to a device context.
//
// @rdesc The device resolution in logical coordinates.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <f DeviceResolutionDP>
CSize ITCLIB::DeviceResolutionLP(CDC* pDC)
{
	CSize size = DeviceResolutionDP(pDC);
	pDC->DPtoLP(&size);
	return size;
}

// @func Sets the viewport origin of the specified device context.
//
// @parm A pointer to a device context.
// @parm The origin in logical coordinates.
//
// @rdesc The previous origin in logical coordinates.
//
// @comm This function is in the ITCLIB namespace.
CPoint ITCLIB::SetViewportOrgLP(CDC* pDC, CPoint origin)
{
	ASSERT_VALID(pDC);

	// SetViewportOrg expects a POINT that is
	// in device units.  This function deals with
	// logical units.

	pDC->LPtoDP(&origin);
	origin = pDC->SetViewportOrg(origin.x, origin.y);
	pDC->DPtoLP(&origin);

	return origin;
}

// @func This function uses <f GetComputerName> to determine the
// computer name of the current system.
//
// @rdesc The computer name of the current system.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <c GetUserName>
CString ITCLIB::GetComputerName()
{
	CString strComputer;
	DWORD dwLength = MAX_COMPUTERNAME_LENGTH + 1;

	// Have GetComputerName fill dwLength
	::GetComputerName(strComputer.GetBufferSetLength(dwLength), &dwLength);
	strComputer.ReleaseBuffer();

	// Now use a buffer length of dwLength
	dwLength++; // I don't trust the API
	::GetComputerName(strComputer.GetBufferSetLength(dwLength), &dwLength);
	strComputer.ReleaseBuffer();

	return strComputer;
}

// @func This function uses <f GetUserName> to determine the
// user name of the current thread.
//
// @rdesc The user name of the current thread.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <c GetComputerName>
CString ITCLIB::GetUserName()
{
	CString strUser;
	DWORD dwLength = 2;

	// Have GetUserName fill dwLength
	::GetUserName(strUser.GetBufferSetLength(dwLength), &dwLength);
	strUser.ReleaseBuffer();

	// Now use a buffer length of dwLength
	dwLength++; // I don't trust the API
	::GetUserName(strUser.GetBufferSetLength(dwLength), &dwLength);
	strUser.ReleaseBuffer();

	return strUser;
}

// @func This function splits a complete path into its folder and
// file name components. The components can be separated by '\' or '/'.
//
// @parm A <c CString> containing the full path of a file.
// @parm A reference to a <c CString> to be set to the folder portion
// of the file's path. The folder will not contain a trailing backslash.
// @parm A reference to a <c CString> to be set to the file name.
//
// @comm This function is in the ITCLIB namespace.
void ITCLIB::SplitPath(CString strFilePath, CString& strFolder, CString& strFileName)
{
	int n = strFilePath.ReverseFind('\\');
	if (n < 0)
		n = strFilePath.ReverseFind('/');

	if (n >= 0)
	{
		strFolder = strFilePath.Left(n);
		strFileName = strFilePath.Right(strFilePath.GetLength()-n-1);
	}
	else
	{
		strFolder.Empty();
		strFileName = strFilePath;
	}
}

// @func This function has the same functionality as the MFC
// <f DYNAMIC_DOWNCAST> macro. The difference is that this function
// will ASSERT if the object is NULL or is not of the specified type.
//
// @parm The runtime class of a class.
// @parm A pointer to a <c CObject>.
//
// @rdesc The same thing <f DYNAMIC_DOWNCAST> does.
//
// @xref <c ITCLIB>
CObject* ITCLIB::TypeCastKindOf(CRuntimeClass* pClass, CObject* pObject)
{
	ASSERT(pObject != NULL);
	ASSERT(pObject->IsKindOf(pClass));

	// Do what DYNAMIC_DOWNCAST does
	if (pObject != NULL && pObject->IsKindOf(pClass))
		return pObject;
	else
		return NULL;
}

const CObject* ITCLIB::TypeCastKindOf(CRuntimeClass* pClass, const CObject* pObject)
{
	return (CObject*)ITCLIB::TypeCastKindOf(pClass, (const CObject*)pObject);
}

// @func This function does the same thing that <f strncpy> does except
// it always null terminates the string.
//
// @parm The destination buffer.
// @parm The source string.
// @parm The size of the destination buffer.
//
// @rdesc The destination buffer <p lpszDest>.
//
// @comm This function is in the ITCLIB namespace.
LPCTSTR ITCLIB::CopyString(LPTSTR lpszDest, LPCTSTR lpszSrc, size_t nDestLen)
{
	lpszDest[nDestLen-1] = '\0'; // strncpy doesn't guarentee this
	return strncpy(lpszDest, lpszSrc, nDestLen-1);
}

// @func This function is called when a pointer to a <c CStringArray> object
// is destroyed by one of the MFC template classes. This function deletes
// the pointer.
//
// @parm A pointer to the <c CStringArray>* to delete.
// @parm The number of elements to be destroyed.
void AFXAPI DestructElements(CStringArray** pElements, int nCount)
{
	ASSERT(nCount == 0 ||
		AfxIsValidAddress(pElements, nCount * sizeof(CStringArray*)));

	// The default DestructElements just calls the destructor.
	// We want our list to free the memory too.
	for (; nCount--; pElements++)
		delete *pElements;
}

// @func This function is the same as the Win32 version of <f GetEnvironmentVariable>
// it takes a <c CString> parameter as the buffer value.
//
// @parm Pointer to a null-terminated string that specifies the environment variable.
// @parm The <c CString> to receive the value of the specified environment variable.
//
// @rdesc If the function succeeds, the return value is the number of characters
// stored into the buffer <p strBuffer>, not including the terminating null
// character. 
// 
// If the specified environment variable name was not found in the environment
// block for the current process, the return value is zero. 
//
// @comm This function is in the ITCLIB namespace.
DWORD ITCLIB::GetEnvironmentVariable(LPCTSTR lpName, CString& strBuffer)
{
	DWORD dwSize = ::GetEnvironmentVariable(lpName, NULL, 0);

	if (dwSize)
	{
		LPSTR lpszBuffer = strBuffer.GetBufferSetLength(dwSize);
		dwSize = ::GetEnvironmentVariable(lpName, lpszBuffer, dwSize);
		strBuffer.ReleaseBuffer();
	}

	return dwSize;
}

// @mfunc Determines if the specified file exists.
//
// @parm The complete path to a file.
//
// @rdesc Nonzero if the file exists; otherwise 0.
BOOL ITCLIB::FileExists(CString strFilePath)
{
	CFileStatus fs;
	return CFile::GetStatus(strFilePath, fs);
}

// @mfunc Determines if the specified folder exists.
//
// @parm The complete path of a folder.
//
// @rdesc Nonzero if the file exists; otherwise 0.
BOOL ITCLIB::FolderExists(CString strFolder)
{
	CFileStatus fs;
	return CFile::GetStatus(strFolder, fs);
}

//////////////////////////////////////////////////////////////////////////////
// Inline function declarations expanded out-of-line

#ifndef _ITC_ENABLE_INLINES

static char _szInl[] = "ITAPI.inl";
#undef THIS_FILE
#define THIS_FILE _szInl
#define _ITC_INLINE
#include "ITAPI.inl"

#endif // _ITC_ENABLE_INLINES

//////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -