Example #1
0
//Does this text contain Non-ASCII characters?
bool AbstractEncoder::isUnicode(const QString &original)
{
	if(!original.isEmpty())
	{
		QString asLatin1 = QString::fromLatin1(original.toLatin1().constData());
		return (wcscmp(MUTILS_WCHR(original), MUTILS_WCHR(asLatin1)) != 0);
	}
	return false;
}
Example #2
0
static __forceinline void doLockFile(HANDLE &fileHandle, const QString &filePath, QFile *const outFile)
{
	bool success = false;
	fileHandle = INVALID_HANDLE_VALUE;

	//Try to open the file!
	for(int i = 0; i < 64; i++)
	{
		const HANDLE hTemp = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
		if(VALID_HANDLE(hTemp))
		{
			PROTECT_HANDLE(fileHandle = hTemp, true);
			break; /*file opened successfully*/
		}
		if(i == 0)
		{
			qWarning("Failed to open file on first attemp, retrying...");
		}
		Sleep(1);
	}
	
	//Now try to actually lock the file!
	if (VALID_HANDLE(fileHandle))
	{
		for (int i = 0; i < 64; i++)
		{
			LARGE_INTEGER fileSize;
			if (GetFileSizeEx(fileHandle, &fileSize))
			{
				OVERLAPPED overlapped = { 0U, 0U, 0U, 0U, 0U };
				if (LockFileEx(fileHandle, LOCKFILE_FAIL_IMMEDIATELY, 0, fileSize.LowPart, fileSize.HighPart, &overlapped))
				{
					success = true;
					break; /*file locked successfully*/
				}
				Sleep(1);
			}
			if (i == 0)
			{
				qWarning("Failed to lock file on first attemp, retrying...");
			}
		}
	}

	//Locked successfully?
	if(!success)
	{
		CLOSE_HANDLE(fileHandle);
		if(outFile)
		{
			QFile::remove(QFileInfo(*outFile).canonicalFilePath());
		}
		MUTILS_THROW_FMT("File '%s' could not be locked!", MUTILS_UTF8(QFileInfo(filePath).fileName()));
	}
}