26 |
//******************************************************************** |
//******************************************************************** |
27 |
// |
// |
28 |
|
|
|
#include "stdafx.h" |
|
|
#include <direct.h> |
|
|
|
|
29 |
#ifdef _DEBUG |
#ifdef _DEBUG |
30 |
#define new DEBUG_NEW |
#define new DEBUG_NEW |
31 |
#undef THIS_FILE |
#undef THIS_FILE |
32 |
static char THIS_FILE[] = __FILE__; |
static char THIS_FILE[] = __FILE__; |
33 |
#endif |
#endif |
34 |
|
|
35 |
|
#include <string> |
36 |
|
#include "Utility.h" |
37 |
|
#include <boost/filesystem/path.hpp> |
38 |
|
#include <boost/filesystem/operations.hpp> |
39 |
|
#include <boost/filesystem/convenience.hpp> |
40 |
|
#include <boost/format.hpp> |
41 |
|
|
42 |
|
namespace bfs = boost::filesystem; |
43 |
|
|
44 |
// Returns the directory location corresponding to some path |
// Returns the directory location corresponding to some path |
45 |
CString GetFileDirFromPath(const TCHAR *tszPath) |
std::string GetFileDirFromPath(const char *tszPath) |
46 |
{ |
{ |
47 |
CString csDir = tszPath; |
bfs::path path = bfs::path(tszPath, bfs::native); |
48 |
int nPos = csDir.ReverseFind(_T('\\')); |
return path.branch_path().native_file_string(); |
|
if (nPos <= 0) return _T(""); |
|
|
csDir = csDir.Left(nPos + 1); |
|
|
return csDir; |
|
49 |
} |
} |
50 |
|
|
51 |
// Returns the filename from the supplied path |
// Returns the filename from the supplied path |
52 |
CString GetFileNameFromPath(const TCHAR *tszPath) |
std::string GetFileNameFromPath(const char *tszPath) |
53 |
{ |
{ |
54 |
|
bfs::path path = bfs::path(tszPath, bfs::native); |
55 |
TCHAR Drive[_MAX_DRIVE]; |
return path.leaf(); |
|
TCHAR Dir[_MAX_DIR]; |
|
|
TCHAR File[_MAX_FNAME]; |
|
|
TCHAR Ext[_MAX_EXT]; |
|
|
|
|
|
_tsplitpath( tszPath, Drive, Dir, File, Ext ); |
|
|
|
|
|
CString csFile = File; |
|
|
csFile += Ext; |
|
|
return csFile; |
|
|
|
|
56 |
} |
} |
57 |
|
|
58 |
// Returns the filetitle from the supplied path |
// Returns the filetitle from the supplied path |
59 |
CString GetFileTitleFromPath(const TCHAR *tszPath) |
std::string GetFileTitleFromPath(const char *tszPath) |
60 |
{ |
{ |
61 |
TCHAR Drive[_MAX_DRIVE]; |
bfs::path path = bfs::path(tszPath, bfs::native); |
62 |
TCHAR Dir[_MAX_DIR]; |
std::string fname = path.leaf(); |
63 |
TCHAR File[_MAX_FNAME]; |
std::string::size_type lastdot = fname.rfind('.'); |
64 |
TCHAR Ext[_MAX_EXT]; |
if (lastdot != std::string::npos) |
65 |
|
{ |
66 |
_tsplitpath( tszPath, Drive, Dir, File, Ext ); |
return fname.substr(0, lastdot); |
67 |
|
} |
68 |
CString csFileTitle = File; |
return fname; |
|
return csFileTitle; |
|
|
|
|
69 |
} |
} |
70 |
|
|
71 |
// Returns the file extension from the supplied path |
// Returns the file extension from the supplied path |
72 |
CString GetFileExtFromPath(const TCHAR *tszPath) |
std::string GetFileExtFromPath(const char *tszPath) |
73 |
{ |
{ |
74 |
TCHAR Drive[_MAX_DRIVE]; |
bfs::path path = bfs::path(tszPath, bfs::native); |
75 |
TCHAR Dir[_MAX_DIR]; |
std::string fname = path.leaf(); |
76 |
TCHAR File[_MAX_FNAME]; |
std::string::size_type lastdot = fname.rfind('.'); |
77 |
TCHAR Ext[_MAX_EXT]; |
if (lastdot != std::string::npos) |
78 |
|
{ |
79 |
_tsplitpath( tszPath, Drive, Dir, File, Ext ); |
return fname.substr(lastdot); |
80 |
|
} |
81 |
CString csExt = Ext; |
return fname; |
|
return csExt; |
|
|
|
|
82 |
} |
} |
83 |
|
|
84 |
CString ExtractFilePath(CString FileName) |
std::string ExtractFilePath(std::string FileName) |
85 |
{ |
{ |
86 |
CString Path; |
bfs::path path = bfs::path(FileName, bfs::native); |
87 |
int i=-1; |
return path.branch_path().native_file_string(); |
|
|
|
|
i = FileName.ReverseFind( '\\' ); |
|
|
|
|
|
if (i==-1) i = FileName.ReverseFind( ':' ); |
|
|
|
|
|
if (i>=0) { |
|
|
Path = FileName.Left(i); |
|
|
if( Path != "\\" ) Path += '\\'; |
|
|
} else |
|
|
Path = FileName; |
|
|
|
|
|
return Path; |
|
88 |
} |
} |
89 |
|
|
90 |
bool IsFileNameValid(LPCTSTR lpFileName) |
bool IsFileNameValid(const char * lpFileName) |
91 |
{ |
{ |
92 |
if(lpFileName==NULL) { |
if (lpFileName == NULL) return false; |
93 |
return false; |
return bfs::native (lpFileName); |
94 |
} |
} |
95 |
|
|
96 |
int nLen = _tcslen(lpFileName); |
bool MakeSureDirectoryPathExists(std::string Dir) |
97 |
if(nLen<=0) { |
{ |
98 |
return false; |
bfs::path pth = bfs::path(Dir, bfs::native).branch_path(); |
99 |
} |
if (bfs::exists(pth)) |
|
|
|
|
//check first char |
|
|
switch(lpFileName[0]) |
|
100 |
{ |
{ |
101 |
case _T('.'): |
if (bfs::is_directory(pth)) |
|
case _T(' '): |
|
|
case _T('\t'): |
|
|
return false; |
|
|
} |
|
|
|
|
|
//check last char |
|
|
switch(lpFileName[nLen-1]) |
|
|
{ |
|
|
case _T('.'): |
|
|
case _T(' '): |
|
|
case _T('\t'): |
|
|
return false; |
|
|
} |
|
|
|
|
|
//check all |
|
|
int i=0; |
|
|
while(lpFileName[i]!=0) |
|
|
{ |
|
|
switch(lpFileName[i]) |
|
102 |
{ |
{ |
103 |
case _T('\\'): |
return true; |
104 |
case _T('/'): |
} |
105 |
case _T(':'): |
else |
106 |
case _T('*'): |
{ |
|
case _T('?'): |
|
|
case _T('\"'): |
|
|
case _T('<'): |
|
|
case _T('>'): |
|
|
case _T('|'): |
|
107 |
return false; |
return false; |
108 |
} |
} |
|
i++; |
|
109 |
} |
} |
110 |
|
bfs::create_directories (pth); |
|
|
|
111 |
return true; |
return true; |
|
} |
|
|
|
|
|
bool MakeSureDirectoryPathExists(CString Dir) |
|
|
{ |
|
|
|
|
|
if( Dir.IsEmpty() ) return true; |
|
|
if( Dir.GetLength() < 3 ) return true; |
|
|
|
|
|
CString Path = ExtractFilePath(Dir); |
|
|
Path = Path.Left( Path.GetLength() - 1 ); |
|
|
if( DirectoryExists(Path) ) |
|
|
return true; |
|
|
else { |
|
|
|
|
|
if( !MakeSureDirectoryPathExists(Path) ) |
|
|
return false; |
|
|
|
|
|
if( CreateDirectory(Path, NULL) == 0 ) |
|
|
return false; |
|
|
else |
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
112 |
} |
} |
113 |
|
|
114 |
bool DirectoryExists(const CString Name) |
bool DirectoryExists(const std::string Name) |
115 |
{ |
{ |
116 |
|
bfs::path pth (Name, bfs::native); |
117 |
DWORD Code = GetFileAttributes(Name); |
return bfs::exists(pth) && bfs::is_directory(pth); |
|
|
|
|
if( (Code != -1) && (Code & FILE_ATTRIBUTE_DIRECTORY ) ) |
|
|
return true; |
|
|
else |
|
|
return false; |
|
|
|
|
118 |
} |
} |
119 |
|
|
120 |
CString SrtTimeString( int nTime ) |
using boost::format; |
121 |
|
using boost::str; |
122 |
|
std::string SrtTimeString( int nTime ) |
123 |
{ |
{ |
124 |
CString csTime; |
std::string csTime; |
125 |
|
|
126 |
int h = nTime / ( 60 * 60 * 1000 ); |
int h = nTime / ( 60 * 60 * 1000 ); |
127 |
nTime = nTime - h * ( 60 * 60 * 1000 ); |
nTime = nTime - h * ( 60 * 60 * 1000 ); |
132 |
int s = nTime / 1000; |
int s = nTime / 1000; |
133 |
int t = nTime - s * 1000; |
int t = nTime - s * 1000; |
134 |
|
|
135 |
csTime.Format( "%02d:%02d:%02d,%03d", h, m, s, t ); |
csTime = str(format( "%02d:%02d:%02d,%03d") % h % m % s % t ); |
136 |
return csTime; |
return csTime; |
137 |
|
|
138 |
} |
} |