// private function
void CabLib::Compress::RecursiveEnumFiles(ArrayList* i_FileList, String* s_Path, String* s_Filter, bool b_Subfolders)
{
	int Len = s_Path->Length;
	if (!s_Path || Len == 0)
		return;

	// Path must terminate with "\"
	s_Path = TerminatePath(s_Path);

	String* s_Files[] = Directory::GetFiles(s_Path, s_Filter->Trim());
	for (int i=0; i<s_Files->Length; i++)
	{
		i_FileList->Add(s_Files->Item[i]);
	}

	if (b_Subfolders)
	{
		String* s_Dirs[] = Directory::GetDirectories(s_Path);
		for (int i=0; i<s_Dirs->Length; i++)
		{
			String *s_Dir = static_cast<String*>(s_Dirs->Item[i]);
			RecursiveEnumFiles(i_FileList, s_Dir, s_Filter, true);
		}
	}
}
Example #2
0
/*===========================================================================
 *
 * Function - bool MakePathEx (pPath);
 *
 * Creates multiple levels of paths. Returns false on any error.
 *
 *=========================================================================*/
bool MakePathEx (const SSCHAR* pPath) {
  SSCHAR  TempPath[_MAX_PATH+1];
  SSCHAR  TempDrive[8] = "c:\\";
  SSCHAR  InitialPath[_MAX_PATH+1];
  SSCHAR* pParse;
  int     iResult;

	/* Ignore invalid input */
  if (pPath == NULL) return (false);
  strnncpy(TempPath, pPath, _MAX_PATH);
  
	/* Save the initial directory */
  _getcwd(InitialPath, _MAX_PATH - 2);
  TerminatePath(InitialPath);
  pParse = strtok(TempPath, "\\");

  while (pParse != NULL && *pParse != NULL_CHAR) {

    		/* Does the given directory/drive exist? */
    if (pParse[1] == ':') {
      TempDrive[0] = pParse[0];
      iResult = _chdir(TempDrive);
    }
    else {
      iResult = _chdir(pParse);
    }

		/* Attempt to make the given directory */
    if (iResult != 0) {
      iResult = _mkdir(pParse);
      
      if (iResult < 0) {
        AddSrGeneralError("Failed to create the directory '%s'!", pParse);
	return (false);
      }

      iResult = _chdir(pParse);      
      if (iResult != 0) return (false);
    }

    pParse = strtok(NULL, "\\");
  }

	/* Restore the initial path and return success */
  _chdir(InitialPath);
  return (true);
}
bool CEsmOptions::ReadFromRegistry (void) {
  CWinApp* pApp = AfxGetApp();
  CString  Buffer;
  bool	   Result;

	/* General options */
  Buffer = pApp->GetProfileString(ESMSCR_REGSEC_GENERAL, ESMSCR_REGENTRY_AUTHORNAME, NULL);
  SetAuthorName(Buffer);

  Buffer = pApp->GetProfileString(ESMSCR_REGSEC_GENERAL, ESMSCR_REGENTRY_DATAPATH, NULL);
  SetDataPath(Buffer);
  TerminatePath(m_DataPath);

  if (Buffer.IsEmpty()) {
    FindMWRegistryPath();
  }
  else {
    SetMWDataPath(Buffer);
  }

  m_BackupSaves    = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_BACKUPSAVES,    m_BackupSaves) != 0);
  m_AllowExtFuncs  = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_ALLOWEXTFUNCS,  m_AllowExtFuncs) != 0);
  m_StrictIDs      = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_STRICTIDS,      m_StrictIDs) != 0);
  m_AllowBloodmoon = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_ALLOWBLOODMOON, m_AllowBloodmoon) != 0);
  m_AllowTribunal  = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_ALLOWTRIBUNAL,  m_AllowTribunal) != 0);

	/* Script options */
  m_ScriptWarnLevel    = pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_WARNLEVEL,     m_ScriptWarnLevel);
  m_NoScriptFormat     = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_NOSCRFORMAT,  m_NoScriptFormat) != 0);
  m_UseExtraFile       = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_USEEXTRAFILE, m_UseExtraFile) != 0);
  m_NoScriptPrompt     = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_NOSCRPROMPT,  m_NoScriptPrompt) != 0);
  m_InitialIndentLevel = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_INITIALINDENTLEVEL,  m_InitialIndentLevel) != 0);
  m_IndentCommentsMore = (pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_INDENTCOMMENTSMORE,  m_IndentCommentsMore) != 0);
  m_ScriptFormatType   = pApp->GetProfileInt(ESMSCR_REGSEC_SCRIPT,  ESMSCR_REGENTRY_SCRFORMAT,    m_ScriptFormatType);

  Buffer = pApp->GetProfileString(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_EXTRAFILE, m_ExtraFile);
  SetExtraFile(Buffer);

  Buffer = pApp->GetProfileString(ESMSCR_REGSEC_SCRIPT, ESMSCR_REGENTRY_SCRIPTINDENTSTRING, m_ScriptIndentString);
  SetScriptIndentString(Buffer);

	/* Script user format */
  Result = m_UserScriptOptions.ReadFromRegistry();
  return (Result);
 }
