1 |
jeremyd |
79 |
// Utility.cpp : implementation of the utility apis declared in utility.h
|
2 |
|
|
//
|
3 |
|
|
//********************************************************************
|
4 |
|
|
//
|
5 |
|
|
// Copyright (c) 2005 James Memmott <jmemmott@braeman.com>
|
6 |
|
|
//
|
7 |
|
|
// This software is provided 'as-is', without any express or implied warranty.
|
8 |
|
|
// In no event will the author be held liable for any damages arising from the
|
9 |
|
|
// use of this software.
|
10 |
|
|
//
|
11 |
|
|
// Permission is granted to anyone to use this software for any purpose, including
|
12 |
|
|
// commercial applications, and to alter it and redistribute it freely, subject to
|
13 |
|
|
// the following restrictions:
|
14 |
|
|
//
|
15 |
|
|
// 1. Redistributions of source code must retain the above copyright
|
16 |
|
|
// notice, this list of conditions and the disclaimer.
|
17 |
|
|
//
|
18 |
|
|
// 2. The origin of this software must not be misrepresented; you must not claim
|
19 |
|
|
// that you wrote the original software. If you use this software in a product,
|
20 |
|
|
// an acknowledgment in the product documentation would be appreciated but is
|
21 |
|
|
// not required.
|
22 |
|
|
//
|
23 |
|
|
// 3. Altered source versions must be plainly marked as such, and must not be
|
24 |
|
|
// misrepresented as being the original software.
|
25 |
|
|
//
|
26 |
|
|
//********************************************************************
|
27 |
|
|
//
|
28 |
|
|
|
29 |
jeremyd |
83 |
#include <string>
|
30 |
|
|
#include "Utility.h"
|
31 |
|
|
#include <boost/filesystem/path.hpp>
|
32 |
|
|
#include <boost/filesystem/operations.hpp>
|
33 |
|
|
#include <boost/filesystem/convenience.hpp>
|
34 |
|
|
#include <boost/format.hpp>
|
35 |
|
|
|
36 |
|
|
namespace bfs = boost::filesystem;
|
37 |
|
|
|
38 |
jeremyd |
79 |
// Returns the directory location corresponding to some path
|
39 |
jeremyd |
83 |
std::string GetFileDirFromPath(const char *tszPath)
|
40 |
jeremyd |
79 |
{
|
41 |
jeremyd |
83 |
bfs::path path = bfs::path(tszPath, bfs::native);
|
42 |
|
|
return path.branch_path().native_file_string();
|
43 |
jeremyd |
79 |
}
|
44 |
|
|
|
45 |
|
|
// Returns the filename from the supplied path
|
46 |
jeremyd |
83 |
std::string GetFileNameFromPath(const char *tszPath)
|
47 |
jeremyd |
79 |
{
|
48 |
jeremyd |
83 |
bfs::path path = bfs::path(tszPath, bfs::native);
|
49 |
|
|
return path.leaf();
|
50 |
jeremyd |
79 |
}
|
51 |
|
|
|
52 |
|
|
// Returns the filetitle from the supplied path
|
53 |
jeremyd |
83 |
std::string GetFileTitleFromPath(const char *tszPath)
|
54 |
jeremyd |
79 |
{
|
55 |
jeremyd |
83 |
bfs::path path = bfs::path(tszPath, bfs::native);
|
56 |
|
|
std::string fname = path.leaf();
|
57 |
|
|
std::string::size_type lastdot = fname.rfind('.');
|
58 |
|
|
if (lastdot != std::string::npos)
|
59 |
|
|
{
|
60 |
|
|
return fname.substr(0, lastdot);
|
61 |
|
|
}
|
62 |
|
|
return fname;
|
63 |
jeremyd |
79 |
}
|
64 |
|
|
|
65 |
|
|
// Returns the file extension from the supplied path
|
66 |
jeremyd |
83 |
std::string GetFileExtFromPath(const char *tszPath)
|
67 |
jeremyd |
79 |
{
|
68 |
jeremyd |
83 |
bfs::path path = bfs::path(tszPath, bfs::native);
|
69 |
|
|
std::string fname = path.leaf();
|
70 |
|
|
std::string::size_type lastdot = fname.rfind('.');
|
71 |
|
|
if (lastdot != std::string::npos)
|
72 |
|
|
{
|
73 |
|
|
return fname.substr(lastdot);
|
74 |
|
|
}
|
75 |
|
|
return fname;
|
76 |
jeremyd |
79 |
}
|
77 |
|
|
|
78 |
jeremyd |
83 |
std::string ExtractFilePath(std::string FileName)
|
79 |
jeremyd |
79 |
{
|
80 |
jeremyd |
83 |
bfs::path path = bfs::path(FileName, bfs::native);
|
81 |
|
|
return path.branch_path().native_file_string();
|
82 |
jeremyd |
79 |
}
|
83 |
|
|
|
84 |
jeremyd |
83 |
bool IsFileNameValid(const char * lpFileName)
|
85 |
jeremyd |
79 |
{
|
86 |
jeremyd |
83 |
if (lpFileName == NULL) return false;
|
87 |
|
|
return bfs::native (lpFileName);
|
88 |
|
|
}
|
89 |
jeremyd |
79 |
|
90 |
jeremyd |
83 |
bool MakeSureDirectoryPathExists(std::string Dir)
|
91 |
|
|
{
|
92 |
|
|
bfs::path pth = bfs::path(Dir, bfs::native).branch_path();
|
93 |
|
|
if (bfs::exists(pth))
|
94 |
jeremyd |
79 |
{
|
95 |
jeremyd |
83 |
if (bfs::is_directory(pth))
|
96 |
jeremyd |
79 |
{
|
97 |
jeremyd |
83 |
return true;
|
98 |
|
|
}
|
99 |
|
|
else
|
100 |
|
|
{
|
101 |
jeremyd |
79 |
return false;
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
jeremyd |
83 |
bfs::create_directories (pth);
|
105 |
jeremyd |
79 |
return true;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
jeremyd |
83 |
bool DirectoryExists(const std::string Name)
|
109 |
jeremyd |
79 |
{
|
110 |
jeremyd |
83 |
bfs::path pth (Name, bfs::native);
|
111 |
|
|
return bfs::exists(pth) && bfs::is_directory(pth);
|
112 |
jeremyd |
79 |
}
|
113 |
|
|
|
114 |
jmemmott |
168 |
#ifdef WIN32
|
115 |
|
|
std::string t2sprintf(const char* format,...)
|
116 |
|
|
{
|
117 |
|
|
va_list arguments;
|
118 |
|
|
va_start(arguments,format);
|
119 |
|
|
char buffer[1024];
|
120 |
|
|
vsprintf(buffer,format,arguments);
|
121 |
|
|
va_end(arguments);
|
122 |
|
|
std::string rtvalue = buffer;
|
123 |
|
|
return rtvalue;
|
124 |
|
|
}
|
125 |
|
|
#endif
|
126 |
|
|
|
127 |
jeremyd |
83 |
using boost::format;
|
128 |
|
|
using boost::str;
|
129 |
|
|
std::string SrtTimeString( int nTime )
|
130 |
jeremyd |
79 |
{
|
131 |
jeremyd |
83 |
std::string csTime;
|
132 |
jeremyd |
79 |
|
133 |
|
|
int h = nTime / ( 60 * 60 * 1000 );
|
134 |
|
|
nTime = nTime - h * ( 60 * 60 * 1000 );
|
135 |
|
|
|
136 |
|
|
int m = nTime / ( 60 * 1000 );
|
137 |
|
|
nTime = nTime - m * ( 60 * 1000 );
|
138 |
|
|
|
139 |
|
|
int s = nTime / 1000;
|
140 |
|
|
int t = nTime - s * 1000;
|
141 |
|
|
|
142 |
jeremyd |
83 |
csTime = str(format( "%02d:%02d:%02d,%03d") % h % m % s % t );
|
143 |
jeremyd |
79 |
return csTime;
|
144 |
|
|
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
|