campo-sirio/SSAservice/PathName.cpp
guy d40707a134 Versione 12
git-svn-id: svn://10.65.10.50/branches/R_10_00@23098 c028cbd2-c16b-5b4b-a496-9718f37d4682
2015-05-15 09:09:10 +00:00

257 lines
5.8 KiB
C++

#pragma region Includes
#define WIN32_LEAN_AND_MEAN
#define STRICT
#include <windows.h>
#include "PathName.h"
#include <cassert>
#include <stdlib.h>
#pragma endregion
///////////////////////////////////////////////////////////
// utility
///////////////////////////////////////////////////////////
char* wstring2string(const wchar_t* wstr)
{
static char str[1024];
::WideCharToMultiByte(CP_ACP, WC_SEPCHARS, wstr, -1, str, sizeof(str), NULL, NULL);
return str;
}
wchar_t* string2wstring(const char* str)
{
static wchar_t wstr[1024];
::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstr, 1024);
return wstr;
}
///////////////////////////////////////////////////////////
// CPathName
///////////////////////////////////////////////////////////
static gt_string __tmp;
errno_t gt_strcpy(gt_string dst, const char* str)
{
assert(dst);
errno_t e = 0;
if (str && *str)
e = ::strcpy_s(dst, sizeof(gt_string), str);
else
*dst = '\0';
return e;
}
errno_t gt_strcat(gt_string dst, const char* str)
{
assert(dst);
errno_t e = 0;
if (str && *str)
e = ::strcat_s(dst, sizeof(gt_string), str);
return e;
}
errno_t gt_chrcat(gt_string dst, const char chr)
{
char s[2] = { chr, '\0' };
return gt_strcat(dst, s);
}
bool gt_strsame(const char* dst, const char* src)
{
return dst && src ? _stricmp(dst, src) == 0 : false;
}
const char* CPathName::Path() const
{
char drive[_MAX_DRIVE], dir[_MAX_DIR];
_splitpath_s(_name, drive, sizeof(drive), dir, sizeof(dir), NULL, 0, NULL, 0);
gt_strcpy(__tmp, drive);
gt_strcat(__tmp, dir);
return __tmp;
}
const char* CPathName::FullName() const
{
char name[_MAX_FNAME], ext[_MAX_EXT];
_splitpath_s(c_str(), NULL, 0, NULL, 0, name, sizeof(name), ext, sizeof(ext));
gt_strcpy(__tmp, name);
gt_strcat(__tmp, ext);
return __tmp;
}
const char* CPathName::Name() const
{
_splitpath_s(c_str(), NULL, 0, NULL, 0, __tmp, sizeof(__tmp), NULL, 0);
return __tmp;
}
const char* CPathName::Ext() const
{
_splitpath_s(c_str(), NULL, 0, NULL, 0, NULL, 0, __tmp, sizeof(__tmp));
return __tmp;
}
CPathName& CPathName::Set(const char* p)
{
memset(_name, 0, sizeof(_name));
if (p && *p == '"')
{
gt_strcpy(_name, p+1);
_name[strlen(_name)-1] = '\0';
}
else
gt_strcpy(_name, p);
return *this;
}
CPathName& CPathName::Set(const char* p, const char* n, const char* e)
{
memset(_name, 0, sizeof(_name));
_makepath_s(_name, sizeof(_name), NULL, p, n, e);
return *this;
}
CPathName& CPathName::SetPath(const char* p)
{
char name[_MAX_FNAME], ext[_MAX_EXT];
_splitpath_s(c_str(), NULL, NULL, NULL, NULL, name, sizeof(name), ext, sizeof(ext));
_makepath_s(_name, sizeof(_name), NULL, p, name, ext);
return *this;
}
CPathName& CPathName::SetName(const char* n)
{
char drive[_MAX_DRIVE], dir[_MAX_DIR], ext[_MAX_EXT], path[_MAX_PATH];
_splitpath_s(c_str(), drive, sizeof(drive), dir, sizeof(dir), NULL, 0, ext, sizeof(ext));
_makepath_s(path, sizeof(path), drive, dir, n, ext);
return operator=(path);
}
CPathName& CPathName::SetExt(const char* e)
{
char drive[_MAX_DRIVE], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
_splitpath_s(c_str(), drive, sizeof(drive), dir, sizeof(dir), name, sizeof(name), ext, sizeof(ext));
_makepath_s(_name, sizeof(_name), drive, dir, name, e);
return *this;
}
bool CPathName::IsFile() const
{
if (empty())
return false;
DWORD dwAttrib = ::GetFileAttributesA(c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
bool CPathName::IsDirectory() const
{
if (empty())
return false;
DWORD dwAttrib = ::GetFileAttributesA(c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
size_t CPathName::Scan(CPathList* files) const
{
WIN32_FIND_DATAA ffd = { 0 };
PVOID pOldValue = NULL;
::Wow64DisableWow64FsRedirection(&pOldValue);
size_t nCount = 0;
CPathName tmp = c_str();
if (tmp.IsDirectory())
tmp.Set(c_str(), "*", "*");
HANDLE hFind = ::FindFirstFileA(tmp, &ffd);
while (hFind != INVALID_HANDLE_VALUE)
{
if (files)
{
tmp.Set(Path(), ffd.cFileName, NULL);
files->push_back(tmp);
}
nCount++;
if (!::FindNextFileA(hFind, &ffd))
{
::FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
}
::Wow64RevertWow64FsRedirection(pOldValue);
return nCount;
}
bool CPathName::Copy(const CPathName& dst) const
{
return ::CopyFileA(c_str(), dst.c_str(), FALSE) != 0;
}
bool CPathName::Move(const CPathName& dst) const
{
if (dst.IsFile())
dst.Remove();
return ::MoveFileA(c_str(), dst.c_str()) != 0;
}
bool CPathName::Remove() const
{
return ::DeleteFileA(c_str()) != 0;
}
///////////////////////////////////////////////////////////
// CPathList
///////////////////////////////////////////////////////////
size_t CPathList::push_back(const char* p)
{
CPathName& pn = operator[](size_t(-1));
pn = p;
return _items-1;
}
size_t CPathList::push_back(const CPathName& p)
{
CPathName& pn = operator[](size_t(-1));
pn = p;
return _items-1;
}
const CPathName& CPathList::operator[](size_t i) const
{
assert(i < _items);
return _file[i];
}
CPathName& CPathList::operator[](size_t i)
{
if (i >= _items)
{
if (_items+1 >= _max)
{
const size_t s = _max ? _max*2 : 128;
CPathName* n = new CPathName[s];
for (size_t j = 0; j < _items; j++)
n[j].Set(_file[j]);
delete [] _file;
_file = n;
_max = s;
}
i = _items++;
}
return _file[i];
}
CPathList::~CPathList()
{
if (_file)
delete [] _file;
}