--- linuxsampler/trunk/src/db/InstrumentsDb.cpp 2008/04/29 15:44:09 1727 +++ linuxsampler/trunk/src/db/InstrumentsDb.cpp 2009/07/14 18:25:11 1943 @@ -1,6 +1,6 @@ /*************************************************************************** * * - * Copyright (C) 2007, 2008 Grigor Iliev * + * Copyright (C) 2007-2009 Grigor Iliev, Benno Senoner * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -21,14 +21,18 @@ #include "InstrumentsDb.h" #include "../common/File.h" +#include "../common/Path.h" #include "../common/global_private.h" #include #include #include #include +#ifndef WIN32 #include - +#else +#include +#endif #include "../common/Exception.h" namespace LinuxSampler { @@ -86,7 +90,6 @@ InstrumentsDb::InstrumentsDb() { db = NULL; - DbInstrumentsMutex = Mutex(); InTransaction = false; } @@ -119,7 +122,29 @@ sqlite3* InstrumentsDb::GetDb() { if ( db != NULL) return db; - if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; + if (DbFile.empty()) { + #ifndef WIN32 + DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; + #else + char *userprofile = getenv("USERPROFILE"); + if(userprofile) { + String DbPath = userprofile; + DbPath += "\\.linuxsampler"; + DbFile = DbPath + "\\instruments.db"; + File InstrumentsDbFile(DbFile); + // if no DB exists create the subdir and then the DB + if( !InstrumentsDbFile.Exist() ) { + _mkdir( DbPath.c_str() ); + // formats the DB, which creates a new instruments.db file + Format(); + } + } + else { + // in case USERPROFILE is not set (which should not occur) + DbFile = "instruments.db"; + } + #endif + } #if defined(__APPLE__) /* 20071224 Toshi Nagata */ if (DbFile.find("~") == 0) DbFile.replace(0, 1, getenv("HOME")); @@ -130,8 +155,10 @@ db = NULL; throw Exception("Cannot open instruments database: " + DbFile); } +#ifndef WIN32 rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL); if (rc) { throw Exception("Failed to add user function for handling regular expressions."); } +#endif // TODO: remove this in the next version try { @@ -641,18 +668,18 @@ FireDirectoryInfoChanged(Dir); } - int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground) { - dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground)); + int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground, bool insDir) { + dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); if(!bBackground) { switch (Mode) { case NON_RECURSIVE: - AddInstrumentsNonrecursive(DbDir, FsDir); + AddInstrumentsNonrecursive(DbDir, FsDir, insDir); break; case RECURSIVE: - AddInstrumentsRecursive(DbDir, FsDir); + AddInstrumentsRecursive(DbDir, FsDir, false, insDir); break; case FLAT: - AddInstrumentsRecursive(DbDir, FsDir, true); + AddInstrumentsRecursive(DbDir, FsDir, true, insDir); break; default: throw Exception("Unknown scan mode"); @@ -663,7 +690,7 @@ ScanJob job; int jobId = Jobs.AddJob(job); - InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir)); + InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir, insDir)); return jobId; } @@ -671,19 +698,19 @@ int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) { dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); if(!bBackground) { - AddInstruments(DbDir, FilePath, Index); + AddInstruments(DbDir, false, FilePath, Index); return -1; } ScanJob job; int jobId = Jobs.AddJob(job); - InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index)); + InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index, false)); return jobId; - } + } - void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) { - dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); + void InstrumentsDb::AddInstruments(String DbDir, bool insDir, String FilePath, int Index, ScanProgress* pProgress) { + dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index)); if (DbDir.empty() || FilePath.empty()) return; DbInstrumentsMutex.Lock(); @@ -704,7 +731,8 @@ throw Exception(ss.str()); } - AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress); + String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir; + AddInstrumentsFromFile(dir, FilePath, Index, pProgress); } catch (Exception e) { DbInstrumentsMutex.Unlock(); throw e; @@ -713,8 +741,8 @@ DbInstrumentsMutex.Unlock(); } - void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, ScanProgress* pProgress) { - dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s)\n", DbDir.c_str(), FsDir.c_str())); + void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) { + dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir)); if (DbDir.empty() || FsDir.empty()) return; DbInstrumentsMutex.Lock(); @@ -740,7 +768,8 @@ try { FileListPtr fileList = File::GetFiles(FsDir); for (int i = 0; i < fileList->size(); i++) { - AddInstrumentsFromFile(DbDir, FsDir + fileList->at(i), -1, pProgress); + String dir = insDir ? PrepareSubdirectory(DbDir, fileList->at(i)) : DbDir; + AddInstrumentsFromFile(dir, FsDir + fileList->at(i), -1, pProgress); } } catch(Exception e) { e.PrintMessage(); @@ -755,15 +784,15 @@ DbInstrumentsMutex.Unlock(); } - void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) { - dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat)); + void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) { + dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat, insDir)); if (pProgress != NULL) { InstrumentFileCounter c; pProgress->SetTotalFileCount(c.Count(FsDir)); } DirectoryScanner d; - d.Scan(DbDir, FsDir, Flat, pProgress); + d.Scan(DbDir, FsDir, Flat, insDir, pProgress); } int InstrumentsDb::GetInstrumentCount(int DirId) { @@ -1174,6 +1203,7 @@ throw Exception(ss.str()); } + bool unlocked = false; RIFF::File* riff = NULL; gig::File* gig = NULL; try { @@ -1194,7 +1224,8 @@ throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); } - String s = toEscapedFsPath(FilePath); + String s = FilePath; + s = toEscapedFsPath(s); BindTextParam(pStmt, 2, s); String ver = ""; if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); @@ -1202,8 +1233,19 @@ if (Index == -1) { int instrIndex = 0; + // Assume that it's locked and should be unlocked at this point + // to be able to use the database from another threads + if (!InTransaction) { + DbInstrumentsMutex.Unlock(); + unlocked = true; + } else { + std::cerr << "Shouldn't be in transaction when adding instruments." << std::endl; + } + if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed gig::Instrument* pInstrument = gig->GetFirstInstrument(); + + if (!InTransaction) DbInstrumentsMutex.Lock(); while (pInstrument) { BindTextParam(pStmt, 7, gig->pInfo->Product); BindTextParam(pStmt, 8, gig->pInfo->Artists); @@ -1231,6 +1273,7 @@ } catch (RIFF::Exception e) { if (gig != NULL) delete gig; if (riff != NULL) delete riff; + if (unlocked) DbInstrumentsMutex.Lock(); std::stringstream ss; ss << "Failed to scan `" << FilePath << "`: " << e.Message; @@ -1238,10 +1281,12 @@ } catch (Exception e) { if (gig != NULL) delete gig; if (riff != NULL) delete riff; + if (unlocked) DbInstrumentsMutex.Lock(); throw e; } catch (...) { if (gig != NULL) delete gig; if (riff != NULL) delete riff; + if (unlocked) DbInstrumentsMutex.Lock(); throw Exception("Failed to scan `" + FilePath + "`"); } } @@ -1250,7 +1295,7 @@ dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); String name = pInstrument->pInfo->Name; if (name == "") return; - name = GetUniqueInstrumentName(DirId, name); + name = GetUniqueName(DirId, name); std::stringstream sql2; sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; @@ -1640,6 +1685,7 @@ } } +#ifndef WIN32 void InstrumentsDb::Regexp(sqlite3_context* pContext, int argc, sqlite3_value** ppValue) { if (argc != 2) return; @@ -1650,6 +1696,7 @@ sqlite3_result_int(pContext, 1); } } +#endif String InstrumentsDb::GetDirectoryPath(String File) { if (File.empty()) return String(""); @@ -1724,7 +1771,7 @@ if (File.empty()) throw Exception("Invalid file name: " + File); } - String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { + String InstrumentsDb::GetUniqueName(int DirId, String Name) { dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str())); if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; @@ -1739,6 +1786,22 @@ throw Exception("Unable to find an unique name: " + Name); } + + String InstrumentsDb::PrepareSubdirectory(String DbDir, String FsPath) { + std::string dir = Path::getBaseName(FsPath); + dir = toAbstractName(dir); + if(dir.empty()) dir = "New Directory"; + dir = GetUniqueName(GetDirectoryId(DbDir), dir); + dir = AppendNode(DbDir, dir); + AddDirectory(dir); + return dir; + } + + String InstrumentsDb::AppendNode(String DbDir, String Node) { + if(DbDir.length() == 1 && DbDir.at(0) == '/') return DbDir + Node; + if(DbDir.at(DbDir.length() - 1) == '/') return DbDir + Node; + return DbDir + "/" + Node; + } String InstrumentsDb::toDbName(String AbstractName) { for (int i = 0; i < AbstractName.length(); i++) { @@ -1794,11 +1857,18 @@ } String InstrumentsDb::toEscapedFsPath(String FsPath) { +#ifdef WIN32 + replace(FsPath.begin(), FsPath.end(), '\\', '/'); +#endif return toEscapedText(FsPath); } String InstrumentsDb::toNonEscapedFsPath(String FsPath) { - return toNonEscapedText(FsPath); + FsPath = toNonEscapedText(FsPath); +#ifdef WIN32 + replace(FsPath.begin(), FsPath.end(), '/', '\\'); +#endif + return FsPath; } String InstrumentsDb::toAbstractName(String DbName) {