-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
175 lines (164 loc) · 4.43 KB
/
utils.cpp
File metadata and controls
175 lines (164 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/////////////////////////////////////////////////////////////////////////////
// Name: utils.cpp
// Author: Alex Thuering
// Created: 7.07.2012
// Copyright: (c) Alex Thuering
// Licence: GPL
/////////////////////////////////////////////////////////////////////////////
#include "utils.h"
#include <wx/dir.h>
#include <wx/log.h>
#include <wx/filename.h>
#ifdef __UNIX_LIKE__
#include <stdlib.h>
#include <sys/param.h>
#endif
wxString appPath;
wxRegEx s_timeRE(wxT("^(([[:digit:]]+:)?[[:digit:]][[:digit:]]?:)?[[:digit:]][[:digit:]]?(\\.[[:digit:]]+)?$"));
wxString wxGetAppPath()
{
if (appPath.length() == 0)
#if defined(__WXMAC__) && !defined(__DARWIN__)
// On Mac, the current directory is the relevant one when
// the application starts.
appPath = wxGetWorkingDirectory();
#else
{
if (wxIsAbsolutePath(wxTheApp->argv[0]))
appPath = wxTheApp->argv[0];
else
{
// Is it a relative path?
wxString currentDir(wxGetCwd());
if (currentDir.Last() != wxFILE_SEP_PATH)
currentDir += wxFILE_SEP_PATH;
wxString str = currentDir + wxTheApp->argv[0];
if (wxFileExists(str))
appPath = str;
else
{
// OK, it's neither an absolute path nor a relative path.
// Search PATH.
wxPathList pathList;
pathList.AddEnvList(wxT("PATH"));
str = pathList.FindAbsoluteValidPath(wxTheApp->argv[0]);
if (!str.IsEmpty())
appPath = str;
}
}
#ifdef __UNIX_LIKE__
// realfullname
char realnameBuf[MAXPATHLEN];
char* realname = realpath(appPath.mb_str(), (char*)realnameBuf);
appPath = wxString(realname, *wxConvCurrent);
#endif
appPath = wxPathOnly(appPath);
}
#endif
if (appPath.Last() != wxFILE_SEP_PATH)
appPath += wxFILE_SEP_PATH;
return appPath;
}
void wxSetAppPath(wxString value)
{
appPath = value;
}
wxString wxFindDataDirectory(wxString dir)
{
wxString d = wxGetAppPath() + dir;
if (wxDir::Exists(d))
return d;
wxFileName dname(wxGetAppPath() + wxT("..") +
wxFILE_SEP_PATH + dir + wxFILE_SEP_PATH);
dname.Normalize();
if (wxDir::Exists(dname.GetFullPath()))
return dname.GetFullPath();
#ifdef DATADIR
return wxString(DATADIR,wxConvLocal) + wxFILE_SEP_PATH + dir + wxFILE_SEP_PATH;
#else
return wxGetAppPath() + dir + wxFILE_SEP_PATH;
#endif
}
wxString wxFindDataFile(wxString filename)
{
wxString d = wxGetAppPath() + filename;
if (wxFileExists(d))
return d;
wxFileName fname(wxGetAppPath() + wxT("..") + wxFILE_SEP_PATH + filename);
fname.Normalize();
if (wxFileExists(fname.GetFullPath()))
return fname.GetFullPath();
#ifdef DATADIR
return wxString(DATADIR,wxConvLocal) + wxFILE_SEP_PATH + filename;
#else
return wxGetAppPath() + filename;
#endif
}
/**
* Converts time span value (milliseconds) in string
*/
wxString Time2String(long value, bool full) {
int t = value / 1000;
int ms = value % 1000;
if (full || (t >= 3600 && ms > 0))
return wxString::Format(wxT("%d:%02d:%02d.%03d"), t/3600, (t/60) % 60, t % 60, ms);
else if (t >= 3600)
return wxString::Format(wxT("%d:%02d:%02d"), t/3600, (t/60) % 60, t % 60);
else if (ms > 0)
return wxString::Format(wxT("%d:%02d.%03d"), (t/60) % 60, t % 60, ms);
return wxString::Format(wxT("%d:%02d"), (t/60) % 60, t % 60);
}
/**
* Converts string in time span value (milliseconds)
*/
long String2Time(const wxString& value, float fps) {
long result = 0;
wxString val = value;
for (int i = 0; i <= 3; i++) {
if (i < 3)
result *= 60;
if (val.Find(wxT(':')) == wxNOT_FOUND) {
result *= 1000;
if (i == 3) { // SMPTE format 00:00:00:00
long t = 0;
if (val.ToLong(&t))
result += lround(t*1000/fps);
} else {
double t = 0;
if (val.ToDouble(&t))
result += lround(t*1000);
}
break;
} else {
long t = 0;
if (val.BeforeFirst(wxT(':')).ToLong(&t))
result += t;
val = val.AfterFirst(wxT(':'));
}
}
return result;
}
/**
* Converts string in time span value (milliseconds)
*/
double TimeToDouble(wxString timeStr) {
wxRegEx re(wxT("^(([[:digit:]]+:)?([[:digit:]]?[[:digit:]]):)?([[:digit:]]?[[:digit:]])(\\.[[:digit:]]+)?$"));
if (!re.Matches(timeStr))
return -1;
double result = 0;
long lval;
wxString val = re.GetMatch(timeStr, 2).BeforeFirst(wxT(':'));
if (val.ToLong(&lval))
result += lval*3600;
val = re.GetMatch(timeStr, 3).BeforeFirst(wxT(':'));
if (val.ToLong(&lval))
result += lval*60;
val = re.GetMatch(timeStr, 4);
if (val.ToLong(&lval))
result += lval;
val = re.GetMatch(timeStr, 5);
double dval;
if (val.ToDouble(&dval))
result += dval;
return result;
}