forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PresetLoader.cpp
280 lines (214 loc) · 6.88 KB
/
PresetLoader.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// C++ Implementation: PresetLoader
//
// Description:
//
//
// Author: Carmelo Piccione <[email protected]>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "PresetLoader.hpp"
#include "Preset.hpp"
#include "PresetFactory.hpp"
#include <iostream>
#include <sstream>
#include <set>
#ifdef __unix__
extern "C"
{
#include <errno.h>
}
#endif
#ifdef __APPLE__
extern "C"
{
#include <errno.h>
}
#endif
#include <cassert>
#include "fatal.h"
#include "Common.hpp"
PresetLoader::PresetLoader (int gx, int gy, std::string dirname = std::string()) :_dirname ( dirname ), _dir ( 0 )
{
_presetFactoryManager.initialize(gx,gy);
// Do one scan
if ( _dirname != std::string() )
rescan();
else
clear();
}
PresetLoader::~PresetLoader()
{
if ( _dir )
closedir ( _dir );
}
void PresetLoader::setScanDirectory ( std::string dirname )
{
_dirname = dirname;
}
void PresetLoader::rescan()
{
// std::cerr << "Rescanning..." << std::endl;
// Clear the directory entry collection
clear();
// If directory already opened, close it first
if ( _dir )
{
closedir ( _dir );
_dir = 0;
}
// Allocate a new a stream given the current directory name
if ( ( _dir = opendir ( _dirname.c_str() ) ) == NULL )
{
handleDirectoryError();
return; // no files loaded. _entries is empty
}
struct dirent * dir_entry;
std::set<std::string> alphaSortedFileSet;
std::set<std::string> alphaSortedPresetNameSet;
while ( ( dir_entry = readdir ( _dir ) ) != NULL )
{
if (dir_entry->d_name[0] == 0)
continue;
std::ostringstream out;
// Convert char * to friendly string
std::string filename ( dir_entry->d_name );
// Verify extension is projectm or milkdrop
if (!_presetFactoryManager.extensionHandled(parseExtension(filename)))
continue;
if ( filename.length() > 0 && filename[0] == '.' )
continue;
// Create full path name
out << _dirname << PATH_SEPARATOR << filename;
// Add to our directory entry collection
alphaSortedFileSet.insert ( out.str() );
alphaSortedPresetNameSet.insert ( filename );
// the directory entry struct is freed elsewhere
}
// Push all entries in order from the file set to the file entries member (which is an indexed vector)
for ( std::set<std::string>::iterator pos = alphaSortedFileSet.begin();
pos != alphaSortedFileSet.end();++pos )
_entries.push_back ( *pos );
// Push all preset names in similar fashion
for ( std::set<std::string>::iterator pos = alphaSortedPresetNameSet.begin();
pos != alphaSortedPresetNameSet.end();++pos )
_presetNames.push_back ( *pos );
// Give all presets equal rating of 3 - why 3? I don't know
_ratings = std::vector<RatingList>(TOTAL_RATING_TYPES, RatingList( _presetNames.size(), 3 ));
_ratingsSums = std::vector<int>(TOTAL_RATING_TYPES, 3 * _presetNames.size());
assert ( _entries.size() == _presetNames.size() );
}
std::unique_ptr<Preset> PresetLoader::loadPreset ( unsigned int index ) const
{
// Check that index isn't insane
assert ( index < _entries.size() );
return _presetFactoryManager.allocate
( _entries[index], _presetNames[index] );
}
std::unique_ptr<Preset> PresetLoader::loadPreset ( const std::string & url ) const
{
// std::cout << "Loading preset " << url << std::endl;
try {
/// @bug probably should not use url for preset name
return _presetFactoryManager.allocate
(url, url);
} catch (const std::exception & e) {
throw PresetFactoryException(e.what());
} catch (...) {
throw PresetFactoryException("preset factory exception of unknown cause");
}
return std::unique_ptr<Preset>();
}
void PresetLoader::handleDirectoryError()
{
#ifdef WIN32
std::cerr << "[PresetLoader] warning: errno unsupported on win32 platforms. fix me" << std::endl;
#else
switch ( errno )
{
case ENOENT:
std::cerr << "[PresetLoader] ENOENT error. The path \"" << this->_dirname << "\" probably does not exist. \"man open\" for more info." << std::endl;
break;
case ENOMEM:
std::cerr << "[PresetLoader] out of memory! Are you running Windows?" << std::endl;
abort();
case ENOTDIR:
std::cerr << "[PresetLoader] directory specified is not a preset directory! Trying to continue..." << std::endl;
break;
case ENFILE:
std::cerr << "[PresetLoader] Your system has reached its open file limit. Trying to continue..." << std::endl;
break;
case EMFILE:
std::cerr << "[PresetLoader] too many files in use by projectM! Bailing!" << std::endl;
break;
case EACCES:
std::cerr << "[PresetLoader] permissions issue reading the specified preset directory." << std::endl;
break;
default:
break;
}
#endif
}
void PresetLoader::setRating(unsigned int index, int rating, const PresetRatingType ratingType)
{
const unsigned int ratingTypeIndex = static_cast<unsigned int>(ratingType);
assert (index < _ratings[ratingTypeIndex].size());
_ratingsSums[ratingTypeIndex] -= _ratings[ratingTypeIndex][index];
_ratings[ratingTypeIndex][index] = rating;
_ratingsSums[ratingType] += rating;
}
unsigned int PresetLoader::addPresetURL ( const std::string & url, const std::string & presetName, const std::vector<int> & ratings)
{
_entries.push_back(url);
_presetNames.push_back ( presetName );
assert(ratings.size() == TOTAL_RATING_TYPES);
assert(ratings.size() == _ratings.size());
for (unsigned int i = 0; i < _ratings.size(); i++)
_ratings[i].push_back(ratings[i]);
for (unsigned int i = 0; i < ratings.size(); i++)
_ratingsSums[i] += ratings[i];
return _entries.size()-1;
}
void PresetLoader::removePreset ( unsigned int index )
{
_entries.erase ( _entries.begin() + index );
_presetNames.erase ( _presetNames.begin() + index );
for (unsigned int i = 0; i < _ratingsSums.size(); i++) {
_ratingsSums[i] -= _ratings[i][index];
_ratings[i].erase ( _ratings[i].begin() + index );
}
}
const std::string & PresetLoader::getPresetURL ( unsigned int index ) const
{
return _entries[index];
}
const std::string & PresetLoader::getPresetName ( unsigned int index ) const
{
return _presetNames[index];
}
int PresetLoader::getPresetRating ( unsigned int index, const PresetRatingType ratingType ) const
{
return _ratings[ratingType][index];
}
const std::vector<RatingList> & PresetLoader::getPresetRatings () const
{
return _ratings;
}
const std::vector<int> & PresetLoader::getPresetRatingsSums() const {
return _ratingsSums;
}
void PresetLoader::setPresetName(unsigned int index, std::string name) {
_presetNames[index] = name;
}
void PresetLoader::insertPresetURL ( unsigned int index, const std::string & url, const std::string & presetName, const RatingList & ratings)
{
_entries.insert ( _entries.begin() + index, url );
_presetNames.insert ( _presetNames.begin() + index, presetName );
for (unsigned int i = 0; i < _ratingsSums.size();i++) {
_ratingsSums[i] += _ratings[i][index];
_ratings[i].insert ( _ratings[i].begin() + index, ratings[i] );
}
assert ( _entries.size() == _presetNames.size() );
}