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

Contents of /t2sami/trunk/Utility.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 257 - (hide annotations) (download)
Tue Dec 27 08:17:02 2011 UTC (7 years, 2 months ago) by jeremyd2019
File size: 4057 byte(s)
update for boost_filesystem api version 3.
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 jeremyd2019 198 #ifdef HAVE_CONFIG_H
30     # include "config.h"
31     #endif
32 jeremyd 83 #include <string>
33     #include "Utility.h"
34 jeremyd2019 257 #define BOOST_FILESYSTEM_VERSION 3
35 jeremyd 83 #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 jeremyd 79 // Returns the directory location corresponding to some path
43 jeremyd 83 std::string GetFileDirFromPath(const char *tszPath)
44 jeremyd 79 {
45 jeremyd2019 257 bfs::path path = bfs::path(tszPath);
46     return path.parent_path().string();
47 jeremyd 79 }
48    
49     // Returns the filename from the supplied path
50 jeremyd 83 std::string GetFileNameFromPath(const char *tszPath)
51 jeremyd 79 {
52 jeremyd2019 257 bfs::path path = bfs::path(tszPath);
53     return path.filename().string();
54 jeremyd 79 }
55    
56     // Returns the filetitle from the supplied path
57 jeremyd 83 std::string GetFileTitleFromPath(const char *tszPath)
58 jeremyd 79 {
59 jeremyd2019 257 bfs::path path = bfs::path(tszPath);
60     std::string fname = path.filename().string();
61 jeremyd 83 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 jeremyd 79 }
68    
69     // Returns the file extension from the supplied path
70 jeremyd 83 std::string GetFileExtFromPath(const char *tszPath)
71 jeremyd 79 {
72 jeremyd2019 257 bfs::path path = bfs::path(tszPath);
73     std::string fname = path.filename().string();
74 jeremyd 83 std::string::size_type lastdot = fname.rfind('.');
75     if (lastdot != std::string::npos)
76     {
77     return fname.substr(lastdot);
78     }
79     return fname;
80 jeremyd 79 }
81    
82 jeremyd 83 std::string ExtractFilePath(std::string FileName)
83 jeremyd 79 {
84 jeremyd2019 257 bfs::path path = bfs::path(FileName);
85     return path.parent_path().string();
86 jeremyd 79 }
87    
88 jeremyd2019 182 std::string ConcatPaths(const std::string & dir_name, const std::string & file_name)
89     {
90 jeremyd2019 257 bfs::path dir_path (dir_name),
91     file_path (file_name);
92     return (dir_path / file_path).string();
93 jeremyd2019 182 }
94    
95 jeremyd 83 bool IsFileNameValid(const char * lpFileName)
96 jeremyd 79 {
97 jeremyd 83 if (lpFileName == NULL) return false;
98     return bfs::native (lpFileName);
99     }
100 jeremyd 79
101 jeremyd 83 bool MakeSureDirectoryPathExists(std::string Dir)
102     {
103 jeremyd2019 257 bfs::path pth = bfs::path(Dir).parent_path();
104 jeremyd 83 if (bfs::exists(pth))
105 jeremyd 79 {
106 jeremyd 83 if (bfs::is_directory(pth))
107 jeremyd 79 {
108 jeremyd 83 return true;
109     }
110     else
111     {
112 jeremyd 79 return false;
113     }
114     }
115 jeremyd 83 bfs::create_directories (pth);
116 jeremyd 79 return true;
117     }
118    
119 jeremyd 83 bool DirectoryExists(const std::string Name)
120 jeremyd 79 {
121 jeremyd2019 257 bfs::path pth (Name);
122 jeremyd 83 return bfs::exists(pth) && bfs::is_directory(pth);
123 jeremyd 79 }
124    
125 jmemmott 168 #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 jeremyd 83 using boost::format;
139     using boost::str;
140     std::string SrtTimeString( int nTime )
141 jeremyd 79 {
142 jeremyd 83 std::string csTime;
143 jeremyd 79
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 jeremyd 83 csTime = str(format( "%02d:%02d:%02d,%03d") % h % m % s % t );
154 jeremyd 79 return csTime;
155    
156     }
157    
158    

cvs@jdrake.com
ViewVC Help
Powered by ViewVC 1.1.13