Example #4
0
bool FileMisc::CopyFolder(const TCHAR* szSrcFolder, const TCHAR* szDestFolder, BOOL bIncludeSubFolders,
	const TCHAR* szFileMask, HANDLE hTerminate, BOOL bProcessMsgLoop)
{
	if (!CreateFolder(szDestFolder))
	{
		return false;
	}

	if (!FolderExists(szSrcFolder))
	{
		return false;
	}

	// if a file mask has been specified with subfolders we need to do 2 passes on each folder,
	// one for the files and one for the sub folders
	int nPasses = (bIncludeSubFolders && (szFileMask && lstrlen(szFileMask))) ? 2 : 1;

	bool bResult = true;
	bool bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0);

	for (int nPass = 0; !bStopped && nPass < nPasses; nPass++)
	{
		CString sSearchSpec(szSrcFolder), sMask(szFileMask);

		if (sMask.IsEmpty() || nPass == 1) // (nPass == 1) == 2nd pass (for folders)
		{
			sMask = "*.*";
		}

		TerminatePath(sSearchSpec);
		sSearchSpec += sMask;

		WIN32_FIND_DATA finfo;
		HANDLE hSearch = NULL;

		if ((hSearch = FindFirstFile(sSearchSpec, &finfo)) != INVALID_HANDLE_VALUE)
		{
			do
			{
				if (bProcessMsgLoop)
				{
					Misc::ProcessMsgLoop();
				}

				if (finfo.cFileName[0] != '.')
				{
					CString sSource(szSrcFolder);
					sSource += "\\";
					sSource += finfo.cFileName;

					CString sDest(szDestFolder);
					sDest += "\\";
					sDest += finfo.cFileName;

					if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
					{
						if ((nPass == 1 || nPasses == 1) && bIncludeSubFolders)
						{
							bResult = CopyFolder(sSource, sDest, hTerminate);
						}
					}
					else if (nPass == 0) // files
					{
						bResult = (TRUE == CopyFile(sSource, sDest, FALSE));
					}
				}

				bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0);
			}
			while (!bStopped && bResult && FindNextFile(hSearch, &finfo));

			FindClose(hSearch);
		}
	}

	return (!bStopped && bResult);
}
Example #5
0
double FileMisc::GetFolderSize(const TCHAR* szFolder, BOOL bIncludeSubFolders, const TCHAR* szFileMask,
	HANDLE hTerminate, BOOL bProcessMsgLoop)
{
	// if the dir does not exists just return
	if (!FolderExists(szFolder))
	{
		return 0;
	}

	double dSize = 0;

	WIN32_FIND_DATA finfo;
	CString sSearchSpec(szFolder), sFileMask(szFileMask);

	if (sFileMask.IsEmpty())
	{
		sFileMask = "*.*";
	}

	TerminatePath(sSearchSpec);
	sSearchSpec += sFileMask;

	BOOL bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0);
	HANDLE h = NULL;

	if (!bStopped && (h = FindFirstFile(sSearchSpec, &finfo)) != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (bProcessMsgLoop)
			{
				Misc::ProcessMsgLoop();
			}

			if (finfo.cFileName[0] != '.')
			{
				if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					if (bIncludeSubFolders)
					{
						CString sSubFolder(szFolder);
						sSubFolder += "\\";
						sSubFolder += finfo.cFileName;

						dSize += GetFolderSize(sSubFolder, TRUE, sFileMask, hTerminate, bProcessMsgLoop);
					}
				}
				else
				{
					dSize += (finfo.nFileSizeHigh * ((double)MAXDWORD + 1)) + finfo.nFileSizeLow;
				}
			}

			bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0);
		}
		while (!bStopped && FindNextFile(h, &finfo));

		FindClose(h);
	}

	return bStopped ? -1 : dSize;
}
Example #6
0
bool FileMisc::DeleteFolderContents(const TCHAR* szFolder, BOOL bIncludeSubFolders, const TCHAR* szFileMask,
	HANDLE hTerminate, BOOL bProcessMsgLoop)
{
	// if the dir does not exists just return
	if (!FolderExists(szFolder))
	{
		return true;
	}

	// if a file mask has been specified with subfolders we need to do 2 passes on each folder,
	// one for the files and one for the sub folders
	int nPasses = (bIncludeSubFolders && (szFileMask && lstrlen(szFileMask))) ? 2 : 1;

	bool bResult = true;
	bool bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0);

	for (int nPass = 0; !bStopped && nPass < nPasses; nPass++)
	{
		CString sSearchSpec(szFolder), sMask(szFileMask);

		if (sMask.IsEmpty() || nPass == 1) // (nPass == 1) == 2nd pass (for folders)
		{
			sMask = "*.*";
		}

		TerminatePath(sSearchSpec);
		sSearchSpec += sMask;

		WIN32_FIND_DATA finfo;
		HANDLE hSearch = NULL;

		if ((hSearch = FindFirstFile(sSearchSpec, &finfo)) != INVALID_HANDLE_VALUE)
		{
			do
			{
				if (bProcessMsgLoop)
				{
					Misc::ProcessMsgLoop();
				}

				if (finfo.cFileName[0] != '.')
				{
					CString sItem(szFolder);
					sItem += "\\";
					sItem += finfo.cFileName;

					if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
					{
						if (bIncludeSubFolders && (nPass == 1 || nPasses == 1))
						{
							if (DeleteFolderContents(sItem, TRUE, szFileMask, hTerminate, bProcessMsgLoop))
							{
								if (!szFileMask || !lstrlen(szFileMask))
								{
									bResult = (RemoveDirectory(sItem) == TRUE);
								}
							}
						}
					}
					else
					{
						bResult = (DeleteFile(sItem) == TRUE);
					}
				}

				bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0);
			}
			while (!bStopped && bResult && FindNextFile(hSearch, &finfo));

			FindClose(hSearch);
		}
	}

	return (!bStopped && bResult);
}
Example #7
0
CString FileMisc::TerminatePath(LPCTSTR szPath)
{
	CString sPath(szPath);
	return TerminatePath(sPath);
}
/*===========================================================================
 *
 * Class CSrResourceHandler Method - bool AddPathContents (pFolder, pRootPath, IncludeRootFiles);
 *
 *=========================================================================*/
