/[svn]/linuxsampler/trunk/src/db/InstrumentsDbUtilities.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/db/InstrumentsDbUtilities.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1943 - (show annotations) (download)
Tue Jul 14 18:25:11 2009 UTC (14 years, 8 months ago) by persson
File size: 21285 byte(s)
* fixed instrument database recursive import, which was broken on
  Windows

1 /***************************************************************************
2 * *
3 * Copyright (C) 2007 - 2009 Grigor Iliev *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
18 * MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "InstrumentsDbUtilities.h"
22
23 #include "../common/File.h"
24 #include "../common/global_private.h"
25
26 #include <algorithm>
27 #include <errno.h>
28
29 #include "../common/Exception.h"
30 #include "InstrumentsDb.h"
31
32 namespace LinuxSampler {
33
34 void DbInstrument::Copy(const DbInstrument& Instr) {
35 if (this == &Instr) return;
36
37 InstrFile = Instr.InstrFile;
38 InstrNr = Instr.InstrNr;
39 FormatFamily = Instr.FormatFamily;
40 FormatVersion = Instr.FormatVersion;
41 Size = Instr.Size;
42 Created = Instr.Created;
43 Modified = Instr.Modified;
44 Description = Instr.Description;
45 IsDrum = Instr.IsDrum;
46 Product = Instr.Product;
47 Artists = Instr.Artists;
48 Keywords = Instr.Keywords;
49 }
50
51
52 void DbDirectory::Copy(const DbDirectory& Dir) {
53 if (this == &Dir) return;
54
55 Created = Dir.Created;
56 Modified = Dir.Modified;
57 Description = Dir.Description;
58 }
59
60 SearchQuery::SearchQuery() {
61 MinSize = -1;
62 MaxSize = -1;
63 InstrType = BOTH;
64 }
65
66 void SearchQuery::SetFormatFamilies(String s) {
67 if (s.length() == 0) return;
68 int i = 0;
69 int j = s.find(',', 0);
70
71 while (j != std::string::npos) {
72 FormatFamilies.push_back(s.substr(i, j - i));
73 i = j + 1;
74 j = s.find(',', i);
75 }
76
77 if (i < s.length()) FormatFamilies.push_back(s.substr(i));
78 }
79
80 void SearchQuery::SetSize(String s) {
81 String s2 = GetMin(s);
82 if (s2.length() > 0) MinSize = atoll(s2.c_str());
83 else MinSize = -1;
84
85 s2 = GetMax(s);
86 if (s2.length() > 0) MaxSize = atoll(s2.c_str());
87 else MaxSize = -1;
88 }
89
90 void SearchQuery::SetCreated(String s) {
91 CreatedAfter = GetMin(s);
92 CreatedBefore = GetMax(s);
93 }
94
95 void SearchQuery::SetModified(String s) {
96 ModifiedAfter = GetMin(s);
97 ModifiedBefore = GetMax(s);
98 }
99
100 String SearchQuery::GetMin(String s) {
101 if (s.length() < 3) return "";
102 if (s.at(0) == '.' && s.at(1) == '.') return "";
103 int i = s.find("..");
104 if (i == std::string::npos) return "";
105 return s.substr(0, i);
106 }
107
108 String SearchQuery::GetMax(String s) {
109 if (s.length() < 3) return "";
110 if (s.find("..", s.length() - 2) != std::string::npos) return "";
111 int i = s.find("..");
112 if (i == std::string::npos) return "";
113 return s.substr(i + 2);
114 }
115
116 void ScanJob::Copy(const ScanJob& Job) {
117 if (this == &Job) return;
118
119 JobId = Job.JobId;
120 FilesTotal = Job.FilesTotal;
121 FilesScanned = Job.FilesScanned;
122 Scanning = Job.Scanning;
123 Status = Job.Status;
124 }
125
126 int JobList::AddJob(ScanJob Job) {
127 if (Counter + 1 < Counter) Counter = 0;
128 else Counter++;
129 Job.JobId = Counter;
130 Jobs.push_back(Job);
131 if (Jobs.size() > 3) {
132 std::vector<ScanJob>::iterator iter = Jobs.begin();
133 Jobs.erase(iter);
134 }
135 return Job.JobId;
136 }
137
138 ScanJob& JobList::GetJobById(int JobId) {
139 for (int i = 0; i < Jobs.size(); i++) {
140 if (Jobs.at(i).JobId == JobId) return Jobs.at(i);
141 }
142
143 throw Exception("Invalid job ID: " + ToString(JobId));
144 }
145
146 bool AbstractFinder::IsRegex(String Pattern) {
147 if(Pattern.find('?') != String::npos) return true;
148 if(Pattern.find('*') != String::npos) return true;
149 return false;
150 }
151
152 void AbstractFinder::AddSql(String Col, String Pattern, std::stringstream& Sql) {
153 if (Pattern.length() == 0) return;
154
155 if (IsRegex(Pattern)) {
156 #ifndef WIN32
157 Sql << " AND " << Col << " regexp ?";
158 #else
159 for (int i = 0; i < Pattern.length(); i++) {
160 if (Pattern.at(i) == '?') Pattern.at(i) = '_';
161 else if (Pattern.at(i) == '*') Pattern.at(i) = '%';
162 }
163 Sql << " AND " << Col << " LIKE ?";
164 #endif
165 Params.push_back(Pattern);
166 return;
167 }
168
169 String buf;
170 std::vector<String> tokens;
171 std::vector<String> tokens2;
172 std::stringstream ss(Pattern);
173 while (ss >> buf) tokens.push_back(buf);
174
175 if (tokens.size() == 0) {
176 Sql << " AND " << Col << " LIKE ?";
177 Params.push_back("%" + Pattern + "%");
178 return;
179 }
180
181 bool b = false;
182 for (int i = 0; i < tokens.size(); i++) {
183 Sql << (i == 0 ? " AND (" : "");
184
185 for (int j = 0; j < tokens.at(i).length(); j++) {
186 if (tokens.at(i).at(j) == '+') tokens.at(i).at(j) = ' ';
187 }
188
189 ss.clear();
190 ss.str("");
191 ss << tokens.at(i);
192
193 tokens2.clear();
194 while (ss >> buf) tokens2.push_back(buf);
195
196 if (b && tokens2.size() > 0) Sql << " OR ";
197 if (tokens2.size() > 1) Sql << "(";
198 for (int j = 0; j < tokens2.size(); j++) {
199 if (j != 0) Sql << " AND ";
200 Sql << Col << " LIKE ?";
201 Params.push_back("%" + tokens2.at(j) + "%");
202 b = true;
203 }
204 if (tokens2.size() > 1) Sql << ")";
205 }
206 if (!b) Sql << "0)";
207 else Sql << ")";
208 }
209
210 DirectoryFinder::DirectoryFinder(SearchQuery* pQuery) : pDirectories(new std::vector<String>) {
211 pStmt = NULL;
212 this->pQuery = pQuery;
213 std::stringstream sql;
214 sql << "SELECT dir_name from instr_dirs WHERE dir_id!=0 AND parent_dir_id=?";
215
216 if (pQuery->CreatedAfter.length() != 0) {
217 sql << " AND created > ?";
218 Params.push_back(pQuery->CreatedAfter);
219 }
220 if (pQuery->CreatedBefore.length() != 0) {
221 sql << " AND created < ?";
222 Params.push_back(pQuery->CreatedBefore);
223 }
224 if (pQuery->ModifiedAfter.length() != 0) {
225 sql << " AND modified > ?";
226 Params.push_back(pQuery->ModifiedAfter);
227 }
228 if (pQuery->ModifiedBefore.length() != 0) {
229 sql << " AND modified < ?";
230 Params.push_back(pQuery->ModifiedBefore);
231 }
232
233 AddSql("dir_name", pQuery->Name, sql);
234 AddSql("description", pQuery->Description, sql);
235 SqlQuery = sql.str();
236
237 InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();
238
239 int res = sqlite3_prepare(idb->GetDb(), SqlQuery.c_str(), -1, &pStmt, NULL);
240 if (res != SQLITE_OK) {
241 throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));
242 }
243
244 for(int i = 0; i < Params.size(); i++) {
245 idb->BindTextParam(pStmt, i + 2, Params.at(i));
246 }
247 }
248
249 DirectoryFinder::~DirectoryFinder() {
250 if (pStmt != NULL) sqlite3_finalize(pStmt);
251 }
252
253 StringListPtr DirectoryFinder::GetDirectories() {
254 return pDirectories;
255 }
256
257 void DirectoryFinder::ProcessDirectory(String Path, int DirId) {
258 InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();
259 idb->BindIntParam(pStmt, 1, DirId);
260
261 String s = Path;
262 if(Path.compare("/") != 0) s += "/";
263 int res = sqlite3_step(pStmt);
264 while(res == SQLITE_ROW) {
265 pDirectories->push_back(s + idb->toAbstractName(ToString(sqlite3_column_text(pStmt, 0))));
266 res = sqlite3_step(pStmt);
267 }
268
269 if (res != SQLITE_DONE) {
270 sqlite3_finalize(pStmt);
271 throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));
272 }
273
274 res = sqlite3_reset(pStmt);
275 if (res != SQLITE_OK) {
276 sqlite3_finalize(pStmt);
277 throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));
278 }
279 }
280
281 InstrumentFinder::InstrumentFinder(SearchQuery* pQuery) : pInstruments(new std::vector<String>) {
282 pStmt = NULL;
283 this->pQuery = pQuery;
284 std::stringstream sql;
285 sql << "SELECT instr_name from instruments WHERE dir_id=?";
286
287 if (pQuery->CreatedAfter.length() != 0) {
288 sql << " AND created > ?";
289 Params.push_back(pQuery->CreatedAfter);
290 }
291 if (pQuery->CreatedBefore.length() != 0) {
292 sql << " AND created < ?";
293 Params.push_back(pQuery->CreatedBefore);
294 }
295 if (pQuery->ModifiedAfter.length() != 0) {
296 sql << " AND modified > ?";
297 Params.push_back(pQuery->ModifiedAfter);
298 }
299 if (pQuery->ModifiedBefore.length() != 0) {
300 sql << " AND modified < ?";
301 Params.push_back(pQuery->ModifiedBefore);
302 }
303 if (pQuery->MinSize != -1) sql << " AND instr_size > " << pQuery->MinSize;
304 if (pQuery->MaxSize != -1) sql << " AND instr_size < " << pQuery->MaxSize;
305
306 if (pQuery->InstrType == SearchQuery::CHROMATIC) sql << " AND is_drum = 0";
307 else if (pQuery->InstrType == SearchQuery::DRUM) sql << " AND is_drum != 0";
308
309 if (pQuery->FormatFamilies.size() > 0) {
310 sql << " AND (format_family=?";
311 Params.push_back(pQuery->FormatFamilies.at(0));
312 for (int i = 1; i < pQuery->FormatFamilies.size(); i++) {
313 sql << "OR format_family=?";
314 Params.push_back(pQuery->FormatFamilies.at(i));
315 }
316 sql << ")";
317 }
318
319 AddSql("instr_name", pQuery->Name, sql);
320 AddSql("description", pQuery->Description, sql);
321 AddSql("product", pQuery->Product, sql);
322 AddSql("artists", pQuery->Artists, sql);
323 AddSql("keywords", pQuery->Keywords, sql);
324 SqlQuery = sql.str();
325
326 InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();
327
328 int res = sqlite3_prepare(idb->GetDb(), SqlQuery.c_str(), -1, &pStmt, NULL);
329 if (res != SQLITE_OK) {
330 throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));
331 }
332
333 for(int i = 0; i < Params.size(); i++) {
334 idb->BindTextParam(pStmt, i + 2, Params.at(i));
335 }
336 }
337
338 InstrumentFinder::~InstrumentFinder() {
339 if (pStmt != NULL) sqlite3_finalize(pStmt);
340 }
341
342 void InstrumentFinder::ProcessDirectory(String Path, int DirId) {
343 InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();
344 idb->BindIntParam(pStmt, 1, DirId);
345
346 String s = Path;
347 if(Path.compare("/") != 0) s += "/";
348 int res = sqlite3_step(pStmt);
349 while(res == SQLITE_ROW) {
350 pInstruments->push_back(s + idb->toAbstractName(ToString(sqlite3_column_text(pStmt, 0))));
351 res = sqlite3_step(pStmt);
352 }
353
354 if (res != SQLITE_DONE) {
355 sqlite3_finalize(pStmt);
356 throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));
357 }
358
359 res = sqlite3_reset(pStmt);
360 if (res != SQLITE_OK) {
361 sqlite3_finalize(pStmt);
362 throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));
363 }
364 }
365
366 StringListPtr InstrumentFinder::GetInstruments() {
367 return pInstruments;
368 }
369
370 void DirectoryCounter::ProcessDirectory(String Path, int DirId) {
371 count += InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(DirId);
372 }
373
374 void InstrumentCounter::ProcessDirectory(String Path, int DirId) {
375 count += InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(DirId);
376 }
377
378 DirectoryCopier::DirectoryCopier(String SrcParentDir, String DestDir) {
379 this->SrcParentDir = SrcParentDir;
380 this->DestDir = DestDir;
381
382 if (DestDir.at(DestDir.length() - 1) != '/') {
383 this->DestDir.append("/");
384 }
385 if (SrcParentDir.at(SrcParentDir.length() - 1) != '/') {
386 this->SrcParentDir.append("/");
387 }
388 }
389
390 void DirectoryCopier::ProcessDirectory(String Path, int DirId) {
391 InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
392
393 String dir = DestDir;
394 String subdir = Path;
395 if(subdir.length() > SrcParentDir.length()) {
396 subdir = subdir.substr(SrcParentDir.length());
397 dir += subdir;
398 db->AddDirectory(dir);
399 }
400
401 int dstDirId = db->GetDirectoryId(dir);
402 if(dstDirId == -1) {
403 throw Exception("Unkown DB directory: " + InstrumentsDb::toEscapedPath(dir));
404 }
405 IntListPtr ids = db->GetInstrumentIDs(DirId);
406 for (int i = 0; i < ids->size(); i++) {
407 String name = db->GetInstrumentName(ids->at(i));
408 db->CopyInstrument(ids->at(i), name, dstDirId, dir);
409 }
410 }
411
412 ScanProgress::ScanProgress() {
413 TotalFileCount = ScannedFileCount = Status = 0;
414 CurrentFile = "";
415 GigFileProgress.custom = this;
416 GigFileProgress.callback = GigFileProgressCallback;
417 }
418
419 void ScanProgress::StatusChanged() {
420 InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
421 db->Jobs.GetJobById(JobId).FilesTotal = GetTotalFileCount();
422 db->Jobs.GetJobById(JobId).FilesScanned = GetScannedFileCount();
423 db->Jobs.GetJobById(JobId).Scanning = CurrentFile;
424 db->Jobs.GetJobById(JobId).Status = GetStatus();
425
426 InstrumentsDb::GetInstrumentsDb()->FireJobStatusChanged(JobId);
427 }
428
429 int ScanProgress::GetTotalFileCount() {
430 return TotalFileCount;
431 }
432
433 void ScanProgress::SetTotalFileCount(int Count) {
434 if (TotalFileCount == Count) return;
435 TotalFileCount = Count;
436 StatusChanged();
437 }
438
439 int ScanProgress::GetScannedFileCount() {
440 return ScannedFileCount;
441 }
442
443 void ScanProgress::SetScannedFileCount(int Count) {
444 if (ScannedFileCount == Count) return;
445 ScannedFileCount = Count;
446 if (Count > TotalFileCount) TotalFileCount = Count;
447 StatusChanged();
448 }
449
450 int ScanProgress::GetStatus() {
451 return Status;
452 }
453
454 void ScanProgress::SetStatus(int Status) {
455 if (this->Status == Status) return;
456 if (Status < 0) this->Status = 0;
457 else if (Status > 100) this->Status = 100;
458 else this->Status = Status;
459 StatusChanged();
460 }
461
462 void ScanProgress::SetErrorStatus(int Err) {
463 if (Err > 0) Err *= -1;
464 Status = Err;
465 StatusChanged();
466 }
467
468 void ScanProgress::GigFileProgressCallback(gig::progress_t* pProgress) {
469 if (pProgress == NULL) return;
470 ScanProgress* sp = static_cast<ScanProgress*> (pProgress->custom);
471
472 sp->SetStatus((int)(pProgress->factor * 100));
473 }
474
475 AddInstrumentsJob::AddInstrumentsJob(int JobId, ScanMode Mode, String DbDir, String FsDir, bool insDir) {
476 this->JobId = JobId;
477 Progress.JobId = JobId;
478 this->Mode = Mode;
479 this->DbDir = DbDir;
480 this->FsDir = FsDir;
481 this->insDir = insDir;
482 }
483
484 void AddInstrumentsJob::Run() {
485 try {
486 InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
487
488 switch (Mode) {
489 case NON_RECURSIVE:
490 Progress.SetTotalFileCount(GetFileCount());
491 db->AddInstrumentsNonrecursive(DbDir, FsDir, insDir, &Progress);
492 break;
493 case RECURSIVE:
494 db->AddInstrumentsRecursive(DbDir, FsDir, false, insDir, &Progress);
495 break;
496 case FLAT:
497 db->AddInstrumentsRecursive(DbDir, FsDir, true, insDir, &Progress);
498 break;
499 default:
500 throw Exception("Unknown scan mode");
501 }
502
503 // Just to be sure that the frontends will be notified about the job completion
504 if (Progress.GetTotalFileCount() != Progress.GetScannedFileCount()) {
505 Progress.SetTotalFileCount(Progress.GetScannedFileCount());
506 }
507 if (Progress.GetStatus() != 100) Progress.SetStatus(100);
508 } catch(Exception e) {
509 Progress.SetErrorStatus(-1);
510 throw e;
511 }
512 }
513
514 int AddInstrumentsJob::GetFileCount() {
515 int count = 0;
516
517 try {
518 FileListPtr fileList = File::GetFiles(FsDir);
519
520 for (int i = 0; i < fileList->size(); i++) {
521 String s = fileList->at(i);
522 if (s.length() < 4) continue;
523 if(!strcasecmp(".gig", s.substr(s.length() - 4).c_str())) count++;
524 }
525 } catch(Exception e) {
526 e.PrintMessage();
527 return 0;
528 }
529
530 return count;
531 }
532
533 AddInstrumentsFromFileJob::AddInstrumentsFromFileJob(int JobId, String DbDir, String FilePath, int Index, bool insDir) {
534 this->JobId = JobId;
535 Progress.JobId = JobId;
536 Progress.SetTotalFileCount(1);
537
538 this->DbDir = DbDir;
539 this->FilePath = FilePath;
540 this->Index = Index;
541 this->insDir = insDir;
542 }
543
544 void AddInstrumentsFromFileJob::Run() {
545 try {
546 InstrumentsDb::GetInstrumentsDb()->AddInstruments(DbDir, insDir, FilePath, Index, &Progress);
547
548 // Just to be sure that the frontends will be notified about the job completion
549 if (Progress.GetTotalFileCount() != Progress.GetScannedFileCount()) {
550 Progress.SetTotalFileCount(Progress.GetScannedFileCount());
551 }
552 if (Progress.GetStatus() != 100) Progress.SetStatus(100);
553 } catch(Exception e) {
554 Progress.SetErrorStatus(-1);
555 throw e;
556 }
557 }
558
559
560 void DirectoryScanner::Scan(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) {
561 dmsg(2,("DirectoryScanner: Scan(DbDir=%s,FsDir=%s,Flat=%d,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat, insDir));
562 if (DbDir.empty() || FsDir.empty()) throw Exception("Directory expected");
563
564 this->DbDir = DbDir;
565 this->FsDir = FsDir;
566 this->insDir = insDir;
567 if (DbDir.at(DbDir.length() - 1) != '/') {
568 this->DbDir.append("/");
569 }
570 if (FsDir.at(FsDir.length() - 1) != File::DirSeparator) {
571 this->FsDir.push_back(File::DirSeparator);
572 }
573 this->Flat = Flat;
574 this->pProgress = pProgress;
575
576 File::WalkDirectoryTree(FsDir, this);
577 }
578
579 void DirectoryScanner::DirectoryEntry(std::string Path) {
580 dmsg(2,("DirectoryScanner: DirectoryEntry(Path=%s)\n", Path.c_str()));
581
582 String dir = DbDir;
583 if (!Flat) {
584 String subdir = Path;
585 if(subdir.length() > FsDir.length()) {
586 subdir = subdir.substr(FsDir.length());
587 #ifdef WIN32
588 replace(subdir.begin(), subdir.end(), '\\', '/');
589 #endif
590 dir += subdir;
591 }
592 }
593
594 InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
595
596 if (HasInstrumentFiles(Path)) {
597 if (!db->DirectoryExist(dir)) db->AddDirectory(dir);
598 db->AddInstrumentsNonrecursive(dir, Path, insDir, pProgress);
599 }
600 };
601
602 bool DirectoryScanner::HasInstrumentFiles(String Dir) {
603 InstrumentFileCounter c;
604 return c.Count(Dir) > 0;
605 }
606
607 int InstrumentFileCounter::Count(String FsDir) {
608 dmsg(2,("InstrumentFileCounter: Count(FsDir=%s)\n", FsDir.c_str()));
609 if (FsDir.empty()) throw Exception("Directory expected");
610 FileCount = 0;
611
612 File::WalkDirectoryTree(FsDir, this);
613 return FileCount;
614 }
615
616 void InstrumentFileCounter::FileEntry(std::string Path) {
617 dmsg(2,("InstrumentFileCounter: FileEntry(Path=%s)\n", Path.c_str()));
618 if(Path.length() < 4) return;
619 if(!strcasecmp(".gig", Path.substr(Path.length() - 4).c_str())) FileCount++;
620 };
621
622 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC