|
//------------------------------------------------------------------------------
// $Workfile: FileUtils.cpp $
// $Header: $
//
// Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
// Steve Johnson
//
// $Revision: $
//
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "FileUtils.h"
#include "TextFile.h"
#include "FileSys.h"
#include "DebugUtils.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
namespace SbjCore
{
namespace Utils
{
namespace File
{
CString ChangeExtension(LPCTSTR pFilename, LPCTSTR pExtension)
{
SbjCore::Sys::FileSys theFS;
CString strExtension(theFS.GetExtension(pFilename));
CString strFilename(pFilename);
int nLength = strFilename.GetLength();
int nExtLength = strExtension.GetLength();
return strFilename.Left(nLength - nExtLength) + pExtension;
}
bool IsExtension(LPCTSTR pFilename, LPCTSTR pExtension)
{
SbjCore::Sys::FileSys theFileSystem;
CString strExtension(theFileSystem.GetExtension(pFilename));
if (strExtension.CompareNoCase(pExtension) == 0)
{
return true;
}
return false;
}
void CreateDirectoryIfNone(LPCTSTR pFilename)
{
SbjCore::Sys::FileSys theFS;
CString strPath(theFS.GetPath(pFilename));
if (!strPath.IsEmpty())
{
if (!theFS.DirectoryExists(strPath))
{
theFS.MakePath(strPath);
}
}
}
CString CreateTempFileName(LPCTSTR pPrefix, UINT uUnique)
{
CString sTempFile;
TCHAR TempPath[MAX_PATH];
DWORD dwLength = ::GetTempPath(MAX_PATH, TempPath);
if (dwLength == 0)
{
::_tcscpy(TempPath, _T("."));
}
TCHAR NewFile[MAX_PATH];
if (::GetTempFileName(TempPath, pPrefix, uUnique, NewFile))
{
sTempFile = NewFile;
}
return sTempFile;
}
void RemoveTempFile(LPCTSTR pFilename)
{
SbjCore::Sys::FileSys theFS;
if (theFS.FileExists(pFilename))
{
CFile::Remove(pFilename);
}
}
// handles unicode
int ReplaceText(LPCTSTR lpszPath, LPCTSTR lpszFind, LPCTSTR lpszReplace)
{
lpszPath;
lpszFind;
lpszReplace;
int nRslt = 0;
CTextFileRead fIn(lpszPath);
CTextFileBase::TEXTENCODING textEncoding = fIn.GetEncoding();
CString s;
fIn.Read(s);
fIn.Close();
CFile::Remove(lpszPath);
nRslt = s.Replace(lpszFind, lpszReplace);
CTextFileWrite fOut(lpszPath, textEncoding);
fOut.Write(s);
fOut.Close();
return nRslt;
}
CString GetMyDocumentsPath()
{
CString sPath;
TCHAR szPath[255];
CWnd* pWnd(CWnd::GetDesktopWindow());
if (pWnd != NULL)
{
if (SHGetSpecialFolderPath(pWnd->GetSafeHwnd(), (LPTSTR)&szPath, CSIDL_PERSONAL, TRUE))
{
sPath = szPath;
}
}
return sPath;
}
}
}
}
//*** Modification History ***
// $Log: $
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Real name is Steve Johnson. Programming since 1979. Started on a Heathkit Micro with a DEC LSI-11 and UCSD Pascal. Moved to PCs & DOS as soon as Turbo Pascal became available. Did some Assembly, ISR, TSR etc. All this while working for a Manufacturing Co. for 8 years. Had my own solo Co. doing barcode labeling software for 4 years (terrible business man, all I wanted to do was code). Since then working for various software companies. Moved to Windows around the time of 3.1 with Borland C then C++. Then on to VC++ and MFC, and just about anything I could get my hands on or had to learn for my job, and been at it ever since. Of course recently I've been playing with .NET, ASP, C#, WPF etc.