/[tivodecode]/t2sami/trunk/Utility.cpp
ViewVC logotype

Contents of /t2sami/trunk/Utility.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 257 - (show annotations) (download)
Tue Dec 27 08:17:02 2011 UTC (10 years, 7 months ago) by jeremyd2019
File size: 4057 byte(s)
update for boost_filesystem api version 3.
1 // 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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <string>
33 #include "Utility.h"
34 #define BOOST_FILESYSTEM_VERSION 3
35 #include <boost/filesystem/path.hpp>
36 #include <boost/filesystem/operations.hpp>
37 #include <boost/filesystem/convenience.hpp>
38 #include <boost/format.hpp>
39
40 namespace bfs = boost::filesystem;
41
42 // Returns the directory location corresponding to some path
43 std::string GetFileDirFromPath(const char *tszPath)
44 {
45 bfs::path path = bfs::path(tszPath);
46 return path.parent_path().string();
47 }
48
49 // Returns the filename from the supplied path
50 std::string GetFileNameFromPath(const char *tszPath)
51 {
52 bfs::path path = bfs::path(tszPath);
53 return path.filename().string();
54 }
55
56 // Returns the filetitle from the supplied path
57 std::string GetFileTitleFromPath(const char *tszPath)
58 {
59 bfs::path path = bfs::path(tszPath);
60 std::string fname = path.filename().string();
61 std::string::size_type lastdot = fname.rfind('.');
62 if (lastdot != std::string::npos)
63 {
64 return fname.substr(0, lastdot);
65 }
66 return fname;
67 }
68
69 // Returns the file extension from the supplied path
70 std::string GetFileExtFromPath(const char *tszPath)
71 {
72 bfs::path path = bfs::path(tszPath);
73 std::string fname = path.filename().string();
74 std::string::size_type lastdot = fname.rfind('.');
75 if (lastdot != std::string::npos)
76 {
77 return fname.substr(lastdot);
78 }
79 return fname;
80 }
81
82 std::string ExtractFilePath(std::string FileName)
83 {
84 bfs::path path = bfs::path(FileName);
85 return path.parent_path().string();
86 }
87
88 std::string ConcatPaths(const std::string & dir_name, const std::string & file_name)
89 {
90 bfs::path dir_path (dir_name),
91 file_path (file_name);
92 return (dir_path / file_path).string();
93 }
94
95 bool IsFileNameValid(const char * lpFileName)
96 {
97 if (lpFileName == NULL) return false;
98 return bfs::native (lpFileName);
99 }
100
101 bool MakeSureDirectoryPathExists(std::string Dir)
102 {
103 bfs::path pth = bfs::path(Dir).parent_path();
104 if (bfs::exists(pth))
105 {
106 if (bfs::is_directory(pth))
107 {
108 return true;
109 }
110 else
111 {
112 return false;
113 }
114 }
115 bfs::create_directories (pth);
116 return true;
117 }
118
119 bool DirectoryExists(const std::string Name)
120 {
121 bfs::path pth (Name);
122 return bfs::exists(pth) && bfs::is_directory(pth);
123 }
124
125 #ifdef WIN32
126 std::string t2sprintf(const char* format,...)
127 {
128 va_list arguments;
129 va_start(arguments,format);
130 char buffer[1024];
131 vsprintf(buffer,format,arguments);
132 va_end(arguments);
133 std::string rtvalue = buffer;
134 return rtvalue;
135 }
136 #endif
137
138 using boost::format;
139 using boost::str;
140 std::string SrtTimeString( int nTime )
141 {
142 std::string csTime;
143
144 int h = nTime / ( 60 * 60 * 1000 );
145 nTime = nTime - h * ( 60 * 60 * 1000 );
146
147 int m = nTime / ( 60 * 1000 );
148 nTime = nTime - m * ( 60 * 1000 );
149
150 int s = nTime / 1000;
151 int t = nTime - s * 1000;
152
153 csTime = str(format( "%02d:%02d:%02d,%03d") % h % m % s % t );
154 return csTime;
155
156 }
157
158

cvs@jdrake.com
ViewVC Help
Powered by ViewVC 1.1.13