-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.h
209 lines (142 loc) · 4.31 KB
/
utils.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
this code is Public Domain
Peter Semiletov
*/
#ifndef UTILS_H
#define UTILS_H
#include <QObject>
#include <QHash>
#include <QFileInfo>
#include <QDir>
#include <QStringList>
#include <QAction>
#include <QTime>
#include <cmath>
#include <limits>
class CFilesList: public QObject
{
public:
QStringList list;
void get (const QString &path);
void iterate (QFileInfo &fi);
};
class CFTypeChecker: public QObject
{
public:
QStringList lexts;
QStringList lnames;
CFTypeChecker (const QString &fnames, const QString &exts);
bool check (const QString &fname);
};
QString qstring_clear (const QString &s);
bool is_dir (const QString &path);
bool qstring_save (const QString &fileName, const QString &data);
QString qstring_load (const QString &fileName);
//QString string_between (const QString &source, const QString &sep1, const QString &sep2);
QString str_from_locale (const char *s);
float get_value_with_default (const QString &val, float def);
size_t get_value_with_default (const QString &val, size_t def);
int get_value_with_default (const QString &val, int def);
QString get_value_with_default (const QString &val, const QString &def);
QString hash_keyval_to_string (const QHash<QString, QString> &h);
QString hash_get_val (QHash<QString, QString> &h, const QString &key, const QString &def_val);
QMap <QString, QString> map_load_keyval (const QString &fname, const QString &sep);
QString map_keyval_to_string (const QMap <QString, QString> &h, const QString &sep);
QHash<QString, QString> hash_load (const QString &fname);
QHash<QString, QString> hash_load_keyval (const QString &fname);
QHash<QString, QString> stringlist_to_hash (const QStringList &l);
QByteArray file_load (const QString &fileName);
QString file_get_ext (const QString &file_name);
QString change_file_ext (const QString &s, const QString &ext);
bool file_exists (const QString &fileName);
QStringList read_dir_entries (const QString &path);
QStringList read_dir_files (const QString &path);
void qstring_list_print (const QStringList &l);
bool is_image (const QString &filename);
size_t round_to (size_t value, size_t to, bool inc);
inline int get_value (int total, int perc)
{
return static_cast <int> (total * perc / 100);
}
inline float get_fvalue (float total, float perc)
{
return (total * perc / 100);
}
inline double get_percent (double total, double value)
{
return (value / total) * 100;
}
inline float get_percent (float total, float value)
{
return (value / total) * 100;
}
inline bool is_dir (const QString &path)
{
return QFileInfo(path).isDir();
}
inline QString get_file_path (const QString &fileName)
{
return QFileInfo (fileName).absolutePath();
}
inline bool float_greater_than (float a, float b)
{
return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * std::numeric_limits<double>::epsilon());
}
inline bool float_less_than (float a, float b)
{
return (b - a) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * std::numeric_limits<double>::epsilon());
}
inline bool float_equal (float x, float y)
{
return std::abs(x - y) <= std::numeric_limits<double>::epsilon() * std::abs(x);
}
inline float conv (float v, float middle, float max)
{
if (v == middle)
return 0;
if (v > middle)
return (max - middle - v);
else
return middle - v;
}
inline float conv_to_db (float v, float v_min, float v_max, float range_negative, float range_positive)
{
if (v == 0)
return 0;
if (v > 0)
{
float x = v_max / range_positive;
float y = v_max / v;
return v / (y * x);
}
else
{
float x = v_min / range_negative;
float y = v_min / v;
return v / (y * x);
}
}
inline float scale_val (float val, float from_min, float from_max, float to_min, float to_max)
{
return (val - from_min) * (to_max - to_min) /
(from_max - from_min) + to_min;
}
inline QString frames_to_time_str (size_t frames, size_t samplerate)
{
size_t msecs = (float) frames / samplerate * 1000;
QTime a (0, 0);
a = a.addMSecs ((int) msecs);
return a.toString ("hh:mm:ss.zzz");
}
inline size_t msecs_to_frames (size_t msecs, size_t samplerate)
{
return samplerate * msecs / 1000;
}
inline QTime frames_to_time (size_t frames, size_t samplerate)
{
size_t msecs = (float) frames / samplerate * 1000;
QTime a (0, 0);
a = a.addMSecs ((int) msecs);
return a;
}
#endif