namespace UploadUtils
{
bool IsAlreadyRunning();
char* ConvertLPSTRToLPSTR(LPCTSTR lpwszStrIn);
void CreateDir(const char *dir);
void GetCurrentDir(string path, vector<string>& dirs);
bool JudgeDirIsEmpty(string path);
bool IsFileExist(LPCTSTR filePath);
bool IsDir(LPCTSTR filePath);
void GetFiles(LPCTSTR path, std::vector<fstring>& files);
int DeleteDir(LPCTSTR pSrc, bool bIncludeDir = true);
bool GetProjFolder(const char* pProjCode, fstring& strProjFolder);
}
namespace UploadUtils
{
bool IsAlreadyRunning()
{
bool bRet = false;
HANDLE hMutex = CreateMutex(NULL, TRUE, _T("UploadFileMutex"));
if ( (hMutex != NULL) && (GetLastError() == ERROR_ALREADY_EXISTS) )
{
bRet = true;
}
if (hMutex)
{
::ReleaseMutex(hMutex);
}
return bRet;
}
char* ConvertLPSTRToLPSTR(LPCTSTR lpszStrIn)
{
LPSTR pszOut = NULL;
if (lpszStrIn != NULL)
{
int nInputStrLen = wcslen(lpszStrIn);
int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
pszOut = new char[nOutputStrLen];
if (pszOut)
{
memset(pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
}
}
return pszOut;
}
void CreateDir(const char *dir)
{
int m = 0, n = 0;
string str1, str2;
str1 = dir;
if (str1.size() <= 3)
return;
str2 = str1.substr( 0, 2 );
str1 = str1.substr( 3, str1.size() );
while( m >= 0 )
{
m = str1.find('\\');
str2 += '\\' + str1.substr( 0, m );
n = _access( str2.c_str(), 0 );
if( n == -1 )
{
_mkdir(str2.c_str());
}
str1 = str1.substr( m+1, str1.size() );
}
}
void GetCurrentDir(string path, vector<string>& dirs)
{
long hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do {
if ((fileinfo.attrib & _A_SUBDIR) && strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
dirs.push_back(fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
bool JudgeDirIsEmpty(string path)
{
long hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
return false;
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return true;
}
bool IsFileExist(LPCTSTR filePath)
{
DWORD ftyp = GetFileAttributes(filePath);
if (ftyp == INVALID_FILE_ATTRIBUTES)
{
return false;
}
return true;
}
bool IsDir(LPCTSTR filePath)
{
DWORD ftyp = GetFileAttributes(filePath);
if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
{
return true;
}
return false;
}
void GetFiles(LPCTSTR path, std::vector<fstring>& files)
{
HANDLE hFile = 0;
WIN32_FIND_DATA fileInfo;
memset(&fileInfo, 0, sizeof(LPWIN32_FIND_DATA));
fstring wsTemp(path);
if (_T("\\") != wsTemp.substr(wsTemp.length() - 2))
{
wsTemp.append(_T("\\"));
}
wsTemp.append(_T("*"));
hFile = FindFirstFile(wsTemp.c_str(), &fileInfo);
if (INVALID_HANDLE_VALUE == hFile)
{
return;
}
do
{
if ('.' == fileInfo.cFileName[0])
{
continue;
}
files.push_back(fileInfo.cFileName);
} while (FindNextFile(hFile, &fileInfo));
FindClose(hFile);
}
int DeleteDir(LPCTSTR pSrc, bool bIncludeDir )
{
if (NULL == pSrc)
{
return -1;
}
if (!IsFileExist(pSrc))
{
return -1;
}
if (!IsDir(pSrc))
{
DeleteFile(pSrc);
}
std::vector<fstring> files;
GetFiles(pSrc, files);
for (std::vector<fstring>::iterator i = files.begin(); i != files.end(); ++i)
{
fstring sTemp(pSrc);
sTemp.append(_T("\\")).append(*i);
LPCTSTR srcName = sTemp.c_str();
if (IsDir(srcName))
{
DeleteDir(srcName);
}
else
{
DeleteFile(srcName);
}
}
if (bIncludeDir)
{
RemoveDirectory(pSrc);
}
return 1;
}
bool GetProjFolder(const char* pProjCode, fstring& strProjFolder)
{
if (pProjCode == NULL)
return false;
WCHAR wszFolder[256];
memset(wszFolder, 0x00, sizeof(wszFolder));
MultiByteToWideChar(CP_ACP, 0, pProjCode, strlen(pProjCode) + 1, wszFolder, sizeof(wszFolder) / sizeof(wszFolder[0]));
strProjFolder = CHCDataSources::GetInstance()->GetDepotPath(_T("ecp"));
fstring strPrefix = _T("\\\\?\\");
int nPos = strProjFolder.find(strPrefix);
if (nPos != string::npos)
{
strProjFolder.erase(nPos, strPrefix.length());
}
strProjFolder += wszFolder;
if (!IsDir(strProjFolder.c_str()))
return false;
return true;
}
}