bool CSrResourceHandler::AddPathContents (CSrResourceFolder* pFolder, const char* pPath, const bool IncludeRootFiles) 
{
  WIN32_FIND_DATA		FindData;
  WIN32_FIND_DATA*		pNewPath;
  CFindDataArray		FindPaths;
  CSrResourceFolder*	pNewFolder;
  CSrResourceFile*		pFile;
  CSrResourceInstance*	pInstance;
  CSString				FindSpec(pPath);
  HANDLE				hFind;
  BOOL					FindResult;
  dword					Index;
  bool					ReturnResult = true;

  TerminatePath(FindSpec);
  FindSpec += "*.*";

	/* Add all matching files and find all sub-paths */
  hFind = FindFirstFile(FindSpec, &FindData);
  if (hFind == INVALID_HANDLE_VALUE) return (true);

  do {
    if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
		if (strcmp(FindData.cFileName, ".") == 0 || strcmp(FindData.cFileName, "..") == 0)
		{
			FindResult = FindNextFile(hFind, &FindData);
			continue;
		}

      pNewPath = new WIN32_FIND_DATA;
      FindPaths.Add(pNewPath);
      *pNewPath = FindData;
    }
    else if (IncludeRootFiles) {
      pFile = pFolder->GetFileCreate(FindData.cFileName);

		/* TODO: Set filesize/time here from find data */
      if (pFile != NULL) {
        pInstance = pFile->AddInstance(FindData.cFileName);

	if (pInstance != NULL) {
	  pInstance->SetFiletime(FindData.ftLastWriteTime.dwLowDateTime, FindData.ftLastWriteTime.dwHighDateTime);
	  pInstance->SetFilesize(FindData.nFileSizeLow, FindData.nFileSizeHigh);
	}
      }
      else
        ReturnResult = false;
    }

    FindResult = FindNextFile(hFind, &FindData);
  } while (FindResult);

  FindClose(hFind);

	/* Recursively iterate through all found sub-paths */
  for (Index = 0; Index < FindPaths.GetSize(); ++Index) {
    pNewPath = FindPaths.GetAt(Index);
    if (strcmp(pNewPath->cFileName, ".") == 0) continue;
    if (strcmp(pNewPath->cFileName, "..") == 0) continue;

    pNewFolder = pFolder->GetFolderCreate(pNewPath->cFileName);

    if (pNewFolder != NULL) {
      FindSpec  = pPath;
      FindSpec += pNewPath->cFileName;
      FindSpec += "\\";
      ReturnResult &= AddPathContents(pNewFolder, FindSpec, true);
    }
    else {
      ReturnResult = false;
    }
  }

  FindPaths.Destroy();
  return (ReturnResult);
}