1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2007, 2008 Grigor Iliev * |
* Copyright (C) 2007-2009 Grigor Iliev, Benno Senoner * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
21 |
#include "InstrumentsDb.h" |
#include "InstrumentsDb.h" |
22 |
|
|
23 |
#include "../common/File.h" |
#include "../common/File.h" |
24 |
|
#include "../common/Path.h" |
25 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
26 |
|
|
27 |
#include <iostream> |
#include <iostream> |
28 |
#include <sstream> |
#include <sstream> |
29 |
#include <vector> |
#include <vector> |
30 |
|
#include <algorithm> |
31 |
#include <errno.h> |
#include <errno.h> |
32 |
|
#ifndef WIN32 |
33 |
#include <fnmatch.h> |
#include <fnmatch.h> |
34 |
|
#else |
35 |
|
#include <direct.h> |
36 |
|
#endif |
37 |
#include "../common/Exception.h" |
#include "../common/Exception.h" |
38 |
|
|
39 |
namespace LinuxSampler { |
namespace LinuxSampler { |
91 |
|
|
92 |
InstrumentsDb::InstrumentsDb() { |
InstrumentsDb::InstrumentsDb() { |
93 |
db = NULL; |
db = NULL; |
|
DbInstrumentsMutex = Mutex(); |
|
94 |
InTransaction = false; |
InTransaction = false; |
95 |
} |
} |
96 |
|
|
123 |
sqlite3* InstrumentsDb::GetDb() { |
sqlite3* InstrumentsDb::GetDb() { |
124 |
if ( db != NULL) return db; |
if ( db != NULL) return db; |
125 |
|
|
126 |
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
if (DbFile.empty()) { |
127 |
|
#ifndef WIN32 |
128 |
|
DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
129 |
|
#else |
130 |
|
char *userprofile = getenv("USERPROFILE"); |
131 |
|
if(userprofile) { |
132 |
|
String DbPath = userprofile; |
133 |
|
DbPath += "\\.linuxsampler"; |
134 |
|
DbFile = DbPath + "\\instruments.db"; |
135 |
|
File InstrumentsDbFile(DbFile); |
136 |
|
// if no DB exists create the subdir and then the DB |
137 |
|
if( !InstrumentsDbFile.Exist() ) { |
138 |
|
_mkdir( DbPath.c_str() ); |
139 |
|
// formats the DB, which creates a new instruments.db file |
140 |
|
Format(); |
141 |
|
} |
142 |
|
} |
143 |
|
else { |
144 |
|
// in case USERPROFILE is not set (which should not occur) |
145 |
|
DbFile = "instruments.db"; |
146 |
|
} |
147 |
|
#endif |
148 |
|
} |
149 |
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
150 |
if (DbFile.find("~") == 0) |
if (DbFile.find("~") == 0) |
151 |
DbFile.replace(0, 1, getenv("HOME")); |
DbFile.replace(0, 1, getenv("HOME")); |
156 |
db = NULL; |
db = NULL; |
157 |
throw Exception("Cannot open instruments database: " + DbFile); |
throw Exception("Cannot open instruments database: " + DbFile); |
158 |
} |
} |
159 |
|
#ifndef WIN32 |
160 |
rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL); |
rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL); |
161 |
if (rc) { throw Exception("Failed to add user function for handling regular expressions."); } |
if (rc) { throw Exception("Failed to add user function for handling regular expressions."); } |
162 |
|
#endif |
163 |
|
|
164 |
// TODO: remove this in the next version |
// TODO: remove this in the next version |
165 |
try { |
try { |
732 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
733 |
} |
} |
734 |
|
|
735 |
if(insDir) { |
String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir; |
736 |
std::string tmp = f.basename(FilePath, "."); |
AddInstrumentsFromFile(dir, FilePath, Index, pProgress); |
|
String gigDir; |
|
|
if(DbDir.length() == 1 && DbDir.at(0) == '/') //DbDir is / |
|
|
gigDir = DbDir + (String)tmp + "/"; |
|
|
else |
|
|
gigDir = DbDir +"/"+ (String)tmp + "/"; |
|
|
dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(Dir from file mode=%d, Created SubDir=%s)\n",insDir, gigDir.c_str())); |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
AddDirectory(gigDir);//TODO: Add some error checking here to make sure the dir is created |
|
|
DbInstrumentsMutex.Lock(); |
|
|
AddInstrumentsFromFile(gigDir, FilePath, Index, pProgress); |
|
|
} else { |
|
|
AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress); |
|
|
} |
|
737 |
} catch (Exception e) { |
} catch (Exception e) { |
738 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
739 |
throw e; |
throw e; |
769 |
try { |
try { |
770 |
FileListPtr fileList = File::GetFiles(FsDir); |
FileListPtr fileList = File::GetFiles(FsDir); |
771 |
for (int i = 0; i < fileList->size(); i++) { |
for (int i = 0; i < fileList->size(); i++) { |
772 |
if(insDir) |
String dir = insDir ? PrepareSubdirectory(DbDir, fileList->at(i)) : DbDir; |
773 |
{ |
AddInstrumentsFromFile(dir, FsDir + fileList->at(i), -1, pProgress); |
|
//File gFile = File(fileList->at(i)); |
|
|
String gigDir; |
|
|
if(DbDir.length() == 1 && DbDir.at(0) == '/') //DbDir is / |
|
|
gigDir = DbDir + f.basename(fileList->at(i),".") + "/"; |
|
|
else |
|
|
gigDir = DbDir +"/"+ f.basename(fileList->at(i),".") + "/"; |
|
|
dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(Dir from file mode=%d, Created SubDir=%s)\n",insDir, gigDir.c_str())); |
|
|
DbInstrumentsMutex.Unlock(); // UnLock the db so we can add our extra directory |
|
|
AddDirectory(gigDir);//TODO: Add some error checking here to make sure the dir is created |
|
|
DbInstrumentsMutex.Lock(); //Lock and carry on |
|
|
AddInstrumentsFromFile(gigDir, FsDir + fileList->at(i), -1, pProgress); |
|
|
} |
|
|
else |
|
|
{ |
|
|
AddInstrumentsFromFile(DbDir, FsDir + fileList->at(i), -1, pProgress); |
|
|
} |
|
774 |
} |
} |
775 |
} catch(Exception e) { |
} catch(Exception e) { |
776 |
e.PrintMessage(); |
e.PrintMessage(); |
1204 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
1205 |
} |
} |
1206 |
|
|
1207 |
|
bool unlocked = false; |
1208 |
RIFF::File* riff = NULL; |
RIFF::File* riff = NULL; |
1209 |
gig::File* gig = NULL; |
gig::File* gig = NULL; |
1210 |
try { |
try { |
1225 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1226 |
} |
} |
1227 |
|
|
1228 |
String s = toEscapedFsPath(FilePath); |
String s = FilePath; |
1229 |
|
s = toEscapedFsPath(s); |
1230 |
BindTextParam(pStmt, 2, s); |
BindTextParam(pStmt, 2, s); |
1231 |
String ver = ""; |
String ver = ""; |
1232 |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
1234 |
|
|
1235 |
if (Index == -1) { |
if (Index == -1) { |
1236 |
int instrIndex = 0; |
int instrIndex = 0; |
1237 |
|
// Assume that it's locked and should be unlocked at this point |
1238 |
|
// to be able to use the database from another threads |
1239 |
|
if (!InTransaction) { |
1240 |
|
DbInstrumentsMutex.Unlock(); |
1241 |
|
unlocked = true; |
1242 |
|
} else { |
1243 |
|
std::cerr << "Shouldn't be in transaction when adding instruments." << std::endl; |
1244 |
|
} |
1245 |
|
|
1246 |
if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed |
if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed |
1247 |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
1248 |
|
|
1249 |
|
if (!InTransaction) DbInstrumentsMutex.Lock(); |
1250 |
while (pInstrument) { |
while (pInstrument) { |
1251 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1252 |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
1274 |
} catch (RIFF::Exception e) { |
} catch (RIFF::Exception e) { |
1275 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1276 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1277 |
|
if (unlocked) DbInstrumentsMutex.Lock(); |
1278 |
std::stringstream ss; |
std::stringstream ss; |
1279 |
ss << "Failed to scan `" << FilePath << "`: " << e.Message; |
ss << "Failed to scan `" << FilePath << "`: " << e.Message; |
1280 |
|
|
1282 |
} catch (Exception e) { |
} catch (Exception e) { |
1283 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1284 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1285 |
|
if (unlocked) DbInstrumentsMutex.Lock(); |
1286 |
throw e; |
throw e; |
1287 |
} catch (...) { |
} catch (...) { |
1288 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1289 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1290 |
|
if (unlocked) DbInstrumentsMutex.Lock(); |
1291 |
throw Exception("Failed to scan `" + FilePath + "`"); |
throw Exception("Failed to scan `" + FilePath + "`"); |
1292 |
} |
} |
1293 |
} |
} |
1296 |
dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); |
1297 |
String name = pInstrument->pInfo->Name; |
String name = pInstrument->pInfo->Name; |
1298 |
if (name == "") return; |
if (name == "") return; |
1299 |
name = GetUniqueInstrumentName(DirId, name); |
name = GetUniqueName(DirId, name); |
1300 |
|
|
1301 |
std::stringstream sql2; |
std::stringstream sql2; |
1302 |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
1686 |
} |
} |
1687 |
} |
} |
1688 |
|
|
1689 |
|
#ifndef WIN32 |
1690 |
void InstrumentsDb::Regexp(sqlite3_context* pContext, int argc, sqlite3_value** ppValue) { |
void InstrumentsDb::Regexp(sqlite3_context* pContext, int argc, sqlite3_value** ppValue) { |
1691 |
if (argc != 2) return; |
if (argc != 2) return; |
1692 |
|
|
1697 |
sqlite3_result_int(pContext, 1); |
sqlite3_result_int(pContext, 1); |
1698 |
} |
} |
1699 |
} |
} |
1700 |
|
#endif |
1701 |
|
|
1702 |
String InstrumentsDb::GetDirectoryPath(String File) { |
String InstrumentsDb::GetDirectoryPath(String File) { |
1703 |
if (File.empty()) return String(""); |
if (File.empty()) return String(""); |
1772 |
if (File.empty()) throw Exception("Invalid file name: " + File); |
if (File.empty()) throw Exception("Invalid file name: " + File); |
1773 |
} |
} |
1774 |
|
|
1775 |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
String InstrumentsDb::GetUniqueName(int DirId, String Name) { |
1776 |
dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str())); |
dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str())); |
1777 |
|
|
1778 |
if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; |
if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; |
1787 |
|
|
1788 |
throw Exception("Unable to find an unique name: " + Name); |
throw Exception("Unable to find an unique name: " + Name); |
1789 |
} |
} |
1790 |
|
|
1791 |
|
String InstrumentsDb::PrepareSubdirectory(String DbDir, String FsPath) { |
1792 |
|
std::string dir = Path::getBaseName(FsPath); |
1793 |
|
dir = toAbstractName(dir); |
1794 |
|
if(dir.empty()) dir = "New Directory"; |
1795 |
|
dir = GetUniqueName(GetDirectoryId(DbDir), dir); |
1796 |
|
dir = AppendNode(DbDir, dir); |
1797 |
|
AddDirectory(dir); |
1798 |
|
return dir; |
1799 |
|
} |
1800 |
|
|
1801 |
|
String InstrumentsDb::AppendNode(String DbDir, String Node) { |
1802 |
|
if(DbDir.length() == 1 && DbDir.at(0) == '/') return DbDir + Node; |
1803 |
|
if(DbDir.at(DbDir.length() - 1) == '/') return DbDir + Node; |
1804 |
|
return DbDir + "/" + Node; |
1805 |
|
} |
1806 |
|
|
1807 |
String InstrumentsDb::toDbName(String AbstractName) { |
String InstrumentsDb::toDbName(String AbstractName) { |
1808 |
for (int i = 0; i < AbstractName.length(); i++) { |
for (int i = 0; i < AbstractName.length(); i++) { |
1858 |
} |
} |
1859 |
|
|
1860 |
String InstrumentsDb::toEscapedFsPath(String FsPath) { |
String InstrumentsDb::toEscapedFsPath(String FsPath) { |
1861 |
|
#ifdef WIN32 |
1862 |
|
replace(FsPath.begin(), FsPath.end(), '\\', '/'); |
1863 |
|
#endif |
1864 |
return toEscapedText(FsPath); |
return toEscapedText(FsPath); |
1865 |
} |
} |
1866 |
|
|
1867 |
String InstrumentsDb::toNonEscapedFsPath(String FsPath) { |
String InstrumentsDb::toNonEscapedFsPath(String FsPath) { |
1868 |
return toNonEscapedText(FsPath); |
FsPath = toNonEscapedText(FsPath); |
1869 |
|
#ifdef WIN32 |
1870 |
|
replace(FsPath.begin(), FsPath.end(), '/', '\\'); |
1871 |
|
#endif |
1872 |
|
return FsPath; |
1873 |
} |
} |
1874 |
|
|
1875 |
String InstrumentsDb::toAbstractName(String DbName) { |
String InstrumentsDb::toAbstractName(String DbName) { |