Beispiel #1
0
void SplitFilePathName(const CString& FilePathName, CString& FilePath, CString& FileName, CString& FileExt)
{
 CString name_ext;
 int pos = std::max(LastCharPos(FilePathName,'/'),LastCharPos(FilePathName,'\\'));
 if (pos >= 1)
 {
  FilePath = LeftStr(FilePathName,pos-1);
  name_ext = RightStr(FilePathName,pos+1);
 }
 else
 {
  FilePath.Clear();
  name_ext = FilePathName;
 }
 pos = LastCharPos(name_ext,'.');
 if (pos >= 0)
 {
  if (pos >= 1)
  {
   FileName = LeftStr(name_ext,pos-1);
  }
  else
  {
   FileName.Clear();
  }
  FileExt = RightStr(name_ext,pos+1);
 }
 else
 {
  FileName = name_ext;
  FileExt.Clear();
 }
}
Beispiel #2
0
bool CFile::GetHostIpList(CString &oHostIpList)
{
	uint32 i;
	struct hostent *hptr;
	char cHostName[256];
	if (gethostname(cHostName, sizeof(cHostName)))
	{
		FocpLog(FOCP_LOG_ERROR, ("gethostname failure"));
		return false;
	}
	oHostIpList.Clear();
	SystemLock();
	hptr = gethostbyname(cHostName);
	if (NULL == hptr)
	{
		SystemUnLock();
		FocpLog(FOCP_LOG_ERROR, ("gethostbyname failure"));
		return false;
	}
	CStringFormatter oFormatter(&oHostIpList);
	for(i=0; hptr->h_addr_list[i]; i++)
	{
		uint8* pIp = (uint8*)(hptr->h_addr_list[i]);
		oFormatter.Print(";%u8.%u8.%u8.%u8", pIp[0], pIp[1], pIp[2], pIp[3]);
	}
	SystemUnLock();
	oHostIpList.Append(";");
	return true;
}
Beispiel #3
0
bool FileSystem::AllocateFile(const char* filename, int64 size, bool sparse, CString& errmsg)
{
	errmsg.Clear();
	bool ok = false;
#ifdef WIN32
	HANDLE hFile = CreateFileW(UtfPathToWidePath(filename), GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, 0, nullptr);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		errmsg = GetLastErrorMessage();
		return false;
	}

	if (sparse)
	{
		// try to create sparse file (supported only on NTFS partitions); it may fail but that's OK.
		DWORD dwBytesReturned;
		DeviceIoControl(hFile, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, &dwBytesReturned, nullptr);
	}

	LARGE_INTEGER size64;
	size64.QuadPart = size;
	SetFilePointerEx(hFile, size64, nullptr, FILE_END);
	SetEndOfFile(hFile);
	CloseHandle(hFile);
	ok = true;
#else
	// create file
	FILE* file = fopen(filename, FOPEN_AB);
	if (!file)
	{
		errmsg = GetLastErrorMessage();
		return false;
	}
	fclose(file);

	// there are no reliable function to expand file on POSIX, so we must try different approaches,
	// starting with the fastest one and hoping it will work
	// 1) set file size using function "truncate" (this is fast, if it works)
	truncate(filename, size);
	// check if it worked
	ok = FileSize(filename) == size;
	if (!ok)
	{
		// 2) truncate did not work, expanding the file by writing to it (that's slow)
		truncate(filename, 0);
		file = fopen(filename, FOPEN_AB);
		if (!file)
		{
			errmsg = GetLastErrorMessage();
			return false;
		}
		char c = '0';
		fwrite(&c, 1, size, file);
		fclose(file);
		ok = FileSize(filename) == size;
	}
#endif
	return ok;
}
Beispiel #4
0
int32 CFile::ReadFrom(void* pBuf, int32 nBufLen, CString &oFileName, uint32 nTimeOut)
{
	CFileName oFileName2;
	int32 nRet = ReadFrom(pBuf, nBufLen, oFileName2, nTimeOut);
	if(nRet)
		return nRet;
	oFileName.Clear();
	CStringFormatter oFormatter(&oFileName);
	oFormatter.Print("%s://", oFileName2.oProtocol.GetStr());
	oFormatter.Print("%s", oFileName2.oConnectName.GetStr());
	if(!oFileName2.oBindName.Empty())
		oFormatter.Print("|%s", oFileName2.oBindName.GetStr());
	return nRet;
}
Beispiel #5
0
bool FileSystem::DeleteDirectoryWithContent(const char* dirFilename, CString& errmsg)
{
	errmsg.Clear();

	bool del = false;
	bool ok = true;

	{
		DirBrowser dir(dirFilename);
		while (const char* filename = dir.Next())
		{
			BString<1024> fullFilename("%s%c%s", dirFilename, PATH_SEPARATOR, filename);

			if (FileSystem::DirectoryExists(fullFilename))
			{
				del = DeleteDirectoryWithContent(fullFilename, errmsg);
			}
			else
			{
				del = DeleteFile(fullFilename);
			}
			ok &= del;
			if (!del && errmsg.Empty())
			{
				errmsg.Format("could not delete %s: %s", *fullFilename, *GetLastErrorMessage());
			}
		}
	} // make sure "DirBrowser dir" is destroyed (and has closed its handle) before we trying to delete the directory

	del = RemoveDirectory(dirFilename);
	ok &= del;
	if (!del && errmsg.Empty())
	{
		errmsg = GetLastErrorMessage();
	}
	return ok;
}
Beispiel #6
0
int main(int argc, char* argv[])
{
	CString TestString;
	
//ToDel 4 CJ:
/*	TestString = "Mama";
	cout << TestString.getArray() << endl;
	cout << TestString[0] << endl;
	TestString[0] = 'D';
*/
//	assert(strcmp(TestString.getArray(), "") == 0);
//	TestString.test();
///////////////////////////////////////////////////////////

	assert(strcmp(TestString, "") == 0);
	assert(TestString.GetLength()==0);
	assert(TestString.IsEmpthy());
	
	TestString = "Mama";

	cout << "Ich bin der \"TestString\" : " << TestString << endl;
	assert(TestString.GetLength() == 4);
	assert(TestString.IsEmpthy() == false);
	assert(TestString[0] == 'M');
	assert(TestString[1] == 'a');
	assert(TestString[2] == 'm');
	assert(TestString[3] == 'a');	
	assert(strcmp(TestString, "Mama") == 0);

	TestString[0] = 'B';
	TestString[1] = 'i';
	TestString[2] = 'e';
	TestString[3] = 'r';
	assert(strcmp(TestString, "Bier") == 0);
	CString TestString2 = "Bier";
	cout << "Ich bin der \"TestString2\" : " << TestString2 << endl;
	assert(strcmp(TestString,TestString2) == 0 && TestString == TestString2);
	assert(strcmp(TestString,TestString2) == 0 && TestString <= TestString2);
	assert(strcmp(TestString,TestString2) == 0 && TestString >= TestString2);
	assert((TestString != TestString2) == false);
	CString TestString3 = TestString2;
	cout << "Ich bin der \"TestString3\" : " << TestString3 << endl;
	TestString2 = "Bier2";
	assert(strcmp(TestString3, "Bier") == 0);
	assert(TestString3 == CString("Bier"));
	assert(strcmp(TestString, TestString2) == -1 && TestString < TestString2);
	assert(strcmp(TestString2, TestString) == 1 && TestString2 > TestString);
	assert(strcmp(TestString, TestString2) == -1 && TestString <= TestString2);
	assert(strcmp(TestString2, TestString) == 1 && TestString2 >= TestString);
	assert(TestString != TestString2);
	
	assert(strcmp(TestString+TestString, "BierBier") == 0);
	assert(strcmp(TestString+=TestString, "BierBier") == 0);
	assert(strcmp(TestString3+"Bier", "BierBier") == 0);
	assert(strcmp(TestString3+="Bier", "BierBier") == 0);

	TestString2 = "HalloTestIchSucheBiBiBier";
	assert(TestString2.Find("Bier") == 21);
	assert(TestString2.Find("BierBier") == -1);
	assert(TestString2.Find("Mama") == -1);
	assert(TestString2.Find("") == -1);
	
	assert(TestString2.Contains("BiBier"));
	assert(TestString2.Contains("BierBier") == false);
	assert(TestString2.Contains("Mama") == false);
		
	TestString2.Reverse();
	cout << "Ich bin der \"TestString2\" : " << TestString2 << endl;
	assert(strcmp(TestString2, "reiBiBiBehcuShcItseTollaH") == 0);
	
	assert(TestString.IsEmpthy() == false);
	TestString.Clear();
	assert(TestString.IsEmpthy());

	system("pause");

	return 0;
}
Beispiel #7
0
void CCore::OnDeviceRender(IDirect3DDevice9 * pDevice)
{
	// Has the device been lost?
	if(g_bDeviceLost || !m_pGraphics)
		return;

	// Render our chat instance
	if (m_pChat)
		m_pChat->Render();
	
	// Render our gui instance
	if (m_pGUI)
		m_pGUI->Render();

#ifdef _DEBUG
	char szNetworkStats[10000];
	memset(szNetworkStats, 0, sizeof(szNetworkStats));
	RakNet::StatisticsToString(m_pNetworkManager->GetRakPeer()->GetStatistics(RakNet::UNASSIGNED_SYSTEM_ADDRESS), szNetworkStats, 2);
	m_pGraphics->DrawText(26.0f, 500.0f, D3DCOLOR_ARGB(255, 255, 255, 255), 1.0f, DT_NOCLIP, true, szNetworkStats);
#endif

	// Print our IVNetwork "Identifier" in the left upper corner
	unsigned short usPing = m_pNetworkManager != NULL ? (m_pNetworkManager->IsConnected() ? (g_pCore->GetGame()->GetLocalPlayer() ? g_pCore->GetGame()->GetLocalPlayer()->GetPing() : -1) : -1) : -1;

	CString strConnection;
	int iConnectTime = GetGameLoadInitializeTime() != 0 ? (int)((timeGetTime() - GetGameLoadInitializeTime()) / 1000) : 0;

	CString strSeconds;
	if (iConnectTime > 0) {
		strSeconds.AppendF(" | Running since ");

		int iSeconds = iConnectTime % 60;
		int iMinutes = (iConnectTime / 60) % 60;
		int iHours = (iConnectTime / 60 / 60) % 24;
		int iDays = (iConnectTime / 60 / 60 / 24);

		if(iDays > 0)
		{
			if(iDays > 9)
				strSeconds.AppendF("%d Days, ", iDays);
			else
				strSeconds.AppendF("%d Day%s, ", iDays, iDays > 1 ? "s" : "");
		}
		if(iHours > 0)
		{
			if(iHours > 9)
				strSeconds.AppendF("%d Hours, ",iHours);
			else
				strSeconds.AppendF("0%d Hour%s, ",iHours, iHours > 1 ? "s" : "");
		}
		if(iMinutes > 0)
		{
			if(iMinutes > 9)
				strSeconds.AppendF("%d Minutes, ",iMinutes);
			else
				strSeconds.AppendF("0%d Minute%s, ",iMinutes, iMinutes > 1 ? "s" : "");
		}

		if (iSeconds > 9)
			strSeconds.AppendF("%d Seconds", iSeconds);
		else
			strSeconds.AppendF("0%d Second%s", iSeconds, iSeconds > 1 ? "s" : "");

	}

#ifdef _DEBUG
	if(GetGame()->GetLocalPlayer())
		CDevelopment::DrawPedTasks(GetGame()->GetLocalPlayer()->GetPlayerPed());
#endif

	// Simulate temporary loading symbol
	m_byteLoadingStyle++;
	CString strLoadingInformation;
	if(m_byteLoadingStyle >= 0 && m_byteLoadingStyle < 10)
		strLoadingInformation = CString(MOD_NAME " " VERSION_IDENTIFIER " - Loading.. Hold on .").Get();
	else if(m_byteLoadingStyle >= 10 && m_byteLoadingStyle < 20)
		strLoadingInformation = CString(MOD_NAME " " VERSION_IDENTIFIER " - Loading.. Hold on ..").Get();
	else if(m_byteLoadingStyle >= 20 && m_byteLoadingStyle < 30)
		strLoadingInformation = CString(MOD_NAME " " VERSION_IDENTIFIER " - Loading.. Hold on ...").Get();
	else if(m_byteLoadingStyle >= 30 && m_byteLoadingStyle < 40)
		strLoadingInformation = CString(MOD_NAME " " VERSION_IDENTIFIER " - Loading.. Hold on ....").Get();
	else if (m_byteLoadingStyle >= 40 && m_byteLoadingStyle < 50) {
		strLoadingInformation = CString(MOD_NAME " " VERSION_IDENTIFIER " - Loading.. Hold on .....").Get();
		if (m_byteLoadingStyle == 49)
			m_byteLoadingStyle = 0;
	}

	CString strInformation = usPing == 0xFFFF ? CString("%s%s", MOD_NAME " " VERSION_IDENTIFIER, strSeconds.Get()) : CString("%s%s | Ping %hu", MOD_NAME " " VERSION_IDENTIFIER, strSeconds.Get(), usPing);
	
	if(!g_pCore->GetGame()->GetLocalPlayer())
		m_pGraphics->DrawText(60.0f, 5.0f, D3DCOLOR_ARGB(255, 0, 195, 255), 1.0f, DT_NOCLIP, true, strLoadingInformation.Get());
	else
		m_pGraphics->DrawText(60.0f, 5.0f, D3DCOLOR_ARGB(255, 0, 195, 255), 1.0f, DT_NOCLIP, true, strInformation.Get());
	
	strSeconds.Clear();
	strLoadingInformation.Clear();
	strInformation.Clear();

	// Before rendering FPS-Counter instance, update FPS display
	m_pGraphics->DrawText(5.0f, 5.0f, D3DCOLOR_ARGB((unsigned char)255, 255, 255, 255), 1.0f, DT_NOCLIP, true, CString("FPS: %d", m_pFPSCounter->GetFPS()).Get());

	// Check if our snap shot write failed
	if(CSnapShot::IsDone()) {
		if(CSnapShot::HasSucceeded())
			m_pChat->Print(CString("Screen shot written (%s).", CSnapShot::GetWriteName().Get()));
		else
			m_pChat->Print(CString("Screen shot write failed (%s).", CSnapShot::GetError().Get()));

		CSnapShot::Reset();
	}

	// Render our Name Tags
	if (m_pTags && !g_pCore->GetMainMenu()->IsMainMenuVisible() && !CIVScript::IsScreenFadedOut())
		m_pTags->Draw();
}
Beispiel #8
0
bool FileSystem::ForceDirectories(const char* path, CString& errmsg)
{
	errmsg.Clear();
	BString<1024> normPath = path;
	NormalizePathSeparators(normPath);
	int len = strlen(normPath);
	if (len > 3 && normPath[len - 1] == PATH_SEPARATOR)
	{
		normPath[len - 1] = '\0';
	}

	if (DirectoryExists(normPath))
	{
		return true;
	}
		
	if (FileExists(normPath))
	{
		errmsg.Format("path %s is not a directory", *normPath);
		return false;
	}

	if (strlen(normPath) > 2)
	{
		BString<1024> parentPath = *normPath;
		char* p = (char*)strrchr(parentPath, PATH_SEPARATOR);
		if (p)
		{
			if (p - parentPath == 2 && parentPath[1] == ':' && strlen(parentPath) > 2)
			{
				parentPath[3] = '\0';
			}
			else
			{
				*p = '\0';
			}
			if (strlen(parentPath) != strlen(path) && !ForceDirectories(parentPath, errmsg))
			{
				return false;
			}
		}

		if (_wmkdir(UtfPathToWidePath(normPath)) != 0 && errno != EEXIST)
		{
			errmsg.Format("could not create directory %s: %s", *normPath, *GetLastErrorMessage());
			return false;
		}

		if (DirectoryExists(normPath))
		{
			return true;
		}

		if (FileExists(normPath))
		{
			errmsg.Format("path %s is not a directory", *normPath);
			return false;
		}
	}

	return false;
}
Beispiel #9
0
bool FileSystem::ForceDirectories(const char* path, CString& errmsg)
{
	errmsg.Clear();
	BString<1024> normPath = path;
	NormalizePathSeparators(normPath);
	int len = strlen(normPath);
	if ((len > 0) && normPath[len - 1] == PATH_SEPARATOR)
	{
		normPath[len - 1] = '\0';
	}

	struct stat buffer;
	bool ok = !stat(normPath, &buffer);
	if (!ok && errno != ENOENT)
	{
		errmsg.Format("could not read information for directory %s: errno %i, %s",
			*normPath, errno, *GetLastErrorMessage());
		return false;
	}

	if (ok && !S_ISDIR(buffer.st_mode))
	{
		errmsg.Format("path %s is not a directory", *normPath);
		return false;
	}

	if (!ok)
	{
		BString<1024> parentPath = *normPath;
		char* p = (char*)strrchr(parentPath, PATH_SEPARATOR);
		if (p)
		{
			*p = '\0';
			if (strlen(parentPath) != strlen(path) && !ForceDirectories(parentPath, errmsg))
			{
				return false;
			}
		}

		if (mkdir(normPath, S_DIRMODE) != 0 && errno != EEXIST)
		{
			errmsg.Format("could not create directory %s: %s", *normPath, *GetLastErrorMessage());
			return false;
		}

		if (stat(normPath, &buffer) != 0)
		{
			errmsg.Format("could not read information for directory %s: %s",
				*normPath, *GetLastErrorMessage());
			return false;
		}

		if (!S_ISDIR(buffer.st_mode))
		{
			errmsg.Format("path %s is not a directory", *normPath);
			return false;
		}
	}

	return true;
}